Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a criteria provider to allow simple querying without creating a custom provider #473

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion config/bridges/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use LAG\AdminBundle\Bridge\Doctrine\ORM\Metadata\MetadataPropertyFactory;
use LAG\AdminBundle\Bridge\Doctrine\ORM\Metadata\MetadataPropertyFactoryInterface;
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\Processor\ORMProcessor;
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\Provider\CriteriaProvider;
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\Provider\DoctrineCollectionNormalizeProvider;
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\Provider\ORMProvider;
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\Provider\PaginationProvider;
Expand All @@ -37,7 +38,11 @@
->tag('lag_admin.state_provider', ['identifier' => 'doctrine', 'priority' => 0])
;
$services->set(SortingProvider::class)
->decorate(ProviderInterface::class, priority: 300)
->decorate(ProviderInterface::class, priority: 230)
->arg('$provider', service('.inner'))
;
$services->set(CriteriaProvider::class)
->decorate(ProviderInterface::class, priority: 220)
->arg('$provider', service('.inner'))
;
$services->set(PaginationProvider::class)
Expand Down
11 changes: 11 additions & 0 deletions src/Bridge/Doctrine/ORM/Exception/ORMException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace LAG\AdminBundle\Bridge\Doctrine\ORM\Exception;

use LAG\AdminBundle\Exception\Exception;

final class ORMException extends Exception
{
}
45 changes: 45 additions & 0 deletions src/Bridge/Doctrine/ORM/State/Provider/CriteriaProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace LAG\AdminBundle\Bridge\Doctrine\ORM\State\Provider;

use Doctrine\ORM\QueryBuilder;
use LAG\AdminBundle\Resource\Metadata\CollectionOperationInterface;
use LAG\AdminBundle\Resource\Metadata\OperationInterface;
use LAG\AdminBundle\State\Provider\ProviderInterface;
use function Symfony\Component\String\u;

final readonly class CriteriaProvider implements ProviderInterface
{
public function __construct(
private ProviderInterface $provider,
) {
}

public function provide(OperationInterface $operation, array $uriVariables = [], array $context = []): mixed
{
$data = $this->provider->provide($operation, $uriVariables, $context);

if (!$data instanceof QueryBuilder) {
return $data;
}

if (!$operation instanceof CollectionOperationInterface || $operation->getCriteria() === []) {
return $data;
}

foreach ($operation->getCriteria() as $criteria => $value) {
$alias = $data->getRootAliases()[0];
$dql = u('entity.field = :'.$criteria.'_value')
->replace('entity', $alias)
->replace('field', $criteria)
;
$data->andWhere($dql->toString())
->setParameter($criteria.'_value', $value)
;
}

return $data;
}
}
11 changes: 10 additions & 1 deletion src/Bridge/Doctrine/ORM/State/Provider/SortingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LAG\AdminBundle\Bridge\Doctrine\ORM\State\Provider;

