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.

336 lines
15KB

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