No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

AbstractCrudController.php 7.3KB

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