web-base/Core/API/Traits/Pagination.trait.php

117 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace Core\API\Traits;
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\Driver\SQL\Condition\Condition;
use Core\Driver\SQL\SQL;
use Core\Objects\DatabaseEntity\Controller\DatabaseEntityHandler;
use Core\Objects\DatabaseEntity\Controller\DatabaseEntityQuery;
trait Pagination {
2023-01-16 21:47:23 +01:00
function getPaginationParameters(array $orderColumns, string $defaultOrderBy = null, string $defaultSortOrder = "asc"): array {
2023-01-11 15:28:47 +01:00
$this->paginationOrderColumns = $orderColumns;
2023-01-16 21:47:23 +01:00
$defaultOrderBy = $defaultOrderBy ?? current($orderColumns);
return [
'page' => new Parameter('page', Parameter::TYPE_INT, true, 1),
2023-01-16 21:47:23 +01:00
'count' => new Parameter('count', Parameter::TYPE_INT, true, 25),
'orderBy' => new StringType('orderBy', -1, true, $defaultOrderBy, $orderColumns),
'sortOrder' => new StringType('sortOrder', -1, true, $defaultSortOrder, ['asc', 'desc']),
];
}
2023-01-09 14:21:11 +01:00
function initPagination(SQL $sql, string $class, ?Condition $condition = null, int $maxPageSize = 100, ?array $joins = null): bool {
$this->paginationClass = $class;
$this->paginationCondition = $condition;
if (!$this->validateParameters($maxPageSize)) {
return false;
}
2023-01-09 14:21:11 +01:00
$this->entityCount = call_user_func("$this->paginationClass::count", $sql, $condition, $joins);
if ($this->entityCount === false) {
return $this->createError("Error fetching $this->paginationClass::count: " . $sql->getLastError());
}
$pageCount = intval(ceil($this->entityCount / $this->pageSize));
$this->page = min($this->page, $pageCount); // number of pages changed due to pageSize / filter
$this->result["pagination"] = [
"current" => $this->page,
"pageSize" => $this->pageSize,
"pageCount" => $pageCount,
"total" => $this->entityCount
];
return true;
}
function validateParameters(int $maxCount = 100): bool {
$this->page = $this->getParam("page");
if ($this->page < 1) {
return $this->createError("Invalid page count");
}
$this->pageSize = $this->getParam("count");
if ($this->pageSize < 1 || $this->pageSize > $maxCount) {
return $this->createError("Invalid fetch count");
}
return true;
}
2023-01-09 14:21:11 +01:00
function createPaginationQuery(SQL $sql, ?array $additionalValues = null, ?array $joins = null): DatabaseEntityQuery {
$page = $this->getParam("page");
$count = $this->getParam("count");
$orderBy = $this->getParam("orderBy");
$sortOrder = $this->getParam("sortOrder");
$baseQuery = call_user_func("$this->paginationClass::createBuilder", $sql, false);
$entityQuery = $baseQuery
->fetchEntities()
->limit($count)
->offset(($page - 1) * $count);
if ($this->paginationCondition) {
$entityQuery->where($this->paginationCondition);
}
2023-01-09 14:21:11 +01:00
if ($additionalValues) {
foreach ($additionalValues as $additionalValue) {
$entityQuery->addCustomValue($additionalValue);
}
}
2023-01-09 14:21:11 +01:00
if ($joins) {
foreach ($joins as $join) {
$entityQuery->addJoin($join);
}
}
if ($orderBy) {
$handler = $baseQuery->getHandler();
$baseTable = $handler->getTableName();
2023-01-07 15:34:05 +01:00
$sortColumn = DatabaseEntityHandler::buildColumnName($orderBy);
$fullyQualifiedColumn = "$baseTable.$sortColumn";
$selectedColumns = $baseQuery->getSelectValues();
if (in_array($sortColumn, $selectedColumns)) {
$entityQuery->orderBy($sortColumn);
} else if (in_array($fullyQualifiedColumn, $selectedColumns)) {
$entityQuery->orderBy($fullyQualifiedColumn);
} else {
$entityQuery->orderBy($orderBy);
}
}
if ($sortOrder === "asc") {
$entityQuery->ascending();
} else {
$entityQuery->descending();
}
return $entityQuery;
}
}