diff --git a/Controller/Crud.php b/Controller/Crud.php index c38ec12..e09a720 100644 --- a/Controller/Crud.php +++ b/Controller/Crud.php @@ -3,6 +3,7 @@ namespace Arkounay\Bundle\QuickAdminGeneratorBundle\Controller; +use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService; use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\FieldService; use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\RouteExtension; use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\TwigLoaderService; @@ -56,6 +57,7 @@ abstract class Crud extends AbstractController protected TranslatorInterface $translator; protected TwigLoaderService $twigLoader; protected QagExtensionRuntime $qagExtensionRuntime; + protected EntityService $entityService; /** @var EntityRepository */ protected EntityRepository $repository; @@ -88,7 +90,8 @@ public function setInternalDependencies( TranslatorInterface $translator, TwigLoaderService $twigLoader, SluggerInterface $slugger, - QagExtensionRuntime $qagExtensionRuntime + QagExtensionRuntime $qagExtensionRuntime, + EntityService $entityService ): void { $this->em = $em; $this->fieldService = $fieldService; @@ -100,6 +103,7 @@ public function setInternalDependencies( $this->repository = $em->getRepository($this->getEntity()); $this->slugger = $slugger; $this->qagExtensionRuntime = $qagExtensionRuntime; + $this->entityService = $entityService; } /** @@ -323,7 +327,7 @@ public function toggleBooleanPostAction(Request $request, $entity): Response if (!$this->isEditableBoolean($entity)) { throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be edited (boolean)."); } - + $index = $request->request->get('index'); $value = $request->request->getBoolean('checked'); $propertyAccessor = PropertyAccess::createPropertyAccessor(); @@ -637,7 +641,7 @@ protected function updateEntity($entity, bool $creation): void if ($event->isPropagationStopped()) { return; } - $this->addFlash('highlighted_row_id', $entity->getId()); + $this->addFlash('highlighted_row_id', $this->entityService->getId($entity)); } /** @@ -1090,7 +1094,7 @@ protected function entityIsInList($entity): bool { return $this->getListQueryBuilder() ->andWhere('e.id = :id') - ->setParameter('id', $entity->getId()) + ->setParameter('id', $this->entityService->getId($entity)) ->getQuery() ->getOneOrNullResult() !== null; } @@ -1158,7 +1162,7 @@ public function checkSecurity(string $action, $entity = null): void throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be edited."); } if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) { - throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out."); + throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out."); } if ($entity === null) { throw $this->createNotFoundException("No {$this->getNameSentence()} found with id #{$this->request->attributes->get('id')}"); @@ -1174,7 +1178,7 @@ public function checkSecurity(string $action, $entity = null): void throw $this->createAccessDeniedException("Entity {$this->getEntity()} is not removable."); } if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) { - throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out."); + throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out."); } break; case 'view': @@ -1182,7 +1186,7 @@ public function checkSecurity(string $action, $entity = null): void throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be viewed."); } if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) { - throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out."); + throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out."); } if ($entity === null) { throw $this->createNotFoundException("No {$this->getName()} found with id #{$this->request->attributes->get('id')}"); @@ -1193,7 +1197,7 @@ public function checkSecurity(string $action, $entity = null): void throw $this->createAccessDeniedException("Entity {$this->getEntity()} cannot be edited."); } if ($this->hasQuickListQueryBuilderSecurity() && !$this->entityIsInList($entity)) { - throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$entity->getId()} is filtered out."); + throw $this->createAccessDeniedException("Entity {$this->getEntity()} #{$this->entityService->getId($entity)} is filtered out."); } break; } @@ -1202,4 +1206,4 @@ public function checkSecurity(string $action, $entity = null): void $this->eventDispatcher->dispatch($event, 'qag.events.security'); } -} \ No newline at end of file +} diff --git a/Controller/GlobalSearchController.php b/Controller/GlobalSearchController.php index 8fe05ca..ca72c77 100644 --- a/Controller/GlobalSearchController.php +++ b/Controller/GlobalSearchController.php @@ -3,6 +3,7 @@ namespace Arkounay\Bundle\QuickAdminGeneratorBundle\Controller; +use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService; use Doctrine\ORM\Tools\Pagination\Paginator; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\GenericEvent; @@ -18,7 +19,8 @@ public function __construct( /** @var iterable|Crud[] */ private readonly iterable $cruds, private readonly EventDispatcherInterface $eventDispatcher, - private readonly TranslatorInterface $translator + private readonly TranslatorInterface $translator, + private readonly EntityService $entityService ) {} @@ -64,9 +66,9 @@ private function getSearchResults(Request $request, int $maxResults = 5): array if (\count($actions) > 0) { $url = null; if ($crud->isViewable($entity)) { - $url = $this->generateUrl("qag.{$crud->getRoute()}_view", ['id' => $entity->getId(), 'highlight' => $query]); + $url = $this->generateUrl("qag.{$crud->getRoute()}_view", ['id' => $this->entityService->getId($entity), 'highlight' => $query]); } elseif ($crud->isEditable($entity)) { - $url = $this->generateUrl("qag.{$crud->getRoute()}_edit", ['id' => $entity->getId(), 'highlight' => $query]); + $url = $this->generateUrl("qag.{$crud->getRoute()}_edit", ['id' => $this->entityService->getId($entity), 'highlight' => $query]); } if ($url !== null) { @@ -108,4 +110,4 @@ private function getSearchResults(Request $request, int $maxResults = 5): array return $searchResult; } -} \ No newline at end of file +} diff --git a/DependencyInjection/ArkounayQuickAdminGeneratorExtension.php b/DependencyInjection/ArkounayQuickAdminGeneratorExtension.php index 296ef44..d7fcfc4 100644 --- a/DependencyInjection/ArkounayQuickAdminGeneratorExtension.php +++ b/DependencyInjection/ArkounayQuickAdminGeneratorExtension.php @@ -5,6 +5,7 @@ use Arkounay\Bundle\QuickAdminGeneratorBundle\Controller\Crud; +use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService; use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\FieldService; use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\TwigLoaderService; use Arkounay\Bundle\QuickAdminGeneratorBundle\Twig\Runtime\QagExtensionRuntime; @@ -44,9 +45,10 @@ public function load(array $configs, ContainerBuilder $container): void new Reference(TranslatorInterface::class), new Reference(TwigLoaderService::class), new Reference(SluggerInterface::class), - new Reference(QagExtensionRuntime::class) + new Reference(QagExtensionRuntime::class), + new Reference(EntityService::class) ]); } -} \ No newline at end of file +} diff --git a/Extension/EntityService.php b/Extension/EntityService.php new file mode 100644 index 0000000..91d57cb --- /dev/null +++ b/Extension/EntityService.php @@ -0,0 +1,19 @@ +em->getClassMetadata(get_class($entity)); + $idField = $metadata->getSingleIdentifierFieldName(); + return $metadata->getFieldValue($entity, $idField); + } + +} diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 2388b6e..5133c08 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -15,6 +15,9 @@ services: arguments: $formRenderer: '@twig.form.renderer' + Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService: + autowire: true + Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\TwigLoaderService: autowire: true @@ -67,4 +70,4 @@ services: arguments: $projectDir: '%kernel.project_dir%' $cruds: !tagged_iterator quickadmin.crud - autowire: true \ No newline at end of file + autowire: true diff --git a/Twig/Runtime/QagExtensionRuntime.php b/Twig/Runtime/QagExtensionRuntime.php index 7c775a6..1a133f1 100644 --- a/Twig/Runtime/QagExtensionRuntime.php +++ b/Twig/Runtime/QagExtensionRuntime.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Routing\RouterInterface; use Twig\Environment; +use Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService; use Twig\Extension\RuntimeExtensionInterface; class QagExtensionRuntime implements RuntimeExtensionInterface @@ -22,9 +23,10 @@ public function __construct( private readonly RequestStack $requestStack, private readonly MenuInterface $menu, private readonly Environment $twig, - private readonly EventDispatcherInterface $eventDispatcher + private readonly EventDispatcherInterface $eventDispatcher, + private readonly EntityService $entityService ) {} - + private function getMenuItems(): iterable { return $this->menu->generateMenu(); @@ -44,7 +46,7 @@ public function getActionHref(Action $action, $entity = null): string $params = $_GET; /* use $_GET instead of $request->query->all() to avoid knp pagination's change of query attributes */ unset($params['referer']); if ($entity !== null) { - $params['id'] = $entity->getId(); + $params['id'] = $this->entityService->getId($entity); if ($request->attributes->get('qag.from') === 'view') { $params['from'] = 'view'; } @@ -90,4 +92,8 @@ public function getQag(): array return $this->_cache; } + protected function getEntityIdentifier() { + + } + }