security = $security; $this->userManager = $userManager; } protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null) { if ($pos = strpos($dqlFilter, 'currentMerchant')) { $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter)); } if (new $entityClass instanceof StatusInterface) { if($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > 0'); else $dqlFilter .= sprintf(' entity.status > 0'); } $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter); if ($entityClass == 'App\Entity\PointSale') { $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant') ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId()); } return $queryBuilder; } public function renderTemplate($actionName, $templatePath, array $parameters = []) { $parameters = array_merge( $parameters, $this->getTwigExtraParameters() ); return parent::renderTemplate($actionName, $templatePath, $parameters); } public function getTwigExtraParameters() { $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class); $merchants = $repositoryMerchant->findAll(); $user = $this->security->getUser(); return [ 'merchants' => $merchants, 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null, ]; } protected function removeEntity($entity) { $entity->setStatus(-1); $this->em->persist($entity); $this->em->flush(); } public function updateEntity($entity) { $this->setUpdated($entity); parent::updateEntity($entity); } public function persistEntity($entity) { if ($entity instanceof StatusInterface) { $entity->setStatus(1); } if ($entity instanceof SortableInterface) { if ($entity instanceof TreeInterface) { if ($entity->getParent()) { $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => $entity->getParent()->getId())); $entity->setPosition($entity->getParent()->getId() . '_' . $total); } else { $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => null)); $entity->setPosition($total); } }else{ $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1)); $entity->setPosition($total); } } if (method_exists($entity, 'setMerchant')) { $entity->setMerchant($this->security->getUser()->getMerchant()); } if (method_exists($entity, 'addMerchant')) { if ($entity->getMerchant()->isEmpty()) { $entity->addMerchant($this->security->getUser()->getMerchant()); } } if (method_exists($entity, 'setCreatedBy')) { $entity->setCreatedBy($this->security->getUser()); } if (method_exists($entity, 'getAddress') && $entity->getAddress()) { $entity->getAddress()->setCreatedBy($this->security->getUser()); $entity->getAddress()->setCreatedAt(new \DateTime()); } if (method_exists($entity, 'getAddresses') && $entity->getAddresses() && count($entity->getAddresses()) > 0) { foreach($entity->getAddresses() as $address) { $address->setCreatedBy($this->security->getUser()) ; $address->setCreatedAt(new \DateTime()) ; } } $this->setUpdated($entity); parent::persistEntity($entity); } public function setUpdated($entity) { if (method_exists($entity, 'setUpdatedBy')) { $entity->setUpdatedBy($this->security->getUser()); } if (method_exists($entity, 'getAddress') && $entity->getAddress()) { $entity->getAddress()->setUpdatedBy($this->security->getUser()); } if (method_exists($entity, 'getAddresses') && $entity->getAddresses() && count($entity->getAddresses()) > 0) { foreach($entity->getAddresses() as $address) { $address->setUpdatedBy($this->security->getUser()) ; $address->setUpdatedAt(new \DateTime()) ; if(!$address->getCreatedBy()) { $address->setCreatedBy($this->security->getUser()) ; $address->setCreatedAt(new \DateTime()) ; } } } } public function listChildrenAction() { $this->dispatch(EasyAdminEvents::PRE_LIST); $id = $this->request->query->get('id'); $this->entity['list']['dql_filter'] = "entity.parent = " . $id; $easyadmin = $this->request->attributes->get('easyadmin'); $entity = $easyadmin['item']; $fields = $this->entity['list']['fields']; $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']); $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]); $parameters = [ 'paginator' => $paginator, 'fields' => $fields, 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(), 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(), 'entity' => $entity, ]; return $this->executeDynamicMethod('renderTemplate', ['list', $this->entity['templates']['list'], $parameters]); } public function sortAction() { $this->dispatch(EasyAdminEvents::PRE_LIST); $entity = null; if ($this->request->query->get('id')) { $id = $this->request->query->get('id'); $this->entity['list']['dql_filter'] = "entity.parent = " . $id; $easyadmin = $this->request->attributes->get('easyadmin'); $entity = $easyadmin['item']; } $fields = $this->entity['list']['fields']; $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']); $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]); $positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults())) ->add('entities', CollectionType::class, array( 'required' => true, 'allow_add' => true, 'entry_type' => AbstractEditPositionType::class, )) ->getForm(); $positionForm->handleRequest($this->request); if ($positionForm->isSubmitted() && $positionForm->isValid()) { $class = $this->entity['class']; $repo = $this->em->getRepository($class); foreach ($positionForm->get('entities')->getData() as $elm) { $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]); $entity = $repo->find($elm['id']); $entity->setPosition($elm['position']); $this->em->persist($entity); $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]); } $this->em->flush(); $this->addFlash('success', 'Position modifié', array(), 'mweb'); return $this->redirectToReferrer(); } $parameters = [ 'paginator' => $paginator, 'fields' => $fields, 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(), 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(), 'postion_form' => $positionForm->createView(), 'entity' => $entity, 'sortable' => true ]; return $this->executeDynamicMethod('renderTemplate', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]); } }