Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.
Closed
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
12 changes: 12 additions & 0 deletions src/Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ public function hasActiveFilters(): bool
return false;
}

public function hasDisplayableFilters(): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should add this method. Showing a filter or not is logic for the UI, not the general lib

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the library ?

hasActiveFilters and hasDisplayableFilters are really similar. I think it's better to have the two methods in the same Interface/class.

Currently we have a loooot of duplicated code/really similar code. I want to deal with this, and I think we'll have to make some small trade-off.

There are some others UI method in this bundle, like Filter::getRenderSettings, or getTranslationDomain. The Datagrid class is also mainly a UI class, and is never use by others bundle. They only use the Pager and the ProxyQuery.

{
foreach ($this->filters as $name => $filter) {
$showFilter = $filter->getOption('show_filter', null);
if (($filter->isActive() && null === $showFilter) || (true === $showFilter)) {
return true;
}
}

return false;
}

public function getQuery(): ProxyQueryInterface
{
return $this->query;
Expand Down
2 changes: 2 additions & 0 deletions src/Datagrid/DatagridInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ public function hasFilter(string $name): bool;
public function removeFilter(string $name): void;

public function hasActiveFilters(): bool;

public function hasDisplayableFilters(): bool;
}
41 changes: 41 additions & 0 deletions src/Filter/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ public function getFieldOptions(): array
return $this->getOption('field_options', ['required' => false]);
}

public function getFieldOption(string $name, $default = null)
{
if (isset($this->options['field_options'][$name]) && \is_array($this->options['field_options'])) {
return $this->options['field_options'][$name];
}

return $default;
}

public function setFieldOption(string $name, $value): void
{
$this->options['field_options'][$name] = $value;
}

public function getLabel(): ?string
{
return $this->getOption('label');
Expand All @@ -112,6 +126,33 @@ public function getFieldName(): string
return $fieldName;
}

public function getParentAssociationMappings(): array
{
return $this->getOption('parent_association_mappings', []);
}

public function getFieldMapping(): array
{
$fieldMapping = $this->getOption('field_mapping');

if (!$fieldMapping) {
throw new \RuntimeException(sprintf('The option `field_mapping` must be set for field: `%s`', $this->getName()));
}

return $fieldMapping;
}

public function getAssociationMapping(): array
{
$associationMapping = $this->getOption('association_mapping');

if (!$associationMapping) {
throw new \RuntimeException(sprintf('The option `association_mapping` must be set for field: `%s`', $this->getName()));
}

return $associationMapping;
}

public function setOptions(array $options): void
{
$this->options = array_merge($this->getDefaultOptions(), $options);
Expand Down
5 changes: 1 addition & 4 deletions src/Filter/FilterFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@

interface FilterFactoryInterface
{
/**
* @return mixed
*/
public function create(string $name, string $type, array $options = []);
public function create(string $name, string $type, array $options = []): FilterInterface;
}
27 changes: 27 additions & 0 deletions src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,35 @@ public function initialize(string $name, array $options = []): void;

public function getFieldName(): string;

/**
* @return array<string, string> array of mappings
*/
public function getParentAssociationMappings(): array;

/**
* @return array<string, string> field mapping
*/
public function getFieldMapping(): array;

/**
* @return array<string, string> association mapping
*/
public function getAssociationMapping(): array;

public function getFieldOptions(): array;

/**
* @param mixed $default
*
* @return mixed
*/
public function getFieldOption(string $name, $default = null);

/**
* @param mixed $value
*/
public function setFieldOption(string $name, $value): void;

public function getFieldType(): string;

/**
Expand Down
86 changes: 61 additions & 25 deletions src/ProxyQuery/BaseProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ abstract class BaseProxyQuery implements ProxyQueryInterface
protected $results = [];

/**
* @var array
* @var int
*/
private $sortBy = [];
protected $uniqueParameterId = 0;

/**
* @var array
* @var string[]
*/
protected $entityJoinAliases = [];

/**
* @var string|null
*/
private $sortBy;

/**
* @var string|null
*/
private $sortOrder = [];
private $sortOrder;

/**
* @var int|null
Expand All @@ -62,38 +72,27 @@ public function __call(string $name, array $args)
return \call_user_func_array([$this->queryBuilder, $name], $args);
}

/**
* @param mixed $sortBy
*/
public function setSortBy($sortBy): ProxyQueryInterface
public function setSortBy(array $parentAssociationMappings, array $fieldMapping): ProxyQueryInterface
{
$this->sortBy = $sortBy;
$alias = $this->entityJoin($parentAssociationMappings);
$this->sortBy = $alias.'.'.$fieldMapping['fieldName'];

return $this;
}

/**
* @return mixed
*/
public function getSortBy()
public function getSortBy(): ?string
{
return $this->sortBy;
}

/**
* @param mixed $sortOrder
*/
public function setSortOrder($sortOrder): ProxyQueryInterface
public function setSortOrder(string $sortOrder): ProxyQueryInterface
{
$this->sortOrder = $sortOrder;

return $this;
}

/**
* @return mixed
*/
public function getSortOrder()
public function getSortOrder(): ?string
{
return $this->sortOrder;
}
Expand Down Expand Up @@ -122,10 +121,7 @@ public function getMaxResults(): ?int
return $this->maxResults;
}

/**
* @return mixed
*/
public function getQueryBuilder()
public function getQueryBuilder(): QueryBuilder
{
return $this->queryBuilder;
}
Expand All @@ -134,4 +130,44 @@ public function getResults(): array
{
return $this->results;
}

public function getUniqueParameterId(): int
{
return $this->uniqueParameterId++;
}

public function entityJoin(array $associationMappings): string
{
$alias = current($this->queryBuilder->getRootAliases());

$newAlias = 's';

$joinedEntities = $this->queryBuilder->getDQLPart('join');

foreach ($associationMappings as $associationMapping) {
// Do not add left join to already joined entities with custom query
foreach ($joinedEntities as $joinExprList) {
foreach ($joinExprList as $joinExpr) {
$newAliasTmp = $joinExpr->getAlias();

if (sprintf('%s.%s', $alias, $associationMapping['fieldName']) === $joinExpr->getJoin()) {
$this->entityJoinAliases[] = $newAliasTmp;
$alias = $newAliasTmp;

continue 3;
}
}
}

$newAlias .= '_'.$associationMapping['fieldName'];
if (!\in_array($newAlias, $this->entityJoinAliases, true)) {
$this->entityJoinAliases[] = $newAlias;
$this->queryBuilder->leftJoin(sprintf('%s.%s', $alias, $associationMapping['fieldName']), $newAlias);
}

$alias = $newAlias;
}

return $alias;
}
}
24 changes: 8 additions & 16 deletions src/ProxyQuery/ProxyQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,13 @@ public function __call(string $name, array $args);
*/
public function execute(array $params = [], ?int $hydrationMode = null);

/**
* @param mixed $sortBy
*/
public function setSortBy($sortBy): self;
public function setSortBy(array $parentAssociationMappings, array $fieldMapping): self;

/**
* @return mixed
*/
public function getSortBy();
public function getSortBy(): ?string;

/**
* @param mixed $sortOrder
*/
public function setSortOrder($sortOrder): self;
public function setSortOrder(string $sortOrder): self;

/**
* @return mixed
*/
public function getSortOrder();
public function getSortOrder(): ?string;

public function setFirstResult(?int $firstResult): self;

Expand All @@ -57,4 +45,8 @@ public function setMaxResults(?int $maxResults): self;
public function getMaxResults(): ?int;

public function getResults(): array;

public function getUniqueParameterId(): int;

public function entityJoin(array $associationMappings): string;
}