Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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