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ů.

307 lines
14KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Entity\ProductCategory;
  4. use Doctrine\DBAL\Types\TextType;
  5. use Doctrine\ORM\EntityRepository;
  6. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  8. use FOS\UserBundle\Model\UserManagerInterface;
  9. use Lc\ShopBundle\Context\MerchantInterface;
  10. use Lc\ShopBundle\Context\ProductCategoryInterface;
  11. use Lc\ShopBundle\Context\SortableInterface;
  12. use Lc\ShopBundle\Context\StatusInterface;
  13. use Lc\ShopBundle\Context\TaxRateInterface;
  14. use Lc\ShopBundle\Context\TreeInterface;
  15. use Lc\ShopBundle\Form\AbstractEditPositionType;
  16. use Lc\ShopBundle\Form\ChoiceProductCategoryType;
  17. use Lc\ShopBundle\Form\PriceType;
  18. use Lc\ShopBundle\Form\ProductType;
  19. use Lc\ShopBundle\Form\Widget\PriceWidgetType;
  20. use Lc\ShopBundle\Repository\BaseRepository;
  21. use Lc\ShopBundle\Repository\ProductCategoryRepository;
  22. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  23. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  24. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  25. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Security\Core\Security;
  28. class AdminController extends EasyAdminController
  29. {
  30. protected $security;
  31. protected $userManager;
  32. public function __construct(Security $security, UserManagerInterface $userManager)
  33. {
  34. $this->security = $security;
  35. $this->userManager = $userManager;
  36. }
  37. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  38. {
  39. if ($pos = strpos($dqlFilter, 'currentMerchant')) {
  40. $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
  41. }
  42. if (new $entityClass instanceof StatusInterface && strpos($dqlFilter, 'entity.status') === false) {
  43. if ($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > = 0');
  44. else $dqlFilter .= sprintf(' entity.status > = 0');
  45. }
  46. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  47. if ($entityClass == 'App\Entity\PointSale') {
  48. $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant')
  49. ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  50. }
  51. return $queryBuilder;
  52. }
  53. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  54. {
  55. $parameters = array_merge(
  56. $parameters,
  57. $this->getTwigExtraParameters()
  58. );
  59. return parent::renderTemplate($actionName, $templatePath, $parameters);
  60. }
  61. public function getTwigExtraParameters()
  62. {
  63. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  64. $merchants = $repositoryMerchant->findAll();
  65. $user = $this->security->getUser();
  66. return [
  67. 'merchants' => $merchants,
  68. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  69. ];
  70. }
  71. protected function removeEntity($entity)
  72. {
  73. $entity->setStatus(-1);
  74. $this->em->persist($entity);
  75. $this->em->flush();
  76. }
  77. public function updateEntity($entity)
  78. {
  79. $this->setUpdated($entity);
  80. parent::updateEntity($entity);
  81. }
  82. public function persistEntity($entity)
  83. {
  84. if ($entity instanceof StatusInterface) {
  85. $entity->setStatus(1);
  86. }
  87. if ($entity instanceof SortableInterface) {
  88. if ($entity instanceof TreeInterface) {
  89. if ($entity->getParent()) {
  90. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
  91. $entity->setPosition($entity->getParent()->getId() . '_' . $total);
  92. } else {
  93. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => null));
  94. $entity->setPosition($total);
  95. }
  96. } else {
  97. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1));
  98. $entity->setPosition($total);
  99. }
  100. }
  101. if (method_exists($entity, 'setMerchant')) {
  102. $entity->setMerchant($this->security->getUser()->getMerchant());
  103. }
  104. if (method_exists($entity, 'addMerchant')) {
  105. if ($entity->getMerchant()->isEmpty()) {
  106. $entity->addMerchant($this->security->getUser()->getMerchant());
  107. }
  108. }
  109. if (method_exists($entity, 'setCreatedBy')) {
  110. $entity->setCreatedBy($this->security->getUser());
  111. }
  112. if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
  113. $entity->getAddress()->setCreatedBy($this->security->getUser());
  114. $entity->getAddress()->setCreatedAt(new \DateTime());
  115. }
  116. if (method_exists($entity, 'getAddresses')
  117. && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
  118. foreach($entity->getAddresses() as $address) {
  119. $address->setCreatedBy($this->security->getUser()) ;
  120. $address->setCreatedAt(new \DateTime()) ;
  121. }
  122. }
  123. $this->setUpdated($entity);
  124. parent::persistEntity($entity);
  125. }
  126. public function setUpdated($entity)
  127. {
  128. if (method_exists($entity, 'setUpdatedBy')) {
  129. $entity->setUpdatedBy($this->security->getUser());
  130. }
  131. if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
  132. $entity->getAddress()->setUpdatedBy($this->security->getUser());
  133. }
  134. if (method_exists($entity, 'getAddresses')
  135. && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
  136. foreach($entity->getAddresses() as $address) {
  137. $address->setUpdatedBy($this->security->getUser()) ;
  138. $address->setUpdatedAt(new \DateTime()) ;
  139. if(!$address->getCreatedBy()) {
  140. $address->setCreatedBy($this->security->getUser()) ;
  141. $address->setCreatedAt(new \DateTime()) ;
  142. }
  143. }
  144. }
  145. }
  146. public function listChildrenAction()
  147. {
  148. $this->dispatch(EasyAdminEvents::PRE_LIST);
  149. $id = $this->request->query->get('id');
  150. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  151. $easyadmin = $this->request->attributes->get('easyadmin');
  152. $entity = $easyadmin['item'];
  153. $fields = $this->entity['list']['fields'];
  154. $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']);
  155. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  156. $parameters = [
  157. 'paginator' => $paginator,
  158. 'fields' => $fields,
  159. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  160. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  161. 'entity' => $entity,
  162. ];
  163. return $this->executeDynamicMethod('render<EntityName>Template', ['list', $this->entity['templates']['list'], $parameters]);
  164. }
  165. public function sortAction()
  166. {
  167. $this->dispatch(EasyAdminEvents::PRE_LIST);
  168. $entity = null;
  169. //Replace this with query builder function, do not use finAll of easyAdmin
  170. if ($this->request->query->get('id')) {
  171. $id = $this->request->query->get('id');
  172. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  173. $easyadmin = $this->request->attributes->get('easyadmin');
  174. $entity = $easyadmin['item'];
  175. }
  176. if ($this->entity['list']['dql_filter']) $this->entity['list']['dql_filter'] .= sprintf(' AND entity.status = 1');
  177. else $this->entity['list']['dql_filter'] .= sprintf(' entity.status = 1');
  178. $fields = $this->entity['list']['fields'];
  179. $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']);
  180. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  181. $positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults()))
  182. ->add('entities', CollectionType::class, array(
  183. 'required' => true,
  184. 'allow_add' => true,
  185. 'entry_type' => AbstractEditPositionType::class,
  186. ))
  187. ->getForm();
  188. $positionForm->handleRequest($this->request);
  189. if ($positionForm->isSubmitted() && $positionForm->isValid()) {
  190. $class = $this->entity['class'];
  191. $repo = $this->em->getRepository($class);
  192. $latsPos = 0;
  193. foreach ($positionForm->get('entities')->getData() as $elm) {
  194. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  195. $entity = $repo->find($elm['id']);
  196. $entity->setPosition($elm['position']);
  197. $this->em->persist($entity);
  198. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  199. $latsPos = $elm['position'];
  200. }
  201. //to do récupérer les élements hors ligne et incrémenter position
  202. foreach ($repo->findAllOfflineAndDelete() as $offlineEntity) {
  203. $latsPos++;
  204. $offlineEntity->setPosition($latsPos);
  205. $this->em->persist($offlineEntity);
  206. }
  207. $this->em->flush();
  208. $this->addFlash('success', 'Position modifié', array(), 'mweb');
  209. return $this->redirectToReferrer();
  210. }
  211. $parameters = [
  212. 'paginator' => $paginator,
  213. 'fields' => $fields,
  214. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  215. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  216. 'postion_form' => $positionForm->createView(),
  217. 'entity' => $entity,
  218. 'sortable' => true
  219. ];
  220. return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
  221. }
  222. public function createEntityFormBuilder($entity, $view)
  223. {
  224. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  225. $id = (null !== $entity->getId()) ? $entity->getId() : 0;
  226. if ($entity instanceof TreeInterface) {
  227. $formBuilder->add('parent', EntityType::class, array(
  228. 'class' => $this->entity['class'],
  229. 'query_builder' => function (EntityRepository $repo) use ($id) {
  230. return $repo->createQueryBuilder('e')
  231. ->where('e.parent is NULL')
  232. ->andWhere('e.status >= 0')
  233. ->orderBy('e.status', "DESC");
  234. },
  235. 'required' => false,
  236. )
  237. );
  238. }
  239. return $formBuilder;
  240. }
  241. }