Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AdminControllerTrait.php 11KB

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