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.

138 lines
5.3KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  6. use FOS\UserBundle\Model\UserManagerInterface;
  7. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  8. use Lc\ShopBundle\Context\NewsInterface;
  9. use Lc\ShopBundle\Context\OrderUtilsInterface;
  10. use Lc\ShopBundle\Context\UserInterface;
  11. use Lc\ShopBundle\Services\CreditUtils;
  12. use Lc\ShopBundle\Services\Utils;
  13. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  14. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  15. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Security\Core\Security;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class UserMerchantController extends AdminController
  20. {
  21. protected $creditUtils;
  22. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils, MerchantUtilsInterface $merchantUtils, MailjetTransport $mailjetTransport, OrderUtilsInterface $orderUtils, TranslatorInterface $translator, CreditUtils $creditUtils)
  23. {
  24. parent::__construct($security, $userManager, $em, $utils, $merchantUtils, $mailjetTransport, $orderUtils, $translator);
  25. $this->creditUtils = $creditUtils;
  26. }
  27. protected function newAction()
  28. {
  29. $this->dispatch(EasyAdminEvents::PRE_NEW);
  30. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  31. $easyadmin = $this->request->attributes->get('easyadmin');
  32. $easyadmin['item'] = $entity;
  33. $this->request->attributes->set('easyadmin', $easyadmin);
  34. $fields = $this->entity['new']['fields'];
  35. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  36. $newForm->handleRequest($this->request);
  37. //ETAPE 1 Choix de l'utilisateur
  38. $user = $newForm->get('user')->getData();
  39. if ($user == null) {
  40. $user = $this->getUserViaFirstStepForm($entity);
  41. }
  42. if (!$user instanceof UserInterface) return $user;
  43. else {
  44. $userMerchant = $this->creditUtils->activeCredit($user);
  45. return $this->redirectToRoute('easyadmin', [
  46. 'action' => 'edit',
  47. 'entity' => $this->entity['name'],
  48. 'id' => $userMerchant->getId()
  49. ]);
  50. }
  51. }
  52. public function getUserViaFirstStepForm($entity)
  53. {
  54. $userClass = $this->em->getClassMetadata(UserInterface::class);
  55. $userChoiceForm = $this->createFormBuilder($entity)
  56. ->add('user', EntityType::class, array(
  57. 'class' => $userClass->name
  58. ))
  59. ->add('nextStep', SubmitType::class)
  60. ->getForm();
  61. $userChoiceForm->handleRequest($this->request);
  62. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  63. return $userChoiceForm->get('user')->getData();
  64. }
  65. $parameters = [
  66. 'form' => $userChoiceForm->createView(),
  67. 'formView' => 'default',
  68. 'entity' => $entity,
  69. ];
  70. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  71. }
  72. /**
  73. * The method that is executed when the user performs a 'show' action on an entity.
  74. *
  75. * @return Response
  76. */
  77. public function showAction()
  78. {
  79. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  80. $id = $this->request->query->get('id');
  81. $easyadmin = $this->request->attributes->get('easyadmin');
  82. $entity = $easyadmin['item'];
  83. $fields = $this->entity['show']['fields'];
  84. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  85. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  86. 'deleteForm' => $deleteForm,
  87. 'fields' => $fields,
  88. 'entity' => $entity,
  89. ]);
  90. $parameters = [
  91. 'entity' => $entity,
  92. 'fields' => $fields,
  93. 'delete_form' => $deleteForm->createView()
  94. ];
  95. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  96. }
  97. /**
  98. * Réécriture de edit action pr rediriger vers le show */
  99. public function editAction()
  100. {
  101. $id = $this->request->query->get('id');
  102. $entity = $this->request->query->get('entity');
  103. return $this->redirectToRoute('easyadmin', [
  104. 'action' => 'show',
  105. 'entity' => $entity,
  106. 'id' => $id
  107. ]);
  108. }
  109. }