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.

97 lines
3.7KB

  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\Factory\User\UserMerchantFactory;
  9. use Lc\CaracoleBundle\Form\User\UserMerchantFormType;
  10. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  11. use Lc\CaracoleBundle\Resolver\SectionResolver;
  12. use Lc\SovBundle\Controller\AbstractAdminController;
  13. use Lc\SovBundle\Factory\User\UserFactory;
  14. use Lc\SovBundle\Field\BooleanField;
  15. use Lc\SovBundle\Model\User\UserInterface;
  16. use Symfony\Component\HttpFoundation\Response;
  17. abstract class UserMerchantAdminController extends AbstractAdminController
  18. {
  19. public function configureFields(string $pageName): iterable
  20. {
  21. return [
  22. TextField::new('user.email'),
  23. TextField::new('user.lastname'),
  24. TextField::new('user.firstname'),
  25. BooleanField::new('active'),
  26. BooleanField::new('creditActive'),
  27. ArrayField::new('roles'),
  28. ];
  29. }
  30. public function new(AdminContext $context): Response
  31. {
  32. $userFactory = $this->get('user_factory');
  33. $userMerchantFactory = $this->get('user_merchant_factory');
  34. $userRepository = $this->em->getRepository(UserInterface::class);
  35. $merchantResolver = $this->get('merchant_resolver');
  36. $userMerchant= $userMerchantFactory->create();
  37. $form = $this->createForm(UserMerchantFormType::class, $userMerchant);
  38. $form->handleRequest($context->getRequest());
  39. if ($form->isSubmitted() && $form->isValid()) {
  40. $userMerchant = $form->getData();
  41. $existingUser = $userRepository->findOneByEmail($form->get('email')->getData());
  42. //Le user n'existe pas, on le créer
  43. if ($existingUser == null) {
  44. $param['email'] = $form->get('email')->getData();
  45. $param['lastname'] = $form->get('lastname')->getData();
  46. $param['lastname'] = $form->get('firstname')->getData();
  47. $param['roles'] = array();
  48. $user = $userFactory->create($param);
  49. $this->em->create($user);
  50. $userMerchant->setUser($user);
  51. $this->em->create($userMerchant);
  52. $this->em->flush();
  53. $this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.create'));
  54. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  55. return $this->redirect($url);
  56. }else{
  57. //Le user existe, on vérifie si le usemerchant existe aussi
  58. $existingUserMerchant = $merchantResolver->getUserMerchant($existingUser);
  59. if ($existingUserMerchant == null) {
  60. $userMerchant->setUser($existingUser);
  61. $this->em->create($userMerchant);
  62. $this->em->flush();
  63. $this->addFlash('success', $this->translatorAdmin->trans('form.user_merchant.linked'));
  64. $url = $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
  65. return $this->redirect($url);
  66. }else{
  67. $this->addFlash('error', $this->translatorAdmin->trans('form.user_merchant.already_exist'));
  68. }
  69. }
  70. }
  71. return $this->render(
  72. '@LcCaracole/admin/user/usermerchant_new.html.twig',
  73. [
  74. 'form' => $form->createView(),
  75. ]
  76. );
  77. }
  78. }