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