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.

238 lines
10KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Lc\ShopBundle\Context\MerchantInterface;
  7. use Lc\ShopBundle\Form\AbstractEditPositionType;
  8. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  9. use Symfony\Component\Security\Core\Security;
  10. class AdminController extends EasyAdminController
  11. {
  12. protected $security;
  13. protected $userManager;
  14. public function __construct(Security $security, UserManagerInterface $userManager)
  15. {
  16. $this->security = $security;
  17. $this->userManager = $userManager;
  18. }
  19. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  20. {
  21. if ($pos = strpos($dqlFilter, 'currentMerchant')) {
  22. $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
  23. }
  24. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  25. if ($entityClass == 'App\Entity\PointSale') {
  26. $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant')
  27. ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  28. }
  29. return $queryBuilder;
  30. }
  31. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  32. {
  33. $parameters = array_merge(
  34. $parameters,
  35. $this->getTwigExtraParameters()
  36. );
  37. return parent::renderTemplate($actionName, $templatePath, $parameters);
  38. }
  39. public function getTwigExtraParameters()
  40. {
  41. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  42. $merchants = $repositoryMerchant->findAll();
  43. $user = $this->security->getUser();
  44. return [
  45. 'merchants' => $merchants,
  46. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  47. ];
  48. }
  49. public function updateEntity($entity)
  50. {
  51. $this->setUpdated($entity);
  52. parent::updateEntity($entity);
  53. }
  54. public function persistEntity($entity)
  55. {
  56. if (method_exists($entity, 'setPosition')) {
  57. //Product Category avec parent
  58. if (method_exists($entity, 'getProductCategory') && $entity->getProductCategory()) {
  59. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'productCategory' => $entity->getProductCategory()->getId()));
  60. $entity->setPosition($entity->getProductCategory()->getId() . '_' . $total);
  61. //set position
  62. }else{
  63. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'productCategory' => null ));
  64. $entity->setPosition($total);
  65. }
  66. }
  67. if (method_exists($entity, 'setMerchant')) {
  68. $entity->setMerchant($this->security->getUser()->getMerchant());
  69. }
  70. if (method_exists($entity, 'addMerchant')) {
  71. if ($entity->getMerchant()->isEmpty()) {
  72. $entity->addMerchant($this->security->getUser()->getMerchant());
  73. }
  74. }
  75. if (method_exists($entity, 'setCreatedBy')) {
  76. $entity->setCreatedBy($this->security->getUser());
  77. }
  78. //ça bloque tout ce truc là faut qu'on en parle
  79. /* if (method_exists($entity, 'getAddress')) {
  80. $entity->getAddress()->setCreatedBy($this->security->getUser());
  81. $entity->getAddress()->setCreatedAt(new \DateTime());
  82. }
  83. if (method_exists($entity, 'getAddresses')) {
  84. foreach($entity->getAddresses() as $address) {
  85. $address->setCreatedBy($this->security->getUser()) ;
  86. $address->setCreatedAt(new \DateTime()) ;
  87. }
  88. }*/
  89. $this->setUpdated($entity);
  90. if (method_exists($entity, 'setStatus')) {
  91. $entity->setStatus(1);
  92. }
  93. parent::persistEntity($entity);
  94. }
  95. public function setUpdated($entity)
  96. {
  97. if (method_exists($entity, 'setUpdatedBy')) {
  98. $entity->setUpdatedBy($this->security->getUser());
  99. }
  100. //ça bloque tout ce truc là faut qu'on en parle
  101. /*
  102. if (method_exists($entity, 'getAddress')) {
  103. $entity->getAddress()->setUpdatedBy($this->security->getUser());
  104. }
  105. if (method_exists($entity, 'getAddresses')) {
  106. foreach($entity->getAddresses() as $address) {
  107. $address->setUpdatedBy($this->security->getUser()) ;
  108. $address->setUpdatedAt(new \DateTime()) ;
  109. if(!$address->getCreatedBy()) {
  110. $address->setCreatedBy($this->security->getUser()) ;
  111. $address->setCreatedAt(new \DateTime()) ;
  112. }
  113. }
  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.productCategory = " . $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. if($this->request->query->get('id')) {
  140. $id = $this->request->query->get('id');
  141. $this->entity['list']['dql_filter'] = "entity.productCategory = " . $id;
  142. $easyadmin = $this->request->attributes->get('easyadmin');
  143. $entity = $easyadmin['item'];
  144. }
  145. $fields = $this->entity['list']['fields'];
  146. $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']);
  147. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  148. $positionForm =$this->createFormBuilder(array('entities', $paginator->getCurrentPageResults()))
  149. ->add('entities', CollectionType::class ,array(
  150. 'required' => true,
  151. 'allow_add' => true,
  152. 'entry_type' => AbstractEditPositionType::class,
  153. ))
  154. ->getForm();
  155. $positionForm->handleRequest($this->request);
  156. if($positionForm->isSubmitted()&& $positionForm->isValid()){
  157. $class = $this->entity['class'];
  158. $repo = $this->em->getRepository($class);
  159. foreach ($positionForm->get('entities')->getData() as $elm){
  160. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  161. $entity = $repo->find($elm['id']);
  162. $entity->setPosition($elm['position']);
  163. $this->em->persist($entity);
  164. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  165. }
  166. $this->em->flush();
  167. $this->addFlash('success','Position modifié', array(), 'mweb');
  168. return $this->redirectToReferrer();
  169. }
  170. $parameters = [
  171. 'paginator' => $paginator,
  172. 'fields' => $fields,
  173. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  174. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  175. 'postion_form' => $positionForm->createView(),
  176. 'entity' => $entity,
  177. 'sortable'=> true
  178. ];
  179. return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
  180. }
  181. }