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.

AdminControllerTrait.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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\Container\Address\AddressContainer;
  17. use Lc\CaracoleBundle\Container\Config\TaxRateContainer;
  18. use Lc\CaracoleBundle\Container\Config\UnitContainer;
  19. use Lc\CaracoleBundle\Container\Credit\CreditHistoryContainer;
  20. use Lc\CaracoleBundle\Container\File\DocumentContainer;
  21. use Lc\CaracoleBundle\Container\Merchant\MerchantContainer;
  22. use Lc\CaracoleBundle\Container\Order\OrderPaymentContainer;
  23. use Lc\CaracoleBundle\Container\Order\OrderProductContainer;
  24. use Lc\CaracoleBundle\Container\Order\OrderProductReductionCatalogContainer;
  25. use Lc\CaracoleBundle\Container\Order\OrderProductRefundContainer;
  26. use Lc\CaracoleBundle\Container\Order\OrderReductionCartContainer;
  27. use Lc\CaracoleBundle\Container\Order\OrderReductionCreditContainer;
  28. use Lc\CaracoleBundle\Container\Order\OrderRefundContainer;
  29. use Lc\CaracoleBundle\Container\Order\OrderShopContainer;
  30. use Lc\CaracoleBundle\Container\Order\OrderStatusContainer;
  31. use Lc\CaracoleBundle\Container\Order\OrderStatusHistoryContainer;
  32. use Lc\CaracoleBundle\Container\PointSale\PointSaleContainer;
  33. use Lc\CaracoleBundle\Container\Product\ProductCategoryContainer;
  34. use Lc\CaracoleBundle\Container\Product\ProductContainer;
  35. use Lc\CaracoleBundle\Container\Product\ProductFamilyContainer;
  36. use Lc\CaracoleBundle\Container\Reduction\ReductionCartContainer;
  37. use Lc\CaracoleBundle\Container\Reduction\ReductionCatalogContainer;
  38. use Lc\CaracoleBundle\Container\Reduction\ReductionCreditContainer;
  39. use Lc\CaracoleBundle\Container\Section\OpeningContainer;
  40. use Lc\CaracoleBundle\Container\Section\SectionContainer;
  41. use Lc\CaracoleBundle\Container\Setting\MerchantSettingContainer;
  42. use Lc\CaracoleBundle\Container\Setting\SectionSettingContainer;
  43. use Lc\CaracoleBundle\Container\User\UserMerchantContainer;
  44. use Lc\CaracoleBundle\Container\User\UserPointSaleContainer;
  45. use Lc\CaracoleBundle\Container\User\VisitorContainer;
  46. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  47. use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
  48. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  49. use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
  50. use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType;
  51. use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType;
  52. use Lc\CaracoleBundle\Repository\Reduction\ReductionCatalogStore;
  53. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  54. use Lc\CaracoleBundle\Resolver\SectionResolver;
  55. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  56. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  57. use Lc\SovBundle\Component\EntityComponent;
  58. use Lc\SovBundle\Container\File\FileContainer;
  59. use Lc\SovBundle\Container\Newsletter\NewsletterContainer;
  60. use Lc\SovBundle\Container\Reminder\ReminderContainer;
  61. use Lc\SovBundle\Container\Setting\SiteSettingContainer;
  62. use Lc\SovBundle\Container\Site\NewsContainer;
  63. use Lc\SovBundle\Container\Site\PageContainer;
  64. use Lc\SovBundle\Container\Site\SiteContainer;
  65. use Lc\SovBundle\Container\Ticket\TicketContainer;
  66. use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
  67. use Lc\SovBundle\Container\User\GroupUserContainer;
  68. use Lc\SovBundle\Container\User\UserContainer;
  69. use Lc\SovBundle\Factory\User\UserFactory;
  70. use Lc\SovBundle\Translation\TranslatorAdmin;
  71. use Symfony\Component\HttpFoundation\Response;
  72. trait AdminControllerTrait
  73. {
  74. public static function getSubscribedServices()
  75. {
  76. return array_merge(
  77. parent::getSubscribedServices(),
  78. [
  79. MerchantResolver::class => MerchantResolver::class,
  80. SectionResolver::class=> SectionResolver::class,
  81. AddressContainer::class => AddressContainer::class,
  82. TaxRateContainer::class => TaxRateContainer::class,
  83. UnitContainer::class => UnitContainer::class,
  84. CreditHistoryContainer::class => CreditHistoryContainer::class,
  85. DocumentContainer::class => DocumentContainer::class,
  86. MerchantContainer::class => MerchantContainer::class,
  87. OrderPaymentContainer::class => OrderPaymentContainer::class,
  88. OrderProductContainer::class => OrderProductContainer::class,
  89. OrderProductReductionCatalogContainer::class => OrderProductReductionCatalogContainer::class,
  90. OrderProductRefundContainer::class => OrderProductRefundContainer::class,
  91. OrderReductionCartContainer::class => OrderReductionCartContainer::class,
  92. OrderReductionCreditContainer::class => OrderReductionCreditContainer::class,
  93. OrderRefundContainer::class => OrderRefundContainer::class,
  94. OrderShopContainer::class => OrderShopContainer::class,
  95. OrderStatusContainer::class => OrderStatusContainer::class,
  96. OrderStatusHistoryContainer::class => OrderStatusHistoryContainer::class,
  97. PointSaleContainer::class => PointSaleContainer::class,
  98. ProductCategoryContainer::class => ProductCategoryContainer::class,
  99. ProductContainer::class => ProductContainer::class,
  100. ProductFamilyContainer::class => ProductFamilyContainer::class,
  101. ReductionCartContainer::class => ReductionCartContainer::class,
  102. ReductionCatalogContainer::class => ReductionCatalogContainer::class,
  103. ReductionCreditContainer::class => ReductionCreditContainer::class,
  104. OpeningContainer::class => OpeningContainer::class,
  105. SectionContainer::class => SectionContainer::class,
  106. MerchantSettingContainer::class => MerchantSettingContainer::class,
  107. SectionSettingContainer::class => SectionSettingContainer::class,
  108. UserMerchantContainer::class => UserMerchantContainer::class,
  109. UserPointSaleContainer::class => UserPointSaleContainer::class,
  110. VisitorContainer::class => VisitorContainer::class
  111. ]
  112. );
  113. }
  114. public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  115. {
  116. $responseParameters = parent::configureResponseParameters($responseParameters);
  117. // affichage du filtre sur section
  118. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  119. $responseParameters->set('display_switch_section', true);
  120. }
  121. return $responseParameters;
  122. }
  123. public function createIndexQueryBuilder(
  124. SearchDto $searchDto,
  125. EntityDto $entityDto,
  126. FieldCollection $fields,
  127. FilterCollection $filters
  128. ): QueryBuilder {
  129. $queryBuilder = parent::createIndexQueryBuilder(
  130. $searchDto,
  131. $entityDto,
  132. $fields,
  133. $filters
  134. );
  135. //TODO Gérer depuis les événements
  136. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  137. $queryBuilder->andWhereMerchant('entity', $this->get('merchant_resolver')->getCurrent());
  138. }
  139. if ($this->isInstanceOf(FilterMultipleMerchantsInterface::class)) {
  140. $queryBuilder->andWhere(':merchant MEMBER OF entity.merchants');
  141. $queryBuilder->setParameter('merchant', $this->get('merchant_resolver')->getCurrent());
  142. }
  143. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  144. $queryBuilder->andWhereSection('entity', $this->get('section_resolver')->getCurrent());
  145. }
  146. return $queryBuilder;
  147. }
  148. public function duplicateToOtherMerchant(
  149. AdminContext $context,
  150. EntityComponent $entityComponent,
  151. TranslatorAdmin $translatorAdmin,
  152. EntityManagerInterface $em
  153. ) {
  154. if (!$this->isGranted(
  155. Permission::EA_EXECUTE_ACTION,
  156. ['action' => "duplicate", 'entity' => $context->getEntity()]
  157. )) {
  158. throw new ForbiddenActionException($context);
  159. }
  160. if (!$context->getEntity()->isAccessible()) {
  161. throw new InsufficientEntityPermissionException($context);
  162. }
  163. if (!$this->isInstanceOf(FilterMerchantInterface::class)) {
  164. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  165. }
  166. $duplicateOtherMerchantForm = $this->createForm(
  167. DuplicateToOtherMerchantFormType::class,
  168. null,
  169. array(
  170. 'entityClass' => $context->getEntity()->getFqcn(),
  171. 'entityId' => $context->getEntity()->getInstance()->getId(),
  172. 'action' => $context->getRequest()->getUri(),
  173. 'attr' => ['id' => 'duplicate-other-merchant-form'],
  174. )
  175. );
  176. $duplicateOtherMerchantForm->handleRequest($context->getRequest());
  177. if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) {
  178. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  179. $em->create($newEntity);
  180. $merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
  181. $newEntity->setMerchant($merchant);
  182. $em->update($newEntity);
  183. $em->flush();
  184. $url = $this->get(AdminUrlGenerator::class)
  185. ->setAction(Action::EDIT)
  186. ->setEntityId($newEntity->getId())
  187. ->generateUrl();
  188. $this->addFlash(
  189. 'success',
  190. $translatorAdmin->transFlashMessage(
  191. 'duplicateToOtherMerchant',
  192. ['%merchant%' => $merchant->getTitle()]
  193. ),
  194. array()
  195. );
  196. //TODO switch merchant route
  197. return $this->redirect($url);
  198. }
  199. if ($context->getRequest()->isXmlHttpRequest()) {
  200. $response['data'] = $this->renderView(
  201. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
  202. array(
  203. 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
  204. )
  205. );
  206. return new Response(json_encode($response));
  207. } else {
  208. throw new \ErrorException('La requête doit être effectué en ajax');
  209. }
  210. }
  211. public function duplicateToOtherSection(
  212. AdminContext $context,
  213. EntityComponent $entityComponent,
  214. TranslatorAdmin $translatorAdmin,
  215. EntityManagerInterface $em
  216. ) {
  217. if (!$this->isGranted(
  218. Permission::EA_EXECUTE_ACTION,
  219. ['action' => "duplicate", 'entity' => $context->getEntity()]
  220. )) {
  221. throw new ForbiddenActionException($context);
  222. }
  223. if (!$context->getEntity()->isAccessible()) {
  224. throw new InsufficientEntityPermissionException($context);
  225. }
  226. if (!$this->isInstanceOf(FilterSectionInterface::class)) {
  227. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  228. }
  229. $duplicateOtherSectionForm = $this->createForm(
  230. DuplicateToOtherSectionFormType::class,
  231. null,
  232. array(
  233. 'entityClass' => $context->getEntity()->getFqcn(),
  234. 'entityId' => $context->getEntity()->getInstance()->getId(),
  235. 'action' => $context->getRequest()->getUri(),
  236. 'attr' => ['id' => 'duplicate-other-section-form'],
  237. )
  238. );
  239. $duplicateOtherSectionForm->handleRequest($context->getRequest());
  240. if ($duplicateOtherSectionForm->isSubmitted() && $duplicateOtherSectionForm->isValid()) {
  241. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  242. $em->create($newEntity);
  243. $section = $duplicateOtherSectionForm->get('sections')->getData();
  244. $newEntity->setSection($section);
  245. $em->update($newEntity);
  246. $em->flush();
  247. $url = $this->get(AdminUrlGenerator::class)
  248. ->setAction(Action::EDIT)
  249. ->setEntityId($newEntity->getId())
  250. ->generateUrl();
  251. $this->addFlash(
  252. 'success',
  253. $translatorAdmin->transFlashMessage(
  254. 'duplicateToOtherSection',
  255. ['%section%' => $section->getTitle()]
  256. ),
  257. array()
  258. );
  259. //TODO switch merchant route
  260. return $this->redirect($url);
  261. }
  262. if ($context->getRequest()->isXmlHttpRequest()) {
  263. $response['data'] = $this->renderView(
  264. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_section.html.twig',
  265. array(
  266. 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
  267. )
  268. );
  269. return new Response(json_encode($response));
  270. } else {
  271. throw new \ErrorException('La requête doit être effectué en ajax');
  272. }
  273. }
  274. public function buildIndexActions(Actions $actions): void
  275. {
  276. parent::buildIndexActions($actions);
  277. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  278. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
  279. }
  280. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  281. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
  282. }
  283. }
  284. public function getDuplicateToOhterMerchantAction(): Action
  285. {
  286. $duplicateAction = Action::new(
  287. 'duplicateToOtherMerchant',
  288. $this->get('translator_admin')->transAction('duplicateToOtherMerchant'),
  289. 'fa fa-fw fa-copy'
  290. )
  291. ->linkToCrudAction('duplicateToOtherMerchant')
  292. ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
  293. return $duplicateAction;
  294. }
  295. public function getDuplicateToOhterSectionAction(): Action
  296. {
  297. $duplicateAction = Action::new(
  298. 'duplicateToOtherSection',
  299. $this->get('translator_admin')->transAction('duplicateToOtherSection'),
  300. 'fa fa-fw fa-copy'
  301. )
  302. ->linkToCrudAction('duplicateToOtherSection')
  303. ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
  304. return $duplicateAction;
  305. }
  306. }