Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

714 lines
27KB

  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 configureFields(string $pageName): iterable
  151. {
  152. $seoPanel = $confPanel = array();
  153. if ($this->isInstanceOf(SeoInterface::class)) {
  154. $seoPanel = [
  155. FormField::addPanel('seo')->setTemplateName('crud/field/generic'),
  156. TextField::new('metaTitle')->setLabel('Meta Title')->setHelp(
  157. 'Affiché dans les résultats de recherche Google'
  158. )->hideOnIndex(),
  159. TextareaField::new('metaDescription')->setLabel('Meta description')->setHelp(
  160. 'Affiché dans les résultats de recherche Google'
  161. )->hideOnIndex(),
  162. CollectionField::new('oldUrls')
  163. ->setFormTypeOption('entry_type', TextType::class)->setLabel(
  164. 'Anciennes urls du document'
  165. )->hideOnIndex(),
  166. ];
  167. }
  168. if ($this->isInstanceOf(DevAliasInterface::class)) {
  169. $confPanel = [
  170. FormField::addPanel('configuration')->setTemplateName('crud/field/generic'),
  171. TextField::new('devAlias')->hideOnIndex(),
  172. ];
  173. }
  174. return array_merge($seoPanel, $confPanel);
  175. }
  176. public function sort(AdminContext $context)
  177. {
  178. $event = new BeforeCrudActionEvent($context);
  179. $this->get('event_dispatcher')->dispatch($event);
  180. if ($event->isPropagationStopped()) {
  181. return $event->getResponse();
  182. }
  183. //if (!$this->isGranted(Permission::EA_EXECUTE_ACTION) || !$this->isInstanceOf(SortableInterface::class)) {
  184. if (!$this->isInstanceOf(SortableInterface::class)) {
  185. throw new ForbiddenActionException($context);
  186. }
  187. $fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
  188. $filters = $this->get(FilterFactory::class)->create(
  189. $context->getCrud()->getFiltersConfig(),
  190. $fields,
  191. $context->getEntity()
  192. );
  193. $queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), $fields, $filters);
  194. $paginator = $this->get(PaginatorFactory::class)->create($queryBuilder);
  195. $entities = $this->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
  196. $this->get(EntityFactory::class)->processFieldsForAll($entities, $fields);
  197. $sortableForm = $this->createFormBuilder(array('entities', $paginator->getResults()))
  198. ->add(
  199. 'entities',
  200. CollectionType::class,
  201. array(
  202. 'required' => true,
  203. 'allow_add' => true,
  204. 'entry_type' => PositionType::class,
  205. )
  206. )
  207. ->getForm();
  208. $entityManager = $this->getDoctrine()->getManagerForClass($this->getEntityFqcn());
  209. $repository = $entityManager->getRepository($this->getEntityFqcn());
  210. $sortableForm->handleRequest($context->getRequest());
  211. if ($sortableForm->isSubmitted() && $sortableForm->isValid()) {
  212. foreach ($sortableForm->get('entities')->getData() as $elm) {
  213. $entityInstance = $repository->find($elm['id']);
  214. $entityDto = $context->getEntity()->newWithInstance($entityInstance);
  215. if (!$entityDto->isAccessible()) {
  216. throw new InsufficientEntityPermissionException($context);
  217. }
  218. $event = new BeforeEntityDeletedEvent($entityInstance);
  219. $this->get('event_dispatcher')->dispatch($event);
  220. $entityInstance = $event->getEntityInstance();
  221. $entityInstance->setPosition($elm['position']);
  222. $this->updateEntity($entityManager, $entityInstance);
  223. $this->get('event_dispatcher')->dispatch(new AfterEntityUpdatedEvent($entityInstance));
  224. }
  225. $url = $this->get(AdminUrlGenerator::class)
  226. ->setAction(Action::INDEX)
  227. ->generateUrl();
  228. $this->addFlash('success', $this->translatorAdmin->transFlashMessage('sort'), array());
  229. return $this->redirect($url);
  230. }
  231. $responseParameters = $this->configureResponseParameters(
  232. KeyValueStore::new(
  233. [
  234. 'pageName' => Crud::PAGE_INDEX,
  235. 'templatePath' => '@LcSov/adminlte/crud/sort.html.twig',
  236. 'entities' => $entities,
  237. 'paginator' => $paginator,
  238. 'global_actions' => array(),
  239. 'batch_actions' => array(),
  240. 'filters' => $filters,
  241. 'sortable_form' => $sortableForm,
  242. ]
  243. )
  244. );
  245. $responseParameters->set('fields', $this->configureFields('index'));
  246. $event = new AfterCrudActionEvent($context, $responseParameters);
  247. $this->get('event_dispatcher')->dispatch($event);
  248. if ($event->isPropagationStopped()) {
  249. return $event->getResponse();
  250. }
  251. return $responseParameters;
  252. }
  253. public function createIndexQueryBuilder(
  254. SearchDto $searchDto,
  255. EntityDto $entityDto,
  256. FieldCollection $fields,
  257. FilterCollection $filters
  258. ): QueryBuilder {
  259. $queryBuilder = parent::createIndexQueryBuilder(
  260. $searchDto,
  261. $entityDto,
  262. $fields,
  263. $filters
  264. );
  265. //TOdo utiliser les repositoryQuery ?
  266. if ($this->isInstanceOf(TreeInterface::class)) {
  267. $entityId = $searchDto->getRequest()->get('entityId');
  268. if ($entityId !== null) {
  269. $queryBuilder->andWhere('entity.parent = :entityId');
  270. $queryBuilder->setParameter('entityId', $searchDto->getRequest()->get('entityId'));
  271. } else {
  272. $queryBuilder->andWhere('entity.parent IS NULL');
  273. }
  274. }
  275. $this->filtersForm = $this->createForm(
  276. FiltersFormType::class,
  277. null,
  278. array(
  279. 'fields' => $fields,
  280. 'entity_dto' => $entityDto,
  281. 'entity_class' => $this->getEntityFqcn(),
  282. 'entity_name' => $entityDto->getName(),
  283. )
  284. );
  285. $filterManager = $this->get('filter_manager');
  286. $this->filtersForm->handleRequest($searchDto->getRequest());
  287. /*if (($this->filtersForm->isSubmitted() && $this->filtersForm->isValid())) {
  288. }*/
  289. $filterManager->handleFiltersForm($queryBuilder, $this->filtersForm, $fields, $entityDto);
  290. return $queryBuilder;
  291. }
  292. public function createSortQueryBuilder(
  293. SearchDto $searchDto,
  294. EntityDto $entityDto,
  295. FieldCollection $fields,
  296. FilterCollection $filters
  297. ): QueryBuilder {
  298. $queryBuilder = $this->createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
  299. return $queryBuilder;
  300. }
  301. public function edit(AdminContext $context)
  302. {
  303. $response = parent::edit($context);;
  304. // on vide le flash bag si édition en ajax (notification déjà affichée en Javascript)
  305. if ($context->getRequest()->isXmlHttpRequest()) {
  306. $this->get('session')->getFlashBag()->clear();
  307. }
  308. return $response;
  309. }
  310. public function isInstanceOf(string $interfaceName): bool
  311. {
  312. return in_array($interfaceName, class_implements($this->getEntityFqcn()));
  313. }
  314. public function getControllerFqcnByInterface(string $interface): string
  315. {
  316. $context = $this->get(AdminContextProvider::class)->getContext();
  317. return $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  318. $this->get('em')->getEntityName($interface)
  319. );
  320. }
  321. public function createEntity(string $entityFqcn)
  322. {
  323. if(method_exists($this, 'getEntityFactory')) {
  324. $factoryClass = $this->getEntityFactory();
  325. $factory = new $factoryClass;
  326. return $factory->create();
  327. }
  328. else {
  329. return parent::createEntity($entityFqcn);
  330. }
  331. }
  332. public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
  333. {
  334. $entityManager->update($entityInstance);
  335. $entityManager->flush();
  336. }
  337. public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
  338. {
  339. $entityManager->create($entityInstance);
  340. $entityManager->flush();
  341. }
  342. public function configureActions(Actions $actions): Actions
  343. {
  344. $this->buildIndexActions($actions);
  345. $this->buildEditActions($actions);
  346. $this->buildDetailActions($actions);
  347. $this->buildNewActions($actions);
  348. $this->handleTranslatableEntityActions($actions);
  349. $this->handleSortableEntityActions($actions);
  350. $this->handleTreeEntityActions($actions);
  351. /*$actions->reorder(Crud::PAGE_EDIT, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE, Action::DELETE]);
  352. $actions->reorder(Crud::PAGE_NEW, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_ADD_ANOTHER]);*/
  353. return $actions;
  354. }
  355. public function buildIndexActions(Actions $actions): void
  356. {
  357. $this->actionUpdate(
  358. $actions,
  359. Crud::PAGE_INDEX,
  360. Action::NEW,
  361. [
  362. 'icon' => 'plus',
  363. 'label' => $this->get('translator_admin')->transAction('create'),
  364. 'add_class' => 'btn-sm',
  365. ]
  366. );
  367. $this->actionUpdate(
  368. $actions,
  369. Crud::PAGE_INDEX,
  370. Action::EDIT,
  371. [
  372. 'class' => 'btn btn-sm btn-primary',
  373. 'icon' => 'edit',
  374. 'label' => false,
  375. 'html_attributes' => array(
  376. 'data-toggle' => 'tooltip',
  377. 'title' => $this->get('translator_admin')->transAction('edit'),
  378. ),
  379. ]
  380. );
  381. $this->actionUpdate(
  382. $actions,
  383. Crud::PAGE_INDEX,
  384. Action::DETAIL,
  385. [
  386. 'icon' => 'eye',
  387. 'add_class' => 'btn btn-sm btn-success',
  388. 'label' => false,
  389. 'html_attributes' => array(
  390. 'data-toggle' => 'tooltip',
  391. 'title' => $this->get('translator_admin')->transAction('detail'),
  392. ),
  393. ]
  394. );
  395. $this->actionUpdate(
  396. $actions,
  397. Crud::PAGE_INDEX,
  398. Action::DELETE,
  399. [
  400. 'icon' => 'trash',
  401. 'dropdown' => true,
  402. 'label' => $this->get('translator_admin')->transAction('delete'),
  403. ]
  404. );
  405. $this->actionUpdate(
  406. $actions,
  407. Crud::PAGE_INDEX,
  408. Action::BATCH_DELETE,
  409. [
  410. 'class' => 'btn btn-sm btn-danger',
  411. 'icon' => 'trash',
  412. 'label' => $this->get('translator_admin')->transAction('delete'),
  413. ]
  414. );
  415. }
  416. public function buildEditActions(Actions $actions): void
  417. {
  418. $actions->add(Crud::PAGE_EDIT, Action::INDEX);
  419. $actions->add(Crud::PAGE_EDIT, Action::DELETE);
  420. $this->actionUpdate(
  421. $actions,
  422. Crud::PAGE_EDIT,
  423. Action::SAVE_AND_RETURN,
  424. [
  425. 'add_class' => 'float-right',
  426. 'icon' => 'check',
  427. 'label' => $this->get('translator_admin')->transAction('save_and_return'),
  428. ]
  429. );
  430. $this->actionUpdate(
  431. $actions,
  432. Crud::PAGE_EDIT,
  433. Action::INDEX,
  434. [
  435. 'icon' => 'chevron-left',
  436. 'class' => 'btn btn-link',
  437. 'label' => $this->get('translator_admin')->transAction('back_index'),
  438. ]
  439. );
  440. $this->actionUpdate(
  441. $actions,
  442. Crud::PAGE_EDIT,
  443. Action::SAVE_AND_CONTINUE,
  444. [
  445. 'class' => 'btn btn-info float-right',
  446. 'label' => $this->get('translator_admin')->transAction('save_and_continue'),
  447. ]
  448. );
  449. $this->actionUpdate(
  450. $actions,
  451. Crud::PAGE_EDIT,
  452. Action::DELETE,
  453. [
  454. 'icon' => 'trash',
  455. 'class' => 'btn btn-outline-danger action-delete',
  456. 'label' => $this->get('translator_admin')->transAction('delete'),
  457. ]
  458. );
  459. }
  460. public function buildDetailActions(Actions $actions): void
  461. {
  462. }
  463. public function buildNewActions(Actions $actions): void
  464. {
  465. $actions->add(Crud::PAGE_NEW, Action::INDEX);
  466. $this->actionUpdate(
  467. $actions,
  468. Crud::PAGE_EDIT,
  469. Action::SAVE_AND_RETURN,
  470. [
  471. 'add_class' => 'float-right',
  472. 'icon' => 'check',
  473. 'label' => $this->get('translator_admin')->transAction('save_and_return'),
  474. ]
  475. );
  476. $this->actionUpdate(
  477. $actions,
  478. Crud::PAGE_EDIT,
  479. Action::INDEX,
  480. [
  481. 'icon' => 'chevron-left',
  482. 'class' => 'btn btn-link',
  483. 'label' => $this->get('translator_admin')->transAction('back_index'),
  484. ]
  485. );
  486. $this->actionUpdate(
  487. $actions,
  488. Crud::PAGE_EDIT,
  489. Action::SAVE_AND_ADD_ANOTHER,
  490. [
  491. 'class' => 'btn btn-info float-right',
  492. 'label' => $this->get('translator_admin')->transAction('save_and_add_another'),
  493. ]
  494. );
  495. }
  496. public function handleTranslatableEntityActions(Actions $actions): void
  497. {
  498. if ($this->isInstanceOf(TranslatableInterface::class)) {
  499. $actions->update(
  500. Crud::PAGE_INDEX,
  501. Action::EDIT,
  502. function (Action $action) {
  503. $action->setTemplatePath('@LcSov/adminlte/crud/action/translatable.html.twig');
  504. return $action;
  505. }
  506. );
  507. }
  508. }
  509. public function handleSortableEntityActions(Actions $actions): void
  510. {
  511. if ($this->isInstanceOf(SortableInterface::class)) {
  512. $sortAction = Action::new('sort', $this->get('translator_admin')->transAction('sort'), 'fa fa-sort')
  513. ->linkToCrudAction('sort')
  514. ->setCssClass('btn btn-sm btn-success')
  515. ->createAsGlobalAction();
  516. $actions->add(Crud::PAGE_INDEX, $sortAction);
  517. }
  518. }
  519. public function handleTreeEntityActions(Actions $actions): void
  520. {
  521. if ($this->isInstanceOf(TreeInterface::class)) {
  522. $indexChildAction = Action::new(
  523. 'index_children',
  524. $this->get('translator_admin')->transAction('index_children'),
  525. 'fa fa-list'
  526. )
  527. ->linkToCrudAction(Action::INDEX)
  528. ->setLabel('')
  529. ->setHtmlAttributes(array('data-toggle' => 'tooltip', 'title' => 'Afficher les enfants'))
  530. ->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig')
  531. ->setCssClass('btn btn-sm btn-success');
  532. $backParentAction = Action::new(
  533. 'index_parent',
  534. $this->get('translator_admin')->transAction('index_parent'),
  535. 'fa fa-chevron-left'
  536. )
  537. ->linkToCrudAction(Action::INDEX)
  538. ->setCssClass('btn btn-sm btn-info')
  539. ->createAsGlobalAction();
  540. $actions->add(Crud::PAGE_INDEX, $backParentAction);
  541. $actions->add(Crud::PAGE_INDEX, $indexChildAction);
  542. }
  543. }
  544. public function actionUpdate($actions, $crudActionName, $actionName, array $button): void
  545. {
  546. if ($actions->getAsDto('actions')->getAction($crudActionName, $actionName)) {
  547. $actions->update(
  548. $crudActionName,
  549. $actionName,
  550. function (Action $action) use ($button) {
  551. if (isset($button['add_class'])) {
  552. $action->addCssClass($button['add_class']);
  553. }
  554. if (isset($button['class'])) {
  555. $action->setCssClass($button['class']);
  556. }
  557. if (isset($button['icon'])) {
  558. $action->setIcon('fa fa-'.$button['icon']);
  559. }
  560. if (isset($button['label'])) {
  561. $action->setLabel($button['label']);
  562. }
  563. if (isset($button['dropdown']) && $button['dropdown']) {
  564. $action->addCssClass('in-dropdown');
  565. }
  566. if (isset($button['html_attributes']) && $button['html_attributes']) {
  567. $action->setHtmlAttributes($button['html_attributes']);
  568. }
  569. return $action;
  570. }
  571. );
  572. }
  573. }
  574. public function autocompleteFilter(AdminContext $context): JsonResponse
  575. {
  576. $queryBuilder = $this->createIndexQueryBuilder(
  577. $context->getSearch(),
  578. $context->getEntity(),
  579. FieldCollection::new([]),
  580. FilterCollection::new()
  581. );
  582. $autocompleteContext = $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
  583. /** @var CrudControllerInterface $controller */
  584. $controller = $this->get(ControllerFactory::class)->getCrudControllerInstance(
  585. $autocompleteContext[EA::CRUD_CONTROLLER_FQCN],
  586. Action::INDEX,
  587. $context->getRequest()
  588. );
  589. /** @var FieldDto $field */
  590. $field = FieldCollection::new(
  591. $controller->configureFields($autocompleteContext['originatingPage'])
  592. )->getByProperty($autocompleteContext['propertyName']);
  593. $filterManager = $this->get('filter_manager');
  594. $filterManager->applyFilter($queryBuilder, $field, $context->getRequest()->query->get('q'));
  595. if ($filterManager->isRelationField($field->getProperty())) {
  596. $queryBuilder->select($autocompleteContext['propertyName']);
  597. } else {
  598. $queryBuilder->select('entity.'.$autocompleteContext['propertyName']);
  599. }
  600. $responses = array();
  601. foreach ($queryBuilder->getQuery()->getArrayResult() as $result) {
  602. $responses[] = array_values($result)[0];
  603. }
  604. return JsonResponse::fromJsonString(json_encode($responses));
  605. }
  606. }