|
- <?php
-
- namespace Lc\SovBundle\Controller\Admin;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
- use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController as EaAbstractCrudController;
- use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
- use Lc\SovBundle\Doctrine\EntityManager;
- use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
- use Lc\SovBundle\Doctrine\Extension\SeoInterface;
- use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
- use Lc\SovBundle\Field\CollectionField;
- use Lc\SovBundle\Field\GalleryManagerField;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
- use Twig\Environment;
-
- abstract class AbstractCrudController extends EaAbstractCrudController
- {
- protected $session;
- protected $request;
-
- public function __construct(SessionInterface $session, RequestStack $request, EntityManager $em, Environment $twig)
- {
- $this->session = $session;
- $this->request = $request;
- $this->em = $em;
- $this->twig = $twig;
- }
-
- public function configureActions(Actions $actions): Actions
- {
- /* Translatable */
- if (in_array(TranslatableInterface::class, class_implements($this->getEntityFqcn()))) {
- $actions->update(
- Crud::PAGE_INDEX,
- Action::EDIT,
- function (Action $action) {
- $action->setTemplatePath('@LcSov/adminlte/crud/action/translatable.html.twig');
-
- return $action;
- }
- );
- }
-
- /* Boutons des actions dans les listes */
- $actionsArray[Crud::PAGE_INDEX] = [
- Action::NEW => [
- 'icon' => 'plus',
- 'label' => 'Créer',
- ],
- Action::EDIT => [
- 'class' => 'btn btn-sm btn-primary',
- 'icon' => 'edit',
- 'label' => false,
- ],
- Action::DELETE => [
- 'icon' => 'trash',
- 'dropdown' => true,
- ],
- Action::BATCH_DELETE => [
- 'class' => 'btn btn-danger',
- 'icon' => 'trash',
- ],
- ];
-
- /* Boutons des actions dans l'édition */
-
- $actionSaveAndReturn = [
- 'add-class' => 'float-right',
- 'icon' => 'check',
- ];
- $actionIndex = [
- 'icon' => 'chevron-left',
- 'class' => 'btn btn-link',
- ];
-
- $actionsArray[Crud::PAGE_EDIT] = [
- Action::SAVE_AND_CONTINUE => [
- 'class' => 'btn btn-info float-right',
- ],
- Action::DELETE => [
- 'icon' => 'trash',
- 'class' => 'btn btn-outline-danger action-delete',
- ],
- Action::SAVE_AND_RETURN => $actionSaveAndReturn,
- Action::INDEX => $actionIndex,
- ];
-
- $actionsArray[Crud::PAGE_NEW] = [
- Action::SAVE_AND_ADD_ANOTHER => [
- 'class' => 'btn btn-info float-right',
- ],
- Action::SAVE_AND_RETURN => $actionSaveAndReturn,
- Action::INDEX => $actionIndex,
- ];
-
- $actions->add(Crud::PAGE_EDIT, Action::INDEX);
- $actions->add(Crud::PAGE_EDIT, Action::DELETE);
- $actions->add(Crud::PAGE_NEW, Action::INDEX);
-
- $actions->reorder(Crud::PAGE_EDIT, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE]);
- $actions->reorder(Crud::PAGE_NEW, [Action::INDEX, Action::SAVE_AND_RETURN, Action::SAVE_AND_ADD_ANOTHER]);
-
-
- foreach ($actionsArray as $crudActionName => $actionsStyle) {
- foreach ($actionsStyle as $actionName => $button) {
- $actions->update(
- $crudActionName,
- $actionName,
- function (Action $action) use ($button) {
- if (isset($button['add-class'])) {
- $action->addCssClass($button['add-class']);
- }
-
- if (isset($button['class'])) {
- $action->setCssClass($button['class']);
- }
-
- if (isset($button['icon'])) {
- $action->setIcon('fa fa-'.$button['icon']);
- }
-
- if (isset($button['label'])) {
- $action->setLabel($button['label']);
- }
-
- if (isset($button['dropdown']) && $button['dropdown']) {
- $action->addCssClass('in-dropdown');
- }
-
- return $action;
- }
- );
- }
- }
-
- return $actions;
- }
-
- public function configureCrud(Crud $crud): Crud
- {
- $crud = parent::configureCrud($crud);
-
- $this->setMaxResults($crud);
-
- return $crud;
- }
-
- public function setMaxResults(Crud $crud)
- {
- $entityClass = $this->getEntityFqcn();
- $paramListMaxResults = 'listMaxResults';
- $paramSessionListMaxResults = $entityClass.'-'.$paramListMaxResults;
- $requestListMaxResults = $this->request->getCurrentRequest()->get($paramListMaxResults);
-
- if ($requestListMaxResults) {
- $this->session->set($paramSessionListMaxResults, $requestListMaxResults);
- }
- $maxResults = $this->session->get($paramSessionListMaxResults) ? $this->session->get(
- $paramSessionListMaxResults
- ) : 30;
-
- $crud->setPaginatorPageSize($maxResults);
- }
-
- public function index(AdminContext $context)
- {
- $responseParameters = parent::index($context);
-
- // Liste des fields
- $responseParameters->set('fields', $this->configureFields('index'));
-
- return $responseParameters;
- }
-
- public function configureFields(string $pageName): iterable
- {
- if (in_array(SeoInterface::class, class_implements($this->getEntityFqcn()))) {
- $seoPanel = [
- FormField::addPanel('Seo'),
- TextField::new('metaTitle')->setLabel('Meta Title')->setHelp(
- 'Affiché dans les résultats de recherche Google'
- )->hideOnIndex(),
- TextareaField::new('metaDescription')->setLabel('Meta description')->setHelp(
- 'Affiché dans les résultats de recherche Google'
- )->hideOnIndex(),
- CollectionField::new('oldUrls')
- ->setFormTypeOption('entry_type', TextType::class)->setLabel('Anciennes urls du document')->hideOnIndex()
- ];
- }
-
- if (in_array(DevAliasInterface::class, class_implements($this->getEntityFqcn()))) {
- $confPanel = [
- FormField::addPanel('Conf'),
- TextField::new('devAlias')->hideOnIndex(),
- ];
- }
-
- return array_merge($seoPanel, $confPanel);
- }
- }
|