Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

826 Zeilen
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'), $responseParameters->get('pageName'));
  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, string $pageName): 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. if($this->isInstanceOf(StatusInterface::class)){
  406. $entityInstance->setStatus(-1);
  407. $entityManager->update($entityInstance);
  408. }else{
  409. $entityManager->delete($entityInstance);
  410. }
  411. $entityManager->flush();
  412. $this->get(FlashBagTranslator::class)->add('success', 'deleted', $this->getTranslationEntityName());
  413. }
  414. public function configureActions(Actions $actions): Actions
  415. {
  416. $this->buildIndexActions($actions);
  417. $this->buildEditActions($actions);
  418. $this->buildDetailActions($actions);
  419. $this->buildNewActions($actions);
  420. $this->handleTranslatableEntityActions($actions);
  421. $this->handleSortableEntityActions($actions);
  422. $this->handleTreeEntityActions($actions);
  423. /*$actions->reorder(Crud::PAGE_EDIT, [ActionDefinition::INDEX, ActionDefinition::SAVE_AND_RETURN, ActionDefinition::SAVE_AND_CONTINUE, ActionDefinition::DELETE]);
  424. $actions->reorder(Crud::PAGE_NEW, [ActionDefinition::INDEX, ActionDefinition::SAVE_AND_RETURN, ActionDefinition::SAVE_AND_ADD_ANOTHER]);*/
  425. return $actions;
  426. }
  427. public function getDuplicateAction(): Action
  428. {
  429. $duplicateAction = Action::new(
  430. ActionDefinition::DUPLICATE,
  431. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE),
  432. 'fa fa-fw fa-copy'
  433. )
  434. ->linkToCrudAction(ActionDefinition::DUPLICATE)
  435. ->setLabel($this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE))
  436. ->setCssClass('in-dropdown text-info action-confirm');
  437. return $duplicateAction;
  438. }
  439. public function buildIndexActions(Actions $actions): void
  440. {
  441. $actions->add(Crud::PAGE_INDEX, $this->getDuplicateAction());
  442. $this->actionUpdate(
  443. $actions,
  444. Crud::PAGE_INDEX,
  445. ActionDefinition::NEW,
  446. [
  447. 'icon' => 'plus',
  448. 'label' => $this->get(TranslatorAdmin::class)->transAction('create'),
  449. 'add_class' => 'btn-sm',
  450. ]
  451. );
  452. $this->actionUpdate(
  453. $actions,
  454. Crud::PAGE_INDEX,
  455. ActionDefinition::EDIT,
  456. [
  457. 'class' => 'btn btn-sm btn-primary',
  458. 'icon' => 'edit',
  459. 'label' => false,
  460. 'html_attributes' => array(
  461. 'data-toggle' => 'tooltip',
  462. 'title' => $this->get(TranslatorAdmin::class)->transAction('edit'),
  463. ),
  464. ]
  465. );
  466. $this->actionUpdate(
  467. $actions,
  468. Crud::PAGE_INDEX,
  469. ActionDefinition::DETAIL,
  470. [
  471. 'icon' => 'eye',
  472. 'add_class' => 'btn btn-sm btn-success',
  473. 'label' => false,
  474. 'html_attributes' => array(
  475. 'data-toggle' => 'tooltip',
  476. 'title' => $this->get(TranslatorAdmin::class)->transAction('detail'),
  477. ),
  478. ]
  479. );
  480. $this->actionUpdate(
  481. $actions,
  482. Crud::PAGE_INDEX,
  483. ActionDefinition::DELETE,
  484. [
  485. 'icon' => 'trash',
  486. 'dropdown' => true,
  487. 'label' => $this->get(TranslatorAdmin::class)->transAction('delete'),
  488. 'class' => 'dropdown-item text-danger in-dropdown action-delete',
  489. ]
  490. );
  491. $this->actionUpdate(
  492. $actions,
  493. Crud::PAGE_INDEX,
  494. ActionDefinition::BATCH_DELETE,
  495. [
  496. 'class' => 'btn btn-sm btn-danger',
  497. 'icon' => 'trash',
  498. 'label' => $this->get(TranslatorAdmin::class)->transAction('delete'),
  499. ]
  500. );
  501. }
  502. public function buildEditActions(Actions $actions): void
  503. {
  504. $actions->add(Crud::PAGE_EDIT, ActionDefinition::INDEX);
  505. $actions->add(Crud::PAGE_EDIT, ActionDefinition::DELETE);
  506. $this->actionUpdate(
  507. $actions,
  508. Crud::PAGE_EDIT,
  509. ActionDefinition::SAVE_AND_RETURN,
  510. [
  511. 'add_class' => 'float-right ',
  512. 'icon' => 'check',
  513. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_return'),
  514. ]
  515. );
  516. $this->actionUpdate(
  517. $actions,
  518. Crud::PAGE_EDIT,
  519. ActionDefinition::INDEX,
  520. [
  521. 'icon' => 'chevron-left',
  522. 'class' => 'btn btn-link',
  523. 'label' => $this->get(TranslatorAdmin::class)->transAction('back_index'),
  524. ]
  525. );
  526. $this->actionUpdate(
  527. $actions,
  528. Crud::PAGE_EDIT,
  529. ActionDefinition::SAVE_AND_CONTINUE,
  530. [
  531. 'class' => 'btn btn-info float-right action-save-continue',
  532. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_continue'),
  533. ]
  534. );
  535. $this->actionUpdate(
  536. $actions,
  537. Crud::PAGE_EDIT,
  538. ActionDefinition::DELETE,
  539. [
  540. 'icon' => 'trash',
  541. 'class' => 'btn btn-outline-danger action-delete',
  542. 'label' => $this->get(TranslatorAdmin::class)->transAction('delete'),
  543. ]
  544. );
  545. }
  546. public function buildDetailActions(Actions $actions): void
  547. {
  548. }
  549. public function buildNewActions(Actions $actions): void
  550. {
  551. $actions->add(Crud::PAGE_NEW, ActionDefinition::INDEX);
  552. $this->actionUpdate(
  553. $actions,
  554. Crud::PAGE_NEW,
  555. ActionDefinition::SAVE_AND_RETURN,
  556. [
  557. 'add_class' => 'float-right',
  558. 'icon' => 'check',
  559. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_return'),
  560. ]
  561. );
  562. $this->actionUpdate(
  563. $actions,
  564. Crud::PAGE_NEW,
  565. ActionDefinition::INDEX,
  566. [
  567. 'icon' => 'chevron-left',
  568. 'class' => 'btn btn-link',
  569. 'label' => $this->get(TranslatorAdmin::class)->transAction('back_index'),
  570. ]
  571. );
  572. $this->actionUpdate(
  573. $actions,
  574. Crud::PAGE_NEW,
  575. ActionDefinition::SAVE_AND_ADD_ANOTHER,
  576. [
  577. 'class' => 'btn btn-info float-right',
  578. 'label' => $this->get(TranslatorAdmin::class)->transAction('save_and_add_another'),
  579. ]
  580. );
  581. }
  582. public function handleTranslatableEntityActions(Actions $actions): void
  583. {
  584. if ($this->isInstanceOf(TranslatableInterface::class)) {
  585. $actions->update(
  586. Crud::PAGE_INDEX,
  587. ActionDefinition::EDIT,
  588. function (Action $action) {
  589. $action->setTemplatePath('@LcSov/adminlte/crud/action/translatable.html.twig');
  590. return $action;
  591. }
  592. );
  593. }
  594. }
  595. public function handleSortableEntityActions(Actions $actions): void
  596. {
  597. if ($this->isInstanceOf(SortableInterface::class)) {
  598. $sortAction = Action::new(
  599. ActionDefinition::SORT,
  600. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SORT),
  601. 'fa fa-sort'
  602. )
  603. ->linkToCrudAction(ActionDefinition::SORT)
  604. ->setCssClass('btn btn-sm btn-success')
  605. ->createAsGlobalAction();
  606. $actions->add(Crud::PAGE_INDEX, $sortAction);
  607. }
  608. }
  609. public function handleTreeEntityActions(Actions $actions): void
  610. {
  611. if ($this->isInstanceOf(TreeInterface::class)) {
  612. $indexChildAction = Action::new(
  613. ActionDefinition::INDEX_CHILDREN,
  614. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::INDEX_CHILDREN),
  615. 'fa fa-list'
  616. )
  617. ->linkToCrudAction(ActionDefinition::INDEX)
  618. ->setLabel('')
  619. ->setHtmlAttributes(array('data-toggle' => 'tooltip', 'title' => 'Afficher les enfants'))
  620. ->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig')
  621. ->setCssClass('btn btn-sm btn-success');
  622. $backParentAction = Action::new(
  623. ActionDefinition::INDEX_PARENT,
  624. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::INDEX_PARENT),
  625. 'fa fa-chevron-left'
  626. )
  627. ->linkToCrudAction(ActionDefinition::INDEX)
  628. ->setCssClass('btn btn-sm btn-info')
  629. ->createAsGlobalAction();
  630. $actions->add(Crud::PAGE_INDEX, $backParentAction);
  631. $actions->add(Crud::PAGE_INDEX, $indexChildAction);
  632. }
  633. }
  634. public function actionUpdate($actions, $crudActionName, $actionName, array $button): void
  635. {
  636. if ($actions->getAsDto('actions')->getAction($crudActionName, $actionName)) {
  637. $actions->update(
  638. $crudActionName,
  639. $actionName,
  640. function (Action $action) use ($button) {
  641. if (isset($button['add_class'])) {
  642. $action->addCssClass($button['add_class']);
  643. }
  644. if (isset($button['class'])) {
  645. $action->setCssClass($button['class']);
  646. }
  647. if (isset($button['icon'])) {
  648. $action->setIcon('fa fa-' . $button['icon']);
  649. }
  650. if (isset($button['label'])) {
  651. $action->setLabel($button['label']);
  652. }
  653. if (isset($button['dropdown']) && $button['dropdown']) {
  654. $action->addCssClass('in-dropdown');
  655. }
  656. if (isset($button['html_attributes']) && $button['html_attributes']) {
  657. $action->setHtmlAttributes($button['html_attributes']);
  658. }
  659. return $action;
  660. }
  661. );
  662. }
  663. }
  664. public function autocompleteFilter(AdminContext $context): JsonResponse
  665. {
  666. $queryBuilder = $this->createIndexQueryBuilder(
  667. $context->getSearch(),
  668. $context->getEntity(),
  669. FieldCollection::new([]),
  670. FilterCollection::new()
  671. );
  672. $autocompleteContext = $context->getRequest()->get(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
  673. /** @var CrudControllerInterface $controller */
  674. $controller = $this->get(ControllerFactory::class)->getCrudControllerInstance(
  675. $autocompleteContext[EA::CRUD_CONTROLLER_FQCN],
  676. ActionDefinition::INDEX,
  677. $context->getRequest()
  678. );
  679. /** @var FieldDto $field */
  680. $field = FieldCollection::new(
  681. $controller->configureFields($autocompleteContext['originatingPage'])
  682. )->getByProperty($autocompleteContext['propertyName']);
  683. $filterManager = $this->get(FilterManager::class);
  684. $filterManager->applyFilter($queryBuilder, $field, $context->getRequest()->query->get('q'));
  685. if ($filterManager->isRelationField($field->getProperty())) {
  686. $queryBuilder->select($autocompleteContext['propertyName']);
  687. } else {
  688. $queryBuilder->select('entity.' . $autocompleteContext['propertyName']);
  689. }
  690. $responses = array();
  691. foreach ($queryBuilder->getQuery()->getArrayResult() as $result) {
  692. $responses[] = array_values($result)[0];
  693. }
  694. return JsonResponse::fromJsonString(json_encode($responses));
  695. }
  696. public function createCustomForm($class, $action, $controller, $entity, $dataInOption = true, $options = array())
  697. {
  698. if ($dataInOption) {
  699. $data = $entity;
  700. } else {
  701. $data = null;
  702. }
  703. $options['action'] = $this->get(AdminUrlGenerator::class)
  704. ->setAction($action)
  705. ->setController($controller)
  706. ->setEntityId($entity->getId())
  707. ->generateUrl();
  708. return $this->createForm($class, $data, $options);
  709. }
  710. }