Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

703 lines
25KB

  1. <?php
  2. namespace Lc\SovBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\QueryBuilder;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
  7. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  8. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  10. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  11. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  12. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  13. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  14. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  15. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
  16. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController as EaAbstractCrudController;
  17. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  18. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  19. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  20. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  21. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  22. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  23. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
  24. use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
  25. use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
  26. use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
  27. use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
  28. use EasyCorp\Bundle\EasyAdminBundle\Factory\FilterFactory;
  29. use EasyCorp\Bundle\EasyAdminBundle\Factory\PaginatorFactory;
  30. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  31. use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
  32. use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
  33. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  34. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  35. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  36. use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
  37. use Lc\SovBundle\Doctrine\Extension\SeoInterface;
  38. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  39. use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
  40. use Lc\SovBundle\Doctrine\Extension\TreeInterface;
  41. use Lc\SovBundle\Field\CollectionField;
  42. use Lc\SovBundle\Field\Filter\FilterManager;
  43. use Lc\SovBundle\Form\Common\FiltersFormType;
  44. use Lc\SovBundle\Form\Common\PositionType;
  45. use Lc\SovBundle\Translation\TranslatorAdmin;
  46. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  47. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  48. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  49. use Symfony\Component\Form\Extension\Core\Type\DateType;
  50. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  51. use Symfony\Component\Form\Extension\Core\Type\TextType;
  52. use Symfony\Component\HttpFoundation\JsonResponse;
  53. use Symfony\Component\HttpFoundation\RequestStack;
  54. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  55. abstract class AbstractAdminController extends EaAbstractCrudController
  56. {
  57. protected $filtersForm;
  58. public static function getSubscribedServices()
  59. {
  60. return array_merge(
  61. parent::getSubscribedServices(),
  62. [
  63. 'session' => SessionInterface::class,
  64. 'request' => RequestStack::class,
  65. 'em' => EntityManagerInterface::class,
  66. 'translator_admin' => TranslatorAdmin::class,
  67. 'filter_manager' => FilterManager::class,
  68. ]
  69. );
  70. }
  71. public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  72. {
  73. $this->overrideGlobalActions($responseParameters->get('global_actions'));
  74. $this->overrideEntitiesActions($responseParameters->get('entities'));
  75. if (Crud::PAGE_INDEX === $responseParameters->get('pageName')) {
  76. $responseParameters->set('fields', $this->configureFields('index'));
  77. if ($this->filtersForm === null) {
  78. die('nncncd');
  79. //TODO delete under code
  80. $options['fields'] = $responseParameters->get('fields');
  81. $options['entity_class'] = $this->getEntityFqcn();
  82. $options['entity_name'] = $responseParameters->get('entity')->getName();
  83. $this->filtersForm = $this->createForm(FiltersFormType::class, null, $options);
  84. }
  85. $responseParameters->set('filters_form', $this->filtersForm);
  86. }
  87. return $responseParameters;
  88. }
  89. public function overrideEntitiesActions(?EntityCollection $entities): void
  90. {
  91. }
  92. public function overrideGlobalActions(?ActionCollection $actions): void
  93. {
  94. if ($actions) {
  95. $context = $this->get(AdminContextProvider::class)->getContext();
  96. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  97. foreach ($actions as $i => $action) {
  98. //récriture du bouton 'retour au parent'
  99. if ($action->getName() == 'index_parent') {
  100. $entity = $context->getEntity()->getInstance();
  101. if ($entity !== null) {
  102. if ($entity->getParent() !== null) {
  103. $url = $adminUrlGenerator
  104. ->setController($context->getCrud()->getControllerFqcn())
  105. ->set('entityId', $entity->getParent()->getId())
  106. ->generateUrl();
  107. $action->setLinkUrl($url);
  108. }
  109. } else {
  110. unset($actions[$i]);
  111. }
  112. }
  113. if ($action->getName() == 'sort') {
  114. $entityId = $context->getRequest()->get('entityId');
  115. if ($entityId != null) {
  116. $url = $adminUrlGenerator
  117. ->setController($context->getCrud()->getControllerFqcn())
  118. ->setAction($action->getName())
  119. ->set('entityId', $entityId)
  120. ->generateUrl();
  121. $action->setLinkUrl($url);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. public function configureCrud(Crud $crud): Crud
  128. {
  129. $crud = parent::configureCrud($crud);
  130. $this->setMaxResults($crud);
  131. if ($this->isInstanceOf(SortableInterface::class)) {
  132. $crud->setDefaultSort(['position' => 'ASC']);
  133. }
  134. return $crud;
  135. }
  136. public function setMaxResults(Crud $crud): void
  137. {
  138. $entityClass = $this->getEntityFqcn();
  139. $paramListMaxResults = 'listMaxResults';
  140. $paramSessionListMaxResults = $entityClass . '-' . $paramListMaxResults;
  141. $requestListMaxResults = $this->get('request')->getCurrentRequest()->get($paramListMaxResults);
  142. if ($requestListMaxResults) {
  143. $this->get('session')->set($paramSessionListMaxResults, $requestListMaxResults);
  144. }
  145. $maxResults = $this->get('session')->get($paramSessionListMaxResults) ? $this->get('session')->get(
  146. $paramSessionListMaxResults
  147. ) : 30;
  148. $crud->setPaginatorPageSize($maxResults);
  149. }
  150. public function getSeoPanel(): ?array
  151. {
  152. if ($this->isInstanceOf(SeoInterface::class)) {
  153. return [
  154. FormField::addPanel('seo')->setTemplateName('crud/field/generic'),
  155. TextField::new('metaTitle')->setLabel('Meta Title')->setHelp(
  156. 'Affiché dans les résultats de recherche Google'
  157. )->hideOnIndex(),
  158. TextareaField::new('metaDescription')->setLabel('Meta description')->setHelp(
  159. 'Affiché dans les résultats de recherche Google'
  160. )->hideOnIndex(),
  161. CollectionField::new('oldUrls')
  162. ->setFormTypeOption('entry_type', TextType::class)->setLabel(
  163. 'Anciennes urls du document'
  164. )->hideOnIndex(),
  165. ];
  166. } else {
  167. return null;
  168. }
  169. }
  170. public function getConfPanel(): ?array
  171. {
  172. if ($this->isInstanceOf(DevAliasInterface::class)) {
  173. return [
  174. FormField::addPanel('configuration')->setTemplateName('crud/field/generic'),
  175. TextField::new('devAlias')->hideOnIndex(),
  176. ];
  177. } else {
  178. return null;
  179. }
  180. }
  181. public function sort(AdminContext $context)
  182. {
  183. $event = new BeforeCrudActionEvent($context);
  184. $this->get('event_dispatcher')->dispatch($event);
  185. if ($event->isPropagationStopped()) {
  186. return $event->getResponse();
  187. }
  188. //if (!$this->isGranted(Permission::EA_EXECUTE_ACTION) || !$this->isInstanceOf(SortableInterface::class)) {
  189. if (!$this->isInstanceOf(SortableInterface::class)) {
  190. throw new ForbiddenActionException($context);
  191. }
  192. $fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
  193. $filters = $this->get(FilterFactory::class)->create(
  194. $context->getCrud()->getFiltersConfig(),
  195. $fields,
  196. $context->getEntity()
  197. );
  198. $queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), $fields, $filters);
  199. $paginator = $this->get(PaginatorFactory::class)->create($queryBuilder);
  200. $entities = $this->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
  201. $this->get(EntityFactory::class)->processFieldsForAll($entities, $fields);
  202. $sortableForm = $this->createFormBuilder(array('entities', $paginator->getResults()))
  203. ->add(
  204. 'entities',
  205. CollectionType::class,
  206. array(
  207. 'required' => true,
  208. 'allow_add' => true,
  209. 'entry_type' => PositionType::class,
  210. )
  211. )
  212. ->getForm();
  213. $entityManager = $this->getDoctrine()->getManagerForClass($this->getEntityFqcn());
  214. $repository = $entityManager->getRepository($this->getEntityFqcn());
  215. $sortableForm->handleRequest($context->getRequest());
  216. if ($sortableForm->isSubmitted() && $sortableForm->isValid()) {
  217. foreach ($sortableForm->get('entities')->getData() as $elm) {
  218. $entityInstance = $repository->find($elm['id']);
  219. $entityDto = $context->getEntity()->newWithInstance($entityInstance);
  220. if (!$entityDto->isAccessible()) {
  221. throw new InsufficientEntityPermissionException($context);
  222. }
  223. $event = new BeforeEntityDeletedEvent($entityInstance);
  224. $this->get('event_dispatcher')->dispatch($event);
  225. $entityInstance = $event->getEntityInstance();
  226. $entityInstance->setPosition($elm['position']);
  227. $this->updateEntity($entityManager, $entityInstance);
  228. $this->get('event_dispatcher')->dispatch(new AfterEntityUpdatedEvent($entityInstance));
  229. }
  230. $url = $this->get(AdminUrlGenerator::class)
  231. ->setAction(Action::INDEX)
  232. ->generateUrl();
  233. $this->addFlash('success', $this->translatorAdmin->transFlashMessage('sort'), array());
  234. return $this->redirect($url);
  235. }
  236. $responseParameters = $this->configureResponseParameters(
  237. KeyValueStore::new(
  238. [
  239. 'pageName' => Crud::PAGE_INDEX,
  240. 'templatePath' => '@LcSov/adminlte/crud/sort.html.twig',
  241. 'entities' => $entities,
  242. 'paginator' => $paginator,
  243. 'global_actions' => array(),
  244. 'batch_actions' => array(),
  245. 'filters' => $filters,
  246. 'sortable_form' => $sortableForm,
  247. ]
  248. )
  249. );
  250. $responseParameters->set('fields', $this->configureFields('index'));
  251. $event = new AfterCrudActionEvent($context, $responseParameters);
  252. $this->get('event_dispatcher')->dispatch($event);
  253. if ($event->isPropagationStopped()) {
  254. return $event->getResponse();
  255. }
  256. return $responseParameters;
  257. }
  258. public function createIndexQueryBuilder(
  259. SearchDto $searchDto,
  260. EntityDto $entityDto,
  261. FieldCollection $fields,
  262. FilterCollection $filters
  263. ): QueryBuilder {
  264. $queryBuilder = parent::createIndexQueryBuilder(
  265. $searchDto,
  266. $entityDto,
  267. $fields,
  268. $filters
  269. );
  270. //TOdo utiliser les repositoryQuery ?
  271. if ($this->isInstanceOf(TreeInterface::class)) {
  272. $entityId = $searchDto->getRequest()->get('entityId');
  273. if ($entityId !== null) {
  274. $queryBuilder->andWhereParent('entity', $entityId);
  275. } else {
  276. $queryBuilder->andWhereParentIsNull('entity');
  277. }
  278. }
  279. $this->filtersForm = $this->createForm(
  280. FiltersFormType::class,
  281. null,
  282. array(
  283. 'fields' => $fields,
  284. 'entity_dto' => $entityDto,
  285. 'entity_class' => $this->getEntityFqcn(),
  286. 'entity_name' => $entityDto->getName(),
  287. )
  288. );
  289. $filterManager = $this->get('filter_manager');
  290. $this->filtersForm->handleRequest($searchDto->getRequest());
  291. /*if (($this->filtersForm->isSubmitted() && $this->filtersForm->isValid())) {
  292. }*/
  293. $filterManager->handleFiltersForm($queryBuilder, $this->filtersForm, $fields, $entityDto);
  294. return $queryBuilder;
  295. }
  296. public function createSortQueryBuilder(
  297. SearchDto $searchDto,
  298. EntityDto $entityDto,
  299. FieldCollection $fields,
  300. FilterCollection $filters
  301. ): QueryBuilder {
  302. $queryBuilder = $this->createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
  303. return $queryBuilder;
  304. }
  305. public function edit(AdminContext $context)
  306. {
  307. $response = parent::edit($context);;
  308. // on vide le flash bag si édition en ajax (notification déjà affichée en Javascript)
  309. if ($context->getRequest()->isXmlHttpRequest()) {
  310. $this->get('session')->getFlashBag()->clear();
  311. }
  312. return $response;
  313. }
  314. public function isInstanceOf(string $interfaceName): bool
  315. {
  316. return in_array($interfaceName, class_implements($this->getEntityFqcn()));
  317. }
  318. public function getControllerFqcnByInterface(string $interface): string
  319. {
  320. $context = $this->get(AdminContextProvider::class)->getContext();
  321. return $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  322. $this->get('em')->getEntityName($interface)
  323. );
  324. }
  325. public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
  326. {
  327. $entityManager->update($entityInstance);
  328. $entityManager->flush();
  329. }
  330. public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
  331. {
  332. $entityManager->create($entityInstance);
  333. $entityManager->flush();
  334. }
  335. public function configureActions(Actions $actions): Actions
  336. {
  337. $this->buildIndexActions($actions);
  338. $this->buildEditActions($actions);
  339. $this->buildDetailActions($actions);
  340. $this->buildNewActions($actions);
  341. $this->handleTranslatableEntityActions($actions);
  342. $this->handleSortableEntityActions($actions);
  343. $this->handleTreeEntityActions($actions);
  344. /*$actions->reorder(Crud::PAGE_EDIT, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE, Action::DELETE]);
  345. $actions->reorder(Crud::PAGE_NEW, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_ADD_ANOTHER]);*/
  346. return $actions;
  347. }
  348. public function buildIndexActions(Actions $actions): void
  349. {
  350. $this->actionUpdate(
  351. $actions,
  352. Crud::PAGE_INDEX,
  353. Action::NEW,
  354. [
  355. 'icon' => 'plus',
  356. 'label' => $this->get('translator_admin')->transAction('create'),
  357. 'add_class' => 'btn-sm',
  358. ]
  359. );
  360. $this->actionUpdate(
  361. $actions,
  362. Crud::PAGE_INDEX,
  363. Action::EDIT,
  364. [
  365. 'class' => 'btn btn-sm btn-primary',
  366. 'icon' => 'edit',
  367. 'label' => false,
  368. 'html_attributes' => array(
  369. 'data-toggle' => 'tooltip',
  370. 'title' => $this->get('translator_admin')->transAction('edit'),
  371. ),
  372. ]
  373. );
  374. $this->actionUpdate(
  375. $actions,
  376. Crud::PAGE_INDEX,
  377. Action::DETAIL,
  378. [
  379. 'icon' => 'eye',
  380. 'add_class' => 'btn btn-sm btn-success',
  381. 'label' => false,
  382. 'html_attributes' => array(
  383. 'data-toggle' => 'tooltip',
  384. 'title' => $this->get('translator_admin')->transAction('detail'),
  385. ),
  386. ]
  387. );
  388. $this->actionUpdate(
  389. $actions,
  390. Crud::PAGE_INDEX,
  391. Action::DELETE,
  392. [
  393. 'icon' => 'trash',
  394. 'dropdown' => true,
  395. 'label' => $this->get('translator_admin')->transAction('delete'),
  396. ]
  397. );
  398. $this->actionUpdate(
  399. $actions,
  400. Crud::PAGE_INDEX,
  401. Action::BATCH_DELETE,
  402. [
  403. 'class' => 'btn btn-sm btn-danger',
  404. 'icon' => 'trash',
  405. 'label' => $this->get('translator_admin')->transAction('delete'),
  406. ]
  407. );
  408. }
  409. public function buildEditActions(Actions $actions): void
  410. {
  411. $actions->add(Crud::PAGE_EDIT, Action::INDEX);
  412. $actions->add(Crud::PAGE_EDIT, Action::DELETE);
  413. $this->actionUpdate(
  414. $actions,
  415. Crud::PAGE_EDIT,
  416. Action::SAVE_AND_RETURN,
  417. [
  418. 'add_class' => 'float-right',
  419. 'icon' => 'check',
  420. 'label' => $this->get('translator_admin')->transAction('save_and_return'),
  421. ]
  422. );
  423. $this->actionUpdate(
  424. $actions,
  425. Crud::PAGE_EDIT,
  426. Action::INDEX,
  427. [
  428. 'icon' => 'chevron-left',
  429. 'class' => 'btn btn-link',
  430. 'label' => $this->get('translator_admin')->transAction('back_index'),
  431. ]
  432. );
  433. $this->actionUpdate(
  434. $actions,
  435. Crud::PAGE_EDIT,
  436. Action::SAVE_AND_CONTINUE,
  437. [
  438. 'class' => 'btn btn-info float-right',
  439. 'label' => $this->get('translator_admin')->transAction('save_and_continue'),
  440. ]
  441. );
  442. $this->actionUpdate(
  443. $actions,
  444. Crud::PAGE_EDIT,
  445. Action::DELETE,
  446. [
  447. 'icon' => 'trash',
  448. 'class' => 'btn btn-outline-danger action-delete',
  449. 'label' => $this->get('translator_admin')->transAction('delete'),
  450. ]
  451. );
  452. }
  453. public function buildDetailActions(Actions $actions): void
  454. {
  455. }
  456. public function buildNewActions(Actions $actions): void
  457. {
  458. $actions->add(Crud::PAGE_NEW, Action::INDEX);
  459. $this->actionUpdate(
  460. $actions,
  461. Crud::PAGE_EDIT,
  462. Action::SAVE_AND_RETURN,
  463. [
  464. 'add_class' => 'float-right',
  465. 'icon' => 'check',
  466. 'label' => $this->get('translator_admin')->transAction('save_and_return'),
  467. ]
  468. );
  469. $this->actionUpdate(
  470. $actions,
  471. Crud::PAGE_EDIT,
  472. Action::INDEX,
  473. [
  474. 'icon' => 'chevron-left',
  475. 'class' => 'btn btn-link',
  476. 'label' => $this->get('translator_admin')->transAction('back_index'),
  477. ]
  478. );
  479. $this->actionUpdate(
  480. $actions,
  481. Crud::PAGE_EDIT,
  482. Action::SAVE_AND_ADD_ANOTHER,
  483. [
  484. 'class' => 'btn btn-info float-right',
  485. 'label' => $this->get('translator_admin')->transAction('save_and_add_another'),
  486. ]
  487. );
  488. }
  489. public function handleTranslatableEntityActions(Actions $actions): void
  490. {
  491. if ($this->isInstanceOf(TranslatableInterface::class)) {
  492. $actions->update(
  493. Crud::PAGE_INDEX,
  494. Action::EDIT,
  495. function (Action $action) {
  496. $action->setTemplatePath('@LcSov/adminlte/crud/action/translatable.html.twig');
  497. return $action;
  498. }
  499. );
  500. }
  501. }
  502. public function handleSortableEntityActions(Actions $actions): void
  503. {
  504. if ($this->isInstanceOf(SortableInterface::class)) {
  505. $sortAction = Action::new('sort', $this->get('translator_admin')->transAction('sort'), 'fa fa-sort')
  506. ->linkToCrudAction('sort')
  507. ->setCssClass('btn btn-sm btn-success')
  508. ->createAsGlobalAction();
  509. $actions->add(Crud::PAGE_INDEX, $sortAction);
  510. }
  511. }
  512. public function handleTreeEntityActions(Actions $actions): void
  513. {
  514. if ($this->isInstanceOf(TreeInterface::class)) {
  515. $indexChildAction = Action::new(
  516. 'index_children',
  517. $this->get('translator_admin')->transAction('index_children'),
  518. 'fa fa-list'
  519. )
  520. ->linkToCrudAction(Action::INDEX)
  521. ->setLabel('')
  522. ->setHtmlAttributes(array('data-toggle' => 'tooltip', 'title' => 'Afficher les enfants'))
  523. ->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig')
  524. ->setCssClass('btn btn-sm btn-success');
  525. $backParentAction = Action::new(
  526. 'index_parent',
  527. $this->get('translator_admin')->transAction('index_parent'),
  528. 'fa fa-chevron-left'
  529. )
  530. ->linkToCrudAction(Action::INDEX)
  531. ->setCssClass('btn btn-sm btn-info')
  532. ->createAsGlobalAction();
  533. $actions->add(Crud::PAGE_INDEX, $backParentAction);
  534. $actions->add(Crud::PAGE_INDEX, $indexChildAction);
  535. }
  536. }
  537. public function actionUpdate($actions, $crudActionName, $actionName, array $button): void
  538. {
  539. if ($actions->getAsDto('actions')->getAction($crudActionName, $actionName)) {
  540. $actions->update(
  541. $crudActionName,
  542. $actionName,
  543. function (Action $action) use ($button) {
  544. if (isset($button['add_class'])) {
  545. $action->addCssClass($button['add_class']);
  546. }
  547. if (isset($button['class'])) {
  548. $action->setCssClass($button['class']);
  549. }
  550. if (isset($button['icon'])) {
  551. $action->setIcon('fa fa-' . $button['icon']);
  552. }
  553. if (isset($button['label'])) {
  554. $action->setLabel($button['label']);
  555. }
  556. if (isset($button['dropdown']) && $button['dropdown']) {
  557. $action->addCssClass('in-dropdown');
  558. }
  559. if (isset($button['html_attributes']) && $button['html_attributes']) {
  560. $action->setHtmlAttributes($button['html_attributes']);
  561. }
  562. return $action;
  563. }
  564. );
  565. }
  566. }
  567. public function autocompleteFilter(AdminContext $context): JsonResponse
  568. {
  569. $queryBuilder = $this->createIndexQueryBuilder(
  570. $context->getSearch(),
  571. $context->getEntity(),
  572. FieldCollection::new([]),
  573. FilterCollection::new()
  574. );
  575. $autocompleteContext = $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
  576. /** @var CrudControllerInterface $controller */
  577. $controller = $this->get(ControllerFactory::class)->getCrudControllerInstance(
  578. $autocompleteContext[EA::CRUD_CONTROLLER_FQCN],
  579. Action::INDEX,
  580. $context->getRequest()
  581. );
  582. /** @var FieldDto $field */
  583. $field = FieldCollection::new(
  584. $controller->configureFields($autocompleteContext['originatingPage'])
  585. )->getByProperty($autocompleteContext['propertyName']);
  586. $filterManager = $this->get('filter_manager');
  587. $filterManager->applyFilter($queryBuilder, $field, $context->getRequest()->query->get('q'));
  588. if ($filterManager->isRelationField($field->getProperty())) {
  589. $queryBuilder->select($autocompleteContext['propertyName']);
  590. } else {
  591. $queryBuilder->select('entity.' . $autocompleteContext['propertyName']);
  592. }
  593. $responses = array();
  594. foreach ($queryBuilder->getQuery()->getArrayResult() as $result) {
  595. $responses[] = array_values($result)[0];
  596. }
  597. return JsonResponse::fromJsonString(json_encode($responses));
  598. }
  599. }