Skip to content

Commit

Permalink
refactor: Datatable apply* methods return self
Browse files Browse the repository at this point in the history
  • Loading branch information
garett-at-liquidlight committed Sep 2, 2024
1 parent b5546fe commit 739c9db
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions Classes/Controller/DatatableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,12 @@ protected function prepareQuery(array $params): QueryBuilder
;

// Re-apply restrictions
$this->applyDeleteFilter($query, $this->table, $this->table);

$query = $this->applyJoins($query, $query);

// Apply filters
if ($params['filters'] ?? false) {
$query = $this->applyFilters($query, $params);
}

// Apply search
$query = $this->applySearch($query, $params);
$this
->applyDeleteFilter($query, $this->table, $this->table)
->applyJoins($query, $query)
->applyFilters($query, $params)
->applySearch($query, $params)
;

return $query;
}
Expand Down Expand Up @@ -204,7 +199,7 @@ protected function getCount(array $params): int
/**
* Apply search to query
*/
protected function applySearch(QueryBuilder $query, array $params): QueryBuilder
protected function applySearch(QueryBuilder $query, array $params): self
{
if ($params['search']['value']) {
$searchableColumns = GeneralUtility::trimExplode(',', $this->searchableColumns);
Expand All @@ -222,13 +217,13 @@ protected function applySearch(QueryBuilder $query, array $params): QueryBuilder
$query->andWhere($searchQuery);
}

return $query;
return $this;
}

/**
* Apply joins to query
*/
protected function applyJoins(QueryBuilder $query): QueryBuilder
protected function applyJoins(QueryBuilder $query): self
{
$joins = $this->joins ?? [];

Expand Down Expand Up @@ -266,10 +261,10 @@ protected function applyJoins(QueryBuilder $query): QueryBuilder
$this->applyDeleteFilter($query, $table, $alias);
}

return $query;
return $this;
}

protected function applyDeleteFilter(QueryBuilder $query, string $table, string $alias, bool $restrict = true)
protected function applyDeleteFilter(QueryBuilder $query, string $table, string $alias, bool $restrict = true): self
{
// Exclude anything that is deleted
if ($deleteFiled = $GLOBALS['TCA'][$table]['ctrl']['delete'] ?? false) {
Expand All @@ -281,12 +276,14 @@ protected function applyDeleteFilter(QueryBuilder $query, string $table, string
),
);
}

return $this;
}

/**
* Apply filters to query
*/
protected function applyFilters(QueryBuilder $query, array $params): QueryBuilder
protected function applyFilters(QueryBuilder $query, array $params): self
{
foreach ($params['filters'] ?? [] as $field => $filter) {
// If filtering by usergroup
Expand Down Expand Up @@ -339,7 +336,7 @@ protected function applyFilters(QueryBuilder $query, array $params): QueryBuilde
;
}
}
return $query;
return $this;
}

/**
Expand Down

0 comments on commit 739c9db

Please sign in to comment.