No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

UserMerchantController.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Lc\ShopBundle\Context\CreditHistoryInterface;
  7. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  8. use Lc\ShopBundle\Context\OrderUtilsInterface;
  9. use Lc\ShopBundle\Context\UserInterface;
  10. use Lc\ShopBundle\Form\Backend\UserMerchant\CreditHistoryType;
  11. use Lc\ShopBundle\Services\CreditUtils;
  12. use Lc\ShopBundle\Services\Utils;
  13. use Lc\ShopBundle\Services\UtilsManager;
  14. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  15. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  16. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class UserMerchantController extends AdminController
  21. {
  22. protected $creditUtils;
  23. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator, SessionInterface $session)
  24. {
  25. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator, $session);
  26. $this->creditUtils = $utilsManager->getCreditUtils();
  27. }
  28. public function addCreditHistoryAction(){
  29. $easyadmin = $this->request->attributes->get('easyadmin');
  30. $userMerchant = $easyadmin['item'];
  31. $creditHistoryClass = $this->em->getClassMetadata(CreditHistoryInterface::class);
  32. $creditHistory = new $creditHistoryClass->name;
  33. $addCreditHistoryForm = $this->createForm(CreditHistoryType::class, $creditHistory);
  34. $addCreditHistoryForm->handleRequest($this->request);
  35. if ($addCreditHistoryForm->isSubmitted() && $addCreditHistoryForm->isValid()) {
  36. $creditHistory->setUserMerchant($userMerchant);
  37. if($this->creditUtils->saveCreditHistory($creditHistory)){
  38. //TODo envoyer un email à l'utilisateur
  39. $this->utils->addFlash('success', 'success.credit.added');
  40. }else{
  41. $this->utils->addFlash('error', 'error.credit.notAdded');
  42. }
  43. } else {
  44. $this->utils->addFlash('error', $addCreditHistoryForm->getErrors());
  45. }
  46. return $this->redirectToRoute('easyadmin', [
  47. 'action' => 'show',
  48. 'entity' => $this->request->query->get('entity'),
  49. 'id' => $userMerchant->getId()
  50. ]);
  51. }
  52. protected function newAction()
  53. {
  54. $this->dispatch(EasyAdminEvents::PRE_NEW);
  55. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  56. $easyadmin = $this->request->attributes->get('easyadmin');
  57. $easyadmin['item'] = $entity;
  58. $this->request->attributes->set('easyadmin', $easyadmin);
  59. $fields = $this->entity['new']['fields'];
  60. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  61. $newForm->handleRequest($this->request);
  62. //ETAPE 1 Choix de l'utilisateur
  63. $user = $newForm->get('user')->getData();
  64. if ($user == null) {
  65. $user = $this->getUserViaFirstStepForm($entity);
  66. }
  67. if (!$user instanceof UserInterface) return $user;
  68. else {
  69. $userMerchant = $this->creditUtils->activeCredit($user);
  70. return $this->redirectToRoute('easyadmin', [
  71. 'action' => 'edit',
  72. 'entity' => $this->entity['name'],
  73. 'id' => $userMerchant->getId()
  74. ]);
  75. }
  76. }
  77. public function getUserViaFirstStepForm($entity)
  78. {
  79. $userClass = $this->em->getClassMetadata(UserInterface::class);
  80. $userChoiceForm = $this->createFormBuilder($entity)
  81. ->add('user', EntityType::class, array(
  82. 'class' => $userClass->name
  83. ))
  84. ->add('nextStep', SubmitType::class)
  85. ->getForm();
  86. $userChoiceForm->handleRequest($this->request);
  87. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  88. return $userChoiceForm->get('user')->getData();
  89. }
  90. $parameters = [
  91. 'form' => $userChoiceForm->createView(),
  92. 'formView' => 'default',
  93. 'entity' => $entity,
  94. ];
  95. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  96. }
  97. /**
  98. * The method that is executed when the user performs a 'show' action on an entity.
  99. *
  100. * @return Response
  101. */
  102. public function showAction()
  103. {
  104. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  105. $id = $this->request->query->get('id');
  106. $easyadmin = $this->request->attributes->get('easyadmin');
  107. $entity = $easyadmin['item'];
  108. $fields = $this->entity['show']['fields'];
  109. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  110. //'deleteForm' => $deleteForm,
  111. 'fields' => $fields,
  112. 'entity' => $entity,
  113. ]);
  114. $parameters = [
  115. 'entity' => $entity,
  116. 'fields' => $fields
  117. ];
  118. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  119. }
  120. public function renderUserMerchantTemplate($actionName, $templatePath, array $parameters = []){
  121. if ($actionName == 'show') {
  122. $parameters['add_credit_history_form'] = $this->createCustomForm(CreditHistoryType::class, 'addCreditHistory', $parameters, false)->createView();
  123. }
  124. return parent::renderTemplate($actionName, $templatePath, $parameters);
  125. }
  126. /**
  127. * Réécriture de edit action pr rediriger vers le show */
  128. public function editAction()
  129. {
  130. $id = $this->request->query->get('id');
  131. $entity = $this->request->query->get('entity');
  132. return $this->redirectToRoute('easyadmin', [
  133. 'action' => 'show',
  134. 'entity' => $entity,
  135. 'id' => $id
  136. ]);
  137. }
  138. }