Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

182 lines
6.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\Context\AdminContext;
  8. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController as EaAbstractCrudController;
  9. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  10. use Lc\SovBundle\Doctrine\EntityManager;
  11. use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Validator\Constraints\Json;
  16. use Twig\Environment;
  17. abstract class AbstractCrudController extends EaAbstractCrudController
  18. {
  19. protected $session;
  20. protected $request;
  21. protected $em;
  22. protected $twig;
  23. public function __construct(SessionInterface $session, RequestStack $request, EntityManager $em, Environment $twig)
  24. {
  25. $this->session = $session;
  26. $this->request = $request;
  27. $this->em = $em;
  28. $this->twig = $twig;
  29. }
  30. public function configureActions(Actions $actions): Actions
  31. {
  32. /* Translatable */
  33. if (in_array(TranslatableInterface::class, class_implements($this->getEntityFqcn()))) {
  34. $actions->update(
  35. Crud::PAGE_INDEX,
  36. Action::EDIT,
  37. function (Action $action) {
  38. $action->setTemplatePath('@LcSov/adminlte/crud/action/translatable.html.twig');
  39. return $action;
  40. }
  41. );
  42. }
  43. /* Boutons des actions dans les listes */
  44. $actionsArray[Crud::PAGE_INDEX] = [
  45. Action::NEW => [
  46. 'icon' => 'plus',
  47. 'label' => 'Créer'
  48. ],
  49. Action::EDIT => [
  50. 'class' => 'btn btn-sm btn-primary',
  51. 'icon' => 'edit',
  52. 'label' => false,
  53. ],
  54. Action::DELETE => [
  55. 'icon' => 'trash',
  56. 'dropdown' => true,
  57. ],
  58. Action::BATCH_DELETE => [
  59. 'class' => 'btn btn-danger',
  60. 'icon' => 'trash',
  61. ],
  62. ];
  63. /* Boutons des actions dans l'édition */
  64. $actionSaveAndReturn = [
  65. 'add-class' => 'float-right',
  66. 'icon' => 'check',
  67. ];
  68. $actionIndex = [
  69. 'icon' => 'chevron-left',
  70. 'class' => 'btn btn-link',
  71. ];
  72. $actionsArray[Crud::PAGE_EDIT] = [
  73. Action::SAVE_AND_CONTINUE => [
  74. 'class' => 'btn btn-info float-right',
  75. ],
  76. Action::DELETE => [
  77. 'icon' => 'trash',
  78. 'class' => 'btn btn-outline-danger action-delete',
  79. ],
  80. Action::SAVE_AND_RETURN => $actionSaveAndReturn,
  81. Action::INDEX => $actionIndex
  82. ];
  83. $actionsArray[Crud::PAGE_NEW] = [
  84. Action::SAVE_AND_ADD_ANOTHER => [
  85. 'class' => 'btn btn-info float-right',
  86. ],
  87. Action::SAVE_AND_RETURN => $actionSaveAndReturn,
  88. Action::INDEX => $actionIndex
  89. ];
  90. $actions->add(Crud::PAGE_EDIT, Action::INDEX);
  91. $actions->add(Crud::PAGE_EDIT, Action::DELETE);
  92. $actions->add(Crud::PAGE_NEW, Action::INDEX);
  93. $actions->reorder(Crud::PAGE_EDIT, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE]);
  94. $actions->reorder(Crud::PAGE_NEW, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_ADD_ANOTHER]);
  95. foreach ($actionsArray as $crudActionName => $actionsStyle) {
  96. foreach ($actionsStyle as $actionName => $button) {
  97. $actions->update(
  98. $crudActionName,
  99. $actionName,
  100. function (Action $action) use ($button) {
  101. if (isset($button['add-class'])) {
  102. $action->addCssClass($button['add-class']);
  103. }
  104. if (isset($button['class'])) {
  105. $action->setCssClass($button['class']);
  106. }
  107. if (isset($button['icon'])) {
  108. $action->setIcon('fa fa-' . $button['icon']);
  109. }
  110. if (isset($button['label'])) {
  111. $action->setLabel($button['label']);
  112. }
  113. if (isset($button['dropdown']) && $button['dropdown']) {
  114. $action->addCssClass('in-dropdown');
  115. }
  116. return $action;
  117. }
  118. );
  119. }
  120. }
  121. return $actions;
  122. }
  123. public function configureCrud(Crud $crud): Crud
  124. {
  125. $crud = parent::configureCrud($crud);
  126. $this->setMaxResults($crud);
  127. return $crud;
  128. }
  129. public function setMaxResults(Crud $crud)
  130. {
  131. $entityClass = $this->getEntityFqcn();
  132. $paramListMaxResults = 'listMaxResults';
  133. $paramSessionListMaxResults = $entityClass . '-' . $paramListMaxResults;
  134. $requestListMaxResults = $this->request->getCurrentRequest()->get($paramListMaxResults);
  135. if ($requestListMaxResults) {
  136. $this->session->set($paramSessionListMaxResults, $requestListMaxResults);
  137. }
  138. $maxResults = $this->session->get($paramSessionListMaxResults) ? $this->session->get(
  139. $paramSessionListMaxResults
  140. ) : 30;
  141. $crud->setPaginatorPageSize($maxResults);
  142. }
  143. public function index(AdminContext $context)
  144. {
  145. $responseParameters = parent::index($context);
  146. // Liste des fields
  147. $responseParameters->set('fields', $this->configureFields('index'));
  148. return $responseParameters;
  149. }
  150. }