|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Umanit\EasyAdminTreeBundle\Controller; |
| 4 | + |
| 5 | +use Doctrine\Persistence\ManagerRegistry; |
| 6 | +use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; |
| 7 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
| 8 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; |
| 9 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Assets; |
| 10 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; |
| 11 | +use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore; |
| 12 | +use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; |
| 13 | +use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; |
| 14 | +use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent; |
| 15 | +use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent; |
| 16 | +use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException; |
| 17 | +use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory; |
| 18 | +use EasyCorp\Bundle\EasyAdminBundle\Factory\FilterFactory; |
| 19 | +use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; |
| 20 | +use EasyCorp\Bundle\EasyAdminBundle\Security\Permission; |
| 21 | + |
| 22 | +abstract class TreeCrudController extends AbstractCrudController |
| 23 | +{ |
| 24 | + |
| 25 | + protected ManagerRegistry $doctrine; |
| 26 | + |
| 27 | + public function __construct(ManagerRegistry $doctrine) |
| 28 | + { |
| 29 | + $this->doctrine = $doctrine; |
| 30 | + } |
| 31 | + |
| 32 | + public function index(AdminContext $context) |
| 33 | + { |
| 34 | + $event = new BeforeCrudActionEvent($context); |
| 35 | + $this->container->get('event_dispatcher')->dispatch($event); |
| 36 | + |
| 37 | + if ($event->isPropagationStopped()) { |
| 38 | + return $event->getResponse(); |
| 39 | + } |
| 40 | + |
| 41 | + if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::INDEX, 'entity' => null])) { |
| 42 | + throw new ForbiddenActionException($context); |
| 43 | + } |
| 44 | + |
| 45 | + $fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX)); |
| 46 | + $context->getCrud()->setFieldAssets($this->getFieldAssets($fields)); |
| 47 | + $filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $fields, $context->getEntity()); |
| 48 | + |
| 49 | + $repository = $this->doctrine->getRepository($context->getEntity()->getFqcn()); |
| 50 | + |
| 51 | + $queryBuilder = $repository |
| 52 | + ->createQueryBuilder('entity') |
| 53 | + ->orderBy('entity.root, entity.lft', 'ASC') |
| 54 | + ; |
| 55 | + |
| 56 | + $this->doctrine->getManager()->getConfiguration()->addCustomHydrationMode('tree', 'Gedmo\Tree\Hydrator\ORM\TreeObjectHydrator'); |
| 57 | + $entities = $queryBuilder->getQuery()->getResult(); |
| 58 | + $entities = $this->container->get(EntityFactory::class)->createCollection($context->getEntity(), $entities); |
| 59 | + |
| 60 | + $this->container->get(EntityFactory::class)->processFieldsForAll($entities, $fields); |
| 61 | + $actions = $this->container->get(EntityFactory::class)->processActionsForAll($entities, $context->getCrud()->getActionsConfig()); |
| 62 | + |
| 63 | + $responseParameters = $this->configureResponseParameters(KeyValueStore::new([ |
| 64 | + 'pageName' => Crud::PAGE_INDEX, |
| 65 | + 'templateName' => 'crud/index', |
| 66 | + 'entities' => $entities, |
| 67 | + 'global_actions' => $actions->getGlobalActions(), |
| 68 | + 'batch_actions' => null, |
| 69 | + 'filters' => $filters, |
| 70 | + ])); |
| 71 | + |
| 72 | + $event = new AfterCrudActionEvent($context, $responseParameters); |
| 73 | + $this->container->get('event_dispatcher')->dispatch($event); |
| 74 | + |
| 75 | + if ($event->isPropagationStopped()) { |
| 76 | + return $event->getResponse(); |
| 77 | + } |
| 78 | + |
| 79 | + return $responseParameters; |
| 80 | + } |
| 81 | + |
| 82 | + public function configureFields(string $pageName): iterable |
| 83 | + { |
| 84 | + if ($pageName === Crud::PAGE_INDEX) { |
| 85 | + return [TextField::new($this->getEntityLabelProperty())]; |
| 86 | + } |
| 87 | + |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + public function configureCrud(Crud $crud): Crud |
| 92 | + { |
| 93 | + return $crud |
| 94 | + ->overrideTemplate('crud/index', '@UmanitEasyAdminTreeBundle/index.html.twig') |
| 95 | + ->setPaginatorPageSize(9999999) |
| 96 | + ->showEntityActionsInlined() |
| 97 | + ->setSearchFields(null) |
| 98 | + ; |
| 99 | + } |
| 100 | + |
| 101 | + public static function getEntityFqcn(): string |
| 102 | + { |
| 103 | + throw new \LogicException('Override this method in child class'); |
| 104 | + } |
| 105 | + |
| 106 | + public function configureAssets(Assets $assets): Assets |
| 107 | + { |
| 108 | + return $assets |
| 109 | + ->addCssFile('bundles/umaniteasyadmintree/css/tree.css') |
| 110 | + ; |
| 111 | + } |
| 112 | + |
| 113 | + public function configureActions(Actions $actions): Actions |
| 114 | + { |
| 115 | + return $actions |
| 116 | + ->update(Crud::PAGE_INDEX, Action::EDIT, |
| 117 | + function (Action $action) { |
| 118 | + return $action |
| 119 | + ->addCssClass('umanit_easyadmintree_tree-item-action') |
| 120 | + ; |
| 121 | + } |
| 122 | + ) |
| 123 | + ->update(Crud::PAGE_INDEX, Action::DELETE, |
| 124 | + function (Action $action) { |
| 125 | + return $action |
| 126 | + ->addCssClass('action-delete umanit_easyadmintree_tree-item-action') |
| 127 | + ; |
| 128 | + } |
| 129 | + ) |
| 130 | + ; |
| 131 | + } |
| 132 | + |
| 133 | + abstract protected function getEntityLabelProperty(): string; |
| 134 | +} |
0 commit comments