refactor articles_archive to DI controller and table-list
This commit is contained in:
@@ -331,6 +331,32 @@ class ArticleRepository
|
||||
return (bool)$result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Przywraca artykul z archiwum (status = 0).
|
||||
*/
|
||||
public function restore(int $articleId): bool
|
||||
{
|
||||
$result = $this->db->update('pp_articles', ['status' => 0], ['id' => $articleId]);
|
||||
return (bool)$result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trwale usuwa artykul wraz z relacjami i plikami z dysku.
|
||||
*/
|
||||
public function deletePermanently(int $articleId): bool
|
||||
{
|
||||
$this->db->delete('pp_articles_pages', ['article_id' => $articleId]);
|
||||
$this->db->delete('pp_articles_langs', ['article_id' => $articleId]);
|
||||
$this->db->delete('pp_articles_images', ['article_id' => $articleId]);
|
||||
$this->db->delete('pp_articles_files', ['article_id' => $articleId]);
|
||||
$this->db->delete('pp_articles', ['id' => $articleId]);
|
||||
|
||||
\S::delete_dir('../upload/article_images/article_' . $articleId . '/');
|
||||
\S::delete_dir('../upload/article_files/article_' . $articleId . '/');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca liste artykulow do panelu admin z filtrowaniem, sortowaniem i paginacja.
|
||||
*
|
||||
@@ -431,6 +457,93 @@ class ArticleRepository
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca liste artykulow z archiwum do panelu admin z filtrowaniem, sortowaniem i paginacja.
|
||||
*
|
||||
* @return array{items: array<int, array<string, mixed>>, total: int}
|
||||
*/
|
||||
public function listArchivedForAdmin(
|
||||
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',
|
||||
'date_add' => 'pa.date_add',
|
||||
'date_modify' => 'pa.date_modify',
|
||||
];
|
||||
|
||||
$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 . '%';
|
||||
}
|
||||
|
||||
$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,
|
||||
(
|
||||
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
|
||||
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.
|
||||
*/
|
||||
|
||||
147
autoload/admin/Controllers/ArticlesArchiveController.php
Normal file
147
autoload/admin/Controllers/ArticlesArchiveController.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
namespace admin\Controllers;
|
||||
|
||||
use Domain\Article\ArticleRepository;
|
||||
use admin\ViewModels\Common\PaginatedTableViewModel;
|
||||
|
||||
class ArticlesArchiveController
|
||||
{
|
||||
private ArticleRepository $repository;
|
||||
|
||||
public function __construct(ArticleRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$sortableColumns = ['title', 'date_add', 'date_modify'];
|
||||
|
||||
$filterDefinitions = [
|
||||
[
|
||||
'key' => 'title',
|
||||
'label' => 'Tytul',
|
||||
'type' => 'text',
|
||||
],
|
||||
];
|
||||
|
||||
$listRequest = \admin\Support\TableListRequestFactory::fromRequest(
|
||||
$filterDefinitions,
|
||||
$sortableColumns,
|
||||
'date_add'
|
||||
);
|
||||
|
||||
$result = $this->repository->listArchivedForAdmin(
|
||||
$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'] ?? 0);
|
||||
$title = trim((string)($item['title'] ?? ''));
|
||||
|
||||
$rows[] = [
|
||||
'lp' => $lp++ . '.',
|
||||
'title' => '<a href="/admin/articles/article_edit/id=' . $id . '">' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</a>',
|
||||
'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'])) : '-',
|
||||
'_actions' => [
|
||||
[
|
||||
'label' => 'Przywroc',
|
||||
'url' => '/admin/articles_archive/article_restore/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-success',
|
||||
'confirm' => 'Na pewno chcesz przywrocic wybrany artykul?',
|
||||
'confirm_ok' => 'Przywroc',
|
||||
'confirm_cancel' => 'Anuluj',
|
||||
],
|
||||
[
|
||||
'label' => 'Usun',
|
||||
'url' => '/admin/articles_archive/article_delete/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-danger',
|
||||
'confirm' => 'Na pewno chcesz trwale usunac wybrany artykul?',
|
||||
'confirm_ok' => 'Usun',
|
||||
'confirm_cancel' => 'Anuluj',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$total = (int)$result['total'];
|
||||
$totalPages = max(1, (int)ceil($total / $listRequest['perPage']));
|
||||
|
||||
$viewModel = new PaginatedTableViewModel(
|
||||
[
|
||||
['key' => 'lp', 'label' => 'Lp.', 'class' => 'text-center', 'sortable' => false],
|
||||
['key' => 'title', 'sort_key' => 'title', 'label' => 'Tytul', '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],
|
||||
],
|
||||
$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_archive/view_list/',
|
||||
'Brak danych w tabeli.'
|
||||
);
|
||||
|
||||
return \Tpl::view('articles/articles-archive-list', [
|
||||
'viewModel' => $viewModel,
|
||||
]);
|
||||
}
|
||||
|
||||
public function view_list(): string
|
||||
{
|
||||
return $this->list();
|
||||
}
|
||||
|
||||
public function restore(): void
|
||||
{
|
||||
if ($this->repository->restore((int)\S::get('id'))) {
|
||||
\S::alert('Artykul zostal przywrocony.');
|
||||
}
|
||||
|
||||
header('Location: /admin/articles_archive/view_list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function article_restore(): void
|
||||
{
|
||||
$this->restore();
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
if ($this->repository->deletePermanently((int)\S::get('id'))) {
|
||||
\S::alert('Artykul zostal trwale usuniety.');
|
||||
}
|
||||
|
||||
header('Location: /admin/articles_archive/view_list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function article_delete(): void
|
||||
{
|
||||
$this->delete();
|
||||
}
|
||||
}
|
||||
@@ -211,6 +211,13 @@ class Site
|
||||
new \Domain\Layouts\LayoutsRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'ArticlesArchive' => function() {
|
||||
global $mdb;
|
||||
|
||||
return new \admin\Controllers\ArticlesArchiveController(
|
||||
new \Domain\Article\ArticleRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'Banners' => function() {
|
||||
global $mdb;
|
||||
|
||||
@@ -286,6 +293,14 @@ class Site
|
||||
new \Domain\Newsletter\NewsletterPreviewRenderer()
|
||||
);
|
||||
},
|
||||
'Scontainers' => function() {
|
||||
global $mdb;
|
||||
|
||||
return new \admin\Controllers\ScontainersController(
|
||||
new \Domain\Scontainers\ScontainersRepository( $mdb ),
|
||||
new \Domain\Languages\LanguagesRepository( $mdb )
|
||||
);
|
||||
},
|
||||
];
|
||||
|
||||
return self::$newControllers;
|
||||
@@ -319,6 +334,7 @@ class Site
|
||||
'article_edit' => 'edit',
|
||||
'article_save' => 'save',
|
||||
'article_delete' => 'delete',
|
||||
'article_restore' => 'restore',
|
||||
'banner_edit' => 'edit',
|
||||
'banner_save' => 'save',
|
||||
'banner_delete' => 'delete',
|
||||
@@ -332,6 +348,9 @@ class Site
|
||||
'layout_edit' => 'edit',
|
||||
'layout_save' => 'save',
|
||||
'layout_delete' => 'delete',
|
||||
'container_edit' => 'edit',
|
||||
'container_save' => 'save',
|
||||
'container_delete' => 'delete',
|
||||
];
|
||||
|
||||
public static function route()
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
namespace admin\controls;
|
||||
|
||||
class ArticlesArchive
|
||||
{
|
||||
public static function article_restore()
|
||||
{
|
||||
if ( \admin\factory\ArticlesArchive::article_restore( \S::get( 'id' ) ) )
|
||||
\S::alert( 'Artykuł został przywrócony.' );
|
||||
header( 'Location: /admin/articles_archive/view_list/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function article_delete()
|
||||
{
|
||||
if ( \admin\factory\ArticlesArchive::article_delete( \S::get( 'id' ) ) )
|
||||
\S::alert( 'Artykuł został usunięty.' );
|
||||
header( 'Location: /admin/articles_archive/view_list/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function view_list()
|
||||
{
|
||||
return \admin\view\ArticlesArchive::articles_list();
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
namespace admin\factory;
|
||||
|
||||
class ArticlesArchive
|
||||
{
|
||||
public static function article_restore( $article_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> update( 'pp_articles', [ 'status' => 0 ], [ 'id' => (int)$article_id ] );
|
||||
}
|
||||
|
||||
public static function article_delete( $article_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$mdb -> delete( 'pp_articles_pages', [ 'article_id' => (int)$article_id ] );
|
||||
$mdb -> delete( 'pp_articles_langs', [ 'article_id' => (int)$article_id ] );
|
||||
$mdb -> delete( 'pp_articles_images', [ 'article_id' => (int)$article_id ] );
|
||||
$mdb -> delete( 'pp_articles_files', [ 'article_id' => (int)$article_id ] );
|
||||
$mdb -> delete( 'pp_articles', [ 'id' => (int)$article_id ] );
|
||||
|
||||
\S::delete_dir( '../upload/article_images/article_' . (int)$article_id . '/' );
|
||||
\S::delete_dir( '../upload/article_files/article_' . (int)$article_id . '/' );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
namespace admin\view;
|
||||
|
||||
class ArticlesArchive
|
||||
{
|
||||
public static function articles_list()
|
||||
{
|
||||
$tpl = new \Tpl;
|
||||
return $tpl -> render( 'articles/articles-archive-list' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user