No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

258 líneas
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. if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
  99. $entity->getAddress()->setCreatedBy($this->security->getUser());
  100. $entity->getAddress()->setCreatedAt(new \DateTime());
  101. }
  102. if (method_exists($entity, 'getAddresses')
  103. && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
  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. if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
  118. $entity->getAddress()->setUpdatedBy($this->security->getUser());
  119. }
  120. if (method_exists($entity, 'getAddresses')
  121. && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
  122. foreach($entity->getAddresses() as $address) {
  123. $address->setUpdatedBy($this->security->getUser()) ;
  124. $address->setUpdatedAt(new \DateTime()) ;
  125. if(!$address->getCreatedBy()) {
  126. $address->setCreatedBy($this->security->getUser()) ;
  127. $address->setCreatedAt(new \DateTime()) ;
  128. }
  129. }
  130. }
  131. }
  132. public function listChildrenAction()
  133. {
  134. $this->dispatch(EasyAdminEvents::PRE_LIST);
  135. $id = $this->request->query->get('id');
  136. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  137. $easyadmin = $this->request->attributes->get('easyadmin');
  138. $entity = $easyadmin['item'];
  139. $fields = $this->entity['list']['fields'];
  140. $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']);
  141. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  142. $parameters = [
  143. 'paginator' => $paginator,
  144. 'fields' => $fields,
  145. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  146. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  147. 'entity' => $entity,
  148. ];
  149. return $this->executeDynamicMethod('render<EntityName>Template', ['list', $this->entity['templates']['list'], $parameters]);
  150. }
  151. public function sortAction()
  152. {
  153. $this->dispatch(EasyAdminEvents::PRE_LIST);
  154. $entity = null;
  155. if ($this->request->query->get('id')) {
  156. $id = $this->request->query->get('id');
  157. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  158. $easyadmin = $this->request->attributes->get('easyadmin');
  159. $entity = $easyadmin['item'];
  160. }
  161. $fields = $this->entity['list']['fields'];
  162. $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']);
  163. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  164. $positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults()))
  165. ->add('entities', CollectionType::class, array(
  166. 'required' => true,
  167. 'allow_add' => true,
  168. 'entry_type' => AbstractEditPositionType::class,
  169. ))
  170. ->getForm();
  171. $positionForm->handleRequest($this->request);
  172. if ($positionForm->isSubmitted() && $positionForm->isValid()) {
  173. $class = $this->entity['class'];
  174. $repo = $this->em->getRepository($class);
  175. foreach ($positionForm->get('entities')->getData() as $elm) {
  176. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  177. $entity = $repo->find($elm['id']);
  178. $entity->setPosition($elm['position']);
  179. $this->em->persist($entity);
  180. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  181. }
  182. $this->em->flush();
  183. $this->addFlash('success', 'Position modifié', array(), 'mweb');
  184. return $this->redirectToReferrer();
  185. }
  186. $parameters = [
  187. 'paginator' => $paginator,
  188. 'fields' => $fields,
  189. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  190. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  191. 'postion_form' => $positionForm->createView(),
  192. 'entity' => $entity,
  193. 'sortable' => true
  194. ];
  195. return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
  196. }
  197. }