Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractCrudController.php 5.2KB

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. }