Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace Lc\CaracoleBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\QueryBuilder;
  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\Doctrine\Extension\FilterMerchantInterface;
  17. use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
  18. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  19. use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
  20. use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType;
  21. use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType;
  22. use Lc\CaracoleBundle\Repository\Reduction\ReductionCatalogStore;
  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\Factory\User\UserFactory;
  29. use Lc\SovBundle\Translation\TranslatorAdmin;
  30. use Symfony\Component\HttpFoundation\Response;
  31. trait AdminControllerTrait
  32. {
  33. public static function getSubscribedServices()
  34. {
  35. return array_merge(
  36. parent::getSubscribedServices(),
  37. [
  38. 'merchant_resolver' => MerchantResolver::class,
  39. 'section_resolver' => SectionResolver::class,
  40. 'user_factory' => UserFactory::class,
  41. 'user_merchant_factory' => UserMerchantFactory::class,
  42. ReductionCatalogStore::class => ReductionCatalogStore::class,
  43. ]
  44. );
  45. }
  46. public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  47. {
  48. $responseParameters = parent::configureResponseParameters($responseParameters);
  49. // affichage du filtre sur section
  50. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  51. $responseParameters->set('display_switch_section', true);
  52. }
  53. return $responseParameters;
  54. }
  55. public function createIndexQueryBuilder(
  56. SearchDto $searchDto,
  57. EntityDto $entityDto,
  58. FieldCollection $fields,
  59. FilterCollection $filters
  60. ): QueryBuilder {
  61. $queryBuilder = parent::createIndexQueryBuilder(
  62. $searchDto,
  63. $entityDto,
  64. $fields,
  65. $filters
  66. );
  67. //TODO Gérer depuis les événements
  68. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  69. $queryBuilder->andWhereMerchant('entity', $this->get('merchant_resolver')->getCurrent());
  70. }
  71. if ($this->isInstanceOf(FilterMultipleMerchantsInterface::class)) {
  72. $queryBuilder->andWhere(':merchant MEMBER OF entity.merchants');
  73. $queryBuilder->setParameter('merchant', $this->get('merchant_resolver')->getCurrent());
  74. }
  75. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  76. $queryBuilder->andWhereSection('entity', $this->get('section_resolver')->getCurrent());
  77. }
  78. return $queryBuilder;
  79. }
  80. public function duplicateToOtherMerchant(
  81. AdminContext $context,
  82. EntityComponent $entityComponent,
  83. TranslatorAdmin $translatorAdmin,
  84. EntityManagerInterface $em
  85. ) {
  86. if (!$this->isGranted(
  87. Permission::EA_EXECUTE_ACTION,
  88. ['action' => "duplicate", 'entity' => $context->getEntity()]
  89. )) {
  90. throw new ForbiddenActionException($context);
  91. }
  92. if (!$context->getEntity()->isAccessible()) {
  93. throw new InsufficientEntityPermissionException($context);
  94. }
  95. if (!$this->isInstanceOf(FilterMerchantInterface::class)) {
  96. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  97. }
  98. $duplicateOtherMerchantForm = $this->createForm(
  99. DuplicateToOtherMerchantFormType::class,
  100. null,
  101. array(
  102. 'entityClass' => $context->getEntity()->getFqcn(),
  103. 'entityId' => $context->getEntity()->getInstance()->getId(),
  104. 'action' => $context->getRequest()->getUri(),
  105. 'attr' => ['id'=> 'duplicate-other-merchant-form'],
  106. )
  107. );
  108. $duplicateOtherMerchantForm->handleRequest($context->getRequest());
  109. if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) {
  110. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  111. $em->create($newEntity);
  112. $merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
  113. $newEntity->setMerchant($merchant);
  114. $em->update($newEntity);
  115. $em->flush();
  116. $url = $this->get(AdminUrlGenerator::class)
  117. ->setAction(Action::EDIT)
  118. ->setEntityId($newEntity->getId())
  119. ->generateUrl();
  120. $this->addFlash(
  121. 'success',
  122. $translatorAdmin->transFlashMessage(
  123. 'duplicateToOtherMerchant',
  124. ['%merchant%' => $merchant->getTitle()]
  125. ),
  126. array()
  127. );
  128. //TODO switch merchant route
  129. return $this->redirect($url);
  130. }
  131. if ($context->getRequest()->isXmlHttpRequest()) {
  132. $response['data'] = $this->renderView(
  133. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
  134. array(
  135. 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
  136. )
  137. );
  138. return new Response(json_encode($response));
  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' => "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);
  175. $section = $duplicateOtherSectionForm->get('sections')->getData();
  176. $newEntity->setSection($section);
  177. $em->update($newEntity);
  178. $em->flush();
  179. $url = $this->get(AdminUrlGenerator::class)
  180. ->setAction(Action::EDIT)
  181. ->setEntityId($newEntity->getId())
  182. ->generateUrl();
  183. $this->addFlash(
  184. 'success',
  185. $translatorAdmin->transFlashMessage(
  186. 'duplicateToOtherSection',
  187. ['%section%' => $section->getTitle()]
  188. ),
  189. array()
  190. );
  191. //TODO switch merchant route
  192. return $this->redirect($url);
  193. }
  194. if ($context->getRequest()->isXmlHttpRequest()) {
  195. $response['data'] = $this->renderView(
  196. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_section.html.twig',
  197. array(
  198. 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
  199. )
  200. );
  201. return new Response(json_encode($response));
  202. }else{
  203. throw new \ErrorException('La requête doit être effectué en ajax');
  204. }
  205. }
  206. public function buildIndexActions(Actions $actions): void
  207. {
  208. parent::buildIndexActions($actions);
  209. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  210. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
  211. }
  212. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  213. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
  214. }
  215. }
  216. public function getDuplicateToOhterMerchantAction(): Action
  217. {
  218. $duplicateAction = Action::new(
  219. 'duplicateToOtherMerchant',
  220. $this->get('translator_admin')->transAction('duplicateToOtherMerchant'),
  221. 'fa fa-fw fa-copy'
  222. )
  223. ->linkToCrudAction('duplicateToOtherMerchant')
  224. ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
  225. return $duplicateAction;
  226. }
  227. public function getDuplicateToOhterSectionAction(): Action
  228. {
  229. $duplicateAction = Action::new(
  230. 'duplicateToOtherSection',
  231. $this->get('translator_admin')->transAction('duplicateToOtherSection'),
  232. 'fa fa-fw fa-copy'
  233. )
  234. ->linkToCrudAction('duplicateToOtherSection')
  235. ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
  236. return $duplicateAction;
  237. }
  238. }