Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Nov 15, 2024
1 parent f2f2f4c commit 1e4130e
Show file tree
Hide file tree
Showing 13 changed files with 491 additions and 42 deletions.
82 changes: 45 additions & 37 deletions src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class DataTable implements DataTableInterface
/**
* The copy of a query used to retrieve the data, without any filters applied.
*/
private ProxyQueryInterface $originalQuery;
private ProxyQueryInterface $nonFilteredQuery;

private ?ResultSetInterface $resultSet = null;

Expand All @@ -117,13 +117,14 @@ public function __construct(
private ProxyQueryInterface $query,
private /* readonly */ DataTableConfigInterface $config,
) {
$this->originalQuery = clone $this->query;
$this->nonFilteredQuery = clone $this->query;
}

public function __clone(): void
{
$this->config = clone $this->config;
$this->query = clone $this->query;
$this->nonFilteredQuery = clone $this->nonFilteredQuery;
}

public function initialize(): void
Expand Down Expand Up @@ -464,13 +465,14 @@ public function paginate(PaginationData $data, bool $persistence = true): void

$this->query->paginate($data);

$this->originalQuery = $this->query;
$this->nonFilteredQuery = $this->query;

if ($persistence && $this->config->isPaginationPersistenceEnabled()) {
$this->setPersistenceData(PersistenceContext::Pagination, $data);
}

$this->setPaginationData($data);
$this->paginationData = $data;

$this->resetPagination();

$this->dispatch(DataTableEvents::POST_PAGINATE, new DataTablePaginationEvent($this, $data));
Expand All @@ -486,20 +488,16 @@ public function sort(SortingData $data, bool $persistence = true): void

$data = $event->getSortingData();

$columns = $this->getColumns();

$data->removeRedundantColumns($columns);
$data->ensureValidPropertyPaths($columns);

$this->query->sort($data);

$this->originalQuery = $this->query;
$this->nonFilteredQuery = $this->query;

if ($persistence && $this->config->isSortingPersistenceEnabled()) {
$this->setPersistenceData(PersistenceContext::Sorting, $data);
}

$this->setSortingData($data);
$this->sortingData = $data;

$this->resetPagination();

$this->dispatch(DataTableEvents::POST_SORT, new DataTableSortingEvent($this, $data));
Expand All @@ -511,18 +509,13 @@ public function filter(FiltrationData $data, bool $persistence = true): void
return;
}

$this->query = clone $this->originalQuery;
$this->query = clone $this->nonFilteredQuery;

$this->dispatch(DataTableEvents::PRE_FILTER, $event = new DataTableFiltrationEvent($this, $data));

$data = $event->getFiltrationData();

$filters = $this->getFilters();

$data->appendMissingFilters($filters);
$data->removeRedundantFilters($filters);

