Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

286 lines
11KB

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