Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions Controller/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +57,7 @@ abstract class Crud extends AbstractController
protected TranslatorInterface $translator;
protected TwigLoaderService $twigLoader;
protected QagExtensionRuntime $qagExtensionRuntime;
protected EntityService $entityService;

/** @var EntityRepository<T> */
protected EntityRepository $repository;
Expand Down Expand Up @@ -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;
Expand All @@ -100,6 +103,7 @@ public function setInternalDependencies(
$this->repository = $em->getRepository($this->getEntity());
$this->slugger = $slugger;
$this->qagExtensionRuntime = $qagExtensionRuntime;
$this->entityService = $entityService;
}

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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')}");
Expand All @@ -1174,15 +1178,15 @@ 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':
if (!$this->isViewable($entity)) {
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')}");
Expand All @@ -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;
}
Expand All @@ -1202,4 +1206,4 @@ public function checkSecurity(string $action, $entity = null): void
$this->eventDispatcher->dispatch($event, 'qag.events.security');
}

}
}
10 changes: 6 additions & 4 deletions Controller/GlobalSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
)
{}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -108,4 +110,4 @@ private function getSearchResults(Request $request, int $maxResults = 5): array
return $searchResult;
}

}
}
6 changes: 4 additions & 2 deletions DependencyInjection/ArkounayQuickAdminGeneratorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
]);

}

}
}
19 changes: 19 additions & 0 deletions Extension/EntityService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Arkounay\Bundle\QuickAdminGeneratorBundle\Extension;

use Doctrine\ORM\EntityManagerInterface;

readonly class EntityService
{

public function __construct(private EntityManagerInterface $em) {}

public function getId(object $entity): mixed
{
$metadata = $this->em->getClassMetadata(get_class($entity));
$idField = $metadata->getSingleIdentifierFieldName();
return $metadata->getFieldValue($entity, $idField);
}

}
5 changes: 4 additions & 1 deletion Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ services:
arguments:
$formRenderer: '@twig.form.renderer'

Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\EntityService:
autowire: true

Arkounay\Bundle\QuickAdminGeneratorBundle\Extension\TwigLoaderService:
autowire: true

Expand Down Expand Up @@ -67,4 +70,4 @@ services:
arguments:
$projectDir: '%kernel.project_dir%'
$cruds: !tagged_iterator quickadmin.crud
autowire: true
autowire: true
12 changes: 9 additions & 3 deletions Twig/Runtime/QagExtensionRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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';
}
Expand Down Expand Up @@ -90,4 +92,8 @@ public function getQag(): array
return $this->_cache;
}

protected function getEntityIdentifier() {

}

}