174 lines
7.0KB

  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\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, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
  23. {
  24. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);
  25. $this->creditUtils = $utilsManager->getCreditUtils();
  26. }
  27. public function addCreditHistoryAction(){
  28. $easyadmin = $this->request->attributes->get('easyadmin');
  29. $userMerchant = $easyadmin['item'];
  30. $creditHistoryClass = $this->em->getClassMetadata(CreditHistoryInterface::class);
  31. $creditHistory = new $creditHistoryClass->name;
  32. $addCreditHistoryForm = $this->createForm(CreditHistoryType::class, $creditHistory);
  33. $addCreditHistoryForm->handleRequest($this->request);
  34. if ($addCreditHistoryForm->isSubmitted() && $addCreditHistoryForm->isValid()) {
  35. $creditHistory->setUserMerchant($userMerchant);
  36. if($this->creditUtils->saveCreditHistory($creditHistory)){
  37. //TODo envoyer un email à l'utilisateur
  38. $this->utils->addFlash('success', 'success.credit.added');
  39. }else{
  40. $this->utils->addFlash('error', 'error.credit.notAdded');
  41. }
  42. } else {
  43. $this->utils->addFlash('error', $addCreditHistoryForm->getErrors());
  44. }
  45. return $this->redirectToRoute('easyadmin', [
  46. 'action' => 'show',
  47. 'entity' => $this->request->query->get('entity'),
  48. 'id' => $userMerchant->getId()
  49. ]);
  50. }
  51. protected function newAction()
  52. {
  53. $this->dispatch(EasyAdminEvents::PRE_NEW);
  54. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  55. $easyadmin = $this->request->attributes->get('easyadmin');
  56. $easyadmin['item'] = $entity;
  57. $this->request->attributes->set('easyadmin', $easyadmin);
  58. $fields = $this->entity['new']['fields'];
  59. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  60. $newForm->handleRequest($this->request);
  61. //ETAPE 1 Choix de l'utilisateur
  62. $user = $newForm->get('user')->getData();
  63. if ($user == null) {
  64. $user = $this->getUserViaFirstStepForm($entity);
  65. }
  66. if (!$user instanceof UserInterface) return $user;
  67. else {
  68. $userMerchant = $this->creditUtils->activeCredit($user);
  69. return $this->redirectToRoute('easyadmin', [
  70. 'action' => 'edit',
  71. 'entity' => $this->entity['name'],
  72. 'id' => $userMerchant->getId()
  73. ]);
  74. }
  75. }
  76. public function getUserViaFirstStepForm($entity)
  77. {
  78. $userClass = $this->em->getClassMetadata(UserInterface::class);
  79. $userChoiceForm = $this->createFormBuilder($entity)
  80. ->add('user', EntityType::class, array(
  81. 'class' => $userClass->name
  82. ))
  83. ->add('nextStep', SubmitType::class)
  84. ->getForm();
  85. $userChoiceForm->handleRequest($this->request);
  86. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  87. return $userChoiceForm->get('user')->getData();
  88. }
  89. $parameters = [
  90. 'form' => $userChoiceForm->createView(),
  91. 'formView' => 'default',
  92. 'entity' => $entity,
  93. ];
  94. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  95. }
  96. /**
  97. * The method that is executed when the user performs a 'show' action on an entity.
  98. *
  99. * @return Response
  100. */
  101. public function showAction()
  102. {
  103. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  104. $id = $this->request->query->get('id');
  105. $easyadmin = $this->request->attributes->get('easyadmin');
  106. $entity = $easyadmin['item'];
  107. $fields = $this->entity['show']['fields'];
  108. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  109. //'deleteForm' => $deleteForm,
  110. 'fields' => $fields,
  111. 'entity' => $entity,
  112. ]);
  113. $parameters = [
  114. 'entity' => $entity,
  115. 'fields' => $fields
  116. ];
  117. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  118. }
  119. public function renderUserMerchantTemplate($actionName, $templatePath, array $parameters = []){
  120. if ($actionName == 'show') {
  121. $parameters['add_credit_history_form'] = $this->createCustomForm(CreditHistoryType::class, 'addCreditHistory', $parameters, false)->createView();
  122. }
  123. return parent::renderTemplate($actionName, $templatePath, $parameters);
  124. }
  125. /**
  126. * Réécriture de edit action pr rediriger vers le show */
  127. public function editAction()
  128. {
  129. $id = $this->request->query->get('id');
  130. $entity = $this->request->query->get('entity');
  131. return $this->redirectToRoute('easyadmin', [
  132. 'action' => 'show',
  133. 'entity' => $entity,
  134. 'id' => $id
  135. ]);
  136. }
  137. }