Files
kupshop/admin/lists/ArticlesList.php
2025-08-02 16:30:27 +02:00

190 lines
7.6 KiB
PHP

<?php
namespace Admin\Lists;
use KupShop\AdminBundle\AdminList\BaseList;
use KupShop\AdminBundle\AdminList\FiltersStorage;
use KupShop\AdminBundle\Query\Invert;
use KupShop\I18nBundle\Admin\Util\ListTranslationsFigureBadges;
use KupShop\I18nBundle\Translations\ArticlesTranslation;
use KupShop\KupShopBundle\Util\HtmlBuilder\HTML;
use KupShop\KupShopBundle\Util\StringUtil;
use Query\Operator;
class ArticlesList extends BaseList
{
use FiltersStorage;
use ListTranslationsFigureBadges;
protected $showMassEdit = true;
protected $tableName = 'articles';
protected ?string $tableAlias = 'a';
protected $orderParam = [
'sort' => 'dateAdded',
'direction' => 'DESC',
];
protected $tableDef = [
'id' => 'a.id',
'fields' => [
'title' => ['translate' => true, 'field' => 'a.title', 'size' => 2.5, 'fieldType' => BaseList::TYPE_STRING],
/* 'Číslo' => ['field' => 'a.id', 'size' => 0.5], */
'section' => ['translate' => true, 'field' => 'ab.name', 'size' => 0.5],
'showArticle' => ['translate' => true, 'field' => 'a.figure', 'render' => 'renderBoolean', 'size' => 0.5, 'spec' => 'a.figure', 'fieldType' => BaseList::TYPE_BOOL],
'type' => ['translate' => true, 'field' => 'a.type', 'render' => 'renderArticleType', 'size' => 0.5, 'spec' => 'a.type', 'fieldType' => ArticlesList::TYPE_LIST],
'seen' => ['translate' => true, 'field' => 'a.seen', 'render' => 'renderSeen', 'size' => 0.5],
'dateAdded' => ['translate' => true, 'field' => 'datef', 'render' => 'renderDateTime', 'raw_field' => 'a.date', 'size' => 1.5],
'date_created' => ['translate' => true, 'field' => 'a.date_created', 'render' => 'renderDateTime', 'raw_field' => 'a.date_created', 'size' => 1.5, 'visible' => 'N'],
'contentEditing' => ['translate' => true, 'render' => 'renderBlocekBtn', 'class' => 'hidden-label alignRight'],
'leadIn' => ['translate' => true, 'field' => 'a.lead_in', 'visible' => 'N', 'spec' => 'a.lead_in', 'fieldType' => BaseList::TYPE_STRING],
'link' => ['translate' => true, 'field' => 'a.link', 'visible' => 'N', 'spec' => 'a.link', 'fieldType' => BaseList::TYPE_STRING],
'comments' => ['translate' => true, 'field' => 'a.comments', 'visible' => 'N', 'spec' => 'a.comments', 'fieldType' => BaseList::TYPE_BOOL],
'url' => ['translate' => true, 'field' => 'a.url', 'visible' => 'N', 'spec' => 'a.url', 'fieldType' => BaseList::TYPE_STRING],
'metaTitle' => ['translate' => true, 'field' => 'a.meta_title', 'visible' => 'N', 'spec' => 'a.meta_title', 'fieldType' => BaseList::TYPE_STRING],
'metaDescription' => ['translate' => true, 'field' => 'a.meta_description', 'visible' => 'N', 'spec' => 'a.meta_description', 'fieldType' => BaseList::TYPE_STRING],
],
];
public function customizeMassTableDef($tableDef)
{
$tableDef = parent::customizeMassTableDef($tableDef);
$tableDef['fields']['type']['fieldOptions'] = [
'A' => translate('article', 'articles'),
'L' => translate('link', 'articles'),
];
return $tableDef;
}
public function customizeTableDef($tableDef): array
{
$tableDef = parent::customizeTableDef($tableDef);
if (findModule(\Modules::TRANSLATIONS)) {
$tableDef['fields']['translationsFigure'] = $this->getTranslationsFigureField(
ArticlesTranslation::class,
column: ['translation_section' => 'translations'],
);
}
return $tableDef;
}
public function renderArticleType($values, $column)
{
$value = $this->getListRowValue($values, $column['field']);
if ($value == 'A') {
return 'článek';
} else {
return 'odkaz';
}
}
public function renderSeen($values, $column)
{
$value = $this->getListRowValue($values, $column['field']);
return "{$value}x";
}
public function renderBlocekBtn($values, $clumn)
{
$url = path('kupshop_content_articles_article_1', [
'IDa' => $values['id'],
'slug' => StringUtil::slugify($values['title']),
]);
return HTML::create('a')->class('btn btn-primary btn-xs')
->attr('href', $url.'?inlineEditable=1')
->attr('target', '_blank')
->tag('span')->class('bi bi-pencil-square')->end()->end();
}
public function getQuery()
{
/* TODO pokud je článek ve 2 sekcích, vypíše se jen první */
$qb = sqlQueryBuilder();
$qb->select('a.id, a.title, a.seen, ab.name, a.date as datef, a.date_created')
->from('articles', 'a')
->leftJoin('a', 'articles_relation', 'ar', 'ar.id_art = a.id')
->leftJoin('ar', 'articles_branches', 'ab', 'ab.id = ar.id_branch')
->groupBy('a.id');
extract($_GET, EXTR_SKIP | EXTR_REFS);
// ###########
if (!empty($figure) && ($figure == 'Y' || $figure == 'N')) {
$qb->andWhere(Operator::equals(['a.figure' => $figure]));
}
// ###########
if (!empty($_GET['IDsec'])) {
$qb->andWhere('a.id = ar.id_art')
->andWhere(Operator::equals(['ar.id_branch' => $_GET['IDsec']]));
}
// ###########
if (!empty($_GET['IDa'])) {
$qb->andWhere(Operator::equals(['a.id' => $_GET['IDa']]));
}
if (!empty($_GET['title'])) {
$qb->andWhere(Operator::like(['a.title' => '%'.$_GET['title'].'%']));
}
if (!empty($_GET['figure'])) {
$qb->andWhere(
Invert::checkInvert(
Operator::inStringArray((array) $_GET['figure'], 'a.figure'),
isset($_GET['figure_invert'])
)
);
}
// ###########
if (!empty($_GET['IDauth'])) {
$qb->from('articles_authors_relation', 'aar')
->andWhere('aar.id_art = a.id')
->andWhere(Operator::equals(['aar.id_auth' => $_GET['IDauth']]));
}
// ###########
if (!empty($dateFrom) || !empty($dateTo)) {
if (!empty($dateFrom) && !isset($dateFromNotDecide)) {
$qb->andWhere('a.date >= :dateFrom')
->setParameter('dateFrom', $this->prepareDate($dateFrom).' 00:00:00');
}
if (!empty($dateTo) && !isset($dateToNotDecide)) {
$qb->andWhere('a.date <= :dateTo')
->setParameter('dateTo', $this->prepareDate($dateTo).' 23:59:59');
}
}
// ###########
if (!empty(getVal('tags'))) {
$qb->leftJoin('a', 'articles_tags_relation', 'atr', 'atr.id_article = a.id')
->leftJoin('atr', 'articles_tags', 'at', 'at.id = atr.id_tag')
->andWhere(Operator::inIntArray(getVal('tags'), 'at.id'));
}
// zobrazeni zbozi s novymu komentari
if (isset($comments) && $comments == 'new' && isset($adminID) && $adminID > 0) {
$dateLogin = returnSQLResult('SELECT date_access
FROM '.getTableName('admins_accesses').'
WHERE id_admin='.intval($adminID)." AND login_status='OK'
ORDER BY id DESC
LIMIT 1, 1");
if (empty($dateLogin)) {
$dateLogin = date('Y-m-d').' 00:00:00';
}
$qb->from('articles as a, comments as c')
->andWhere('c.type="article" AND c.id_item = a.id AND c.date >= :dateLogin')
->setParameter('dateLogin', $dateLogin);
}
// ###########
return $qb;
}
}
return ArticlesList::class;