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.

82 lines
3.4KB

  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\Container\Section\SectionContainer;
  12. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  13. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  14. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  15. use Lc\CaracoleBundle\Resolver\SectionResolver;
  16. use Lc\SovBundle\Controller\AbstractAdminController as SovAbstractAdminController;
  17. use Lc\SovBundle\Definition\ActionDefinition;
  18. use Lc\SovBundle\Doctrine\Extension\StatusInterface;
  19. use Lc\SovBundle\Field\Filter\FilterManager;
  20. use Lc\SovBundle\Repository\EntityRepository;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. abstract class AbstractAdminController extends SovAbstractAdminController
  23. {
  24. use AdminControllerTrait;
  25. public function autocompleteFilter(AdminContext $context): JsonResponse
  26. {
  27. $repositoryQuery = $this->get(EntityRepository::class)->createRepositoryQuery(
  28. $this->getRepositoryQuery(),
  29. $context->getSearch(),
  30. $context->getEntity(),
  31. FieldCollection::new([]),
  32. FilterCollection::new()
  33. );
  34. if ($this->isInstanceOf(StatusInterface::class)) {
  35. $repositoryQuery->filterIsOnlineAndOffline();
  36. }
  37. if ($this->isInstanceOf(FilterSectionInterface::class)) {
  38. if($this->getSectionCurrent()){
  39. $repositoryQuery->filterBySection($this->getSectionCurrent());
  40. }else{
  41. $repositoryQuery->filterBySection($this->get(SectionContainer::class)->getStore()->getOneDefault());
  42. }
  43. }
  44. if ($this->isInstanceOf(FilterMerchantInterface::class)) {
  45. $repositoryQuery->filterByMerchant($this->getMerchantCurrent());
  46. }
  47. $autocompleteContext = $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
  48. /** @var CrudControllerInterface $controller */
  49. $controller = $this->get(ControllerFactory::class)->getCrudControllerInstance(
  50. $autocompleteContext[EA::CRUD_CONTROLLER_FQCN],
  51. ActionDefinition::INDEX,
  52. $context->getRequest()
  53. );
  54. /** @var FieldDto $field */
  55. $field = FieldCollection::new(
  56. $controller->configureFields($autocompleteContext['originatingPage'])
  57. )->getByProperty($autocompleteContext['propertyName']);
  58. if($field) {
  59. $filterManager = $this->get(FilterManager::class);
  60. $filteredValue = ['value' => $context->getRequest()->query->get('q')];
  61. $filterManager->applyFilter($repositoryQuery, $field, $filteredValue);
  62. }
  63. $repositoryQuery->select('.' . $autocompleteContext['propertyName']);
  64. $responses = array();
  65. foreach ($repositoryQuery->find() as $result) {
  66. $responses[] = array_values($result)[0];
  67. }
  68. return JsonResponse::fromJsonString(json_encode($responses));
  69. }
  70. }