Files
kupshop/class/smarty_plugins/function.insert_subsections.php
2025-08-02 16:30:27 +02:00

235 lines
8.0 KiB
PHP

<?php
use KupShop\CatalogBundle\Section\SectionTree;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
function getSectionsBlock($params)
{
/** @var \KupShop\CatalogBundle\Util\ActiveCategory $activeCategory */
$activeCategory = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\CatalogBundle\Util\ActiveCategory::class);
$sections = [];
$catProducer = intval(getVal('IDpd'));
if (isset($params['producer'])) {
$catProducer = intval($params['producer']);
}
if (!$catProducer) {
$catProducer = $activeCategory->getProducerId();
}
$cat = $params['category'];
$categoryTree = ServiceContainer::getService(SectionTree::class);
if ($catProducer > 0) {
$dbcfg = Settings::getDefault();
/** @var \Query\QueryBuilder $query */
$query = sqlQueryBuilder()
->select('s.id')
->from('sections', 's')
->leftJoin('s', 'products_in_sections', 'ps', 's.id=ps.id_section')
->leftJoin('s', 'products', 'p', 'ps.id_product=p.id AND p.figure="Y" ')
->where('s.figure="Y"')
->andWhere('p.producer=:producer')
->setParameter('producer', $catProducer)
->groupBy('s.id');
if ($dbcfg->prod_show_not_in_store == 'N') {
$query->andWhere(\Query\Filter::byInStore(Filter::IN_STORE_SUPPLIER));
}
$result = $query->execute();
$selected = [];
foreach ($result as $row) {
$selected[$row['id']] = true;
}
if (!isset($cat['id'])) {
$categories = $activeCategory->getCategory();
} else {
$categories = $categoryTree->getSectionById($cat['id']);
}
if (!empty($categories)) {
$end = false;
do {
$sectionsFallback = $sections;
$sections = recurseSelectCategories($categories, $selected);
// avoid infinite
if (empty($sections)) {
$end = true;
$sections = $sectionsFallback;
}
// if there is only one section
$categories = $categoryTree->getSectionById(reset($sections)['id']);
} while (count($sections) < 2 && $end == false);
}
} elseif (isset($cat['id']) && $cat['id'] >= 0) {
$root = $params['root'] ?? false;
$topsection = ($root && $cat['id'] == 0 ? null : $cat['id']);
if ($topsection == 0 && !$root) {
return [];
}
$SQL = sqlQueryBuilder()
->select('s.*')
->from('sections', 's')
->leftJoin('s', 'sections_relation', 'sr', 's.id=sr.id_section')
->where(
\Query\Operator::equalsNullable(['s.figure' => 'Y', 'sr.id_topsection' => $topsection])
)
->andWhere('s.id != 0')
->andWhere(
\Query\Translation::coalesceTranslatedFields(
\KupShop\I18nBundle\Translations\SectionsTranslation::class
)
)
->orderBy('sr.position, s.name', 'ASC')
->execute();
foreach ($SQL as $row) {
$category = $categoryTree->getSectionById($row['id']);
// Skip subcategory if not visible
if ($category && !$category->isVisible()) {
continue;
}
$row['title'] = $row['name'];
$row['title_short'] = $row['name_short'] ? $row['name_short'] : $row['name'];
if ($row['photo']) {
$row['photo'] = getImage($row['id'], $row['photo'], '../section/', $params['image'], $row['name'], strtotime($row['date_updated']));
}
if (!empty($cat['id']['campaign'])) {
$row['id'] .= $cat['id']['campaign'];
}
$row['data'] = ($row['data'] === null) ? [] : json_decode($row['data'], true);
if ($category == null) {
$logger = ServiceContainer::getService('logger');
$logger->notice('[Proxy Cache] Category is null', ['row' => $row, 'category' => $category]);
}
$row['count'] = $category?->getCount() ?: 0;
$row['in_stock_count'] = $category?->getInStockCount() ?: 0;
$row['data'] = $category?->getData() ?: [];
$sections[] = $row;
}
if (!empty($params['IDpv'])) {
$qb = sqlQueryBuilder()->select('s.id')
->from('sections', 's')
->join('s', 'products_in_sections', 'ps', 's.id = ps.id_section')
->join('ps', 'products', 'p', 'p.id = ps.id_product')
->join('p', 'parameters_products', 'pap', 'p.id=pap.id_product AND pap.value_list=:value_list')
->setParameter('value_list', $params['IDpv']);
$qb->andWhere((new FilterParams())->getSpec());
$sectionIds = sqlFetchAll($qb, 'id');
foreach ($sections as $key => $section) {
if (!in_array($section['id'], array_keys($sectionIds))) {
unset($sections[$key]);
}
}
}
if (!empty($params['campaign'])) {
$qb = sqlQueryBuilder()->select('s.id')
->from('sections', 's')
->join('s', 'products_in_sections', 'ps', 's.id = ps.id_section')
->join('ps', 'products', 'p', 'p.id = ps.id_product AND FIND_IN_SET(:campaign, p.campaign)')
->setParameter('campaign', $params['campaign']);
$qb->andWhere((new FilterParams())->getSpec());
$sectionIds = sqlFetchAll($qb, 'id');
foreach ($sections as $key => $section) {
if (!in_array($section['id'], array_keys($sectionIds))) {
unset($sections[$key]);
}
}
}
unset($divide, $tblWidth);
}
return $sections;
}
function smarty_function_insert_subsections($params, &$smarty)
{
/** @var \KupShop\CatalogBundle\Util\ActiveCategory $activeCategory */
$activeCategory = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\CatalogBundle\Util\ActiveCategory::class);
$tree = $activeCategory->getOpenedTreePath();
$default = [
'template' => 'block.subsections.tpl',
'category' => null,
'up' => true,
'selection' => $tree,
];
$params = array_merge($default, $params);
if (empty($params['category'])) {
throw new InvalidArgumentException('Missing \'category\' parameter - array describing category');
}
if (empty($params['image'])) {
$params['image'] = 6;
}
if (empty($params['count'])) {
$params['count'] = 9999;
}
$params['sections'] = getSectionsBlock($params);
if (!empty($params['template'])) {
$_smarty_tpl_vars = $smarty->tpl_vars;
echo $smarty->_subTemplateRender($params['template'], $smarty->cache_id, $smarty->compile_id, 0, null, $params, 0, false);
$smarty->tpl_vars = $_smarty_tpl_vars;
}
if (!empty($params['assign'])) {
$smarty->assign($params['assign'], $params);
}
}
function recurseSelectCategories($categories, $selected)
{
$result = [];
if (isset($categories['submenu'])) {
$categories = $categories['submenu'];
}
foreach ($categories as $category) {
$children = [];
if (isset($selected[$category['id']]) || $children = recurseSelectCategories($category['submenu'], $selected)) {
if (!empty($category['submenu']) && empty($children)) {
$children = recurseSelectCategories($category['submenu'], $selected);
}
$result[] = [
'id' => $category['id'],
'title' => $category['title'],
'title_short' => $category['title_short'] ?: $category['title'],
'photo' => $category['photo'] ?? null,
'parents' => $category['parents'],
'children' => $children,
];
}
}
return $result;
}