Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

318 lines
14KB

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