Your IP : 3.17.162.17
<?php
namespace Epages\Modules\Google;
use Bitrix\Main\Loader;
use Epages\Modules\Google\XmlNode;
use Epages\Modules\Google\Interfaces\FeedProfileInterface;
class GoogleProductXml
{
private $mandatoryTags = [
'title',
'link',
'price',
];
private $profileSettings;
public $productData;
public $formattedData;
public function __construct($productData, FeedProfileInterface $profileSettings)
{
$this->productData = $productData;
$this->profileSettings = $profileSettings;
}
public function addProduct()
{
$xmlNode = new XmlNode();
$product = [];
$this->formatData($this->productData);
if (!empty($this->formattedData)) {
foreach ($this->formattedData as $tag => $value) {
if (!empty($tag)) {
if (is_array($value)) {
foreach ($value as $valKey => $val) {
$product[$tag . '_' . $valKey] = $xmlNode->getNode($tag)->setValue($val);
}
unset($valKey, $val);
} else {
$product[$tag] = $xmlNode->getNode($tag)->setValue($value);
}
}
}
unset($tag, $value);
}
if (!empty($product['item_group_id'])) {
Loader::includeModule('iblock');
$elementGroups = \CIBlockSection::GetNavChain(
$this->profileSettings->getCatalogIblock(),
strip_tags($product['item_group_id']),
['ID', 'IBLOCK_ID', 'NAME', 'DEPTH_LEVEL']
);
$countGroups = count($elementGroups) - 1;
$productType = '';
while ($elementGroup = $elementGroups->GetNext()) {
$productType .= ($elementGroup['DEPTH_LEVEL'] > 1 ? ' > ' : '').$elementGroup['NAME'];
}
unset($key, $elementGroup);
if (empty($productType)) {
$elementGroupsQuery = \CIBlockSection::GetList(
[],
[
'IBLOCK_ID' => $this->profileSettings->getCatalogIblock(),
'ACTIVE' => 'Y',
'CHECK_PERMISSIONS' => 'N',
'ID' => strip_tags($product['item_group_id']),
],
false,
['ID', 'IBLOCK_ID', 'NAME']
);
if ($elementGroup = $elementGroupsQuery->GetNext()) {
$productType = $elementGroup['NAME'];
}
unset($key, $elementGroup);
}
if (!empty($productType)) {
$product['product_type'] = $xmlNode->getNode('product_type')->setValue($productType);
}
}
$result = '';
if ($this->isValidProduct($product)) {
foreach ($product as $tag) {
$result .= $tag;
}
}
return $result;
}
public function isValidProduct($product)
{
$valid = true;
foreach ($this->mandatoryTags as $tag) {
if (!array_key_exists($tag, $product) || empty($product[$tag])) {
$valid = false;
}
}
if ($valid === false && $this->profileSettings->getDebug()) {
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/local/lib/Lapa/Modules/Google/log/not_valid_products.log');
}
return $valid;
}
public function formatData()
{
$result = $this->productData;
foreach ($this->productData as $tag => $value) {
if (empty($value)) {
unset($result[$tag]);
}
}
$this->formattedData = $result;
}
}