Files
kupshop/bundles/KupShop/FeedsBundle/Admin/feedsProduct.php
2025-08-02 16:30:27 +02:00

125 lines
4.5 KiB
PHP

<?php
use KupShop\AdminBundle\Util\AdminSectionTree;
use KupShop\FeedsBundle\Utils\FeedUtils;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
$main_class = 'FeedsProduct';
class FeedsProduct extends Window
{
private AdminSectionTree $adminSectionTree;
public function __construct()
{
$this->adminSectionTree = ServiceContainer::getService(AdminSectionTree::class);
}
public function get_vars()
{
$vars = parent::get_vars();
$vars['categories'] = $this->adminSectionTree->getCategories();
return $vars;
}
public function handleUpdate()
{
$data = $this->getData();
foreach ($data['feed'] as $feedId => $feed) {
if (!empty($feed['id_feed_products'])) {
$this->deleteSQL('feed_products', ['id' => $feed['id_feed_products']]);
}
if (!empty($feed['title'])
|| !empty($feed['section'])
|| !empty($feed['cpc'])
|| !empty($feed['active'])
) {
$this->insertSQL(
'feed_products',
[
'id_feed' => $feedId,
'id_product' => $data['ID'],
'title' => (empty($feed['title'])) ? null : $feed['title'],
'id_section' => (empty($feed['section'])) ? null : $feed['section'],
'active' => ($feed['active'] == '') ? null : $feed['active'],
'cpc' => (empty($feed['cpc'])) ? null : $feed['cpc'],
]
);
}
}
return true;
}
protected function getObject()
{
$feeds = sqlQueryBuilder()
->select('id, id_language, name, type, section_mapping, data')
->from('feeds')
->execute();
$feedUtils = ServiceContainer::getService(FeedUtils::class);
$languageContext = ServiceContainer::getService(LanguageContext::class);
$data['id'] = $this->getID();
$data['feed_data'] = [];
$typeAndRelatedSectionID = [];
foreach ($feeds as $feed) {
$decodedData = json_decode($feed['data'] ?? '', true);
$sectionType = !empty($feed['section_mapping']) ? $feed['section_mapping'] : $feed['type'];
if ($sectionType === 'heureka' && ($feed['id_language'] === 'SK' || $languageContext->getDefaultId() === 'SK')) {
$sectionType .= '_sk';
}
$sectionType = $feedUtils->filterThirdPartySectionType($sectionType);
$data['feed_data'][$feed['id']] = [
'type' => $feed['type'],
'sectionType' => $sectionType,
'data' => $decodedData,
'product' => $this->getFeedData($feed),
];
if (!empty($data['feed_data'][$feed['id']]['product']['id_section'])) {
$typeAndRelatedSectionID[$sectionType][$data['feed_data'][$feed['id']]['product']['id_section']] = $data['feed_data'][$feed['id']]['product']['id_section'];
}
}
foreach ($typeAndRelatedSectionID as $type => $ids) {
$tmpType = $type === '' ? null : $type;
$sectionData = $feedUtils->getSectionNames($ids, $tmpType);
foreach ($sectionData as $SectionID => $Section) {
foreach ($data['feed_data'] as $feedID => $feedData) {
if ($feedData['sectionType'] !== $tmpType || (string) $feedData['product']['id_section'] !== (string) $SectionID) {
continue;
}
$data['feed_data'][$feedID]['product']['section'] = $Section;
$data['feed_data'][$feedID]['product']['section_text'] = $Section['full_name'] ?? $SectionID;
}
}
}
return $data;
}
private function getFeedData($feed)
{
$data = sqlQueryBuilder()
->select('id, id_product, title, id_section, active, cpc')
->from('feed_products')
->where(\Query\Operator::equals(['id_product' => $this->getID(), 'id_feed' => $feed['id']]))
->execute()->fetch();
return [
'feed_id' => $feed['id'],
'feed_name' => $feed['name'],
'feed_products_id' => $data['id'],
'title' => $data['title'],
'id_section' => $data['id_section'],
'active' => ($data['active'] === null) ? -1 : $data['active'],
'cpc' => $data['cpc'],
];
}
}