|
- <?php
-
- namespace Lc\CaracoleBundle\Controller;
-
- use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
- use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
- use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
- use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
- use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
- use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
- use Lc\CaracoleBundle\Container\Section\SectionContainer;
- use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
- use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Resolver\SectionResolver;
- use Lc\SovBundle\Controller\AbstractAdminController as SovAbstractAdminController;
- use Lc\SovBundle\Definition\ActionDefinition;
- use Lc\SovBundle\Doctrine\Extension\StatusInterface;
- use Lc\SovBundle\Field\Filter\FilterManager;
- use Lc\SovBundle\Repository\EntityRepository;
- use Symfony\Component\HttpFoundation\JsonResponse;
-
- abstract class AbstractAdminController extends SovAbstractAdminController
- {
- use AdminControllerTrait;
-
- public function autocompleteFilter(AdminContext $context): JsonResponse
- {
- $repositoryQuery = $this->get(EntityRepository::class)->createRepositoryQuery(
- $this->getRepositoryQuery(),
- $context->getSearch(),
- $context->getEntity(),
- FieldCollection::new([]),
- FilterCollection::new()
- );
-
- if ($this->isInstanceOf(StatusInterface::class)) {
- $repositoryQuery->filterIsOnlineAndOffline();
- }
- if ($this->isInstanceOf(FilterSectionInterface::class)) {
- if($this->getSectionCurrent()){
- $repositoryQuery->filterBySection($this->getSectionCurrent());
- }else{
- $repositoryQuery->filterBySection($this->get(SectionContainer::class)->getStore()->getOneDefault());
- }
- }
-
- if ($this->isInstanceOf(FilterMerchantInterface::class)) {
- $repositoryQuery->filterByMerchant($this->getMerchantCurrent());
- }
-
- $autocompleteContext = $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
-
- /** @var CrudControllerInterface $controller */
- $controller = $this->get(ControllerFactory::class)->getCrudControllerInstance(
- $autocompleteContext[EA::CRUD_CONTROLLER_FQCN],
- ActionDefinition::INDEX,
- $context->getRequest()
- );
- /** @var FieldDto $field */
- $field = FieldCollection::new(
- $controller->configureFields($autocompleteContext['originatingPage'])
- )->getByProperty($autocompleteContext['propertyName']);
-
- $filterManager = $this->get(FilterManager::class);
- $filteredValue = ['value' => $context->getRequest()->query->get('q')];
- $filterManager->applyFilter($repositoryQuery, $field, $filteredValue);
- $repositoryQuery->select('.' . $autocompleteContext['propertyName']);
- //dump($repositoryQuery->getQueryBuilder()->getQuery()->getDQL());
-
- $responses = array();
- foreach ($repositoryQuery->find() as $result) {
- $responses[] = array_values($result)[0];
- }
-
- return JsonResponse::fromJsonString(json_encode($responses));
- }
- }
|