namespace Lc\CaracoleBundle\Controller\Merchant; | namespace Lc\CaracoleBundle\Controller\Merchant; | ||||
use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType; | use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType; | ||||
use Lc\CaracoleBundle\Resolver\MerchantResolver; | |||||
use Lc\SovBundle\Doctrine\EntityManager; | use Lc\SovBundle\Doctrine\EntityManager; | ||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||||
use Symfony\Component\HttpFoundation\Request; | use Symfony\Component\HttpFoundation\Request; | ||||
class SwitchMerchantController extends AbstractController | class SwitchMerchantController extends AbstractController | ||||
{ | { | ||||
public function switchMerchant(Request $request, Security $security, EntityManager $em) | |||||
public function switchMerchant(Request $request) | |||||
{ | { | ||||
$user = $security->getUser() ; | |||||
$form = $this->createForm(SwitchMerchantFormType::class); | |||||
$form->handleRequest($request); | |||||
if($user) { | |||||
$form = $this->createForm(SwitchMerchantFormType::class); | |||||
$form->handleRequest($request); | |||||
if ($form->isSubmitted() && $form->isValid()) { | |||||
$merchant = $form->get('merchants')->getData(); | |||||
$context = $form->get('context')->getData(); | |||||
if ($form->isSubmitted() && $form->isValid()) { | |||||
$merchant = $form->get('merchants')->getData(); | |||||
$user->setFavoriteMerchant($merchant) ; | |||||
$em->update($user) ; | |||||
$em->flush() ; | |||||
if ($merchant) { | |||||
$url = $merchant->getMerchantConfig('url'); | |||||
return $this->redirectToRoute('admin_dashboard') ; | |||||
} | |||||
} | |||||
else { | |||||
if($context == 'admin') { | |||||
$url .= 'admin' ; | |||||
} | |||||
if ($url) { | |||||
return $this->redirect($url); | |||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } |
use Lc\SovBundle\Translation\TranslatorAdmin; | use Lc\SovBundle\Translation\TranslatorAdmin; | ||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | use Symfony\Bridge\Doctrine\Form\Type\EntityType; | ||||
use Symfony\Component\Form\AbstractType; | use Symfony\Component\Form\AbstractType; | ||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | |||||
use Symfony\Component\Form\FormBuilderInterface; | use Symfony\Component\Form\FormBuilderInterface; | ||||
use Symfony\Component\OptionsResolver\OptionsResolver; | use Symfony\Component\OptionsResolver\OptionsResolver; | ||||
'data' => $options['current_merchant'] | 'data' => $options['current_merchant'] | ||||
] | ] | ||||
); | ); | ||||
$builder->add( | |||||
'context', | |||||
HiddenType::class, | |||||
[ | |||||
'data' => $options['context'] | |||||
] | |||||
); | |||||
} | } | ||||
/** | /** | ||||
{ | { | ||||
$resolver->setDefaults( | $resolver->setDefaults( | ||||
[ | [ | ||||
'context' => 'frontend', | |||||
'current_merchant' => $this->merchantResolver->getCurrent(), | 'current_merchant' => $this->merchantResolver->getCurrent(), | ||||
] | ] | ||||
); | ); |
</ul> | </ul> | ||||
<ul class="navbar-nav ml-auto"> | <ul class="navbar-nav ml-auto"> | ||||
<li class="nav-item"> | <li class="nav-item"> | ||||
{% set form_switch_merchant = carac_get_form_switch_merchhant() %} | |||||
{% set form_switch_merchant = carac_get_form_switch_merchant('admin') %} | |||||
{{ form_start(form_switch_merchant) }} | {{ form_start(form_switch_merchant) }} | ||||
{{ form(form_switch_merchant) }} | {{ form(form_switch_merchant) }} | ||||
{{ form_end(form_switch_merchant) }} | {{ form_end(form_switch_merchant) }} |
{ | { | ||||
return array( | return array( | ||||
new TwigFunction('carac_get_sections', [$this, 'getSections']), | new TwigFunction('carac_get_sections', [$this, 'getSections']), | ||||
new TwigFunction('carac_get_form_switch_merchhant', [$this, 'getFormSwitchMerchant']), | |||||
new TwigFunction('carac_get_form_switch_merchant', [$this, 'getFormSwitchMerchant']), | |||||
); | ); | ||||
} | } | ||||
return $this->merchantRepository->findAll(); | return $this->merchantRepository->findAll(); | ||||
} | } | ||||
public function getFormSwitchMerchant() | |||||
public function getFormSwitchMerchant($context = 'front') | |||||
{ | { | ||||
$form = $this->formFactory->create( | $form = $this->formFactory->create( | ||||
SwitchMerchantFormType::class, | SwitchMerchantFormType::class, | ||||
null, | null, | ||||
[ | [ | ||||
'action' => $this->urlGenerator->generate('carac_switch_merchant'), | 'action' => $this->urlGenerator->generate('carac_switch_merchant'), | ||||
'attr' => ['class' => 'switch-merchant'] | |||||
'attr' => ['class' => 'switch-merchant'], | |||||
'context' => $context | |||||
] | ] | ||||
); | ); | ||||
return $form->createView(); | return $form->createView(); |