|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Doctrine\ORM\EntityRepository;
- use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use FOS\UserBundle\Model\UserManagerInterface;
- use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
- use Lc\ShopBundle\Context\GlobalParamInterface;
- use Lc\ShopBundle\Context\MerchantInterface;
- use Lc\ShopBundle\Context\SortableInterface;
- use Lc\ShopBundle\Context\StatusInterface;
- use Lc\ShopBundle\Context\TreeInterface;
- use Lc\ShopBundle\Form\AbstractEditPositionType;
- use Lc\ShopBundle\Services\Utils;
- use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- use Symfony\Component\Form\FormEvent;
- use Symfony\Component\Form\FormEvents;
- use Symfony\Component\Security\Core\Security;
-
- class AdminController extends EasyAdminController
- {
- protected $security;
- protected $userManager;
- protected $em;
- protected $utils;
- protected $globalParam ;
- protected $mailjetTransport ;
-
- public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils,
- GlobalParamInterface $globalParam, MailjetTransport $mailjetTransport)
- {
- $this->security = $security;
- $this->userManager = $userManager;
- $this->em = $em;
- $this->utils = $utils;
- $this->globalParam = $globalParam ;
- $this->mailjetTransport = $mailjetTransport ;
- }
-
-
-
- public function showAction()
- {
- $id = $this->request->query->get('id');
- $entity = $this->request->query->get('entity');
-
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'edit',
- 'entity' => $entity,
- 'id' => $id
- ]);
- }
-
-
-
-
- protected function removeEntity($entity)
- {
- if ($entity instanceof StatusInterface) {
- $entity->setStatus(-1);
- $this->em->persist($entity);
- $this->em->flush();
-
- } else {
- parent::removeEntity($entity);
- }
- }
-
- protected function commonDqlFilterQueryBuilder($entityClass, $dqlFilter)
- {
- if ($pos = strpos($dqlFilter, 'currentMerchant')) {
- $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
- }
-
- if (new $entityClass instanceof StatusInterface && strpos($dqlFilter, 'entity.status') === false) {
- if ($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > = 0');
- else $dqlFilter .= sprintf(' entity.status > = 0');
- }
-
- return $dqlFilter;
- }
-
- protected function commonQueryFilter($entityClass, $queryBuilder)
- {
-
- if (new $entityClass instanceof FilterMultipleMerchantsInterface) {
- $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchants')
- ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
- }
-
- }
-
- protected function createSearchQueryBuilder($entityClass, $searchQuery, array $searchableFields, $sortField = null, $sortDirection = null, $dqlFilter = null)
- {
- $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter);
- $queryBuilder = parent::createSearchQueryBuilder($entityClass, $searchQuery, $searchableFields, $sortField, $sortDirection, $dqlFilter);
- $this->commonQueryFilter($entityClass, $queryBuilder);
- return $queryBuilder;
- }
-
- protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
- {
- $dqlFilter = $this->commonDqlFilterQueryBuilder($entityClass, $dqlFilter);
- $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
- $this->commonQueryFilter($entityClass, $queryBuilder);
- 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,
- ];
- }
-
-
-
- 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('render<EntityName>Template', ['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'];
- }
-
- if ($this->entity['list']['dql_filter']) $this->entity['list']['dql_filter'] .= sprintf(' AND entity.status = 1');
- else $this->entity['list']['dql_filter'] .= sprintf(' entity.status = 1');
-
- $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);
-
- $latsPos = 0;
- 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]);
- $latsPos = $elm['position'];
-
- }
-
-
-
-
- $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('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
- }
-
- public function createEntityFormBuilder($entity, $view)
- {
-
- $formBuilder = parent::createEntityFormBuilder($entity, $view);
-
- $id = (null !== $entity->getId()) ? $entity->getId() : 0;
- if ($entity instanceof TreeInterface) {
- $formBuilder->add('parent', EntityType::class, array(
- 'class' => $this->entity['class'],
- 'query_builder' => function (EntityRepository $repo) use ($id) {
- return $repo->createQueryBuilder('e')
- ->where('e.parent is NULL')
- ->andWhere('e.status >= 0')
- ->orderBy('e.status', "DESC");
- },
- 'required' => false,
- )
- );
- }
-
-
- $formBuilder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
- $form = $event->getForm();
- $allChilds = $form->all();
- foreach ($allChilds as $child) {
- $type = $child->getConfig()->getType()->getInnerType();
-
- if ($type instanceof EntityType) {
- $passedOptions = $child->getConfig()->getOptions();
- $classImplements = class_implements($passedOptions['class']);
-
- if (in_array('Lc\ShopBundle\Context\FilterMerchantInterface', $classImplements)) {
-
- $propertyMerchant = 'merchant';
-
- $form->add($child->getName(), EntityType::class, array(
- 'class' => $this->em->getClassMetadata($passedOptions['class'])->getName(),
- 'label' => $passedOptions['label'],
- 'multiple' => isset($passedOptions['multiple']) ? $passedOptions['multiple'] : false,
- 'placeholder' => '--',
- 'query_builder' => function (EntityRepository $repo) use ($passedOptions, $propertyMerchant) {
- $queryBuilder = $repo->createQueryBuilder('e');
- $propertyMerchant = 'e.' . $propertyMerchant;
-
- if ($passedOptions['class'] == 'App\Entity\PointSale') {
- $queryBuilder->where(':currentMerchant MEMBER OF ' . $propertyMerchant);
- } else {
- $queryBuilder->where($propertyMerchant . ' = :currentMerchant');
- }
-
- $queryBuilder->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
- return $queryBuilder;
- },
- 'required' => $passedOptions['required'],
- )
- );
- }
- }
- }
- });
-
- return $formBuilder;
- }
- }
-
|