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.
*/

View File

@@ -17,7 +17,123 @@ class ArticlesController
*/
public function list(): string
{
return \admin\view\Articles::articles_list();
$sortableColumns = ['title', 'status', 'date_add', 'date_modify'];
$filterDefinitions = [
[
'key' => 'title',
'label' => 'Tytul',
'type' => 'text',
],
[
'key' => 'status',
'label' => 'Aktywny',
'type' => 'select',
'options' => [
'' => '- aktywny -',
'1' => 'tak',
'0' => 'nie',
],
],
];
$listRequest = \admin\Support\TableListRequestFactory::fromRequest(
$filterDefinitions,
$sortableColumns,
'date_add'
);
$result = $this->repository->listForAdmin(
$listRequest['filters'],
$listRequest['sortColumn'],
$listRequest['sortDir'],
$listRequest['page'],
$listRequest['perPage']
);
$rows = [];
$lp = ($listRequest['page'] - 1) * $listRequest['perPage'] + 1;
foreach ($result['items'] as $item) {
$id = (int)$item['id'];
$title = (string)($item['title'] ?? '');
$pages = (string)\admin\factory\Articles::article_pages($id);
$rows[] = [
'lp' => $lp++ . '.',
'title' => '<a href="/admin/articles/article_edit/id=' . $id . '">' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</a>'
. '<small class="text-muted">' . htmlspecialchars($pages, ENT_QUOTES, 'UTF-8') . '</small>',
'status' => ((int)$item['status'] === 1) ? 'tak' : '<span style="color: #FF0000;">nie</span>',
'date_add' => !empty($item['date_add']) ? date('Y-m-d H:i', strtotime((string)$item['date_add'])) : '-',
'date_modify' => !empty($item['date_modify']) ? date('Y-m-d H:i', strtotime((string)$item['date_modify'])) : '-',
'user' => htmlspecialchars((string)($item['user'] ?? ''), ENT_QUOTES, 'UTF-8'),
'_actions' => [
[
'label' => 'Edytuj',
'url' => '/admin/articles/article_edit/id=' . $id,
'class' => 'btn btn-xs btn-primary',
],
[
'label' => 'Usun',
'url' => '/admin/articles/article_delete/id=' . $id,
'class' => 'btn btn-xs btn-danger',
'confirm' => 'Na pewno chcesz usunac wybrany element?',
],
],
];
}
$total = (int)$result['total'];
$totalPages = max(1, (int)ceil($total / $listRequest['perPage']));
$viewModel = new \admin\ViewModels\Common\PaginatedTableViewModel(
[
['key' => 'lp', 'label' => 'Lp.', 'class' => 'text-center', 'sortable' => false],
['key' => 'title', 'sort_key' => 'title', 'label' => 'Tytul', 'sortable' => true, 'raw' => true],
['key' => 'status', 'sort_key' => 'status', 'label' => 'Aktywny', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
['key' => 'date_add', 'sort_key' => 'date_add', 'label' => 'Data dodania', 'class' => 'text-center', 'sortable' => true],
['key' => 'date_modify', 'sort_key' => 'date_modify', 'label' => 'Data modyfikacji', 'class' => 'text-center', 'sortable' => true],
['key' => 'user', 'sort_key' => 'user', 'label' => 'Modyfikowany przez', 'class' => 'text-center', 'sortable' => true],
],
$rows,
$listRequest['viewFilters'],
[
'column' => $listRequest['sortColumn'],
'dir' => $listRequest['sortDir'],
],
[
'page' => $listRequest['page'],
'per_page' => $listRequest['perPage'],
'total' => $total,
'total_pages' => $totalPages,
],
array_merge($listRequest['queryFilters'], [
'sort' => $listRequest['sortColumn'],
'dir' => $listRequest['sortDir'],
'per_page' => $listRequest['perPage'],
]),
$listRequest['perPageOptions'],
$sortableColumns,
'/admin/articles/view_list/',
'Brak danych w tabeli.',
'/admin/articles/article_edit/',
'Dodaj artykul'
);
return \Tpl::view('articles/articles-list', [
'viewModel' => $viewModel,
]);
}
/**
* Zapis kolejnosci galerii (AJAX)
*/
public function galleryOrderSave(): void
{
if ($this->repository->saveGalleryOrder((int)\S::get('article_id'), (string)\S::get('order'))) {
echo json_encode(['status' => 'ok', 'msg' => 'Artykul zostal zapisany.']);
}
exit;
}
/**
@@ -28,10 +144,10 @@ class ArticlesController
global $user;
$values = json_decode(\S::get('values'), true);
$response = ['status' => 'error', 'msg' => 'Podczas zapisywania artykułu wystąpił błąd. Proszę spróbować ponownie.'];
$response = ['status' => 'error', 'msg' => 'Podczas zapisywania artykulu wystapil blad. Prosze sprobowac ponownie.'];
if ($id = $this->repository->save((int)($values['id'] ?? 0), $values, (int)$user['id'])) {
$response = ['status' => 'ok', 'msg' => 'Artykuł został zapisany.', 'id' => $id];
$response = ['status' => 'ok', 'msg' => 'Artykul zostal zapisany.', 'id' => $id];
}
echo json_encode($response);
@@ -44,7 +160,7 @@ class ArticlesController
public function delete(): void
{
if ($this->repository->archive((int)\S::get('id'))) {
\S::alert('Artykuł został przeniesiony do archiwum.');
\S::alert('Artykul zostal przeniesiony do archiwum.');
}
header('Location: /admin/articles/view_list/');

View File

@@ -0,0 +1,100 @@
<?php
namespace admin\Support;
class TableListRequestFactory
{
public const DEFAULT_PER_PAGE_OPTIONS = [5, 10, 15, 25, 50, 100];
public const DEFAULT_PER_PAGE = 15;
/**
* Buduje kontekst listy (filtry, sortowanie, paginacja) z requestu.
*
* @return array{
* page:int,
* perPage:int,
* perPageOptions:array<int,int>,
* filters:array<string,string>,
* viewFilters:array<int,array<string,mixed>>,
* queryFilters:array<string,string>,
* sortColumn:string,
* sortDir:string
* }
*/
public static function fromRequest(
array $filterDefinitions,
array $sortableColumns,
string $defaultSortColumn = 'date_add',
?array $perPageOptions = null,
?int $defaultPerPage = null
): array {
if ($perPageOptions === null) {
$perPageOptions = self::DEFAULT_PER_PAGE_OPTIONS;
}
if ($defaultPerPage === null) {
$defaultPerPage = self::DEFAULT_PER_PAGE;
}
if (!in_array($defaultPerPage, $perPageOptions, true)) {
$defaultPerPage = (int)$perPageOptions[0];
}
$page = max(1, (int)\S::get('page'));
$perPage = (int)\S::get('per_page');
if (!in_array($perPage, $perPageOptions, true)) {
$perPage = $defaultPerPage;
}
$filters = [];
$viewFilters = [];
$queryFilters = [];
foreach ($filterDefinitions as $definition) {
$key = (string)($definition['key'] ?? '');
if ($key === '') {
continue;
}
$type = (string)($definition['type'] ?? 'text');
$value = (string)\S::get($key);
$filters[$key] = $value;
$queryFilters[$key] = $value;
$filterConfig = [
'key' => $key,
'label' => (string)($definition['label'] ?? $key),
'type' => $type,
'value' => $value,
];
if ($type === 'select' && isset($definition['options']) && is_array($definition['options'])) {
$filterConfig['options'] = $definition['options'];
}
$viewFilters[] = $filterConfig;
}
$sortColumn = trim((string)\S::get('sort'));
if (!in_array($sortColumn, $sortableColumns, true)) {
$sortColumn = $defaultSortColumn;
}
$sortDir = strtoupper(trim((string)\S::get('dir')));
if (!in_array($sortDir, ['ASC', 'DESC'], true)) {
$sortDir = 'DESC';
}
return [
'page' => $page,
'perPage' => $perPage,
'perPageOptions' => $perPageOptions,
'filters' => $filters,
'viewFilters' => $viewFilters,
'queryFilters' => $queryFilters,
'sortColumn' => $sortColumn,
'sortDir' => $sortDir,
];
}
}
?>

