Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AdminControllerTrait.php 11KB

3 anos atrás
3 anos atrás
3 anos atrás
3 anos atrás
1 ano atrás
1 ano atrás
1 ano atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace Lc\CaracoleBundle\Controller;
  3. use App\Entity\Section\Section;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  7. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  10. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  11. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  12. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  13. use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
  14. use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
  15. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  16. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  17. use Lc\CaracoleBundle\Definition\ActionDefinition;
  18. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  19. use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
  20. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  21. use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType;
  22. use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType;
  23. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  24. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  25. use Lc\CaracoleBundle\Resolver\SectionResolver;
  26. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  27. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  28. use Lc\SovBundle\Component\EntityComponent;
  29. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  30. use Lc\SovBundle\Solver\Setting\SettingSolver;
  31. use Lc\SovBundle\Translation\TranslatorAdmin;
  32. use Symfony\Component\HttpFoundation\Response;
  33. trait AdminControllerTrait
  34. {
  35. use ControllerTrait;
  36. public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  37. {
  38. $responseParameters = parent::configureResponseParameters($responseParameters);
  39. $this->configureResponseParametersFilterSection($responseParameters);
  40. return $responseParameters;
  41. }
  42. public function configureResponseParametersFilterSection(KeyValueStore $responseParameters)
  43. {
  44. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  45. $responseParameters->set('display_switch_section', true);
  46. }
  47. }
  48. public function configureResponseParametersDisableShowAllSections(KeyValueStore $responseParameters)
  49. {
  50. if($this->getSectionCurrent() == null) {
  51. $responseParameters->set('replace_content_with_message', "Vous devez sélectionner une section pour afficher ces données.");
  52. }
  53. }
  54. public function configureResponseParametersDisableShowIfNotSection(KeyValueStore $responseParameters, Section $section)
  55. {
  56. if($this->getSectionCurrent() != $section) {
  57. $responseParameters->set('replace_content_with_message', "Vous devez vous rendre sur la section ". $section->getTitle().' pour afficher cette page');
  58. }
  59. }
  60. public function createIndexRepositoryQuery(
  61. SearchDto $searchDto,
  62. EntityDto $entityDto,
  63. FieldCollection $fields,
  64. FilterCollection $filters
  65. ): RepositoryQueryInterface {
  66. $repositoryQuery = parent::createIndexRepositoryQuery(
  67. $searchDto,
  68. $entityDto,
  69. $fields,
  70. $filters
  71. );
  72. $sectionCurrent = $this->get(SectionResolver::class)->getCurrent();
  73. if ($this->isInstanceOf(FilterMerchantInterface::class)
  74. || $this->isInstanceOf(FilterMultipleMerchantsInterface::class)) {
  75. $repositoryQuery->filterByMerchant($this->getMerchantCurrent());
  76. }
  77. if ($sectionCurrent && $this->isInstanceOf(FilterSectionInterface::class)) {
  78. $repositoryQuery->filterBySection($sectionCurrent);
  79. }
  80. if ($this->isOutOfSection() && $this->isInstanceOf(FilterSectionInterface::class) && !$this->isInstanceOf(FilterMerchantInterface::class)) {
  81. $repositoryQuery->filterByMerchantViaSection($this->getMerchantCurrent());
  82. }
  83. return $repositoryQuery;
  84. }
  85. public function duplicateToOtherMerchant(
  86. AdminContext $context,
  87. EntityComponent $entityComponent,
  88. TranslatorAdmin $translatorAdmin,
  89. EntityManagerInterface $entityManager
  90. ) {
  91. if (!$this->isGranted(
  92. Permission::EA_EXECUTE_ACTION,
  93. ['action' => "duplicate", 'entity' => $context->getEntity()]
  94. )) {
  95. throw new ForbiddenActionException($context);
  96. }
  97. if (!$context->getEntity()->isAccessible()) {
  98. throw new InsufficientEntityPermissionException($context);
  99. }
  100. if (!$this->isInstanceOf(FilterMerchantInterface::class)
  101. && !$this->isInstanceOf(ProductFamilyInterface::class)) {
  102. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  103. }
  104. $duplicateOtherMerchantForm = $this->createForm(
  105. DuplicateToOtherMerchantFormType::class,
  106. null,
  107. [
  108. 'entityClass' => $context->getEntity()->getFqcn(),
  109. 'entityId' => $context->getEntity()->getInstance()->getId(),
  110. 'action' => $context->getRequest()->getUri(),
  111. 'attr' => ['id' => 'duplicate-other-merchant-form'],
  112. ]
  113. );
  114. $duplicateOtherMerchantForm->handleRequest($context->getRequest());
  115. if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) {
  116. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  117. $merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
  118. if($this->isInstanceOf(ProductFamilyInterface::class)) {
  119. $this->getProductFamilyContainer()->getBuilder()->setMerchant($newEntity, $merchant);
  120. }
  121. else {
  122. $newEntity->setMerchant($merchant);
  123. }
  124. $entityManager->create($newEntity, false);
  125. $entityManager->flush();
  126. $url = $this->get(AdminUrlGenerator::class)
  127. ->setAction(ActionDefinition::INDEX)
  128. ->generateUrl();
  129. $this->addFlashTranslator(
  130. 'success',
  131. 'duplicateToOtherMerchant',
  132. $this->getTranslationEntityName(),
  133. ['%merchant%' => $merchant->getTitle()]
  134. );
  135. return $this->redirect($url);
  136. }
  137. if ($context->getRequest()->isXmlHttpRequest()) {
  138. $response['data'] = $this->renderView(
  139. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
  140. [
  141. 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
  142. ]
  143. );
  144. return new Response(json_encode($response));
  145. }
  146. else {
  147. throw new \ErrorException('La requête doit être effectué en ajax');
  148. }
  149. }
  150. public function duplicateToOtherSection(
  151. AdminContext $context,
  152. EntityComponent $entityComponent,
  153. TranslatorAdmin $translatorAdmin,
  154. EntityManagerInterface $em
  155. ) {
  156. if (!$this->isGranted(
  157. Permission::EA_EXECUTE_ACTION,
  158. ['action' => ActionDefinition::DUPLICATE, 'entity' => $context->getEntity()]
  159. )) {
  160. throw new ForbiddenActionException($context);
  161. }
  162. if (!$context->getEntity()->isAccessible()) {
  163. throw new InsufficientEntityPermissionException($context);
  164. }
  165. if (!$this->isInstanceOf(FilterSectionInterface::class)) {
  166. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  167. }
  168. $duplicateOtherSectionForm = $this->createForm(
  169. DuplicateToOtherSectionFormType::class,
  170. null,
  171. array(
  172. 'entityClass' => $context->getEntity()->getFqcn(),
  173. 'entityId' => $context->getEntity()->getInstance()->getId(),
  174. 'action' => $context->getRequest()->getUri(),
  175. 'attr' => ['id' => 'duplicate-other-section-form'],
  176. )
  177. );
  178. $duplicateOtherSectionForm->handleRequest($context->getRequest());
  179. if ($duplicateOtherSectionForm->isSubmitted() && $duplicateOtherSectionForm->isValid()) {
  180. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  181. $em->create($newEntity, false);
  182. $section = $duplicateOtherSectionForm->get('sections')->getData();
  183. $newEntity->setSection($section);
  184. $em->update($newEntity, false);
  185. $em->flush();
  186. $url = $this->get(AdminUrlGenerator::class)
  187. ->setAction(ActionDefinition::EDIT)
  188. ->setEntityId($newEntity->getId())
  189. ->generateUrl();
  190. $this->addFlashTranslator(
  191. 'success',
  192. 'duplicateToOtherSection',
  193. $this->getTranslationEntityName(),
  194. ['%section%' => $section->getTitle()]
  195. );
  196. //TODO switch merchant route
  197. return $this->redirect($url);
  198. }
  199. if ($context->getRequest()->isXmlHttpRequest()) {
  200. $response['data'] = $this->renderView(
  201. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_section.html.twig',
  202. array(
  203. 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
  204. )
  205. );
  206. return new Response(json_encode($response));
  207. } else {
  208. throw new \ErrorException('La requête doit être effectué en ajax');
  209. }
  210. }
  211. public function buildIndexActions(Actions $actions): void
  212. {
  213. parent::buildIndexActions($actions);
  214. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  215. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
  216. }
  217. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  218. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
  219. }
  220. }
  221. public function getDuplicateToOhterMerchantAction(): Action
  222. {
  223. $duplicateAction = Action::new(
  224. ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT,
  225. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT),
  226. 'fa fa-fw fa-copy'
  227. )
  228. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT)
  229. ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
  230. return $duplicateAction;
  231. }
  232. public function getDuplicateToOhterSectionAction(): Action
  233. {
  234. $duplicateAction = Action::new(
  235. ActionDefinition::DUPLICATE_TO_OTHER_SECTION,
  236. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION),
  237. 'fa fa-fw fa-copy'
  238. )
  239. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION)
  240. ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
  241. return $duplicateAction;
  242. }
  243. }