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 13KB

vor 3 Jahren
vor 3 Jahren
vor 3 Jahren
vor 3 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. $entityManager->create($newEntity);
  111. $merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
  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. $entityManager->update($newEntity);
  140. $entityManager->flush();
  141. $url = $this->get(AdminUrlGenerator::class)
  142. ->setAction(ActionDefinition::INDEX)
  143. ->generateUrl();
  144. $this->addFlashTranslator(
  145. 'success',
  146. 'duplicateToOtherMerchant',
  147. $this->getTranslationEntityName(),
  148. ['%merchant%' => $merchant->getTitle()]
  149. );
  150. return $this->redirect($url);
  151. }
  152. if ($context->getRequest()->isXmlHttpRequest()) {
  153. $response['data'] = $this->renderView(
  154. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
  155. [
  156. 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
  157. ]
  158. );
  159. return new Response(json_encode($response));
  160. }
  161. else {
  162. throw new \ErrorException('La requête doit être effectué en ajax');
  163. }
  164. }
  165. public function duplicateToOtherSection(
  166. AdminContext $context,
  167. EntityComponent $entityComponent,
  168. TranslatorAdmin $translatorAdmin,
  169. EntityManagerInterface $em
  170. ) {
  171. if (!$this->isGranted(
  172. Permission::EA_EXECUTE_ACTION,
  173. ['action' => ActionDefinition::DUPLICATE, 'entity' => $context->getEntity()]
  174. )) {
  175. throw new ForbiddenActionException($context);
  176. }
  177. if (!$context->getEntity()->isAccessible()) {
  178. throw new InsufficientEntityPermissionException($context);
  179. }
  180. if (!$this->isInstanceOf(FilterSectionInterface::class)) {
  181. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  182. }
  183. $duplicateOtherSectionForm = $this->createForm(
  184. DuplicateToOtherSectionFormType::class,
  185. null,
  186. array(
  187. 'entityClass' => $context->getEntity()->getFqcn(),
  188. 'entityId' => $context->getEntity()->getInstance()->getId(),
  189. 'action' => $context->getRequest()->getUri(),
  190. 'attr' => ['id' => 'duplicate-other-section-form'],
  191. )
  192. );
  193. $duplicateOtherSectionForm->handleRequest($context->getRequest());
  194. if ($duplicateOtherSectionForm->isSubmitted() && $duplicateOtherSectionForm->isValid()) {
  195. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  196. $em->create($newEntity);
  197. $section = $duplicateOtherSectionForm->get('sections')->getData();
  198. $newEntity->setSection($section);
  199. $em->update($newEntity);
  200. $em->flush();
  201. $url = $this->get(AdminUrlGenerator::class)
  202. ->setAction(ActionDefinition::EDIT)
  203. ->setEntityId($newEntity->getId())
  204. ->generateUrl();
  205. $this->addFlashTranslator(
  206. 'success',
  207. 'duplicateToOtherSection',
  208. $this->getTranslationEntityName(),
  209. ['%section%' => $section->getTitle()]
  210. );
  211. //TODO switch merchant route
  212. return $this->redirect($url);
  213. }
  214. if ($context->getRequest()->isXmlHttpRequest()) {
  215. $response['data'] = $this->renderView(
  216. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_section.html.twig',
  217. array(
  218. 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
  219. )
  220. );
  221. return new Response(json_encode($response));
  222. } else {
  223. throw new \ErrorException('La requête doit être effectué en ajax');
  224. }
  225. }
  226. public function buildIndexActions(Actions $actions): void
  227. {
  228. parent::buildIndexActions($actions);
  229. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  230. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
  231. }
  232. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  233. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
  234. }
  235. }
  236. public function getDuplicateToOhterMerchantAction(): Action
  237. {
  238. $duplicateAction = Action::new(
  239. ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT,
  240. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT),
  241. 'fa fa-fw fa-copy'
  242. )
  243. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT)
  244. ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
  245. return $duplicateAction;
  246. }
  247. public function getDuplicateToOhterSectionAction(): Action
  248. {
  249. $duplicateAction = Action::new(
  250. ActionDefinition::DUPLICATE_TO_OTHER_SECTION,
  251. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION),
  252. 'fa fa-fw fa-copy'
  253. )
  254. ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION)
  255. ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
  256. return $duplicateAction;
  257. }
  258. }