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.

281 satır
11KB

  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\Doctrine\Extension\FilterMerchantInterface;
  17. use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
  18. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  19. use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
  20. use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType;
  21. use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType;
  22. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  23. use Lc\CaracoleBundle\Resolver\SectionResolver;
  24. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  25. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  26. use Lc\SovBundle\Component\EntityComponent;
  27. use Lc\SovBundle\Factory\User\UserFactory;
  28. use Lc\SovBundle\Translation\TranslatorAdmin;
  29. use Symfony\Component\HttpFoundation\Response;
  30. trait AdminControllerTrait
  31. {
  32. public static function getSubscribedServices()
  33. {
  34. return array_merge(
  35. parent::getSubscribedServices(),
  36. [
  37. 'merchant_resolver' => MerchantResolver::class,
  38. 'section_resolver' => SectionResolver::class,
  39. 'user_factory' => UserFactory::class,
  40. 'user_merchant_factory' => UserMerchantFactory::class,
  41. ]
  42. );
  43. }
  44. public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  45. {
  46. $responseParameters = parent::configureResponseParameters($responseParameters);
  47. // affichage du filtre sur section
  48. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  49. $responseParameters->set('display_switch_section', true);
  50. }
  51. return $responseParameters;
  52. }
  53. public function createIndexQueryBuilder(
  54. SearchDto $searchDto,
  55. EntityDto $entityDto,
  56. FieldCollection $fields,
  57. FilterCollection $filters
  58. ): QueryBuilder {
  59. $queryBuilder = parent::createIndexQueryBuilder(
  60. $searchDto,
  61. $entityDto,
  62. $fields,
  63. $filters
  64. );
  65. //TODO Gérer depuis les événements
  66. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  67. $queryBuilder->andWhereMerchant('entity', $this->get('merchant_resolver')->getCurrent());
  68. }
  69. if ($this->isInstanceOf(FilterMultipleMerchantsInterface::class)) {
  70. $queryBuilder->andWhere(':merchant MEMBER OF entity.merchants');
  71. $queryBuilder->setParameter('merchant', $this->get('merchant_resolver')->getCurrent());
  72. }
  73. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  74. $queryBuilder->andWhereSection('entity', $this->get('section_resolver')->getCurrent());
  75. }
  76. return $queryBuilder;
  77. }
  78. public function duplicateToOtherMerchant(
  79. AdminContext $context,
  80. EntityComponent $entityComponent,
  81. TranslatorAdmin $translatorAdmin,
  82. EntityManagerInterface $em
  83. ) {
  84. if (!$this->isGranted(
  85. Permission::EA_EXECUTE_ACTION,
  86. ['action' => "duplicate", 'entity' => $context->getEntity()]
  87. )) {
  88. throw new ForbiddenActionException($context);
  89. }
  90. if (!$context->getEntity()->isAccessible()) {
  91. throw new InsufficientEntityPermissionException($context);
  92. }
  93. if (!$this->isInstanceOf(FilterMerchantInterface::class)) {
  94. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  95. }
  96. $duplicateOtherMerchantForm = $this->createForm(
  97. DuplicateToOtherMerchantFormType::class,
  98. null,
  99. array(
  100. 'entityClass' => $context->getEntity()->getFqcn(),
  101. 'entityId' => $context->getEntity()->getInstance()->getId(),
  102. 'action' => $context->getRequest()->getUri(),
  103. 'attr' => ['id'=> 'duplicate-other-merchant-form'],
  104. )
  105. );
  106. $duplicateOtherMerchantForm->handleRequest($context->getRequest());
  107. if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) {
  108. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  109. $em->create($newEntity);
  110. $merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
  111. $newEntity->setMerchant($merchant);
  112. $em->update($newEntity);
  113. $em->flush();
  114. $url = $this->get(AdminUrlGenerator::class)
  115. ->setAction(Action::EDIT)
  116. ->setEntityId($newEntity->getId())
  117. ->generateUrl();
  118. $this->addFlash(
  119. 'success',
  120. $translatorAdmin->transFlashMessage(
  121. 'duplicateToOtherMerchant',
  122. ['%merchant%' => $merchant->getTitle()]
  123. ),
  124. array()
  125. );
  126. //TODO switch merchant route
  127. return $this->redirect($url);
  128. }
  129. if ($context->getRequest()->isXmlHttpRequest()) {
  130. $response['data'] = $this->renderView(
  131. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
  132. array(
  133. 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
  134. )
  135. );
  136. return new Response(json_encode($response));
  137. }else{
  138. throw new \ErrorException('La requête doit être effectué en ajax');
  139. }
  140. }
  141. public function duplicateToOtherSection(
  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(FilterSectionInterface::class)) {
  157. throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
  158. }
  159. $duplicateOtherSectionForm = $this->createForm(
  160. DuplicateToOtherSectionFormType::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-section-form'],
  167. )
  168. );
  169. $duplicateOtherSectionForm->handleRequest($context->getRequest());
  170. if ($duplicateOtherSectionForm->isSubmitted() && $duplicateOtherSectionForm->isValid()) {
  171. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  172. $em->create($newEntity);
  173. $section = $duplicateOtherSectionForm->get('sections')->getData();
  174. $newEntity->setSection($section);
  175. $em->update($newEntity);
  176. $em->flush();
  177. $url = $this->get(AdminUrlGenerator::class)
  178. ->setAction(Action::EDIT)
  179. ->setEntityId($newEntity->getId())
  180. ->generateUrl();
  181. $this->addFlash(
  182. 'success',
  183. $translatorAdmin->transFlashMessage(
  184. 'duplicateToOtherSection',
  185. ['%section%' => $section->getTitle()]
  186. ),
  187. array()
  188. );
  189. //TODO switch merchant route
  190. return $this->redirect($url);
  191. }
  192. if ($context->getRequest()->isXmlHttpRequest()) {
  193. $response['data'] = $this->renderView(
  194. '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_section.html.twig',
  195. array(
  196. 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
  197. )
  198. );
  199. return new Response(json_encode($response));
  200. }else{
  201. throw new \ErrorException('La requête doit être effectué en ajax');
  202. }
  203. }
  204. public function buildIndexActions(Actions $actions): void
  205. {
  206. parent::buildIndexActions($actions);
  207. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  208. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
  209. }
  210. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  211. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
  212. }
  213. }
  214. public function getDuplicateToOhterMerchantAction(): Action
  215. {
  216. $duplicateAction = Action::new(
  217. 'duplicateToOtherMerchant',
  218. $this->get('translator_admin')->transAction('duplicateToOtherMerchant'),
  219. 'fa fa-fw fa-copy'
  220. )
  221. ->linkToCrudAction('duplicateToOtherMerchant')
  222. ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
  223. return $duplicateAction;
  224. }
  225. public function getDuplicateToOhterSectionAction(): Action
  226. {
  227. $duplicateAction = Action::new(
  228. 'duplicateToOtherSection',
  229. $this->get('translator_admin')->transAction('duplicateToOtherSection'),
  230. 'fa fa-fw fa-copy'
  231. )
  232. ->linkToCrudAction('duplicateToOtherSection')
  233. ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
  234. return $duplicateAction;
  235. }
  236. }