194 lines
6.7 KiB
PHP
194 lines
6.7 KiB
PHP
<?php
|
|
namespace admin\Controllers;
|
|
|
|
use Domain\Article\ArticleRepository;
|
|
|
|
class ArticlesController
|
|
{
|
|
private ArticleRepository $repository;
|
|
|
|
public function __construct(ArticleRepository $repository)
|
|
{
|
|
$this->repository = $repository;
|
|
}
|
|
|
|
/**
|
|
* Lista artykulow
|
|
*/
|
|
public function list(): string
|
|
{
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* Zapis artykulu (AJAX)
|
|
*/
|
|
public function save(): void
|
|
{
|
|
global $user;
|
|
|
|
$values = json_decode(\S::get('values'), true);
|
|
$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' => 'Artykul zostal zapisany.', 'id' => $id];
|
|
}
|
|
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Archiwizacja artykulu (ustawia status = -1)
|
|
*/
|
|
public function delete(): void
|
|
{
|
|
if ($this->repository->archive((int)\S::get('id'))) {
|
|
\S::alert('Artykul zostal przeniesiony do archiwum.');
|
|
}
|
|
|
|
header('Location: /admin/articles/view_list/');
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Edycja artykulu
|
|
*/
|
|
public function edit(): string
|
|
{
|
|
global $user;
|
|
|
|
if (!$user) {
|
|
header('Location: /admin/');
|
|
exit;
|
|
}
|
|
|
|
$this->repository->deleteNonassignedImages();
|
|
$this->repository->deleteNonassignedFiles();
|
|
|
|
return \Tpl::view('articles/article-edit', [
|
|
'article' => $this->repository->find((int)\S::get('id')),
|
|
'menus' => \admin\factory\Pages::menus_list(),
|
|
'languages' => \admin\factory\Languages::languages_list(),
|
|
'layouts' => \admin\factory\Layouts::layouts_list(),
|
|
'user' => $user
|
|
]);
|
|
}
|
|
}
|