|
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use Lc\ShopBundle\Context\MerchantInterface;
- use Symfony\Component\Security\Core\Security;
-
- class AdminController extends EasyAdminController
- {
- protected $security;
-
- public function __construct(Security $security)
- {
- $this->security = $security;
- }
-
- protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
- {
- if ($pos = strpos($dqlFilter, 'currentMerchant')) {
- $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
- }
-
- $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,
- ] ;
- }
-
- public function updateEntity($entity)
- {
- $this->setUpdated($entity);
- parent::updateEntity($entity);
- }
-
- public function persistEntity($entity)
- {
-
- 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, 'setCreatedAt')) {
- $entity->setCreatedAt(new \DateTime());
- }
-
- if (method_exists($entity, 'setCreatedAt')) {
- $entity->setCreatedBy($this->security->getUser());
- }
-
- if (method_exists($entity, 'getAddress')
- && method_exists($entity->getAddress(), 'setCreatedBy')) {
- $entity->getAddress()->setCreatedBy($this->security->getUser());
- }
-
- $this->setUpdated($entity);
-
- if (method_exists($entity, 'setPosition')) {
- $entity->setPosition(0);
- }
-
- if (method_exists($entity, 'setStatus')) {
- $entity->setStatus(1);
- }
-
- parent::persistEntity($entity);
- }
-
- public function setUpdated($entity)
- {
- if (method_exists($entity, 'setUpdatedAt')) {
- $entity->setUpdatedAt(new \DateTime());
- }
-
- if (method_exists($entity, 'setUpdatedAt')) {
- $entity->setUpdatedBy($this->security->getUser());
- }
-
- if (method_exists($entity, 'getAddress')
- && method_exists($entity->getAddress(), 'setUpdatedBy')) {
-
- $entity->getAddress()->setUpdatedBy($this->security->getUser());
- }
- }
-
- }
|