View File

@@ -0,0 +1,50 @@
<?php
namespace admin\ViewModels\Common;
class PaginatedTableViewModel
{
public array $columns;
public array $rows;
public array $filters;
public array $sort;
public array $pagination;
public array $query;
public array $perPageOptions;
public array $sortableColumns;
public string $basePath;
public string $emptyMessage;
public ?string $createUrl;
public ?string $createLabel;
public ?string $customScriptView;
public function __construct(
array $columns = [],
array $rows = [],
array $filters = [],
array $sort = [],
array $pagination = [],
array $query = [],
array $perPageOptions = [5, 10, 15, 25, 50, 100],
array $sortableColumns = [],
string $basePath = '',
string $emptyMessage = 'Brak danych.',
?string $createUrl = null,
?string $createLabel = null,
?string $customScriptView = null
) {
$this->columns = $columns;
$this->rows = $rows;
$this->filters = $filters;
$this->sort = $sort;
$this->pagination = $pagination;
$this->query = $query;
$this->perPageOptions = $perPageOptions;
$this->sortableColumns = $sortableColumns;
$this->basePath = $basePath;
$this->emptyMessage = $emptyMessage;
$this->createUrl = $createUrl;
$this->createLabel = $createLabel;
$this->customScriptView = $customScriptView;
}
}
?>

View File

