Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

134 lines
5.4KB

  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. $entityManager = $this->get('em');
  29. $userFactory = $this->get('user_factory');
  30. $userMerchantFactory = $this->get('user_merchant_factory');
  31. $userRepository = $entityManager->getRepository(UserInterface::class);
  32. $merchantResolver = $this->get('merchant_resolver');
  33. $userMerchant = $userMerchantFactory->create();
  34. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  35. $form->handleRequest($context->getRequest());
  36. if ($form->isSubmitted() && $form->isValid()) {
  37. $userMerchant = $form->getData();
  38. $existingUser = $userRepository->findOneByEmail($form->get('email')->getData());
  39. //Le user n'existe pas, on le créer
  40. if ($existingUser == null) {
  41. $param['email'] = $form->get('email')->getData();
  42. $param['lastname'] = $form->get('lastname')->getData();
  43. $param['lastname'] = $form->get('firstname')->getData();
  44. $param['roles'] = array();
  45. $user = $userFactory->create($param);
  46. $entityManager->create($user);
  47. $userMerchant->setUser($user);
  48. $entityManager->create($userMerchant);
  49. $entityManager->flush();
  50. $this->addFlash('success', $this->get('translator_admin')->trans('form.user_merchant.create'));
  51. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  52. return $this->redirect($url);
  53. } else {
  54. //Le user existe, on vérifie si le usemerchant existe aussi
  55. $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
  56. if ($existingUserMerchant == null) {
  57. $userMerchant->setUser($existingUser);
  58. $entityManager->create($userMerchant);
  59. $entityManager->flush();
  60. $this->addFlash('success', $this->get('translator_admin')->trans('form.user_merchant.linked'));
  61. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  62. return $this->redirect($url);
  63. } else {
  64. $this->addFlash('error', $this->get('translator_admin')->trans('form.user_merchant.already_exist'));
  65. }
  66. }
  67. }
  68. return $this->render(
  69. '@LcCaracole/admin/user/usermerchant_new.html.twig',
  70. [
  71. 'form' => $form->createView(),
  72. ]
  73. );
  74. }
  75. public function edit(AdminContext $context): Response
  76. {
  77. $entityManager = $this->get('em');
  78. $userRepository = $entityManager->getRepository(UserInterface::class);
  79. $merchantResolver = $this->get('merchant_resolver');
  80. $userMerchant = $context->getEntity()->getInstance();
  81. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  82. $form->handleRequest($context->getRequest());
  83. if ($form->isSubmitted() && $form->isValid()) {
  84. $userMerchant = $form->getData();
  85. $userMerchant->getUser()->setEmail($form->get('email')->getData());
  86. $userMerchant->getUser()->setLastName($form->get('lastname')->getData());
  87. $userMerchant->getUser()->setFirstname($form->get('firstname')->getData());
  88. $entityManager->update($userMerchant);
  89. $entityManager->update($userMerchant->getUser());
  90. $entityManager->flush();
  91. $this->addFlash('success', $this->get('translator_admin')->trans('form.user_merchant.update'));
  92. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  93. return $this->redirect($url);
  94. } else {
  95. $form->get('email')->setData($userMerchant->getUser()->getEmail());
  96. $form->get('lastname')->setData($userMerchant->getUser()->getLastname());
  97. $form->get('firstname')->setData($userMerchant->getUser()->getFirstname());
  98. }
  99. return $this->render(
  100. '@LcCaracole/admin/user/usermerchant_edit.html.twig',
  101. [
  102. 'form' => $form->createView(),
  103. ]
  104. );
  105. }
  106. }