You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
3.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller;
  3. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  4. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  6. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  7. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  9. use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
  10. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  11. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  12. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  13. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  14. use Lc\SovBundle\Controller\AbstractAdminController as SovAbstractAdminController;
  15. use Lc\SovBundle\Definition\ActionDefinition;
  16. use Lc\SovBundle\Doctrine\Extension\StatusInterface;
  17. use Lc\SovBundle\Field\Filter\FilterManager;
  18. use Lc\SovBundle\Repository\EntityRepository;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. abstract class AbstractAdminController extends SovAbstractAdminController
  21. {
  22. use AdminControllerTrait;
  23. public function autocompleteFilter(AdminContext $context): JsonResponse
  24. {
  25. $repositoryQuery = $this->get(EntityRepository::class)->createRepositoryQuery(
  26. $this->getRepositoryQuery(),
  27. $context->getSearch(),
  28. $context->getEntity(),
  29. FieldCollection::new([]),
  30. FilterCollection::new()
  31. );
  32. if ($this->isInstanceOf(StatusInterface::class)) {
  33. $repositoryQuery->filterIsOnlineAndOffline();
  34. }
  35. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  36. $repositoryQuery->filterBySection($this->getSectionCurrent());
  37. }
  38. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  39. $repositoryQuery->filterByMerchant($this->getMerchantCurrent());
  40. }
  41. $autocompleteContext = $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
  42. /** @var CrudControllerInterface $controller */
  43. $controller = $this->get(ControllerFactory::class)->getCrudControllerInstance(
  44. $autocompleteContext[EA::CRUD_CONTROLLER_FQCN],
  45. ActionDefinition::INDEX,
  46. $context->getRequest()
  47. );
  48. /** @var FieldDto $field */
  49. $field = FieldCollection::new(
  50. $controller->configureFields($autocompleteContext['originatingPage'])
  51. )->getByProperty($autocompleteContext['propertyName']);
  52. $filterManager = $this->get(FilterManager::class);
  53. $filteredValue = ['value' => $context->getRequest()->query->get('q')];
  54. $filterManager->applyFilter($repositoryQuery, $field, $filteredValue);
  55. $repositoryQuery->select('.' . $autocompleteContext['propertyName']);
  56. //dump($repositoryQuery->getQueryBuilder()->getQuery()->getDQL());
  57. $responses = array();
  58. foreach ($repositoryQuery->find() as $result) {
  59. $responses[] = array_values($result)[0];
  60. }
  61. return JsonResponse::fromJsonString(json_encode($responses));
  62. }
  63. }