No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

AdminControllerTrait.php 13KB

hace 3 años
hace 3 años
hace 3 años
hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. //
  112. if($this->isInstanceOf(ProductFamilyInterface::class)) {
  113. $sectionStore = $this->getSectionContainer()->getStore()->setMerchant($merchant);
  114. // Les ProductFamilySectionproperty sont créées en double, on rectifie ici
  115. // @TODO : j'imagine qu'on peut faire mieux que ça. Résoudre le problème à la base par exemple.
  116. $productFamilySectionPropertyArray = [];
  117. foreach($newEntity->getProductFamilySectionProperties() as $productFamilySectionProperty) {
  118. $productFamilySectionPropertyArray[$productFamilySectionProperty->getId()] = $productFamilySectionProperty;
  119. $newEntity->removeProductFamilySectionProperty($productFamilySectionProperty);
  120. }
  121. foreach($productFamilySectionPropertyArray as $productFamilySectionProperty) {
  122. $oldSection = $productFamilySectionProperty->getSection();
  123. $newSection = $sectionStore->getOneByDevAlias($oldSection->getDevAlias());
  124. if($newSection) {
  125. $productFamilySectionProperty->setProductFamily($newEntity);
  126. $productFamilySectionProperty->setSection($newSection);
  127. $newEntity->addProductFamilySectionProperty($productFamilySectionProperty);
  128. }
  129. else {
  130. $entityManager->remove($productFamilySectionProperty);
  131. $newEntity->removeProductFamilySectionProperty($productFamilySectionProperty);
  132. }
  133. }
  134. $newEntity->initProductCategories();
  135. }
  136. else {
  137. $newEntity->setMerchant($merchant);
  138. }
  139. //
  140. //
  141. $entityManager->create($newEntity, false);
  142. $entityManager->flush();
  143. $url = $this->get(AdminUrlGenerator::class)
  144. ->setAction(ActionDefinition::INDEX)
  145. ->generateUrl();
  146. $this->addFlashTranslator(
  147. 'success',
  148. 'duplicateToOtherMerchant',
  149. $this->getTranslationEntityName(),
  150. ['%merchant%' => $merchant->getTitle()]
  151. );
  152. return $this->redirect($url);
  153. }
  154. if ($context->getRequest()->isXmlHttpRequest()) {
  155. $response['data'] = $this->renderView(
  156. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
  157. [
  158. 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
  159. ]
  160. );
  161. return new Response(json_encode($response));
  162. }
  163. else {
  164. throw new \ErrorException('La requête doit être effectué en ajax');
  165. }
  166. }
  167. public function duplicateToOtherSection(
  168. AdminContext $context,
  169. EntityComponent $entityComponent,
  170. TranslatorAdmin $translatorAdmin,
  171. EntityManagerInterface $em
  172. ) {
  173. if (!$this->isGranted(
  174. Permission::EA_EXECUTE_ACTION,
  175. ['action' => ActionDefinition::DUPLICATE, 'entity' => $context->getEntity()]
  176. )) {
  177. throw new ForbiddenActionException($context);
  178. }
  179. if (!$context->getEntity()->isAccessible()) {
  180. throw new InsufficientEntityPermissionException($context);
  181. }
  182. if (!$this->isInstanceOf(FilterSectionInterface::class)) {
  183. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  184. }
  185. $duplicateOtherSectionForm = $this->createForm(
  186. DuplicateToOtherSectionFormType::class,
  187. null,
  188. array(
  189. 'entityClass' => $context->getEntity()->getFqcn(),
  190. 'entityId' => $context->getEntity()->getInstance()->getId(),
  191. 'action' => $context->getRequest()->getUri(),
  192. 'attr' => ['id' => 'duplicate-other-section-form'],
  193. )
  194. );
  195. $duplicateOtherSectionForm->handleRequest($context->getRequest());
  196. if ($duplicateOtherSectionForm->isSubmitted() && $duplicateOtherSectionForm->isValid()) {
  197. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  198. $em->create($newEntity, false);
  199. $section = $duplicateOtherSectionForm->get('sections')->getData();
  200. $newEntity->setSection($section);
  201. $em->update($newEntity, false);
  202. $em->flush();
  203. $url = $this->get(AdminUrlGenerator::class)
  204. ->setAction(ActionDefinition::EDIT)
  205. ->setEntityId($newEntity->getId())
  206. ->generateUrl();
  207. $this->addFlashTranslator(
  208. 'success',
  209. 'duplicateToOtherSection',
  210. $this->getTranslationEntityName(),
  211. ['%section%' => $section->getTitle()]
  212. );
  213. //TODO switch merchant route
  214. return $this->redirect($url);
  215. }
  216. if ($context->getRequest()->isXmlHttpRequest()) {
  217. $response['data'] = $this->renderView(
  218. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_section.html.twig',
  219. array(
  220. 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
  221. )
  222. );
  223. return new Response(json_encode($response));
  224. } else {
  225. throw new \ErrorException('La requête doit être effectué en ajax');
  226. }
  227. }
  228. public function buildIndexActions(Actions $actions): void
  229. {
  230. parent::buildIndexActions($actions);
  231. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  232. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
  233. }
  234. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  235. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
  236. }
  237. }
  238. public function getDuplicateToOhterMerchantAction(): Action
  239. {
  240. $duplicateAction = Action::new(
  241. ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT,
  242. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT),
  243. 'fa fa-fw fa-copy'
  244. )
  245. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT)
  246. ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
  247. return $duplicateAction;
  248. }
  249. public function getDuplicateToOhterSectionAction(): Action
  250. {
  251. $duplicateAction = Action::new(
  252. ActionDefinition::DUPLICATE_TO_OTHER_SECTION,
  253. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION),
  254. 'fa fa-fw fa-copy'
  255. )
  256. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION)
  257. ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
  258. return $duplicateAction;
  259. }
  260. }