@@ -253,6 +253,7 @@ class Site
* Potrzebne gdy stary routing używa innej konwencji nazw
*/
private static $actionMap = [
'gallery_order_save' => 'galleryOrderSave',
'view_list' => 'list',
'article_edit' => 'edit',
'article_save' => 'save',

View File

@@ -1,83 +0,0 @@
<?php
namespace admin\controls;
class Articles
{
public static function gallery_order_save()
{
if ( \admin\factory\Articles::gallery_order_save( \S::get( 'article_id' ), \S::get( 'order' ) ) )
echo json_encode( [ 'status' => 'ok', 'msg' => 'Artykuł został zapisany.' ] );
exit;
}
public static function browse_list()
{
return \admin\view\Articles::browse_list();
}
/**
* @deprecated Routing kieruje do admin\Controllers\ArticlesController::delete().
* Ta metoda pozostaje tylko jako fallback dla starej architektury.
*/
public static function article_delete()
{
if ( \admin\factory\Articles::articles_set_archive( \S::get( 'id' ) ) )
\S::alert( 'Artykuł został przeniesiony do archiwum.' );
header( 'Location: /admin/articles/view_list/' );
exit;
}
/**
* @deprecated Routing kieruje do admin\Controllers\ArticlesController::save().
* Ta metoda pozostaje tylko jako fallback dla starej architektury.
*/
public static function article_save()
{
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania artykułu wystąpił błąd. Proszę spróbować ponownie.' ];
$values = json_decode( \S::get( 'values' ), true );
if ( $id = \admin\factory\Articles::article_save(
$values['id'], $values['title'], $values['main_image'], $values['entry'], $values['text'], $values['table_of_contents'], $values['status'], $values['show_title'], $values['show_table_of_contents'], $values['show_date_add'], $values['date_add'], $values['show_date_modify'], $values['date_modify'], $values['seo_link'], $values['meta_title'],
$values['meta_description'], $values['meta_keywords'], $values['layout_id'], $values['pages'], $values['noindex'], $values['repeat_entry'], $values['copy_from'], $values['social_icons'], $values['block_direct_access']
) )
$response = [ 'status' => 'ok', 'msg' => 'Artykuł został zapisany.', 'id' => $id ];
echo json_encode( $response );
exit;
}
/**
* @deprecated Routing kieruje do admin\Controllers\ArticlesController::edit().
* Ta metoda pozostaje tylko jako fallback dla starej architektury.
*/
public static function article_edit() {
global $user;
if ( !$user ) {
header( 'Location: /admin/' );
exit;
}
\admin\factory\Articles::delete_nonassigned_images();
\admin\factory\Articles::delete_nonassigned_files();
return \Tpl::view( 'articles/article-edit', [
'article' => \admin\factory\Articles::article_details( (int)\S::get( 'id' ) ),
'menus' => \admin\factory\Pages::menus_list(),
'languages' => \admin\factory\Languages::languages_list(),
'layouts' => \admin\factory\Layouts::layouts_list(),
'user' => $user
] );
}
/**
* @deprecated Routing kieruje do admin\Controllers\ArticlesController::list().
* Ta metoda pozostaje tylko jako fallback dla starej architektury.
*/
public static function view_list()
{
return \admin\view\Articles::articles_list();
}
}
?>

View File

@@ -2,23 +2,14 @@
namespace admin\factory;
class Articles
{
/**
* @deprecated Logika przeniesiona do Domain\Article\ArticleRepository::saveGalleryOrder().
*/
public static function gallery_order_save( $article_id, $order )
{
global $mdb;
$order = explode( ';', $order );
if ( is_array( $order ) and !empty( $order ) ) foreach ( $order as $image_id )
{
$mdb -> update( 'pp_articles_images', [
'o' => $i++
], [
'AND' => [
'article_id' => $article_id,
'id' => $image_id
]
] );
}
return true;
$repository = new \Domain\Article\ArticleRepository( $mdb );
return $repository->saveGalleryOrder( (int)$article_id, (string)$order );
}
public static function image_alt_change( $image_id, $image_alt )

View File

@@ -3,12 +3,6 @@ namespace admin\view;
class Articles
{
public static function browse_list()
{
$tpl = new \Tpl;
return $tpl -> render( 'articles/articles-browse-list' );
}
public static function subpages_list( $pages, $article_pages, $parent_id = 0, $step = 1 )
{
$tpl = new \Tpl();
@@ -25,4 +19,4 @@ class Articles
return $tpl -> render( 'articles/articles-list' );
}
}
?>
?>

View File

@@ -874,6 +874,10 @@ class S
$htaccess_data .= 'RewriteCond %{REQUEST_FILENAME} !-d' . PHP_EOL;
$htaccess_data .= 'RewriteRule ^ index.php [L]';
// Niektore hostingi blokuja zmiane wersji PHP przez .htaccess.
// Automatycznie komentujemy niedozwolone dyrektywy, aby generowany plik byl kompatybilny.
$htaccess_data = preg_replace( '/^(\\s*)(AddHandler|SetHandler|ForceType)\\b/im', '$1# $2', $htaccess_data );
$fp = fopen( $dir . '.htaccess', 'w' );
fwrite( $fp, $htaccess_data );
fclose( $fp );