|
- <?php
-
- namespace Lc\CaracoleBundle\Controller\User;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
- use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
- use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
- use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
- use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
- use Lc\CaracoleBundle\Resolver\MerchantResolver;
- use Lc\CaracoleBundle\Resolver\SectionResolver;
- use Lc\SovBundle\Controller\AbstractAdminController;
- use Lc\SovBundle\Factory\User\UserFactory;
- use Lc\SovBundle\Field\BooleanField;
- use Lc\SovBundle\Model\User\UserInterface;
- use Symfony\Component\HttpFoundation\Response;
-
- abstract class UserMerchantAdminController extends AbstractAdminController
- {
-
- public function configureFields(string $pageName): iterable
- {
- return [
- TextField::new('user.email'),
- TextField::new('user.lastname'),
- TextField::new('user.firstname'),
- BooleanField::new('active'),
- BooleanField::new('creditActive'),
- ArrayField::new('roles'),
- ];
- }
-
- public function new(AdminContext $context): Response
- {
- $userFactory = $this->get('user_factory');
- $userMerchantFactory = $this->get('user_merchant_factory');
- $userRepository = $this->em->getRepository(UserInterface::class);
- $merchantResolver = $this->get('merchant_resolver');
-
- $userMerchant= $userMerchantFactory->create();
-
- $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
-
- $form->handleRequest($context->getRequest());
-
- if ($form->isSubmitted() && $form->isValid()) {
- $userMerchant = $form->getData();
-
- $existingUser = $userRepository->findOneByEmail($form->get('email')->getData());
- //Le user n'existe pas, on le créer
- if ($existingUser == null) {
- $param['email'] = $form->get('email')->getData();
- $param['lastname'] = $form->get('lastname')->getData();
- $param['lastname'] = $form->get('firstname')->getData();
- $param['roles'] = array();
-
- $user = $userFactory->create($param);
-
- $this->em->create($user);
- $userMerchant->setUser($user);
- $this->em->create($userMerchant);
- $this->em->flush();
- $this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.create'));
- $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
- return $this->redirect($url);
- }else{
- //Le user existe, on vérifie si le usemerchant existe aussi
- $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
- if ($existingUserMerchant == null) {
- $userMerchant->setUser($existingUser);
- $this->em->create($userMerchant);
- $this->em->flush();
-
- $this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.linked'));
- $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
- return $this->redirect($url);
-
- }else{
- $this->addFlash('error', $this->translatorAdmin->trans('form.user_merchant.already_exist'));
- }
- }
-
-
- }
-
- return $this->render(
- '@LcCaracole/admin/user/usermerchant_new.html.twig',
- [
- 'form' => $form->createView(),
- ]
- );
- }
-
- }
|