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.

149 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',
  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-link text-danger',
  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->add(Crud::PAGE_NEW, Action::DELETE);
  78. $actions->reorder(Crud::PAGE_EDIT, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE]);
  79. $actions->reorder(Crud::PAGE_NEW, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_ADD_ANOTHER]);
  80. foreach ($actionsArray as $crudActionName => $actionsStyle) {
  81. foreach ($actionsStyle as $actionName => $button) {
  82. $actions->update(
  83. $crudActionName,
  84. $actionName,
  85. function (Action $action) use ($button) {
  86. if (isset($button['add-class'])) {
  87. $action->addCssClass($button['add-class']);
  88. }
  89. if (isset($button['class'])) {
  90. $action->setCssClass($button['class']);
  91. }
  92. if (isset($button['icon'])) {
  93. $action->setIcon('fa fa-'.$button['icon']);
  94. }
  95. if (isset($button['label'])) {
  96. $action->setLabel($button['label']);
  97. }
  98. return $action;
  99. }
  100. );
  101. }
  102. }
  103. return $actions;
  104. }
  105. public function configureCrud(Crud $crud): Crud
  106. {
  107. $crud = parent::configureCrud($crud);;
  108. $this->setMaxResults($crud);
  109. return $crud;
  110. }
  111. public function setMaxResults(Crud $crud)
  112. {
  113. $entityClass = $this->getEntityFqcn();
  114. $paramListMaxResults = 'listMaxResults';
  115. $paramSessionListMaxResults = $entityClass.'-'.$paramListMaxResults;
  116. $requestListMaxResults = $this->request->getCurrentRequest()->get($paramListMaxResults);
  117. if ($requestListMaxResults) {
  118. $this->session->set($paramSessionListMaxResults, $requestListMaxResults);
  119. }
  120. $maxResults = $this->session->get($paramSessionListMaxResults) ? $this->session->get(
  121. $paramSessionListMaxResults
  122. ) : 30;
  123. $crud->setPaginatorPageSize($maxResults);
  124. }
  125. }