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
49 changes: 49 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
UPGRADE FROM 3.x to 4.0
=======================

## Added `Sonata\DatagridBundle\Field\FieldDescriptionInterface` and `Sonata\DatagridBundle\Field\BaseFieldDescription`

They are similar to `Sonata\AdminBundle\Admin\FieldDescriptionInterface`
and `Sonata\AdminBundle\Admin\BaseFieldDescription` except that

- `name` is now required to create a new FieldDescription.
- `getFieldMapping` and `setFieldMapping` now use an `FieldMappingInterface`.
- `getAssociationMapping` and `setAssociationMapping` now use an `AssociationMappingInterface`.
- `getParentAssociationMappings` and `setParentAssociationMappings` now use an `AssociationMappingInterface`.
- `FieldDescriptionInterface::getFieldName` were removed
in favor of `FieldMappingInterface::getFieldName`.
- `FieldDescriptionInterface::setFieldName` were removed
in favor of `FieldMappingInterface::setFieldName`.
- `FieldDescriptionInterface::getMappingType` were removed
in favor of `FieldMappingInterface::getMappingType`.
- `FieldDescriptionInterface::setMappingType` were removed
in favor of `FieldMappingInterface::setMappingType`.
- `FieldDescriptionInterface::getLabel` was removed
in favor of `FieldDescriptionInterface::getOption('label')`.
- `FieldDescriptionInterface::getTranslationDomain` was removed
in favor of `FieldDescriptionInterface::getOption('translation_domain')`.
- `FieldDescriptionInterface::isSortable` was removed
in favor of `FieldDescriptionInterface::getOption('sortable')`.
- `FieldDescriptionInterface::getSortFieldMapping` was removed.
- `FieldDescriptionInterface::getSortParentAssociationMapping` was removed.
- All admin-related method was not added.

`Sonata\AdminBundle\Admin\FieldDescriptionInterface` will extend this interface in the next major.

## Changed

- `Sonata\DatagridBundle\ProxyQuery\BaseProxyQuery`:
Moved every DoctrineORM-related methods to `Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery`.

- `Sonata\DatagridBundle\Datagrid\DatagridInterface`:
Must now implement `hasDisplayableFilters`, `getSortParameters` and `getPaginationParameters` methods.

## Removed

- `Sonata\DatagridBundle\ProxyQuery\Elastica\ProxyQuery`.
- `Sonata\DatagridBundle\Pager\Elastica\Pager`.

## Deprecated

