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

441 lines
16 KiB
PHP

<?php
namespace Admin;
use KupShop\AdminBundle\AdminBlocksTrait;
use KupShop\AdminBundle\Util\AdminSectionTree;
use KupShop\AdminBundle\Util\BlocksHistory;
use KupShop\ContentBundle\Util\BlocksTrait;
use KupShop\I18nBundle\Translations\ArticlesTranslation;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\StringUtil;
use Query\Operator;
class Articles extends \Window
{
use BlocksTrait;
use AdminBlocksTrait;
protected $tableName = 'articles';
protected $template = 'window/articles.tpl';
protected $nameField = 'title';
protected $show_on_web = 'article';
/** @var BlocksHistory */
private $blocksHistory;
private AdminSectionTree $adminSectionTree;
public function __construct()
{
$this->blocksHistory = ServiceContainer::getService(BlocksHistory::class);
$this->adminSectionTree = ServiceContainer::getService(AdminSectionTree::class);
}
public function articlesSections($topCat, $Pos = 0, $ignored = null)
{
$SQL = sqlQueryBuilder()
->select('ab.id, ab.name, ab.descr, ab.figure')
->from('articles_branches', 'ab')
->where(\Query\Operator::equalsNullable(['ab.top_branch' => $topCat]))
->orderBy('ab.name', 'ASC')
->execute();
$data = [];
foreach ($SQL as $key => $row) {
if ($ignored == null || $ignored != $row['id']) {
$data[$key]['id'] = $row['id'];
$data[$key]['title'] = $row['name'];
$data[$key]['figure'] = $row['figure'];
$data[$key]['level'] = $Pos;
$data[$key]['submenu'] = $this->articlesSections($row['id'], $Pos + 1, $ignored = null);
}
}
return $data;
}
public function createObject()
{
$data = parent::createObject();
return $data;
}
public function get_vars()
{
$vars = parent::get_vars();
$pageVars = getVal('body', $vars);
$ID = $this->getID();
$acn = $this->getAction();
if (!empty($ID)) {
$pageVars['tree'] = $this->articlesSections(null);
$pageVars['selected'] = $this->adminSectionTree->getSelected($this->getID(), 'articles_relation', 'id_art', 'id_branch');
$this->adminSectionTree->getOpened($pageVars['tree']);
$pageVars['opened'] = $this->adminSectionTree->opened;
$pageVars['articleURL'] = path('kupshop_content_articles_article_1', [
'IDa' => $ID,
'slug' => StringUtil::slugify($pageVars['data']['title']),
]);
$SQL = sqlQuery('SELECT id_auth
FROM '.getTableName('articles_authors_relation')."
WHERE id_art='".$ID."' ");
foreach ($SQL as $row) {
$pageVars['dbAuth'][$row['id_auth']] = 1;
}
$SQL = sqlQuery('SELECT id_branch
FROM '.getTableName('articles_branches_admins')."
WHERE id_admin='".$GLOBALS['adminID']."' ");
$pageVars['permittedBranches'] = [];
foreach ($SQL as $row) {
$pageVars['permittedBranches'][$row['id_branch']] = 'ENABLED';
}
// -----------------------------------------------------------------
// zjistit povolene autory
$SQL = sqlQuery('SELECT id_auth
FROM '.getTableName('articles_authors_admins')."
WHERE id_admin='".$GLOBALS['adminID']."' ");
$pageVars['permittedAuthors'] = [];
foreach ($SQL as $row) {
$pageVars['permittedAuthors'][$row['id_auth']] = 'ENABLED';
}
if ($acn == 'edit') {
$strSections = '';
$strAuthors = '';
// kdyz jsou nejake povolene sekce
if (count($pageVars['permittedBranches']) > 0) {
foreach ($pageVars['permittedBranches'] as $key => $value) {
if ($strSections != '') {
$strSections .= ' OR ';
}
$strSections .= "ar.id_branch='".$key."'";
}
$no = returnSQLResult('SELECT COUNT(*)
FROM '.getTableName('articles').' AS a, '.getTableName('articles_relation')." AS ar
WHERE a.id='".$ID."' AND a.id=ar.id_art AND (".$strSections.') ');
if (intval($no) == 0) {
$acn = 'disabled';
// redirect("launch.php?s=error.php&id=1");
}
}
// kdyz jsou nejaci povoleni autori
if (count($pageVars['permittedAuthors']) > 0) {
foreach ($pageVars['permittedAuthors'] as $key => $value) {
if ($strAuthors != '') {
$strAuthors .= ' OR ';
}
$strAuthors .= "aar.id_auth='".$key."'";
}
$no = returnSQLResult('SELECT COUNT(*)
FROM '.getTableName('articles').' AS a, '.getTableName('articles_authors_relation')." AS aar
WHERE a.id='".$ID."' AND a.id=aar.id_art AND (".$strAuthors.') ');
if (intval($no) == 0) {
$acn = 'disabled';
// redirect("launch.php?s=error.php&id=1");
}
}
if ($pageVars['data']['id_block']) {
$pageVars['data']['blocks_history'] = $this->blocksHistory->getBlocksHistory($pageVars['data']['id_block']);
}
}
foreach ($pageVars['tree'] as $key => $row) {
if (count($pageVars['permittedBranches']) > 0 && !array_key_exists($row['id'], $pageVars['permittedBranches']) == true) {
$pageVars['disabled'][$key] = true;
}
}
$pageVars['data']['photos'] = sqlQueryBuilder()->select('id_photo')->from('photos_articles_relation')
->where(\Query\Operator::equals(['id_art' => $this->getID()]))
->orderBy('position')->execute()->fetchAll();
$tags = sqlQueryBuilder()
->select('at.id, at.tag')
->from('articles_tags_relation', 'atr')
->leftJoin('atr', 'articles_tags', 'at', 'at.id = atr.id_tag')
->where(\Query\Operator::equals(['atr.id_article' => $ID]))
->execute()->fetchAll();
$pageVars['data']['tags'] = array_combine(array_column($tags, 'id'), array_column($tags, 'tag'));
$authors = sqlQueryBuilder()
->select('id_auth as id')
->from('articles_authors_relation')
->where(Operator::equals(['id_art' => $ID]))
->execute()->fetchAll();
$pageVars['data']['authors'] = array_combine(array_column($authors, 'id'), array_column($authors, 'id'));
// related articles
$relatedQb = sqlQueryBuilder()
->select('ar.id_article_related as value, ar.position, a.title as label')
->from('articles_related', 'ar')
->join('ar', 'articles', 'a', 'a.id = ar.id_article_related')
->where(Operator::equals(['ar.id_article' => $ID]))
->orderBy('ar.position')
->groupBy('ar.id_article, ar.id_article_related');
if (findModule(\Modules::ARTICLES_RELATED_TYPES)) {
$relatedQb->addSelect('ar.type')
->addSelect('GROUP_CONCAT(ar.type) as types');
}
$pageVars['articles_related'] = [];
foreach ($relatedQb->execute() as $relatedKey => $relatedItem) {
$pageVars['articles_related'][$relatedKey] = $relatedItem;
if (!empty($relatedItem['types'])) {
$relatedItem['types'] = array_map('intval', explode(',', $relatedItem['types']));
}
}
$pageVars['related_additional_data'] = [
'articlesRelatedTypes' => $this->getArticlesRelatedTypes(),
];
}
$pageVars['acn'] = $acn;
$pageVars['articleTranslationsFigure'] = $this->getTranslationUtil()?->getTranslationsFigure(ArticlesTranslation::class, $this->getID());
$this->unserializeCustomData($pageVars['data']);
$vars['body'] = $pageVars;
return $vars;
}
public function getData()
{
$data = parent::getData();
$acn = $this->getAction();
if (getVal('Submit')) {
$data['date'] = $this->prepareDateTime($data['date']);
$data['date_created'] = $this->prepareDateTime($data['date_created']);
$this->serializeCustomData($data);
$data['url'] = trim($data['url'] ?? '', '/');
if (findModule('seo')) {
if (empty($data['meta_title_changed'])) {
$data['meta_title'] = '';
}
}
}
if ($acn == 'add') {
$data['figure'] = empty($data['figure']) ? 'Y' : $data['figure'];
$data['seen'] = '0';
$data['type'] = empty($data['type']) ? 'A' : $data['type'];
$data['rating_value'] = '0';
$data['rating_voted'] = '0';
$data['comments'] = 'Y';
$data['tags'] = [];
$data['article_body'] = [];
}
return $data;
}
public function getObject()
{
$data = parent::getObject();
$ID = $this->getID();
$data['show_in_lead'] = 'N';
$no = returnSQLResult('SELECT COUNT(*)
FROM photos_articles_relation
WHERE id_art=:id AND show_in_lead="Y"', ['id' => $ID]);
if ($no == 0) {
$data['show_in_lead'] = 'Y';
}
if ($data['id_block'] === null) {
$data['blocks'] = [];
} else {
$data['blocks'] = $this->getBlocks($data['id_block']);
}
return $data;
}
public function handleUpdate()
{
$data = $this->getData();
$SQL = parent::handleUpdate();
if ($SQL) {
$ID = $this->getID();
// SAVE BLOCKS
if (!empty($data)) {
if ($this->getAction() == 'edit') {
// historii je treba ulozit jeste pred ulozenim bloku
$this->blocksHistory->saveBlocksHistory(getVal('blocks', $data, []));
}
$this->saveBlocks($data, $this->getID(), 'articles');
$this->updateBlocksPhotosPositions($this->getID(), 'articles', 'id_art');
}
// ############################################
// # ZARAZENI DO SEKCI
if (findModule('articles_sections') || findModule('news')) {
sqlQuery('DELETE FROM '.getTableName('articles_relation')."
WHERE id_art='".$ID."' ");
if (isset($_POST['sec'])) {
$no = count($_POST['sec']);
if ($no > 0) {
foreach ($_POST['sec'] as $key => $value) {
if (!empty($key)) {
sqlQuery('INSERT INTO '.getTableName('articles_relation')."
SET id_art='".$ID."', id_branch='".$key."' ");
}
}
unset($key, $value);
}
}
}
// ############################################
// # ZAPSANI AUTORU
$this->deleteSQL('articles_authors_relation', ['id_art' => $ID]);
if (!empty($data['authors'])) {
foreach (array_unique($data['authors']) as $authorId) {
$this->insertSQL('articles_authors_relation', ['id_art' => $ID, 'id_auth' => $authorId]);
}
}
// ############################################
// Save related articles
$this->deleteSQL('articles_related', ['id_article' => $ID]);
foreach ($data['articles_related'] ?? [] as $related) {
if (!empty($related['delete'])) {
continue;
}
if (findModule(\Modules::ARTICLES_RELATED_TYPES)) {
$types = $related['type'] ?? [];
if (empty($types)) {
$types = [$this->getArticlesRelatedTypes()[0]['id']];
}
foreach ($types as $type) {
sqlQueryBuilder()
->insert('articles_related')
->directValues(
[
'id_article' => $ID,
'id_article_related' => $related['id_article'],
'position' => $related['position'],
'type' => $type,
]
)->execute();
}
} else {
sqlQueryBuilder()
->insert('articles_related')
->directValues(
[
'id_article' => $ID,
'id_article_related' => $related['id_article'],
'position' => $related['position'],
]
)->execute();
}
}
// Save tags
$this->deleteSQL('articles_tags_relation', ['id_article' => $ID]);
if (!empty($data['tags'])) {
foreach (array_unique($data['tags']) as $id_tag) {
$this->insertSQL('articles_tags_relation', ['id_article' => $ID, 'id_tag' => $id_tag]);
}
}
// Save translations figure
$this->getTranslationUtil()?->updateTranslationsFigure(ArticlesTranslation::class, $this->getID(), $data['translation_figure'] ?? []);
if (!empty($data['products'])) {
foreach ($data['products'] as $id => $item) {
$item['id'] = intval($id);
if (!empty($item['delete']) || !$id) {
if ($id > 0) {
$this->deleteSQL('products_in_articles', ['id_product' => $item['id_product'], 'id_article' => $ID]);
}
continue;
}
try {
if ($id < 0) {
$this->insertSQL('products_in_articles', ['id_product' => $item['id_product'], 'id_article' => $ID]);
}
} catch (Doctrine\DBAL\DBALException $e) {
if (intval($e->getPrevious()->errorInfo[1]) == '1062') {
$this->returnError('Produkt je již k tomuto článku přidán!');
}
}
}
$this->returnOK();
}
}
return $SQL;
}
public function handleDelete()
{
$IDa = $this->getID();
$object = $this->getObject();
writeDownActivity('smazán článek: '.
returnSQLResult('SELECT title
FROM '.getTableName('articles')."
WHERE id='".$IDa."' "));
sqlQuery('DELETE FROM '.getTableName('photos_articles')."
WHERE id_art='".$IDa."' ");
sqlQuery('DELETE FROM '.getTableName('articles_relation')."
WHERE id_art='".$IDa."' ");
sqlQuery('DELETE FROM '.getTableName('articles_authors_relation')."
WHERE id_art='".$IDa."' ");
sqlQuery('DELETE FROM '.getTableName('articles')."
WHERE id='".$IDa."' ");
$this->removeBlocks($object['id_block']);
redirect('launch.php?s=articles.php&acn=erased&refresh=parent&ErrStr='.urlencode('Článek byl odstraněn.'));
}
private function getArticlesRelatedTypes(): array
{
if (!findModule(\Modules::ARTICLES_RELATED_TYPES)) {
return [];
}
return sqlQueryBuilder()
->select('id, name')
->from('articles_related_types')
->orderBy('id', 'ASC')
->execute()->fetchAllAssociative();
}
}
return Articles::class;