use Doctrine\ORM\QueryBuilder;
use LAG\AdminBundle\Bridge\Doctrine\ORM\Exception\ORMException;
use LAG\AdminBundle\Resource\Metadata\CollectionOperationInterface;
use LAG\AdminBundle\Resource\Metadata\OperationInterface;
use LAG\AdminBundle\State\Provider\ProviderInterface;
Expand Down Expand Up @@ -35,7 +36,15 @@ public function provide(OperationInterface $operation, array $uriVariables = [],

foreach ($orderBy as $field => $direction) {
$property = $resource->getProperty($field);
$order = u($property->getPropertyPath() ?? $field);
$sort = $property->getSortingPath();

if ($sort === null) {
throw new ORMException(\sprintf(
'The property "%s" can not be sorted by as the sorting path is null.',
$property->getName(),
));
}
$order = u($sort);

if ($order->containsAny('.')) {
$alias = $rootAlias;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ private function initializeProperty(Resource $resource, PropertyInterface $prope
}
}

if ($property instanceof Link) {
if ($property->getText() === null) {
$property = $property->withText($property->getName());
if ($property instanceof Link && $property->getText() === null) {
$property = $property->withText($property->getName());
}

if ($property->isSortable() && $property->getSortingPath() === null) {
$sortingPath = $property->getName();

if (is_string($property->getPropertyPath())) {
$sortingPath = $property->getPropertyPath();
}
$property = $property->withSortingPath($sortingPath);
}

return $property;
Expand Down
2 changes: 2 additions & 0 deletions src/Resource/Metadata/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

private ?string $route = null,
private array $routeParameters = [],
Expand All @@ -44,6 +45,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Resource/Metadata/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function __construct(
array $headerAttributes = [],
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null
?string $condition = null,
?string $sortingPath = null,
) {
parent::__construct(
name: $name,
Expand All @@ -34,6 +35,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}
}
2 changes: 2 additions & 0 deletions src/Resource/Metadata/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

#[Assert\NotNull(message: 'The collection should have an property for each entry')]
private ?PropertyInterface $entryProperty = null,
Expand All @@ -41,6 +42,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Resource/Metadata/Compound.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

private array $properties = [],
) {
Expand All @@ -36,6 +37,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Resource/Metadata/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
?string $dataTransformer = CountDataTransformer::class,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,
) {
parent::__construct(
name: $name,
Expand All @@ -31,6 +32,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}
}
2 changes: 2 additions & 0 deletions src/Resource/Metadata/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

#[Assert\NotBlank(message: 'The date format should not be empty. Use "none" instead')]
private string $dateFormat = 'medium',
Expand All @@ -41,6 +42,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Resource/Metadata/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public function __construct(
) {
parent::__construct(
name: $name,
propertyPath: '.',
label: $label,
template: '@LAGAdmin/grids/properties/group.html.twig',
sortable: false,
propertyPath: '.',
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
Expand Down
4 changes: 3 additions & 1 deletion src/Resource/Metadata/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

private ?string $route = null,
private array $routeParameters = [],
Expand All @@ -41,8 +42,8 @@ public function __construct(
parent::__construct(
name: $name,
propertyPath: $propertyPath,
template: $template,
label: $label,
template: $template,
sortable: $sortable,
translatable: $translatable,
translationDomain: $translationDomain,
Expand All @@ -52,6 +53,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Resource/Metadata/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

#[Assert\Count(min: 1, minMessage: 'The map should have at least 1 element')]
private array $map = [],
Expand All @@ -38,6 +39,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}

Expand Down
24 changes: 22 additions & 2 deletions src/Resource/Metadata/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(

private bool $translatable = false,

#[Assert\NotBlank(allowNull: true, message: 'The translation domain should not be empty. Use null instead')]
#[Assert\NotBlank(message: 'The translation domain should not be empty. Use null instead', allowNull: true)]
private ?string $translationDomain = null,

private array $attributes = [],
Expand All @@ -44,13 +44,20 @@ public function __construct(

private array $headerAttributes = [],

#[Assert\NotBlank(allowNull: true, message: 'The data transformer should not be empty. Use null instead')]
#[Assert\NotBlank(message: 'The data transformer should not be empty. Use null instead', allowNull: true)]
private ?string $dataTransformer = null,

private ?array $permissions = null,

#[WorkflowInstalled]
private ?string $condition = null,

#[Assert\NotBlank(message: 'The sorting path should not be empty. Use null instead', allowNull: true)]
#[Assert\Expression(
expression: '(this.isSortable() and this.getSortingPath() !== null) or !this.isSortable()',
message: 'The sorting path should not be null if the property is sortable',
)]
private ?string $sortingPath = null,
) {
}

Expand Down Expand Up @@ -235,4 +242,17 @@ public function withCondition(?string $condition): self

return $self;
}

public function getSortingPath(): ?string
{
return $this->sortingPath;
}

public function withSortingPath(?string $sortingPath): self
{
$self = clone $this;
$self->sortingPath = $sortingPath;

return $self;
}
}
4 changes: 4 additions & 0 deletions src/Resource/Metadata/PropertyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,8 @@ public function getPermissions(): ?array;
* Define the property permissions.
*/
public function withPermissions(array $permissions): self;

public function getSortingPath(): ?string;

public function withSortingPath(?string $sortingPath): self;
}
6 changes: 4 additions & 2 deletions src/Resource/Metadata/ResourceLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
string $emptyString = '~',
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

#[NotBlank]
private ?string $application = null,
Expand All @@ -42,11 +43,12 @@ public function __construct(
translationDomain: $translationDomain,
attributes: $attributes,
headerAttributes: $headerAttributes,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
length: $length,
replace: $replace,
empty: $emptyString,
permissions: $permissions,
condition: $condition,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Resource/Metadata/RichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,

#[Assert\Length(min: 1)]
private int $length = 100,
Expand All @@ -42,6 +43,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Resource/Metadata/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(
?string $dataTransformer = null,
?array $permissions = null,
?string $condition = null,
?string $sortingPath = null,
) {
parent::__construct(
name: $name,
Expand All @@ -38,6 +39,7 @@ public function __construct(
dataTransformer: $dataTransformer,
permissions: $permissions,
condition: $condition,
sortingPath: $sortingPath
);
}

Expand Down
Loading
Loading