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.

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