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.

178 satır
6.4KB

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