選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AdminController.php 18KB

4年前
4年前
4年前
4年前
4年前
4年前
4年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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, $override = true)
  194. {
  195. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  196. if($override)$formBuilder = $this->overrideFormBuilder($formBuilder, $entity, $view);
  197. return $formBuilder;
  198. }
  199. public function overrideFormBuilder($formBuilder, $entity, $view){
  200. $id = (null !== $entity->getId()) ? $entity->getId() : 0;
  201. if ($entity instanceof StatusInterface) {
  202. $formBuilder->add('status', ChoiceType::class, array(
  203. 'choices' =>array(
  204. 'field.default.statusOptions.offline' => '0',
  205. 'field.default.statusOptions.online' => '1'
  206. ),
  207. 'expanded' => true,
  208. 'required' => true,
  209. 'translation_domain'=> 'lcshop'
  210. )
  211. );
  212. }
  213. if ($entity instanceof TreeInterface) {
  214. $formBuilder->add('parent', EntityType::class, array(
  215. 'class' => $this->entity['class'],
  216. 'query_builder' => function (EntityRepository $repo) use ($id) {
  217. return $repo->createQueryBuilder('e')
  218. ->where('e.parent is NULL')
  219. ->andWhere('e.status >= 0')
  220. ->orderBy('e.status', "DESC");
  221. },
  222. 'required' => false,
  223. )
  224. );
  225. }
  226. $formBuilder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
  227. $form = $event->getForm();
  228. $childrens = $form->all();
  229. $this->loopFormField($form, $childrens);
  230. });
  231. return $formBuilder;
  232. }
  233. private function loopFormField($form, $childrens){
  234. foreach ($childrens as $child) {
  235. $statusInterface = false;
  236. $treeInterface = false;
  237. $type = $child->getConfig()->getType()->getInnerType();;
  238. if ($type instanceof EntityType) {
  239. $passedOptions = $child->getConfig()->getOptions();
  240. $classImplements = class_implements($passedOptions['class']);
  241. $isFilterMerchantInterface = in_array('Lc\ShopBundle\Context\FilterMerchantInterface', $classImplements) ;
  242. $isFilterMultipleMerchantsInterface = in_array('Lc\ShopBundle\Context\FilterMultipleMerchantsInterface', $classImplements) ;
  243. if ($isFilterMerchantInterface || $isFilterMultipleMerchantsInterface) {
  244. if (in_array('Lc\ShopBundle\Context\StatusInterface', $classImplements)) {
  245. $statusInterface = true;
  246. }
  247. if (in_array('Lc\ShopBundle\Context\TreeInterface', $classImplements)) {
  248. $treeInterface = true;
  249. }
  250. $propertyMerchant = 'merchant';
  251. if($isFilterMultipleMerchantsInterface) {
  252. $propertyMerchant .= 's' ;
  253. }
  254. $form->add($child->getName(), EntityType::class, array(
  255. 'class' => $this->em->getClassMetadata($passedOptions['class'])->getName(),
  256. 'label' => $passedOptions['label'],
  257. 'multiple' => isset($passedOptions['multiple']) ? $passedOptions['multiple'] : false,
  258. 'placeholder' => '--',
  259. 'translation_domain'=> 'lcshop',
  260. 'query_builder' => function (EntityRepository $repo) use ($passedOptions, $propertyMerchant, $statusInterface, $treeInterface, $child) {
  261. $queryBuilder = $repo->createQueryBuilder('e');
  262. $propertyMerchant = 'e.' . $propertyMerchant;
  263. if ($passedOptions['class'] == 'App\Entity\PointSale') {
  264. $queryBuilder->where(':currentMerchant MEMBER OF ' . $propertyMerchant);
  265. } else {
  266. $queryBuilder->where($propertyMerchant . ' = :currentMerchant');
  267. }
  268. if($statusInterface){
  269. $queryBuilder->andWhere('e.status >= 0');
  270. }
  271. if($treeInterface && $child->getName() =='parent'){
  272. $queryBuilder->andWhere('e.parent is null');
  273. }
  274. $queryBuilder->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  275. return $queryBuilder;
  276. },
  277. 'choice_label' => function($choice) {
  278. if($choice instanceof StatusInterface && $choice->getStatus() == 0){
  279. return $choice.' [hors ligne]';
  280. }
  281. return $choice;
  282. },
  283. 'required' => $passedOptions['required'],
  284. )
  285. );
  286. }
  287. }else{
  288. $subChildrens = $child->all();
  289. if(count($subChildrens)){
  290. $this->loopFormField($child, $subChildrens);
  291. }
  292. }
  293. }
  294. }
  295. }