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 7.6KB

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