Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

780 lines
30KB

  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 EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  37. use Lc\SovBundle\Component\EntityComponent;
  38. use Lc\SovBundle\Definition\ActionDefinition;
  39. use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
  40. use Lc\SovBundle\Doctrine\Extension\SeoInterface;
  41. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  42. use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
  43. use Lc\SovBundle\Doctrine\Extension\TreeInterface;
  44. use Lc\SovBundle\Field\CollectionField;
  45. use Lc\SovBundle\Field\Filter\FilterManager;
  46. use Lc\SovBundle\Form\Common\FiltersFormType;
  47. use Lc\SovBundle\Form\Common\PositionType;
  48. use Lc\SovBundle\Repository\EntityRepository;
  49. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  50. use Lc\SovBundle\Translation\FlashBagTranslator;
  51. use Lc\SovBundle\Translation\TranslatorAdmin;
  52. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  53. use Symfony\Component\Form\Extension\Core\Type\TextType;
  54. use Symfony\Component\Form\FormInterface;
  55. use Symfony\Component\HttpFoundation\JsonResponse;
  56. use Symfony\Component\HttpFoundation\RequestStack;
  57. abstract class AbstractAdminController extends EaAbstractCrudController
  58. {
  59. use ControllerTrait;
  60. protected FormInterface $filtersForm;
  61. abstract public function getRepositoryQuery(): RepositoryQueryInterface;
  62. public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  63. {
  64. if ($responseParameters->get('global_actions')) {
  65. $this->overrideGlobalActions($responseParameters->get('global_actions'));
  66. }
  67. if ($responseParameters->get('entities')) {
  68. $this->overrideEntitiesActions($responseParameters->get('entities'));
  69. }
  70. if (Crud::PAGE_INDEX === $responseParameters->get('pageName')) {
  71. $responseParameters->set('fields', $this->configureFields('index'));
  72. //TODO supprimer ce code rapport au filtre dans les index
  73. /*if ($this->filtersForm === null) {
  74. die('nncncd');
  75. $options['fields'] = $responseParameters->get('fields');
  76. $options['entity_class'] = $this->getEntityFqcn();
  77. $options['entity_name'] = $responseParameters->get('entity')->getName();
  78. $this->filtersForm = $this->createForm(FiltersFormType::class, null, $options);
  79. }*/
  80. $responseParameters->set('filters_form', $this->filtersForm);
  81. }
  82. $responseParameters->set('translation_entity_name', $this->getTranslationEntityName());
  83. return $responseParameters;
  84. }
  85. public function getTranslationEntityName()
  86. {
  87. return $this->getEntityFqcn();
  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. $crud->setFormOptions(['translation_entity_name' => $this->getTranslationEntityName()]);
  132. if ($this->isInstanceOf(SortableInterface::class)) {
  133. $crud->setDefaultSort(['position' => 'ASC']);
  134. }else{
  135. $crud->setDefaultSort(['id' => 'DESC']);
  136. }
  137. return $crud;
  138. }
  139. public function setMaxResults(Crud $crud): void
  140. {
  141. $entityClass = $this->getEntityFqcn();
  142. $paramListMaxResults = 'listMaxResults';
  143. $paramSessionListMaxResults = $entityClass . '-' . $paramListMaxResults;
  144. $requestListMaxResults = $this->get(RequestStack::class)->getCurrentRequest()->get($paramListMaxResults);
  145. if ($requestListMaxResults) {
  146. $this->get('session')->set($paramSessionListMaxResults, $requestListMaxResults);
  147. }
  148. $maxResults = $this->get('session')->get($paramSessionListMaxResults) ? $this->get('session')->get(
  149. $paramSessionListMaxResults
  150. ) : 30;
  151. $crud->setPaginatorPageSize($maxResults);
  152. }
  153. public function getSeoPanel(): ?array
  154. {
  155. if ($this->isInstanceOf(SeoInterface::class)) {
  156. return [
  157. FormField::addPanel('seo')->setTemplateName('crud/field/generic'),
  158. TextField::new('metaTitle')->setLabel('Meta Title')->setHelp(
  159. 'Affiché dans les résultats de recherche Google'
  160. )->hideOnIndex(),
  161. TextareaField::new('metaDescription')->setLabel('Meta description')->setHelp(
  162. 'Affiché dans les résultats de recherche Google'
  163. )->hideOnIndex(),
  164. CollectionField::new('oldUrls')
  165. ->setFormTypeOption('entry_type', TextType::class)->setLabel(
  166. 'Anciennes urls du document'
  167. )->hideOnIndex(),
  168. ];
  169. } else {
  170. return null;
  171. }
  172. }
  173. public function getConfPanel(): ?array
  174. {
  175. if ($this->isInstanceOf(DevAliasInterface::class)) {
  176. return [
  177. FormField::addPanel('configuration')->setTemplateName('crud/field/generic'),
  178. TextField::new('devAlias')->hideOnIndex(),
  179. ];
  180. } else {
  181. return null;
  182. }
  183. }
  184. public function sort(AdminContext $context)
  185. {
  186. $event = new BeforeCrudActionEvent($context);
  187. $this->get('event_dispatcher')->dispatch($event);
  188. if ($event->isPropagationStopped()) {
  189. return $event->getResponse();
  190. }
  191. //if (!$this->isGranted(Permission::EA_EXECUTE_ACTION) || !$this->isInstanceOf(SortableInterface::class)) {
  192. if (!$this->isInstanceOf(SortableInterface::class)) {
  193. throw new ForbiddenActionException($context);
  194. }
  195. $fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
  196. $filters = $this->get(FilterFactory::class)->create(
  197. $context->getCrud()->getFiltersConfig(),
  198. $fields,
  199. $context->getEntity()
  200. );
  201. $queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), $fields, $filters);
  202. $paginator = $this->get(PaginatorFactory::class)->create($queryBuilder);
  203. $entities = $this->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
  204. $this->get(EntityFactory::class)->processFieldsForAll($entities, $fields);
  205. $sortableForm = $this->createFormBuilder(array('entities', $paginator->getResults()))
  206. ->add(
  207. 'entities',
  208. CollectionType::class,
  209. array(
  210. 'required' => true,
  211. 'allow_add' => true,
  212. 'entry_type' => PositionType::class,
  213. )
  214. )
  215. ->getForm();
  216. $entityManager = $this->getDoctrine()->getManagerForClass($this->getEntityFqcn());
  217. $repository = $entityManager->getRepository($this->getEntityFqcn());
  218. $sortableForm->handleRequest($context->getRequest());
  219. if ($sortableForm->isSubmitted() && $sortableForm->isValid()) {
  220. foreach ($sortableForm->get('entities')->getData() as $elm) {
  221. $entityInstance = $repository->find($elm['id']);
  222. $entityDto = $context->getEntity()->newWithInstance($entityInstance);
  223. if (!$entityDto->isAccessible()) {
  224. throw new InsufficientEntityPermissionException($context);
  225. }
  226. $event = new BeforeEntityDeletedEvent($entityInstance);
  227. $this->get('event_dispatcher')->dispatch($event);
  228. $entityInstance = $event->getEntityInstance();
  229. $entityInstance->setPosition($elm['position']);
  230. $this->updateEntity($entityManager, $entityInstance);
  231. $this->get('event_dispatcher')->dispatch(new AfterEntityUpdatedEvent($entityInstance));
  232. }
  233. $url = $this->get(AdminUrlGenerator::class)
  234. ->setAction(ActionDefinition::INDEX)
  235. ->generateUrl();
  236. $this->addFlashTranslator('success', 'sorted');
  237. return $this->redirect($url);
  238. }
  239. $responseParameters = $this->configureResponseParameters(
  240. KeyValueStore::new(
  241. [
  242. 'pageName' => Crud::PAGE_INDEX,
  243. 'templatePath' => '@LcSov/adminlte/crud/sort.html.twig',
  244. 'entities' => $entities,
  245. 'paginator' => $paginator,
  246. 'global_actions' => array(),
  247. 'batch_actions' => array(),
  248. 'filters' => $filters,
  249. 'sortable_form' => $sortableForm,
  250. ]
  251. )
  252. );
  253. $responseParameters->set('fields', $this->configureFields('index'));
  254. $event = new AfterCrudActionEvent($context, $responseParameters);
  255. $this->get('event_dispatcher')->dispatch($event);
  256. if ($event->isPropagationStopped()) {
  257. return $event->getResponse();
  258. }
  259. return $responseParameters;
  260. }
  261. public function duplicate(
  262. AdminContext $context,
  263. EntityComponent $entityComponent,
  264. TranslatorAdmin $translatorAdmin,
  265. EntityManagerInterface $em
  266. ) {
  267. if (!$this->isGranted(
  268. Permission::EA_EXECUTE_ACTION,
  269. ['action' => "duplicate", 'entity' => $context->getEntity()]
  270. )) {
  271. throw new ForbiddenActionException($context);
  272. }
  273. if (!$context->getEntity()->isAccessible()) {
  274. throw new InsufficientEntityPermissionException($context);
  275. }
  276. $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
  277. $em->create($newEntity);
  278. $em->flush();
  279. $url = $this->get(AdminUrlGenerator::class)
  280. ->setAction(ActionDefinition::EDIT)
  281. ->setEntityId($newEntity->getId())
  282. ->generateUrl();
  283. $this->addFlashTranslator('success', 'duplicated');
  284. return $this->redirect($url);
  285. }
  286. public function createIndexRepositoryQuery(
  287. SearchDto $searchDto,
  288. EntityDto $entityDto,
  289. FieldCollection $fields,
  290. FilterCollection $filters
  291. ): RepositoryQueryInterface
  292. {
  293. $repositoryQuery = $this->get(EntityRepository::class)->createRepositoryQuery($this->getRepositoryQuery(), $searchDto, $entityDto, $fields, $filters);
  294. // TODO : déplacer dans EntotyRepository
  295. if ($this->isInstanceOf(TreeInterface::class)) {
  296. if ($entityDto->getInstance()) {
  297. $repositoryQuery->filterByParent($entityDto->getInstance());
  298. } else {
  299. $repositoryQuery->filterIsParent();
  300. }
  301. }
  302. $this->filtersForm = $this->createForm(
  303. FiltersFormType::class,
  304. null,
  305. array(
  306. 'fields' => $fields,
  307. 'entity_dto' => $entityDto,
  308. 'entity_class' => $this->getEntityFqcn(),
  309. 'entity_name' => $entityDto->getName(),
  310. )
  311. );
  312. $filterManager = $this->get(FilterManager::class);
  313. $this->filtersForm->handleRequest($searchDto->getRequest());
  314. $filterManager->handleFiltersForm($repositoryQuery, $this->filtersForm, $fields, $entityDto);
  315. return $repositoryQuery;
  316. }
  317. public function createIndexQueryBuilder(
  318. SearchDto $searchDto,
  319. EntityDto $entityDto,
  320. FieldCollection $fields,
  321. FilterCollection $filters
  322. ): QueryBuilder {
  323. $repositoryQuery = $this->createIndexRepositoryQuery($searchDto, $entityDto, $fields, $filters);
  324. return $repositoryQuery->getQueryBuilder();
  325. }
  326. public function createSortQueryBuilder(
  327. SearchDto $searchDto,
  328. EntityDto $entityDto,
  329. FieldCollection $fields,
  330. FilterCollection $filters
  331. ): QueryBuilder {
  332. $queryBuilder = $this->createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
  333. return $queryBuilder;
  334. }
  335. public function edit(AdminContext $context)
  336. {
  337. $response = parent::edit($context);;
  338. // on vide le flash bag si édition en ajax (notification déjà affichée en Javascript)
  339. if ($context->getRequest()->isXmlHttpRequest()) {
  340. $this->get('session')->getFlashBag()->clear();
  341. }
  342. return $response;
  343. }
  344. public function getControllerFqcnByInterface(string $interface): string
  345. {
  346. $context = $this->get(AdminContextProvider::class)->getContext();
  347. return $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
  348. $this->get(EntityManagerInterface::class)->getEntityName($interface)
  349. );
  350. }
  351. public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
  352. {
  353. $entityManager->update($entityInstance);
  354. $entityManager->flush();
  355. $this->get(FlashBagTranslator::class)->add('success', 'updated', $this->getTranslationEntityName());
  356. }
  357. public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
  358. {
  359. $entityManager->create($entityInstance);
  360. $entityManager->flush();
  361. $this->get(FlashBagTranslator::class)->add('success', 'created', $this->getTranslationEntityName());
  362. }
  363. public function deleteEntity(EntityManagerInterface $entityManager, $entityInstance): void
  364. {
  365. $entityManager->delete($entityInstance);
  366. $entityManager->flush();
  367. $this->get(FlashBagTranslator::class)->add('success', 'deleted', $this->getTranslationEntityName());
  368. }
  369. public function configureActions(Actions $actions): Actions
  370. {
  371. $this->buildIndexActions($actions);
  372. $this->buildEditActions($actions);
  373. $this->buildDetailActions($actions);
  374. $this->buildNewActions($actions);
  375. $this->handleTranslatableEntityActions($actions);
  376. $this->handleSortableEntityActions($actions);
  377. $this->handleTreeEntityActions($actions);
  378. /*$actions->reorder(Crud::PAGE_EDIT, [ActionDefinition::INDEX, ActionDefinition::SAVE_AND_RETURN, ActionDefinition::SAVE_AND_CONTINUE, ActionDefinition::DELETE]);
  379. $actions->reorder(Crud::PAGE_NEW, [ActionDefinition::INDEX, ActionDefinition::SAVE_AND_RETURN, ActionDefinition::SAVE_AND_ADD_ANOTHER]);*/
  380. return $actions;
  381. }
  382. public function getDuplicateAction(): Action
  383. {
  384. $duplicateAction = Action::new(
  385. ActionDefinition::DUPLICATE,
  386. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE),
  387. 'fa fa-fw fa-copy'
  388. )
  389. ->linkToCrudAction(ActionDefinition::DUPLICATE)
  390. ->setLabel($this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE))
  391. ->setCssClass('in-dropdown text-info action-confirm');
  392. return $duplicateAction;
  393. }
  394. public function buildIndexActions(Actions $actions): void
  395. {
  396. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateAction());
  397. $this->actionUpdate(
  398. $actions,
  399. Crud::PAGE_INDEX,
  400. ActionDefinition::NEW,
  401. [
  402. 'icon' => 'plus',
  403. 'label' => $this->get(TranslatorAdmin::class)->transAction('create'),
  404. 'add_class' => 'btn-sm',
  405. ]
  406. );
  407. $this->actionUpdate(
  408. $actions,
  409. Crud::PAGE_INDEX,
  410. ActionDefinition::EDIT,
  411. [
  412. 'class' => 'btn btn-sm btn-primary',
  413. 'icon' => 'edit',
  414. 'label' => false,
  415. 'html_attributes' => array(
  416. 'data-toggle' => 'tooltip',
  417. 'title' => $this->get(TranslatorAdmin::class)->transAction('edit'),
  418. ),
  419. ]
  420. );
  421. $this->actionUpdate(
  422. $actions,
  423. Crud::PAGE_INDEX,
  424. ActionDefinition::DETAIL,
  425. [
  426. 'icon' => 'eye',
  427. 'add_class' => 'btn btn-sm btn-success',
  428. 'label' => false,
  429. 'html_attributes' => array(
  430. 'data-toggle' => 'tooltip',
  431. 'title' => $this->get(TranslatorAdmin::class)->transAction('detail'),
  432. ),
  433. ]
  434. );
  435. $this->actionUpdate(
  436. $actions,
  437. Crud::PAGE_INDEX,
  438. ActionDefinition::DELETE,
  439. [
  440. 'icon' => 'trash',
  441. 'dropdown' => true,
  442. 'label' => $this->get(TranslatorAdmin::class)->transAction('delete'),
  443. 'class' => 'dropdown-item text-danger in-dropdown action-delete',
  444. ]
  445. );
  446. $this->actionUpdate(
  447. $actions,
  448. Crud::PAGE_INDEX,
  449. ActionDefinition::BATCH_DELETE,
  450. [
  451. 'class' => 'btn btn-sm btn-danger',
  452. 'icon' => 'trash',
  453. 'label' => $this->get(TranslatorAdmin::class)->transAction('delete'),
  454. ]
  455. );
  456. }
  457. public function buildEditActions(Actions $actions): void
  458. {
  459. $actions->add(Crud::PAGE_EDIT, ActionDefinition::INDEX);
  460. $actions->add(Crud::PAGE_EDIT, ActionDefinition::DELETE);
  461. $this->actionUpdate(
  462. $actions,
  463. Crud::PAGE_EDIT,
  464. ActionDefinition::SAVE_AND_RETURN,
  465. [
  466. 'add_class' => 'float-right ',
  467. 'icon' => 'check',
  468. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_return'),
  469. ]
  470. );
  471. $this->actionUpdate(
  472. $actions,
  473. Crud::PAGE_EDIT,
  474. ActionDefinition::INDEX,
  475. [
  476. 'icon' => 'chevron-left',
  477. 'class' => 'btn btn-link',
  478. 'label' => $this->get(TranslatorAdmin::class)->transAction('back_index'),
  479. ]
  480. );
  481. $this->actionUpdate(
  482. $actions,
  483. Crud::PAGE_EDIT,
  484. ActionDefinition::SAVE_AND_CONTINUE,
  485. [
  486. 'class' => 'btn btn-info float-right action-save-continue',
  487. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_continue'),
  488. ]
  489. );
  490. $this->actionUpdate(
  491. $actions,
  492. Crud::PAGE_EDIT,
  493. ActionDefinition::DELETE,
  494. [
  495. 'icon' => 'trash',
  496. 'class' => 'btn btn-outline-danger action-delete',
  497. 'label' => $this->get(TranslatorAdmin::class)->transAction('delete'),
  498. ]
  499. );
  500. }
  501. public function buildDetailActions(Actions $actions): void
  502. {
  503. }
  504. public function buildNewActions(Actions $actions): void
  505. {
  506. $actions->add(Crud::PAGE_NEW, ActionDefinition::INDEX);
  507. $this->actionUpdate(
  508. $actions,
  509. Crud::PAGE_EDIT,
  510. ActionDefinition::SAVE_AND_RETURN,
  511. [
  512. 'add_class' => 'float-right',
  513. 'icon' => 'check',
  514. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_return'),
  515. ]
  516. );
  517. $this->actionUpdate(
  518. $actions,
  519. Crud::PAGE_EDIT,
  520. ActionDefinition::INDEX,
  521. [
  522. 'icon' => 'chevron-left',
  523. 'class' => 'btn btn-link',
  524. 'label' => $this->get(TranslatorAdmin::class)->transAction('back_index'),
  525. ]
  526. );
  527. $this->actionUpdate(
  528. $actions,
  529. Crud::PAGE_EDIT,
  530. ActionDefinition::SAVE_AND_ADD_ANOTHER,
  531. [
  532. 'class' => 'btn btn-info float-right',
  533. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_add_another'),
  534. ]
  535. );
  536. }
  537. public function handleTranslatableEntityActions(Actions $actions): void
  538. {
  539. if ($this->isInstanceOf(TranslatableInterface::class)) {
  540. $actions->update(
  541. Crud::PAGE_INDEX,
  542. ActionDefinition::EDIT,
  543. function (Action $action) {
  544. $action->setTemplatePath('@LcSov/adminlte/crud/action/translatable.html.twig');
  545. return $action;
  546. }
  547. );
  548. }
  549. }
  550. public function handleSortableEntityActions(Actions $actions): void
  551. {
  552. if ($this->isInstanceOf(SortableInterface::class)) {
  553. $sortAction = Action::new(ActionDefinition::SORT, $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SORT), 'fa fa-sort')
  554. ->linkToCrudAction(ActionDefinition::SORT)
  555. ->setCssClass('btn btn-sm btn-success')
  556. ->createAsGlobalAction();
  557. $actions->add(Crud::PAGE_INDEX, $sortAction);
  558. }
  559. }
  560. public function handleTreeEntityActions(Actions $actions): void
  561. {
  562. if ($this->isInstanceOf(TreeInterface::class)) {
  563. $indexChildAction = Action::new(
  564. ActionDefinition::INDEX_CHILDREN,
  565. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::INDEX_CHILDREN),
  566. 'fa fa-list'
  567. )
  568. ->linkToCrudAction(ActionDefinition::INDEX)
  569. ->setLabel('')
  570. ->setHtmlAttributes(array('data-toggle' => 'tooltip', 'title' => 'Afficher les enfants'))
  571. ->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig')
  572. ->setCssClass('btn btn-sm btn-success');
  573. $backParentAction = Action::new(
  574. ActionDefinition::INDEX_PARENT,
  575. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::INDEX_PARENT),
  576. 'fa fa-chevron-left'
  577. )
  578. ->linkToCrudAction(ActionDefinition::INDEX)
  579. ->setCssClass('btn btn-sm btn-info')
  580. ->createAsGlobalAction();
  581. $actions->add(Crud::PAGE_INDEX, $backParentAction);
  582. $actions->add(Crud::PAGE_INDEX, $indexChildAction);
  583. }
  584. }
  585. public function actionUpdate($actions, $crudActionName, $actionName, array $button): void
  586. {
  587. if ($actions->getAsDto('actions')->getAction($crudActionName, $actionName)) {
  588. $actions->update(
  589. $crudActionName,
  590. $actionName,
  591. function (Action $action) use ($button) {
  592. if (isset($button['add_class'])) {
  593. $action->addCssClass($button['add_class']);
  594. }
  595. if (isset($button['class'])) {
  596. $action->setCssClass($button['class']);
  597. }
  598. if (isset($button['icon'])) {
  599. $action->setIcon('fa fa-' . $button['icon']);
  600. }
  601. if (isset($button['label'])) {
  602. $action->setLabel($button['label']);
  603. }
  604. if (isset($button['dropdown']) && $button['dropdown']) {
  605. $action->addCssClass('in-dropdown');
  606. }
  607. if (isset($button['html_attributes']) && $button['html_attributes']) {
  608. $action->setHtmlAttributes($button['html_attributes']);
  609. }
  610. return $action;
  611. }
  612. );
  613. }
  614. }
  615. public function autocompleteFilter(AdminContext $context): JsonResponse
  616. {
  617. $queryBuilder = $this->createIndexQueryBuilder(
  618. $context->getSearch(),
  619. $context->getEntity(),
  620. FieldCollection::new([]),
  621. FilterCollection::new()
  622. );
  623. $autocompleteContext = $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
  624. /** @var CrudControllerInterface $controller */
  625. $controller = $this->get(ControllerFactory::class)->getCrudControllerInstance(
  626. $autocompleteContext[EA::CRUD_CONTROLLER_FQCN],
  627. ActionDefinition::INDEX,
  628. $context->getRequest()
  629. );
  630. /** @var FieldDto $field */
  631. $field = FieldCollection::new(
  632. $controller->configureFields($autocompleteContext['originatingPage'])
  633. )->getByProperty($autocompleteContext['propertyName']);
  634. $filterManager = $this->get(FilterManager::class);
  635. $filterManager->applyFilter($queryBuilder, $field, $context->getRequest()->query->get('q'));
  636. if ($filterManager->isRelationField($field->getProperty())) {
  637. $queryBuilder->select($autocompleteContext['propertyName']);
  638. } else {
  639. $queryBuilder->select('entity.' . $autocompleteContext['propertyName']);
  640. }
  641. $responses = array();
  642. foreach ($queryBuilder->getQuery()->getArrayResult() as $result) {
  643. $responses[] = array_values($result)[0];
  644. }
  645. return JsonResponse::fromJsonString(json_encode($responses));
  646. }
  647. public function createCustomForm($class, $action, $controller, $entity, $dataInOption = true, $options = array())
  648. {
  649. if ($dataInOption) {
  650. $data = $entity;
  651. }else{
  652. $data = null;
  653. }
  654. $options['action'] = $this->get(AdminUrlGenerator::class)
  655. ->setAction($action)
  656. ->setController($controller)
  657. ->setEntityId($entity->getId())
  658. ->generateUrl();
  659. return $this->createForm($class, $data, $options);
  660. }
  661. }