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.

148 lines
5.2KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\Admin;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  7. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController as EaAbstractCrudController;
  8. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  9. use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. abstract class AbstractCrudController extends EaAbstractCrudController
  13. {
  14. protected $session;
  15. protected $request;
  16. public function __construct(SessionInterface $session, RequestStack $request)
  17. {
  18. $this->session = $session;
  19. $this->request = $request;
  20. }
  21. public function configureActions(Actions $actions): Actions
  22. {
  23. /* Translatable */
  24. if (in_array(TranslatableInterface::class, class_implements($this->getEntityFqcn()))) {
  25. $actions->update(
  26. Crud::PAGE_INDEX,
  27. Action::EDIT,
  28. function (Action $action) {
  29. $action->setTemplatePath('@LcSov/adminlte/crud/action/translatable.html.twig');
  30. return $action;
  31. }
  32. );
  33. }
  34. $actionSaveAndReturn = [
  35. 'add-class' => 'float-right',
  36. 'icon' => 'check',
  37. ];
  38. $actionIndex = [
  39. 'icon' => 'chevron-left',
  40. 'class' => 'btn btn-link',
  41. ];
  42. /* Boutons des actions dans les listes */
  43. $actionsArray[Crud::PAGE_INDEX] = [
  44. Action::EDIT => [
  45. 'class' => 'btn btn-sm btn-primary',
  46. 'icon' => 'edit',
  47. 'label' => false,
  48. ],
  49. Action::DELETE => [
  50. 'class' => 'btn btn-sm btn-default action-delete',
  51. 'icon' => 'trash',
  52. 'label' => false,
  53. ],
  54. ];
  55. /* Boutons des actions dans l'édition */
  56. $actionsArray[Crud::PAGE_EDIT] = [
  57. Action::SAVE_AND_CONTINUE => [
  58. 'class' => 'btn btn-info float-right',
  59. ],
  60. Action::DELETE => [
  61. 'icon' => 'trash',
  62. 'class' => 'btn btn-outline-danger action-delete',
  63. ],
  64. Action::SAVE_AND_RETURN => $actionSaveAndReturn,
  65. Action::INDEX=>$actionIndex
  66. ];
  67. $actionsArray[Crud::PAGE_NEW] = [
  68. Action::SAVE_AND_ADD_ANOTHER => [
  69. 'class' => 'btn btn-info float-right',
  70. ],
  71. Action::SAVE_AND_RETURN => $actionSaveAndReturn,
  72. Action::INDEX=>$actionIndex
  73. ];
  74. $actions->add(Crud::PAGE_EDIT, Action::INDEX);
  75. $actions->add(Crud::PAGE_EDIT, Action::DELETE);
  76. $actions->add(Crud::PAGE_NEW, Action::INDEX);
  77. $actions->reorder(Crud::PAGE_EDIT, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE]);
  78. $actions->reorder(Crud::PAGE_NEW, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_ADD_ANOTHER]);
  79. foreach ($actionsArray as $crudActionName => $actionsStyle) {
  80. foreach ($actionsStyle as $actionName => $button) {
  81. $actions->update(
  82. $crudActionName,
  83. $actionName,
  84. function (Action $action) use ($button) {
  85. if (isset($button['add-class'])) {
  86. $action->addCssClass($button['add-class']);
  87. }
  88. if (isset($button['class'])) {
  89. $action->setCssClass($button['class']);
  90. }
  91. if (isset($button['icon'])) {
  92. $action->setIcon('fa fa-'.$button['icon']);
  93. }
  94. if (isset($button['label'])) {
  95. $action->setLabel($button['label']);
  96. }
  97. return $action;
  98. }
  99. );
  100. }
  101. }
  102. return $actions;
  103. }
  104. public function configureCrud(Crud $crud): Crud
  105. {
  106. $crud = parent::configureCrud($crud);;
  107. $this->setMaxResults($crud);
  108. return $crud;
  109. }
  110. public function setMaxResults(Crud $crud)
  111. {
  112. $entityClass = $this->getEntityFqcn();
  113. $paramListMaxResults = 'listMaxResults';
  114. $paramSessionListMaxResults = $entityClass.'-'.$paramListMaxResults;
  115. $requestListMaxResults = $this->request->getCurrentRequest()->get($paramListMaxResults);
  116. if ($requestListMaxResults) {
  117. $this->session->set($paramSessionListMaxResults, $requestListMaxResults);
  118. }
  119. $maxResults = $this->session->get($paramSessionListMaxResults) ? $this->session->get(
  120. $paramSessionListMaxResults
  121. ) : 30;
  122. $crud->setPaginatorPageSize($maxResults);
  123. }
  124. }