@@ -2,6 +2,7 @@ | |||
namespace Lc\CaracoleBundle\Controller\Dashboard; | |||
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets; | |||
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; | |||
use Lc\SovBundle\Controller\Dashboard\DashboardController as SovDashboardController ; | |||
@@ -17,4 +18,13 @@ class DashboardAdminController extends SovDashboardController | |||
return $crud ; | |||
} | |||
public function configureAssets(): Assets | |||
{ | |||
$assets = parent::configureAssets(); // TODO: Change the autogenerated stub | |||
$assets->addWebpackEncoreEntry('common-switch-merchant') ; | |||
return $assets ; | |||
} | |||
} |
@@ -0,0 +1,35 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Controller\Merchant; | |||
use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType; | |||
use Lc\SovBundle\Doctrine\EntityManager; | |||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |||
use Symfony\Component\HttpFoundation\Request; | |||
use Symfony\Component\Security\Core\Security; | |||
class SwitchMerchantController extends AbstractController | |||
{ | |||
public function switchMerchant(Request $request, Security $security, EntityManager $em) | |||
{ | |||
$user = $security->getUser() ; | |||
if($user) { | |||
$form = $this->createForm(SwitchMerchantFormType::class); | |||
$form->handleRequest($request); | |||
if ($form->isSubmitted() && $form->isValid()) { | |||
$merchant = $form->get('merchants')->getData(); | |||
$user->setFavoriteMerchant($merchant) ; | |||
$em->update($user) ; | |||
$em->flush() ; | |||
return $this->redirectToRoute('admin_dashboard') ; | |||
} | |||
} | |||
else { | |||
} | |||
} | |||
} |
@@ -3,35 +3,51 @@ | |||
namespace Lc\CaracoleBundle\Form\Merchant; | |||
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; | |||
use Lc\SovBundle\DataTransformer\FileManagerTypeToDataTransformer; | |||
use Lc\CaracoleBundle\Resolver\MerchantResolver; | |||
use Lc\SovBundle\Doctrine\EntityManager; | |||
use Lc\SovBundle\Model\User\UserInterface; | |||
use Lc\SovBundle\Translation\TranslatorAdmin; | |||
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | |||
use Symfony\Component\Form\AbstractType; | |||
use Symfony\Component\Form\FormBuilderInterface; | |||
use Symfony\Component\OptionsResolver\OptionsResolver; | |||
class SwitchMerchantFormType extends AbstractType | |||
{ | |||
protected $em; | |||
protected $translatorAdmin; | |||
protected $merchantResolver; | |||
public function __construct(EntityManager $em, TranslatorAdmin $translatorAdmin) | |||
public function __construct(EntityManager $em, TranslatorAdmin $translatorAdmin, MerchantResolver $merchantResolver) | |||
{ | |||
$this->em = $em; | |||
$this->translatorAdmin = $translatorAdmin; | |||
$this->merchantResolver = $merchantResolver; | |||
} | |||
public function buildForm(FormBuilderInterface $builder, array $options) | |||
{ | |||
$entityName = $this->em->getEntityName(MerchantInterface::class); | |||
$builder->add( | |||
'merchants', | |||
EntityType::class, | |||
[ | |||
'class' => $entityName, | |||
'choice_label' => 'title' | |||
'class' => $this->em->getEntityName(MerchantInterface::class), | |||
'choice_label' => 'title', | |||
'data' => $options['current_merchant'] | |||
] | |||
); | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function configureOptions(OptionsResolver $resolver) | |||
{ | |||
$resolver->setDefaults( | |||
[ | |||
'current_merchant' => $this->merchantResolver->getCurrent(), | |||
] | |||
); | |||
} | |||
} |
@@ -35,7 +35,7 @@ abstract class UserModel extends SovUserModel | |||
/** | |||
* @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Merchant\MerchantInterface") | |||
*/ | |||
protected $merchant; | |||
protected $favoriteMerchant; | |||
/** | |||
* @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Address\AddressInterface", mappedBy="user", cascade={"persist"}) | |||
@@ -157,14 +157,14 @@ abstract class UserModel extends SovUserModel | |||
return $this; | |||
} | |||
public function getMerchant(): ?MerchantInterface | |||
public function getFavoriteMerchant(): ?MerchantInterface | |||
{ | |||
return $this->merchant; | |||
return $this->favoriteMerchant; | |||
} | |||
public function setMerchant(?MerchantInterface $merchant): self | |||
public function setFavoriteMerchant(?MerchantInterface $merchant): self | |||
{ | |||
$this->merchant = $merchant; | |||
$this->favoriteMerchant = $merchant; | |||
return $this; | |||
} |
@@ -0,0 +1,6 @@ | |||
import './switchmerchant.js' ; | |||
@@ -0,0 +1,11 @@ | |||
$(document).ready(function() { | |||
initSwitchMerchant() ; | |||
}) ; | |||
function initSwitchMerchant() { | |||
let $form = $('form.switch-merchant') ; | |||
$form.find('select').change(function() { | |||
$form.submit() ; | |||
}) ; | |||
} |
@@ -0,0 +1,4 @@ | |||
carac_switch_merchant: | |||
path: /switch-merchant | |||
controller: Lc\CaracoleBundle\Controller\Merchant\SwitchMerchantController::switchMerchant | |||
@@ -12,6 +12,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | |||
use Symfony\Component\Form\FormFactoryInterface; | |||
use Symfony\Component\HttpFoundation\RequestStack; | |||
use Symfony\Component\HttpKernel\KernelInterface; | |||
use Symfony\Component\Routing\Generator\UrlGenerator; | |||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |||
use Symfony\Contracts\Translation\TranslatorInterface; | |||
use Twig\Extension\AbstractExtension; | |||
@@ -19,42 +20,21 @@ use Twig\TwigFunction; | |||
class TwigExtension extends AbstractExtension | |||
{ | |||
protected $em; | |||
protected $kernel; | |||
protected $parameterBag; | |||
protected $cacheManager; | |||
protected $requestStack; | |||
protected $router; | |||
protected $translator; | |||
protected $translatorAdmin; | |||
protected $merchantRepository; | |||
protected $sectionRepository; | |||
protected $formFactory; | |||
protected $urlGenerator; | |||
public function __construct( | |||
KernelInterface $kernel, | |||
ParameterBagInterface $parameterBag, | |||
CacheManager $cacheManager, | |||
EntityManagerInterface $entityManager, | |||
RequestStack $requestStack, | |||
UrlGeneratorInterface $router, | |||
TranslatorInterface $translator, | |||
TranslatorAdmin $translatorAdmin, | |||
MerchantRepository $merchantRepository, | |||
SectionRepository $sectionRepository, | |||
FormFactoryInterface $formFactory | |||
FormFactoryInterface $formFactory, | |||
UrlGeneratorInterface $urlGenerator | |||
) { | |||
$this->kernel = $kernel; | |||
$this->parameterBag = $parameterBag; | |||
$this->cacheManager = $cacheManager; | |||
$this->em = $entityManager; | |||
$this->requestStack = $requestStack; | |||
$this->router = $router; | |||
$this->translator = $translator; | |||
$this->translatorAdmin = $translatorAdmin; | |||
$this->merchantRepository = $merchantRepository; | |||
$this->sectionRepository = $sectionRepository; | |||
$this->formFactory = $formFactory; | |||
$this->urlGenerator = $urlGenerator; | |||
} | |||
public function getFunctions() | |||
@@ -77,7 +57,14 @@ class TwigExtension extends AbstractExtension | |||
public function getFormSwitchMerchant() | |||
{ | |||
$form = $this->formFactory->create(SwitchMerchantFormType::class, null, ['action' => '#']); | |||
$form = $this->formFactory->create( | |||
SwitchMerchantFormType::class, | |||
null, | |||
[ | |||
'action' => $this->urlGenerator->generate('carac_switch_merchant'), | |||
'attr' => ['class' => 'switch-merchant'] | |||
] | |||
); | |||
return $form->createView(); | |||
} | |||
} |