foreach ($filters as $filter) {
foreach ($this->getFilters() as $filter) {
$filterData = $data->getFilterData($filter->getName());

if ($filterData && $filterData->hasValue()) {
Expand All @@ -534,7 +527,8 @@ public function filter(FiltrationData $data, bool $persistence = true): void
$this->setPersistenceData(PersistenceContext::Filtration, $data);
}

$this->setFiltrationData($data);
$this->filtrationData = $data;

$this->resetPagination();

$this->dispatch(DataTableEvents::POST_FILTER, new DataTableFiltrationEvent($this, $data));
Expand All @@ -553,13 +547,12 @@ public function personalize(PersonalizationData $data, bool $persistence = true)
$columns = $this->getColumns();

$data->addMissingColumns($columns);
$data->removeRedundantColumns($columns);

if ($persistence && $this->config->isPersonalizationPersistenceEnabled()) {
$this->setPersistenceData(PersistenceContext::Personalization, $data);
}

$this->setPersonalizationData($data);
$this->personalizationData = $data;

$data->apply($this->getColumns());

Expand Down Expand Up @@ -647,6 +640,9 @@ public function getSortingData(): ?SortingData
return $this->sortingData;
}

/**
* @deprecated use {@see sort()} method instead
*/
public function setSortingData(?SortingData $sortingData): static
{
$this->sortingData = $sortingData;
Expand All @@ -659,6 +655,9 @@ public function getPaginationData(): ?PaginationData
return $this->paginationData;
}

/**
* @deprecated use {@see paginate()} method instead
*/
public function setPaginationData(?PaginationData $paginationData): static
{
$this->paginationData = $paginationData;
Expand All @@ -671,6 +670,9 @@ public function getFiltrationData(): ?FiltrationData
return $this->filtrationData;
}

/**
* @deprecated use {@see filter()} method instead
*/
public function setFiltrationData(?FiltrationData $filtrationData): static
{
$this->filtrationData = $filtrationData;
Expand All @@ -683,6 +685,9 @@ public function getPersonalizationData(): ?PersonalizationData
return $this->personalizationData;
}

/**
* @deprecated use {@see personalize()} method instead
*/
public function setPersonalizationData(?PersonalizationData $personalizationData): static
{
$this->personalizationData = $personalizationData;
Expand All @@ -695,6 +700,9 @@ public function getExportData(): ?ExportData
return $this->exportData;
}

/**
* @deprecated use {@see export()} method instead
*/
public function setExportData(?ExportData $exportData): static
{
$this->exportData = $exportData;
Expand Down Expand Up @@ -814,21 +822,24 @@ public function createExportView(): DataTableView
return $view;
}

private function dispatch(string $eventName, DataTableEvent $event): void
{
$dispatcher = $this->config->getEventDispatcher();

if ($dispatcher->hasListeners($eventName)) {
$dispatcher->dispatch($event, $eventName);
}
}

private function resetPagination(): void
{
$this->pagination = null;
$this->resultSet = null;
}

private function resetPersonalization(): void
{
$this->personalizationData = null;

foreach ($this->columns as $column) {
$column
->setPriority($column->getConfig()->getOption('priority'))
->setVisible($column->getConfig()->getOption('visible'))
;
}
}

private function getInitialPaginationData(): ?PaginationData
{
if (!$this->config->isPaginationEnabled()) {
Expand Down Expand Up @@ -958,15 +969,12 @@ private function getPersistenceSubject(PersistenceContext $context): Persistence
return $provider->provide();
}

private function resetPersonalization(): void
private function dispatch(string $eventName, DataTableEvent $event): void
{
$this->personalizationData = null;
$dispatcher = $this->config->getEventDispatcher();

foreach ($this->columns as $column) {
$column
->setPriority($column->getConfig()->getOption('priority'))
->setVisible($column->getConfig()->getOption('visible'))
;
if ($dispatcher->hasListeners($eventName)) {
$dispatcher->dispatch($event, $eventName);
}
}
}
25 changes: 20 additions & 5 deletions src/DataTableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,39 @@ public function getPagination(): PaginationInterface;

public function getSortingData(): ?SortingData;

/**
* @deprecated since 0.26, use {@see sort()} instead
*/
public function setSortingData(?SortingData $sortingData): static;

public function setPaginationData(?PaginationData $paginationData): static;

public function getPaginationData(): ?PaginationData;

public function setFiltrationData(?FiltrationData $filtrationData): static;
/**
* @deprecated since 0.26, use {@see paginate()} instead
*/
public function setPaginationData(?PaginationData $paginationData): static;

public function getFiltrationData(): ?FiltrationData;

public function setPersonalizationData(?PersonalizationData $personalizationData): static;
/**
* @deprecated since 0.26, use {@see filter()} instead
*/
public function setFiltrationData(?FiltrationData $filtrationData): static;

public function getPersonalizationData(): ?PersonalizationData;

public function setExportData(?ExportData $exportData): static;
/**
* @deprecated since 0.26, use {@see personalize()} instead
*/
public function setPersonalizationData(?PersonalizationData $personalizationData): static;

public function getExportData(): ?ExportData;

/**
* @deprecated since 0.26, use {@see export()} instead
*/
public function setExportData(?ExportData $exportData): static;

public function createFiltrationFormBuilder(?DataTableView $view = null): FormBuilderInterface;

public function createPersonalizationFormBuilder(?DataTableView $view = null): FormBuilderInterface;
Expand Down
31 changes: 31 additions & 0 deletions src/EventListener/EnsureValidSortingColumnsPropertyPaths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\EventListener;

use Kreyu\Bundle\DataTableBundle\Event\DataTableEvents;
use Kreyu\Bundle\DataTableBundle\Event\DataTableSortingEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class EnsureValidSortingColumnsPropertyPaths implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
DataTableEvents::PRE_SORT => 'ensureValidSortingColumnsPropertyPaths',
];
}

public function ensureValidSortingColumnsPropertyPaths(DataTableSortingEvent $event): void
{
$dataTable = $event->getDataTable();
$data = $event->getSortingData();

foreach ($data->getColumns() as $column) {
if ($dataTable->hasColumn($column->getName())) {
$column->setPropertyPath($dataTable->getColumn($column->getName())->getSortPropertyPath());
}
}
}
}
31 changes: 31 additions & 0 deletions src/EventListener/RemoveRedundantFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\EventListener;

use Kreyu\Bundle\DataTableBundle\Event\DataTableEvents;
use Kreyu\Bundle\DataTableBundle\Event\DataTableFiltrationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class RemoveRedundantFilters implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
DataTableEvents::PRE_FILTER => 'removeRedundantFilters',
];
}

public function removeRedundantFilters(DataTableFiltrationEvent $event): void
{
$dataTable = $event->getDataTable();
$data = $event->getFiltrationData();

foreach ($data->getFilters() as $name => $filter) {
if (!$dataTable->hasFilter($name)) {
$data->removeFilter($name);
}
}
}
}
41 changes: 41 additions & 0 deletions src/EventListener/RemoveRedundantPersonalizationColumns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\EventListener;

use Kreyu\Bundle\DataTableBundle\DataTableInterface;
use Kreyu\Bundle\DataTableBundle\Event\DataTableEvents;
use Kreyu\Bundle\DataTableBundle\Event\DataTablePersonalizationEvent;
use Kreyu\Bundle\DataTableBundle\Event\DataTableSortingEvent;
use Kreyu\Bundle\DataTableBundle\Personalization\PersonalizationColumnData;
use Kreyu\Bundle\DataTableBundle\Sorting\SortingColumnData;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class RemoveRedundantPersonalizationColumns implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
DataTableEvents::PRE_PERSONALIZE => 'removeRedundantPersonalizationColumns',
];
}

public function removeRedundantPersonalizationColumns(DataTablePersonalizationEvent $event): void
{
$dataTable = $event->getDataTable();
$data = $event->getPersonalizationData();

foreach ($data->getColumns() as $column) {
if ($this->isColumnRedundant($dataTable, $column)) {
$data->removeColumn($column);
}
}
}

private function isColumnRedundant(DataTableInterface $dataTable, PersonalizationColumnData $column): bool
{
return !$dataTable->hasColumn($column->getName())
|| !$dataTable->getColumn($column->getName())->getConfig()->isPersonalizable();
}
}
Loading

0 comments on commit 1e4130e

Please sign in to comment.