Release 0.245: refactor articles list and update package

This commit is contained in:
2026-02-08 01:35:13 +01:00
parent 4aea594477
commit d709a3df7b
28 changed files with 936 additions and 339 deletions

View File

@@ -6,6 +6,8 @@ namespace Domain\Article;
*/
class ArticleRepository
{
private const MAX_PER_PAGE = 100;
private $db;
public function __construct($db)
@@ -329,6 +331,159 @@ class ArticleRepository
return (bool)$result;
}
/**
* Zwraca liste artykulow do panelu admin z filtrowaniem, sortowaniem i paginacja.
*
* @return array{items: array<int, array<string, mixed>>, total: int}
*/
public function listForAdmin(
array $filters,
string $sortColumn = 'date_add',
string $sortDir = 'DESC',
int $page = 1,
int $perPage = 15
): array {
$sortColumn = trim($sortColumn);
$sortDir = strtoupper(trim($sortDir));
$allowedSortColumns = [
'title' => 'title',
'status' => 'pa.status',
'date_add' => 'pa.date_add',
'date_modify' => 'pa.date_modify',
'user' => 'user',
];
$sortSql = $allowedSortColumns[$sortColumn] ?? 'pa.date_add';
$sortDir = $sortDir === 'ASC' ? 'ASC' : 'DESC';
$page = max(1, $page);
$perPage = min(self::MAX_PER_PAGE, max(1, $perPage));
$offset = ($page - 1) * $perPage;
$where = ['pa.status != -1'];
$params = [];
$title = trim((string)($filters['title'] ?? ''));
if (strlen($title) > 255) {
$title = substr($title, 0, 255);
}
if ($title !== '') {
$where[] = "(
SELECT title
FROM pp_articles_langs AS pal, pp_langs AS pl
WHERE lang_id = pl.id AND article_id = pa.id AND title != ''
ORDER BY o ASC
LIMIT 1
) LIKE :title";
$params[':title'] = '%' . $title . '%';
}
if (($filters['status'] ?? '') !== '' && ($filters['status'] === '0' || $filters['status'] === '1')) {
$where[] = 'pa.status = :status';
$params[':status'] = (int)$filters['status'];
}
$this->appendDateRangeFilter($where, $params, 'pa.date_add', 'date_add_from', 'date_add_to', $filters);
$this->appendDateRangeFilter($where, $params, 'pa.date_modify', 'date_modify_from', 'date_modify_to', $filters);
$whereSql = implode(' AND ', $where);
$sqlCount = "
SELECT COUNT(0)
FROM pp_articles AS pa
WHERE {$whereSql}
";
$stmtCount = $this->db->query($sqlCount, $params);
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
$sql = "
SELECT
pa.id,
pa.date_add,
pa.date_modify,
pa.status,
(
SELECT title
FROM pp_articles_langs AS pal, pp_langs AS pl
WHERE lang_id = pl.id AND article_id = pa.id AND title != ''
ORDER BY o ASC
LIMIT 1
) AS title,
(
SELECT login
FROM pp_users AS pu
WHERE pu.id = pa.modify_by
) AS user
FROM pp_articles AS pa
WHERE {$whereSql}
ORDER BY {$sortSql} {$sortDir}, pa.id {$sortDir}
LIMIT {$perPage} OFFSET {$offset}
";
$stmt = $this->db->query($sql, $params);
$items = $stmt ? $stmt->fetchAll() : [];
return [
'items' => is_array($items) ? $items : [],
'total' => $total,
];
}
/**
* Zapisuje kolejnosc zdjec galerii artykulu.
*/
public function saveGalleryOrder(int $articleId, string $order): bool
{
$imageIds = explode(';', $order);
if (!is_array($imageIds) || empty($imageIds)) {
return true;
}
$position = 0;
foreach ($imageIds as $imageId) {
if ($imageId === '' || $imageId === null) {
continue;
}
$this->db->update('pp_articles_images', [
'o' => $position++,
], [
'AND' => [
'article_id' => $articleId,
'id' => (int)$imageId,
],
]);
}
return true;
}
private function appendDateRangeFilter(
array &$where,
array &$params,
string $column,
string $fromKey,
string $toKey,
array $filters
): void {
$from = trim((string)($filters[$fromKey] ?? ''));
$to = trim((string)($filters[$toKey] ?? ''));
if ($from !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $from)) {
$fromParam = ':' . str_replace('.', '_', $column) . '_from';
$where[] = "{$column} >= {$fromParam}";
$params[$fromParam] = $from . ' 00:00:00';
}
if ($to !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) {
$toParam = ':' . str_replace('.', '_', $column) . '_to';
$where[] = "{$column} <= {$toParam}";
$params[$toParam] = $to . ' 23:59:59';
}
}
/**
* Usuwa nieprzypisane pliki artykulow (article_id = null) wraz z plikami z dysku.
*/