- `Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery`.
- `Sonata\DatagridBundle\Pager\Doctrine\Pager`.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
],
"require": {
"php": "^7.2",
"doctrine/inflector": "^1.4 || ^2.0",
"symfony/config": "^3.4 || ^4.4 || ^5.0",
"symfony/dependency-injection": "^3.4 || ^4.4 || ^5.0",
"symfony/form": "^3.4 || ^4.4 || ^5.0",
"symfony/http-kernel": "^3.4 || ^4.4 || ^5.0"
},
"require-dev": {
"doctrine/orm": "^2.4",
"doctrine/orm": "^2.7",
"symfony/phpunit-bridge": "^5.0"
},
"config": {
Expand Down
17 changes: 0 additions & 17 deletions phpstan-baseline.neon

This file was deleted.

3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
includes:
- phpstan-baseline.neon

parameters:
level: 0

Expand Down
90 changes: 86 additions & 4 deletions src/Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

namespace Sonata\DatagridBundle\Datagrid;

use Sonata\DatagridBundle\Field\FieldDescriptionInterface;
use Sonata\DatagridBundle\Filter\FilterInterface;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\DatagridBundle\ProxyQuery\ProxyQueryInterface;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormInterface;
Expand Down Expand Up @@ -55,7 +58,7 @@ final class Datagrid implements DatagridInterface
private $formBuilder;

/**
* @var FormInterface
* @var FormInterface|null
*/
private $form;

Expand Down Expand Up @@ -109,19 +112,41 @@ public function buildPager(): void
$this->formBuilder->add('_page', HiddenType::class);
$this->formBuilder->add('_per_page', HiddenType::class);

$this->formBuilder->get('_sort_by')->addViewTransformer(new CallbackTransformer(
static function ($value) {
return $value;
},
static function ($value) {
return $value instanceof FieldDescriptionInterface ? $value->getName() : $value;
}
));

$this->form = $this->formBuilder->getForm();
$this->form->submit($this->values);

$data = $this->form->getData();

foreach ($this->getFilters() as $name => $filter) {
$this->values[$name] = $this->values[$name] ?? null;
$filter->apply($this->query, $data[$filter->getFormName()] ?? null);

$filterFormName = $filter->getFormName();
if (isset($this->values[$filterFormName]['value']) && '' !== $this->values[$filterFormName]['value']) {
$filter->apply($this->query, $data[$filterFormName]);
}
}

if (isset($this->values['_sort_by'])) {
$this->query->setSortBy($this->values['_sort_by']);
$this->query->setSortOrder($this->values['_sort_order'] ?? null);
$sortBy = $this->values['_sort_by'];
if (!$sortBy instanceof FieldDescriptionInterface) {
throw new UnexpectedTypeException($this->values['_sort_by'], FieldDescriptionInterface::class);
}

if (false !== $sortBy->getOption('sortable', false)) {
$this->values['_sort_order'] = $this->values['_sort_order'] ?? 'ASC';

$this->query->setSortBy($this->values['_sort_by']);
$this->query->setSortOrder($this->values['_sort_order']);
}
}

$this->pager->setMaxPerPage($this->values['_per_page'] ?? 25);
Expand Down Expand Up @@ -189,6 +214,18 @@ public function hasActiveFilters(): bool
return false;
}

public function hasDisplayableFilters(): bool
{
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 All @@ -200,4 +237,49 @@ public function getForm(): FormInterface

return $this->form;
}

public function getSortParameters(FieldDescriptionInterface $fieldDescription): array
{
$values = $this->getValues();

if ($this->isFieldAlreadySorted($fieldDescription)) {
if ('ASC' === $values['_sort_order']) {
$values['_sort_order'] = 'DESC';
} else {
$values['_sort_order'] = 'ASC';
}
} else {
$values['_sort_order'] = 'ASC';
}

$values['_sort_by'] = \is_string($fieldDescription->getOption('sortable'))
? $fieldDescription->getOption('sortable')
: $fieldDescription->getName();

return ['filter' => $values];
}

public function getPaginationParameters(int $page): array
{
$values = $this->getValues();

if (isset($values['_sort_by']) && $values['_sort_by'] instanceof FieldDescriptionInterface) {
$values['_sort_by'] = $values['_sort_by']->getName();
}
$values['_page'] = $page;

return ['filter' => $values];
}

private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription): bool
{
$values = $this->getValues();

if (!isset($values['_sort_by']) || !$values['_sort_by'] instanceof FieldDescriptionInterface) {
return false;
}

return $values['_sort_by']->getName() === $fieldDescription->getName()
|| $values['_sort_by']->getName() === $fieldDescription->getOption('sortable');
}
}
7 changes: 7 additions & 0 deletions src/Datagrid/DatagridInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\DatagridBundle\Datagrid;

use Sonata\DatagridBundle\Field\FieldDescriptionInterface;
use Sonata\DatagridBundle\Filter\FilterInterface;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\DatagridBundle\ProxyQuery\ProxyQueryInterface;
Expand Down Expand Up @@ -50,4 +51,10 @@ public function hasFilter(string $name): bool;
public function removeFilter(string $name): void;

public function hasActiveFilters(): bool;

public function hasDisplayableFilters(): bool;

public function getSortParameters(FieldDescriptionInterface $fieldDescription): array;

public function getPaginationParameters(int $page): array;
}
21 changes: 21 additions & 0 deletions src/Exception/NoValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DatagridBundle\Exception;

/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class NoValueException extends \Exception
{
}
Loading