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.

AdminController.php 15KB

4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
4 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\EntityRepository;
  5. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  7. use FOS\UserBundle\Model\UserManagerInterface;
  8. use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
  9. use Lc\ShopBundle\Context\MerchantInterface;
  10. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  11. use Lc\ShopBundle\Context\StatusInterface;
  12. use Lc\ShopBundle\Context\TreeInterface;
  13. use Lc\ShopBundle\Form\AbstractEditPositionType;
  14. use Lc\ShopBundle\Services\Utils;
  15. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  16. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  19. use Symfony\Component\Form\FormEvent;
  20. use Symfony\Component\Form\FormEvents;
  21. use Symfony\Component\Security\Core\Security;
  22. class AdminController extends EasyAdminController
  23. {
  24. protected $security;
  25. protected $userManager;
  26. protected $em;
  27. protected $utils;
  28. protected $merchantUtils ;
  29. protected $mailjetTransport ;
  30. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils,
  31. MerchantUtilsInterface $merchantUtils, MailjetTransport $mailjetTransport)
  32. {
  33. $this->security = $security;
  34. $this->userManager = $userManager;
  35. $this->em = $em;
  36. $this->utils = $utils;
  37. $this->merchantUtils = $merchantUtils ;
  38. $this->mailjetTransport = $mailjetTransport ;
  39. }
  40. /**
  41. * Réécriture de show action pr rediriger vers l'édition
  42. */
  43. public function showAction()
  44. {
  45. $id = $this->request->query->get('id');
  46. $entity = $this->request->query->get('entity');
  47. return $this->redirectToRoute('easyadmin', [
  48. 'action' => 'edit',
  49. 'entity' => $entity,
  50. 'id' => $id
  51. ]);
  52. }
  53. /**
  54. * Réécriture de removeEntity pour passer les status à -1
  55. */
  56. protected function removeEntity($entity)
  57. {
  58. if ($entity instanceof StatusInterface) {
  59. $entity->setStatus(-1);
  60. $this->em->persist($entity);
  61. $this->em->flush();
  62. } else {
  63. parent::removeEntity($entity);
  64. }
  65. }
  66. protected function commonDqlFilterQueryBuilder($entityClass, $dqlFilter)
  67. {
  68. if ($pos = strpos($dqlFilter, 'currentMerchant')) {
  69. $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
  70. }
  71. if (new $entityClass instanceof StatusInterface && strpos($dqlFilter, 'entity.status') === false) {
  72. if ($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > = 0');
  73. else $dqlFilter .= sprintf(' entity.status > = 0');
  74. }
  75. return $dqlFilter;
  76. }
  77. protected function commonQueryFilter($entityClass, $queryBuilder)
  78. {
  79. if (new $entityClass instanceof FilterMultipleMerchantsInterface) {
  80. $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchants')
  81. ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  82. }
  83. }
  84. protected function createSearchQueryBuilder($entityClass, $searchQuery, array $searchableFields, $sortField = null, $sortDirection = null, $dqlFilter = null)
  85. {
  86. $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter);
  87. $queryBuilder = parent::createSearchQueryBuilder($entityClass, $searchQuery, $searchableFields, $sortField, $sortDirection, $dqlFilter);
  88. $this->commonQueryFilter($entityClass, $queryBuilder);
  89. return $queryBuilder;
  90. }
  91. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  92. {
  93. $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter);
  94. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  95. $this->commonQueryFilter($entityClass, $queryBuilder);
  96. return $queryBuilder;
  97. }
  98. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  99. {
  100. $parameters = array_merge(
  101. $parameters,
  102. $this->getTwigExtraParameters()
  103. );
  104. return parent::renderTemplate($actionName, $templatePath, $parameters);
  105. }
  106. public function getTwigExtraParameters()
  107. {
  108. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  109. $merchants = $repositoryMerchant->findAll();
  110. $user = $this->security->getUser();
  111. return [
  112. 'merchants' => $merchants,
  113. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  114. ];
  115. }
  116. public function listChildrenAction()
  117. {
  118. $this->dispatch(EasyAdminEvents::PRE_LIST);
  119. $id = $this->request->query->get('id');
  120. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  121. $easyadmin = $this->request->attributes->get('easyadmin');
  122. $entity = $easyadmin['item'];
  123. $fields = $this->entity['list']['fields'];
  124. $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->entity['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']);
  125. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  126. $parameters = [
  127. 'paginator' => $paginator,
  128. 'fields' => $fields,
  129. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  130. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  131. 'entity' => $entity,
  132. ];
  133. return $this->executeDynamicMethod('render<EntityName>Template', ['list', $this->entity['templates']['list'], $parameters]);
  134. }
  135. public function sortAction()
  136. {
  137. $this->dispatch(EasyAdminEvents::PRE_LIST);
  138. $entity = null;
  139. //Replace this with query builder function, do not use finAll of easyAdmin
  140. if ($this->request->query->get('id')) {
  141. $id = $this->request->query->get('id');
  142. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  143. $easyadmin = $this->request->attributes->get('easyadmin');
  144. $entity = $easyadmin['item'];
  145. }
  146. if ($this->entity['list']['dql_filter']) $this->entity['list']['dql_filter'] .= sprintf(' AND entity.status = 1');
  147. else $this->entity['list']['dql_filter'] .= sprintf(' entity.status = 1');
  148. $fields = $this->entity['list']['fields'];
  149. $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), 500, $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']);
  150. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  151. $positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults()))
  152. ->add('entities', CollectionType::class, array(
  153. 'required' => true,
  154. 'allow_add' => true,
  155. 'entry_type' => AbstractEditPositionType::class,
  156. ))
  157. ->getForm();
  158. $positionForm->handleRequest($this->request);
  159. if ($positionForm->isSubmitted() && $positionForm->isValid()) {
  160. $class = $this->entity['class'];
  161. $repo = $this->em->getRepository($class);
  162. $latsPos = 0;
  163. foreach ($positionForm->get('entities')->getData() as $elm) {
  164. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  165. $entity = $repo->find($elm['id']);
  166. $entity->setPosition($elm['position']);
  167. $this->em->persist($entity);
  168. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  169. $latsPos = $elm['position'];
  170. }
  171. //die();
  172. //to do récupérer les élements hors ligne et incrémenter position
  173. /*foreach ($repo->findBy(array('status'=> false)) as $offlineEntity) {
  174. $latsPos++;
  175. $offlineEntity->setPosition($latsPos);
  176. $this->em->persist($offlineEntity);
  177. }*/
  178. $this->em->flush();
  179. $this->addFlash('success', 'Position modifié', array(), 'mweb');
  180. return $this->redirectToReferrer();
  181. }
  182. $parameters = [
  183. 'paginator' => $paginator,
  184. 'fields' => $fields,
  185. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  186. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  187. 'postion_form' => $positionForm->createView(),
  188. 'entity' => $entity,
  189. 'sortable' => true
  190. ];
  191. return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
  192. }
  193. public function createEntityFormBuilder($entity, $view)
  194. {
  195. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  196. $id = (null !== $entity->getId()) ? $entity->getId() : 0;
  197. if ($entity instanceof StatusInterface) {
  198. $formBuilder->add('status', ChoiceType::class, array(
  199. 'choices' =>array(
  200. 'field.default.statusOptions.offline' => '0',
  201. 'field.default.statusOptions.online' => '1'
  202. ),
  203. 'expanded' => true,
  204. 'required' => true,
  205. 'translation_domain'=> 'lcshop'
  206. )
  207. );
  208. }
  209. if ($entity instanceof TreeInterface) {
  210. $formBuilder->add('parent', EntityType::class, array(
  211. 'class' => $this->entity['class'],
  212. 'query_builder' => function (EntityRepository $repo) use ($id) {
  213. return $repo->createQueryBuilder('e')
  214. ->where('e.parent is NULL')
  215. ->andWhere('e.status >= 0')
  216. ->orderBy('e.status', "DESC");
  217. },
  218. 'required' => false,
  219. )
  220. );
  221. }
  222. // @TODO : À passer dans le controller de App
  223. $formBuilder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
  224. $form = $event->getForm();
  225. $allChilds = $form->all();
  226. foreach ($allChilds as $child) {
  227. $type = $child->getConfig()->getType()->getInnerType();
  228. if ($type instanceof EntityType) {
  229. $passedOptions = $child->getConfig()->getOptions();
  230. $classImplements = class_implements($passedOptions['class']);
  231. if (in_array('Lc\ShopBundle\Context\FilterMerchantInterface', $classImplements)) {
  232. $propertyMerchant = 'merchant';
  233. $form->add($child->getName(), EntityType::class, array(
  234. 'class' => $this->em->getClassMetadata($passedOptions['class'])->getName(),
  235. 'label' => $passedOptions['label'],
  236. 'multiple' => isset($passedOptions['multiple']) ? $passedOptions['multiple'] : false,
  237. 'placeholder' => '--',
  238. 'query_builder' => function (EntityRepository $repo) use ($passedOptions, $propertyMerchant) {
  239. $queryBuilder = $repo->createQueryBuilder('e');
  240. $propertyMerchant = 'e.' . $propertyMerchant;
  241. if ($passedOptions['class'] == 'App\Entity\PointSale') {
  242. $queryBuilder->where(':currentMerchant MEMBER OF ' . $propertyMerchant);
  243. } else {
  244. $queryBuilder->where($propertyMerchant . ' = :currentMerchant');
  245. }
  246. $queryBuilder->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  247. return $queryBuilder;
  248. },
  249. 'required' => $passedOptions['required'],
  250. )
  251. );
  252. }
  253. }
  254. }
  255. });
  256. return $formBuilder;
  257. }
  258. }