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

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