You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 line
11KB

  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->get(MerchantResolver::class)->getCurrent());
  67. }
  68. if ($sectionCurrent && $this->isInstanceOf(FilterSectionInterface::class)) {
  69. $repositoryQuery->filterBySection($sectionCurrent);
  70. }
  71. return $repositoryQuery;
  72. }
  73. public function duplicateToOtherMerchant(
  74. AdminContext $context,
  75. EntityComponent $entityComponent,
  76. TranslatorAdmin $translatorAdmin,
  77. EntityManagerInterface $em
  78. ) {
  79. if (!$this->isGranted(
  80. Permission::EA_EXECUTE_ACTION,
  81. ['action' => "duplicate", 'entity' => $context->getEntity()]
  82. )) {
  83. throw new ForbiddenActionException($context);
  84. }
  85. if (!$context->getEntity()->isAccessible()) {
  86. throw new InsufficientEntityPermissionException($context);
  87. }
  88. if (!$this->isInstanceOf(FilterMerchantInterface::class)) {
  89. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  90. }
  91. $duplicateOtherMerchantForm = $this->createForm(
  92. DuplicateToOtherMerchantFormType::class,
  93. null,
  94. array(
  95. 'entityClass' => $context->getEntity()->getFqcn(),
  96. 'entityId' => $context->getEntity()->getInstance()->getId(),
  97. 'action' => $context->getRequest()->getUri(),
  98. 'attr' => ['id' => 'duplicate-other-merchant-form'],
  99. )
  100. );
  101. $duplicateOtherMerchantForm->handleRequest($context->getRequest());
  102. if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) {
  103. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  104. $em->create($newEntity);
  105. $merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
  106. $newEntity->setMerchant($merchant);
  107. $em->update($newEntity);
  108. $em->flush();
  109. $url = $this->get(AdminUrlGenerator::class)
  110. ->setAction(ActionDefinition::EDIT)
  111. ->setEntityId($newEntity->getId())
  112. ->generateUrl();
  113. $this->addFlashTranslator(
  114. 'success',
  115. 'duplicateToOtherMerchant',
  116. $this->getTranslationEntityName(),
  117. ['%merchant%' => $merchant->getTitle()]
  118. );
  119. //TODO switch merchant route
  120. return $this->redirect($url);
  121. }
  122. if ($context->getRequest()->isXmlHttpRequest()) {
  123. $response['data'] = $this->renderView(
  124. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
  125. array(
  126. 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
  127. )
  128. );
  129. return new Response(json_encode($response));
  130. } else {
  131. throw new \ErrorException('La requête doit être effectué en ajax');
  132. }
  133. }
  134. public function duplicateToOtherSection(
  135. AdminContext $context,
  136. EntityComponent $entityComponent,
  137. TranslatorAdmin $translatorAdmin,
  138. EntityManagerInterface $em
  139. ) {
  140. if (!$this->isGranted(
  141. Permission::EA_EXECUTE_ACTION,
  142. ['action' => ActionDefinition::DUPLICATE, 'entity' => $context->getEntity()]
  143. )) {
  144. throw new ForbiddenActionException($context);
  145. }
  146. if (!$context->getEntity()->isAccessible()) {
  147. throw new InsufficientEntityPermissionException($context);
  148. }
  149. if (!$this->isInstanceOf(FilterSectionInterface::class)) {
  150. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  151. }
  152. $duplicateOtherSectionForm = $this->createForm(
  153. DuplicateToOtherSectionFormType::class,
  154. null,
  155. array(
  156. 'entityClass' => $context->getEntity()->getFqcn(),
  157. 'entityId' => $context->getEntity()->getInstance()->getId(),
  158. 'action' => $context->getRequest()->getUri(),
  159. 'attr' => ['id' => 'duplicate-other-section-form'],
  160. )
  161. );
  162. $duplicateOtherSectionForm->handleRequest($context->getRequest());
  163. if ($duplicateOtherSectionForm->isSubmitted() && $duplicateOtherSectionForm->isValid()) {
  164. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  165. $em->create($newEntity);
  166. $section = $duplicateOtherSectionForm->get('sections')->getData();
  167. $newEntity->setSection($section);
  168. $em->update($newEntity);
  169. $em->flush();
  170. $url = $this->get(AdminUrlGenerator::class)
  171. ->setAction(ActionDefinition::EDIT)
  172. ->setEntityId($newEntity->getId())
  173. ->generateUrl();
  174. $this->addFlashTranslator(
  175. 'success',
  176. 'duplicateToOtherSection',
  177. $this->getTranslationEntityName(),
  178. ['%section%' => $section->getTitle()]
  179. );
  180. //TODO switch merchant route
  181. return $this->redirect($url);
  182. }
  183. if ($context->getRequest()->isXmlHttpRequest()) {
  184. $response['data'] = $this->renderView(
  185. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_section.html.twig',
  186. array(
  187. 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
  188. )
  189. );
  190. return new Response(json_encode($response));
  191. } else {
  192. throw new \ErrorException('La requête doit être effectué en ajax');
  193. }
  194. }
  195. public function buildIndexActions(Actions $actions): void
  196. {
  197. parent::buildIndexActions($actions);
  198. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  199. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
  200. }
  201. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  202. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
  203. }
  204. }
  205. public function getDuplicateToOhterMerchantAction(): Action
  206. {
  207. $duplicateAction = Action::new(
  208. ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT,
  209. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT),
  210. 'fa fa-fw fa-copy'
  211. )
  212. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT)
  213. ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
  214. return $duplicateAction;
  215. }
  216. public function getDuplicateToOhterSectionAction(): Action
  217. {
  218. $duplicateAction = Action::new(
  219. ActionDefinition::DUPLICATE_TO_OTHER_SECTION,
  220. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION),
  221. 'fa fa-fw fa-copy'
  222. )
  223. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION)
  224. ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
  225. return $duplicateAction;
  226. }
  227. }