Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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