Fab 4 years ago
parent
commit
b34befb7fb
2 changed files with 72 additions and 7 deletions
  1. +72
    -6
      ShopBundle/Controller/Admin/AdminController.php
  2. +0
    -1
      ShopBundle/Resources/views/backend/default/layout.html.twig

+ 72
- 6
ShopBundle/Controller/Admin/AdminController.php View File



namespace Lc\ShopBundle\Controller\Admin; namespace Lc\ShopBundle\Controller\Admin;


use App\Entity\PointSale;
use App\Entity\ProductCategory; use App\Entity\ProductCategory;
use App\Entity\ProductFamily;
use Doctrine\DBAL\Types\TextType; use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController; use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents; use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType; use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;


{ {
protected $security; protected $security;
protected $userManager; protected $userManager;
protected $em ;



public function __construct(Security $security, UserManagerInterface $userManager)
public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em)
{ {
$this->security = $security; $this->security = $security;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->em = $em ;
} }


public function showAction()
{
$id = $this->request->query->get('id');
$entity = $this->request->query->get('entity');


protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
return $this->redirectToRoute('easyadmin', [
'action' => 'edit',
'entity' => $entity,
'id' => $id
]) ;
}

protected function commonDqlFilterQueryBuilder($entityClass, $dqlFilter)
{ {
if ($pos = strpos($dqlFilter, 'currentMerchant')) { if ($pos = strpos($dqlFilter, 'currentMerchant')) {
$dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter)); $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
else $dqlFilter .= sprintf(' entity.status > = 0'); else $dqlFilter .= sprintf(' entity.status > = 0');
} }


$queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
return $dqlFilter;
}


protected function commonQueryFilter($entityClass, $queryBuilder)
{
if ($entityClass == 'App\Entity\PointSale') { if ($entityClass == 'App\Entity\PointSale') {
$queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant') $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant')
->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId()); ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
} }


return $queryBuilder;
}

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 ;
} }




) )
); );
} }

// @TODO : À passer dans le controller de App
$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) {
$attributes = $child->getConfig()->getAttributes() ;
$passedOptions = $attributes['data_collector/passed_options'] ;
$classImplements = class_implements($passedOptions['class']) ;

if(in_array('App\Context\FilterHubInterface', $classImplements)) {
$form->add($child->getName(), EntityType::class, array(
'class' => $this->em->getClassMetadata($passedOptions['class'])->getName(),
'label' => $passedOptions['label'],
'query_builder' => function (EntityRepository $repo) {
return $repo->createQueryBuilder('e')
->where('e.hub = :currentMerchant')
->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
},
'required' => $passedOptions['required'],
)
);
}
}
}
});

return $formBuilder; return $formBuilder;
} }
} }

+ 0
- 1
ShopBundle/Resources/views/backend/default/layout.html.twig View File

</div> </div>
</nav> </nav>



<div id="switch-merchant"> <div id="switch-merchant">
{% if is_granted('ROLE_ADMIN') %} {% if is_granted('ROLE_ADMIN') %}
<form action="{{ path('switch_merchant') }}" method="post"> <form action="{{ path('switch_merchant') }}" method="post">

Loading…
Cancel
Save