Fab 4 年前
父节点
当前提交
b34befb7fb
共有 2 个文件被更改,包括 72 次插入7 次删除
  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 查看文件

@@ -2,8 +2,12 @@

namespace Lc\ShopBundle\Controller\Admin;

use App\Entity\PointSale;
use App\Entity\ProductCategory;
use App\Entity\ProductFamily;
use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
@@ -24,7 +28,10 @@ use Lc\ShopBundle\Repository\ProductCategoryRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
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\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

@@ -32,17 +39,28 @@ class AdminController extends EasyAdminController
{
protected $security;
protected $userManager;
protected $em ;


public function __construct(Security $security, UserManagerInterface $userManager)
public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em)
{
$this->security = $security;
$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')) {
$dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
@@ -53,14 +71,32 @@ class AdminController extends EasyAdminController
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') {
$queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant')
->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 ;
}


@@ -300,6 +336,36 @@ class AdminController extends EasyAdminController
)
);
}

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

+ 0
- 1
ShopBundle/Resources/views/backend/default/layout.html.twig 查看文件

@@ -69,7 +69,6 @@
</div>
</nav>


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

正在加载...
取消
保存