You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.2KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\User;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  4. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  5. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  6. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  7. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  8. use Lc\CaracoleBundle\Controller\AdminControllerTrait;
  9. use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
  10. use Lc\SovBundle\Controller\AbstractAdminController;
  11. use Lc\SovBundle\Field\BooleanField;
  12. use Lc\SovBundle\Model\User\UserInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. abstract class UserMerchantAdminController extends AbstractAdminController
  15. {
  16. use AdminControllerTrait;
  17. public function configureFields(string $pageName): iterable
  18. {
  19. yield TextField::new('user.email');
  20. yield TextField::new('user.lastname');
  21. yield TextField::new('user.firstname');
  22. yield BooleanField::new('active');
  23. yield BooleanField::new('creditActive');
  24. if($this->isGranted('ROLE_SUPER_ADMIN'))yield ArrayField::new('roles');
  25. }
  26. public function new(AdminContext $context): Response
  27. {
  28. $userFactory = $this->get('user_factory');
  29. $userMerchantFactory = $this->get('user_merchant_factory');
  30. $userRepository = $this->em->getRepository(UserInterface::class);
  31. $merchantResolver = $this->get('merchant_resolver');
  32. $userMerchant = $userMerchantFactory->create();
  33. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  34. $form->handleRequest($context->getRequest());
  35. if ($form->isSubmitted() && $form->isValid()) {
  36. $userMerchant = $form->getData();
  37. $existingUser = $userRepository->findOneByEmail($form->get('email')->getData());
  38. //Le user n'existe pas, on le créer
  39. if ($existingUser == null) {
  40. $param['email'] = $form->get('email')->getData();
  41. $param['lastname'] = $form->get('lastname')->getData();
  42. $param['lastname'] = $form->get('firstname')->getData();
  43. $param['roles'] = array();
  44. $user = $userFactory->create($param);
  45. $this->em->create($user);
  46. $userMerchant->setUser($user);
  47. $this->em->create($userMerchant);
  48. $this->em->flush();
  49. $this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.create'));
  50. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  51. return $this->redirect($url);
  52. } else {
  53. //Le user existe, on vérifie si le usemerchant existe aussi
  54. $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
  55. if ($existingUserMerchant == null) {
  56. $userMerchant->setUser($existingUser);
  57. $this->em->create($userMerchant);
  58. $this->em->flush();
  59. $this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.linked'));
  60. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  61. return $this->redirect($url);
  62. } else {
  63. $this->addFlash('error', $this->translatorAdmin->trans('form.user_merchant.already_exist'));
  64. }
  65. }
  66. }
  67. return $this->render(
  68. '@LcCaracole/admin/user/usermerchant_new.html.twig',
  69. [
  70. 'form' => $form->createView(),
  71. ]
  72. );
  73. }
  74. public function edit(AdminContext $context): Response
  75. {
  76. $userRepository = $this->em->getRepository(UserInterface::class);
  77. $merchantResolver = $this->get('merchant_resolver');
  78. $userMerchant = $context->getEntity()->getInstance();
  79. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  80. $form->handleRequest($context->getRequest());
  81. if ($form->isSubmitted() && $form->isValid()) {
  82. $userMerchant = $form->getData();
  83. $userMerchant->getUser()->setEmail($form->get('email')->getData());
  84. $userMerchant->getUser()->setLastName($form->get('lastname')->getData());
  85. $userMerchant->getUser()->setFirstname($form->get('firstname')->getData());
  86. $this->em->update($userMerchant);
  87. $this->em->update($userMerchant->getUser());
  88. $this->em->flush();
  89. $this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.update'));
  90. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  91. return $this->redirect($url);
  92. } else {
  93. $form->get('email')->setData($userMerchant->getUser()->getEmail());
  94. $form->get('lastname')->setData($userMerchant->getUser()->getLastname());
  95. $form->get('firstname')->setData($userMerchant->getUser()->getFirstname());
  96. }
  97. return $this->render(
  98. '@LcCaracole/admin/user/usermerchant_edit.html.twig',
  99. [
  100. 'form' => $form->createView(),
  101. ]
  102. );
  103. }
  104. }