Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AdminController.php 19KB

vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Entity\MerchantConfig;
  4. use Lc\ShopBundle\Context\AddressInterface;
  5. use Lc\ShopBundle\Repository\MerchantConfigRepository;
  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\MerchantConfigInterface;
  12. use Lc\ShopBundle\Context\MerchantInterface;
  13. use Lc\ShopBundle\Context\SortableInterface;
  14. use Lc\ShopBundle\Context\StatusInterface;
  15. use Lc\ShopBundle\Context\TreeInterface;
  16. use Lc\ShopBundle\Form\AbstractEditPositionType;
  17. use Lc\ShopBundle\Form\ChoiceProductCategoryType;
  18. use Lc\ShopBundle\Form\PriceType;
  19. use Lc\ShopBundle\Form\Widget\PriceWidgetType;
  20. use Lc\ShopBundle\Services\Utils;
  21. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  22. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  23. use Symfony\Component\Form\FormEvent;
  24. use Symfony\Component\Form\FormEvents;
  25. use Symfony\Component\Security\Core\Security;
  26. class AdminController extends EasyAdminController
  27. {
  28. protected $security;
  29. protected $userManager;
  30. protected $em ;
  31. protected $utils ;
  32. public function __construct(
  33. Security $security,
  34. UserManagerInterface $userManager,
  35. EntityManagerInterface $em,
  36. Utils $utils)
  37. {
  38. $this->security = $security;
  39. $this->userManager = $userManager;
  40. $this->em = $em ;
  41. $this->utils = $utils ;
  42. }
  43. public function showAction()
  44. {
  45. $id = $this->request->query->get('id');
  46. $entity = $this->request->query->get('entity');
  47. return $this->redirectToRoute('easyadmin', [
  48. 'action' => 'edit',
  49. 'entity' => $entity,
  50. 'id' => $id
  51. ]) ;
  52. }
  53. protected function commonDqlFilterQueryBuilder($entityClass, $dqlFilter)
  54. {
  55. if ($pos = strpos($dqlFilter, 'currentMerchant')) {
  56. $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
  57. }
  58. if (new $entityClass instanceof StatusInterface && strpos($dqlFilter, 'entity.status') === false) {
  59. if ($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > = 0');
  60. else $dqlFilter .= sprintf(' entity.status > = 0');
  61. }
  62. return $dqlFilter;
  63. }
  64. protected function commonQueryFilter($entityClass, $queryBuilder)
  65. {
  66. if ($entityClass == 'App\Entity\PointSale') {
  67. $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant')
  68. ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
  69. }
  70. }
  71. protected function createSearchQueryBuilder($entityClass, $searchQuery, array $searchableFields, $sortField = null, $sortDirection = null, $dqlFilter = null)
  72. {
  73. $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter) ;
  74. $queryBuilder = parent::createSearchQueryBuilder($entityClass, $searchQuery, $searchableFields, $sortField, $sortDirection, $dqlFilter) ;
  75. $this->commonQueryFilter($entityClass, $queryBuilder) ;
  76. return $queryBuilder ;
  77. }
  78. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  79. {
  80. $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter) ;
  81. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  82. $this->commonQueryFilter($entityClass, $queryBuilder) ;
  83. return $queryBuilder ;
  84. }
  85. public function renderTemplate($actionName, $templatePath, array $parameters = [])
  86. {
  87. $parameters = array_merge(
  88. $parameters,
  89. $this->getTwigExtraParameters()
  90. );
  91. return parent::renderTemplate($actionName, $templatePath, $parameters);
  92. }
  93. public function getTwigExtraParameters()
  94. {
  95. $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
  96. $merchants = $repositoryMerchant->findAll();
  97. $user = $this->security->getUser();
  98. return [
  99. 'merchants' => $merchants,
  100. 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
  101. ];
  102. }
  103. protected function removeEntity($entity)
  104. {
  105. if(method_exists($entity, 'setStatus')) {
  106. $entity->setStatus(-1);
  107. $this->em->persist($entity);
  108. $this->em->flush();
  109. }
  110. else {
  111. parent::removeEntity($entity);
  112. }
  113. }
  114. public function updateEntity($entity)
  115. {
  116. $this->setUpdated($entity);
  117. $this->setAddressCreatedBy($entity) ;
  118. parent::updateEntity($entity);
  119. }
  120. public function persistEntity($entity)
  121. {
  122. if ($entity instanceof StatusInterface) {
  123. $entity->setStatus(1);
  124. }
  125. if ($entity instanceof SortableInterface) {
  126. if ($entity instanceof TreeInterface) {
  127. if ($entity->getParent()) {
  128. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
  129. $entity->setPosition($entity->getParent()->getId() . '_' . $total);
  130. } else {
  131. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => null));
  132. $entity->setPosition($total);
  133. }
  134. } else {
  135. $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1));
  136. $entity->setPosition($total);
  137. }
  138. }
  139. if (method_exists($entity, 'setMerchant')) {
  140. $entity->setMerchant($this->security->getUser()->getMerchant());
  141. }
  142. if (method_exists($entity, 'addMerchant')) {
  143. if ($entity->getMerchant()->isEmpty()) {
  144. $entity->addMerchant($this->security->getUser()->getMerchant());
  145. }
  146. }
  147. if (method_exists($entity, 'setCreatedBy')) {
  148. $entity->setCreatedBy($this->security->getUser());
  149. }
  150. $this->setUpdated($entity);
  151. $this->setAddressCreatedBy($entity) ;
  152. parent::persistEntity($entity);
  153. }
  154. public function setAddressCreatedBy($entity)
  155. {
  156. if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
  157. $entity->getAddress()->setCreatedBy($this->security->getUser());
  158. $entity->getAddress()->setCreatedAt(new \DateTime());
  159. }
  160. if (method_exists($entity, 'getAddresses')
  161. && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
  162. foreach($entity->getAddresses() as $address) {
  163. $address->setCreatedBy($this->security->getUser()) ;
  164. $address->setCreatedAt(new \DateTime()) ;
  165. }
  166. }
  167. }
  168. public function setUpdated($entity)
  169. {
  170. if (method_exists($entity, 'setUpdatedBy')) {
  171. $entity->setUpdatedBy($this->security->getUser());
  172. }
  173. if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
  174. $entity->getAddress()->setUpdatedBy($this->security->getUser());
  175. }
  176. if (method_exists($entity, 'getAddresses')
  177. && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
  178. foreach($entity->getAddresses() as $address) {
  179. $address->setUpdatedBy($this->security->getUser()) ;
  180. $address->setUpdatedAt(new \DateTime()) ;
  181. if(!$address->getCreatedBy()) {
  182. $address->setCreatedBy($this->security->getUser()) ;
  183. $address->setCreatedAt(new \DateTime()) ;
  184. }
  185. }
  186. }
  187. }
  188. public function listChildrenAction()
  189. {
  190. $this->dispatch(EasyAdminEvents::PRE_LIST);
  191. $id = $this->request->query->get('id');
  192. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  193. $easyadmin = $this->request->attributes->get('easyadmin');
  194. $entity = $easyadmin['item'];
  195. $fields = $this->entity['list']['fields'];
  196. $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']);
  197. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  198. $parameters = [
  199. 'paginator' => $paginator,
  200. 'fields' => $fields,
  201. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  202. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  203. 'entity' => $entity,
  204. ];
  205. return $this->executeDynamicMethod('render<EntityName>Template', ['list', $this->entity['templates']['list'], $parameters]);
  206. }
  207. public function sortAction()
  208. {
  209. $this->dispatch(EasyAdminEvents::PRE_LIST);
  210. $entity = null;
  211. //Replace this with query builder function, do not use finAll of easyAdmin
  212. if ($this->request->query->get('id')) {
  213. $id = $this->request->query->get('id');
  214. $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
  215. $easyadmin = $this->request->attributes->get('easyadmin');
  216. $entity = $easyadmin['item'];
  217. }
  218. if ($this->entity['list']['dql_filter']) $this->entity['list']['dql_filter'] .= sprintf(' AND entity.status = 1');
  219. else $this->entity['list']['dql_filter'] .= sprintf(' entity.status = 1');
  220. $fields = $this->entity['list']['fields'];
  221. $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']);
  222. $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
  223. $positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults()))
  224. ->add('entities', CollectionType::class, array(
  225. 'required' => true,
  226. 'allow_add' => true,
  227. 'entry_type' => AbstractEditPositionType::class,
  228. ))
  229. ->getForm();
  230. $positionForm->handleRequest($this->request);
  231. if ($positionForm->isSubmitted() && $positionForm->isValid()) {
  232. $class = $this->entity['class'];
  233. $repo = $this->em->getRepository($class);
  234. $latsPos = 0;
  235. foreach ($positionForm->get('entities')->getData() as $elm) {
  236. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  237. $entity = $repo->find($elm['id']);
  238. $entity->setPosition($elm['position']);
  239. $this->em->persist($entity);
  240. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  241. $latsPos = $elm['position'];
  242. }
  243. //to do récupérer les élements hors ligne et incrémenter position
  244. foreach ($repo->findAllOfflineAndDelete() as $offlineEntity) {
  245. $latsPos++;
  246. $offlineEntity->setPosition($latsPos);
  247. $this->em->persist($offlineEntity);
  248. }
  249. $this->em->flush();
  250. $this->addFlash('success', 'Position modifié', array(), 'mweb');
  251. return $this->redirectToReferrer();
  252. }
  253. $parameters = [
  254. 'paginator' => $paginator,
  255. 'fields' => $fields,
  256. 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
  257. 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
  258. 'postion_form' => $positionForm->createView(),
  259. 'entity' => $entity,
  260. 'sortable' => true
  261. ];
  262. return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
  263. }
  264. public function createEntityFormBuilder($entity, $view)
  265. {
  266. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  267. $id = (null !== $entity->getId()) ? $entity->getId() : 0;
  268. if ($entity instanceof TreeInterface) {
  269. $formBuilder->add('parent', EntityType::class, array(
  270. 'class' => $this->entity['class'],
  271. 'query_builder' => function (EntityRepository $repo) use ($id) {
  272. return $repo->createQueryBuilder('e')
  273. ->where('e.parent is NULL')
  274. ->andWhere('e.status >= 0')
  275. ->orderBy('e.status', "DESC");
  276. },
  277. 'required' => false,
  278. )
  279. );
  280. }
  281. // @TODO : À passer dans le controller de App
  282. $formBuilder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
  283. $form = $event->getForm();
  284. $allChilds = $form->all() ;
  285. foreach($allChilds as $child) {
  286. $type = $child->getConfig()->getType()->getInnerType() ;
  287. if($type instanceof EntityType) {
  288. $passedOptions = $child->getConfig()->getOptions() ;
  289. $classImplements = class_implements($passedOptions['class']) ;
  290. if(in_array('App\Context\FilterHubInterface', $classImplements) ||
  291. in_array('Lc\ShopBundle\Context\FilterMerchantInterface', $classImplements)) {
  292. $propertyMerchant = 'merchant' ;
  293. if(in_array('App\Context\FilterHubInterface', $classImplements)) {
  294. $propertyMerchant = 'hub' ;
  295. }
  296. $form->add($child->getName(), EntityType::class, array(
  297. 'class' => $this->em->getClassMetadata($passedOptions['class'])->getName(),
  298. 'label' => $passedOptions['label'],
  299. 'multiple' => isset($passedOptions['multiple']) ? $passedOptions['multiple'] : false,
  300. 'placeholder' => '--',
  301. 'query_builder' => function (EntityRepository $repo) use ($passedOptions, $propertyMerchant) {
  302. $queryBuilder = $repo->createQueryBuilder('e') ;
  303. $propertyMerchant = 'e.'.$propertyMerchant ;
  304. if($passedOptions['class'] == 'App\Entity\PointSale') {
  305. $queryBuilder->where(':currentMerchant MEMBER OF '.$propertyMerchant) ;
  306. }
  307. else {
  308. $queryBuilder->where($propertyMerchant.' = :currentMerchant') ;
  309. }
  310. $queryBuilder->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId()) ;
  311. return $queryBuilder ;
  312. },
  313. 'required' => $passedOptions['required'],
  314. )
  315. );
  316. }
  317. }
  318. }
  319. });
  320. return $formBuilder;
  321. }
  322. }