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.

270 satır
10KB

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