Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

259 lines
11KB

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