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

156 lines
5.1 KiB
PHP

<?php
use KupShop\CatalogBundle\Util\ActiveCategory;
use KupShop\I18nBundle\Translations\ProducersTranslation;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\RestrictionsBundle\Utils\Restrictions;
use Query\Operator;
function smarty_function_insert_producers_get_data(&$params)
{
$dbcfg = Settings::getDefault();
$qb = sqlQueryBuilder()
->select('pr.id, pr.name, pr.photo, pr.data, pr.date_updated')
->andWhere(\Query\Translation::coalesceTranslatedFields(
ProducersTranslation::class,
['name' => 'name', 'web' => 'web', 'data' => 'data_translated']
))
->from('products', 'p')
->leftJoin('p', 'producers', 'pr', 'p.producer = pr.id')
->andWhere(Operator::equals(['pr.active' => 'Y', 'p.figure' => 'Y']))
->andWhere('p.id IS NOT NULL')
->groupBy('pr.id');
if ($params['count']) {
$qb->setMaxResults($params['count']);
}
if ($params['top'] ?? false) {
$qb->andWhere(Operator::equals(['pr.top' => 'Y']));
}
if ($dbcfg->prod_show_not_in_store == 'N') {
$qb->andWhere('p.in_store > 0');
}
if ($params['totals']) {
if ($params['totals'] == 'in_store') {
$qb->addSelect('SUM(IF(p.in_store > 0, 1, 0)) as total');
} else {
$qb->addSelect('COUNT(p.id) as total');
}
}
if (findModule('elnino')) {
$select = join(', ', $qb->getQueryPart('select'));
$inStoreSpec = findModule('products', 'in_store_spec');
if ($inStoreSpec && is_callable($inStoreSpec)) {
$select = str_replace('p.in_store', call_user_func($inStoreSpec, false, $qb), $select);
if ($dbcfg->prod_show_not_in_store == 'N') {
$qb->andWhere(call_user_func($inStoreSpec, false, $qb).' > 0');
}
}
$qb->select($select)
->leftJoin('p', 'products_in_sections', 'ps', 'p.id=ps.id_product')
->join('ps', 'sections', 's', 's.id = ps.id_section');
}
if (findModule(\Modules::RESTRICTIONS)) {
$qb->andWhere(ServiceContainer::getService(Restrictions::class)->getRestrictionSpec());
}
$qb->orderBy('COALESCE(pr.position, 9999), pr.name', 'ASC');
if (!empty($params['best_sellers'])) {
$qb->orderBy('SUM(p.pieces_sold)', 'DESC');
}
if (!empty($params['category'])) {
$categories = getDescendantCategories($params['category']);
$qb->leftJoin('p', 'products_in_sections', 'ps', 'p.id=ps.id_product')
->andWhere(Operator::inIntArray($categories, 'ps.id_section'));
}
$SQL = $qb->execute();
$producers = [];
foreach ($SQL as $producer) {
if (!empty($producer['photo'])) {
$producer['image'] = getImage($producer['id'], $producer['photo'], '../producer/', 7, $producer['name'], strtotime($producer['date_updated']));
}
$producer['data'] = array_replace_recursive(json_decode($producer['data'], true) ?: [], json_decode($producer['data_translated'], true) ?: []);
$producers[$producer['id']] = $producer;
}
// Fetch producer photos
if (isset($params['fetchPhotos'])) {
$photoSize = $params['fetchPhotos'];
$producerIds = array_keys($producers);
$photos = sqlQueryBuilder()->select('ppr.id_producer, p.id, p.descr, p.source, p.image_2')
->from('photos', 'p')
->join('p', 'photos_producers_relation', 'ppr', 'ppr.id_photo = p.id')
->andWhere(Operator::inIntArray($producerIds, 'ppr.id_producer'))
->orderBy('ppr.position')
->execute();
foreach ($photos as $photo) {
$producers[$photo['id_producer']]['photos'][] = getImage($photo['id'], $photo['image_2'], $photo['source'], $photoSize, $photo['descr']);
}
}
$params['producers'] = $producers;
}
function smarty_function_insert_producers($params, &$smarty)
{
/** @var ActiveCategory $activeCategory */
$activeCategory = ServiceContainer::getService(ActiveCategory::class);
if (getVal('IDproduct')) {
$selection = $activeCategory->loadProducerId();
} else {
$selection = $activeCategory->getProducerId();
}
$default = [
'template' => 'block.producers.tpl',
'count' => null,
'totals' => null,
'key' => 'producers-html',
'get_data_function' => 'smarty_function_insert_producers_get_data',
'selection' => [intval($selection)],
'subsections' => $activeCategory->getOpenedTreePath(),
];
$params = array_merge($default, $params);
$key = [
$params['key'],
$params['template'],
ServiceContainer::getService(LanguageContext::class)->getActiveId(),
];
$params['key'] = join('-', $key);
$params['file'] = $params['template'];
if (!empty($params['assign'])) {
smarty_function_insert_producers_get_data($params);
$smarty->assign($params['assign'], $params);
return;
}
$smarty->loadPlugin('Smarty_function_include_cached');
return smarty_function_include_cached($params, $smarty);
}
/* vim: set expandtab: */