1
0
mirror of synced 2024-11-30 01:06:06 +03:00
bitrix-module/intaro.intarocrm/classes/general/ICMLLoader.php

336 lines
13 KiB
PHP
Raw Normal View History

<?php
2013-09-22 20:03:35 +04:00
global $MESS;
IncludeModuleLangFile(__FILE__);
class ICMLLoader {
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
public $iblocks;
public $filename;
public $articleProperties;
2013-09-12 17:31:32 +04:00
public $application;
public $encoding = 'utf-8';
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected $fp;
2013-08-20 12:53:32 +04:00
protected $mainSection = 1000000;
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
public function Load()
{
2013-08-19 15:14:36 +04:00
global $USER;
if(!isset($USER))
$USER = new CUser;
2013-09-22 20:03:35 +04:00
2013-08-20 13:42:13 +04:00
if (count($this->iblocks) < count($this->articleProperties))
2013-08-20 12:53:32 +04:00
return false;
2013-09-22 20:03:35 +04:00
2013-08-19 15:14:36 +04:00
$this->PrepareFile();
$this->PreWriteCatalog();
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
$categories = $this->GetCategories();
2013-09-22 20:03:35 +04:00
2013-08-19 15:14:36 +04:00
$this->WriteCategories($categories);
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
$this->PreWriteOffers();
$this->BuildOffers($categories);
$this->PostWriteOffers();
2013-08-19 15:14:36 +04:00
$this->PostWriteCatalog();
$this->CloseFile();
2013-08-20 12:53:32 +04:00
return true;
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function PrepareValue($text)
{
2013-08-20 13:06:31 +04:00
$newText = $this->application->ConvertCharset($text, LANG_CHARSET, $this->encoding);
$newText = strip_tags($newText);
$newText = str_replace("&", "&#x26;", $newText);
return $newText;
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function PrepareFile()
{
2013-08-19 15:14:36 +04:00
$fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename;
CheckDirPath($fullFilename);
if (!$this->fp = @fopen($fullFilename, "w"))
return false;
else
return true;
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function PreWriteCatalog()
{
2013-08-19 15:14:36 +04:00
@fwrite($this->fp, "<yml_catalog date=\"" . $this->PrepareValue(Date("Y-m-d H:i:s")) . "\">\n");
@fwrite($this->fp, "<shop>\n");
@fwrite($this->fp, "<name>". $this->PrepareValue(COption::GetOptionString("main", "site_name", ""))."</name>\n");
@fwrite($this->fp, "<company>".$this->PrepareValue(COption::GetOptionString("main", "site_name", ""))."</company>\n");
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function WriteCategories($categories)
{
$stringCategories = "";
2013-08-19 15:14:36 +04:00
@fwrite($this->fp, "<categories>\n");
foreach ($categories as $category) {
2013-09-12 17:31:32 +04:00
$stringCategories .= $this->BuildCategory($category);
2013-08-19 15:14:36 +04:00
}
2013-09-12 17:31:32 +04:00
@fwrite($this->fp, $stringCategories);
2013-08-19 15:14:36 +04:00
@fwrite($this->fp, "</categories>\n");
2013-09-12 17:31:32 +04:00
}
protected function PreWriteOffers()
{
2013-08-19 15:14:36 +04:00
@fwrite($this->fp, "<offers>\n");
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function PostWriteOffers()
{
2013-08-19 15:14:36 +04:00
@fwrite($this->fp, "</offers>\n");
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function WriteOffers($offers)
{
@fwrite($this->fp, $offers);
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function PostWriteCatalog()
{
2013-08-19 15:14:36 +04:00
@fwrite($this->fp, "</shop>\n");
@fwrite($this->fp, "</yml_catalog>\n");
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function CloseFile()
{
2013-08-19 15:14:36 +04:00
@fclose($this->fp);
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function GetCategories()
{
2013-08-19 15:14:36 +04:00
$categories = array();
2013-09-22 20:03:35 +04:00
foreach ($this->iblocks as $id)
2013-08-19 15:14:36 +04:00
{
$filter = Array(
"IBLOCK_ID" => $id,
"ACTIVE" => "Y",
"IBLOCK_ACTIVE" => "Y",
"GLOBAL_ACTIVE" => "Y"
);
$dbRes = CIBlockSection::GetList(array("left_margin" => "asc"), $filter);
2013-09-12 17:31:32 +04:00
$hasCategories = false;
2013-08-19 15:14:36 +04:00
while ($arRes = $dbRes->Fetch())
{
2013-09-12 17:31:32 +04:00
$categories[$arRes['ID']] = $arRes;
$hasCategories = true;
2013-08-19 15:14:36 +04:00
}
2013-09-12 17:31:32 +04:00
if (!$hasCategories)
2013-08-20 12:53:32 +04:00
{
2013-09-12 17:31:32 +04:00
$iblock = CIBlock::GetByID($id)->Fetch();
2013-09-22 20:03:35 +04:00
2013-08-20 12:53:32 +04:00
$arRes = Array();
$arRes['ID'] = $this->mainSection + $id;
$arRes['IBLOCK_SECTION_ID'] = 0;
2013-09-22 20:03:35 +04:00
$arRes['NAME'] = sprintf(GetMessage('ROOT_CATEGORY_FOR_CATALOG'), $iblock['NAME']);
2013-09-12 17:31:32 +04:00
$categories[$arRes['ID']] = $arRes;
2013-08-20 12:53:32 +04:00
}
2013-08-19 15:14:36 +04:00
}
return $categories;
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function BuildCategory($arCategory)
{
2013-08-19 15:14:36 +04:00
return "
<category id=\"" . $this->PrepareValue($arCategory["ID"]) . "\""
. ( intval($arCategory["IBLOCK_SECTION_ID"] ) > 0 ?
" parentId=\"" . $this->PrepareValue($arCategory["IBLOCK_SECTION_ID"]) . "\""
:"")
. ">"
. $this->PrepareValue($arCategory["NAME"])
2013-09-12 17:31:32 +04:00
. "</category>\n";
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function BuildOffers(&$allCategories)
{
2013-09-22 20:03:35 +04:00
foreach ($this->iblocks as $key => $id)
2013-08-19 15:14:36 +04:00
{
$iblock['IBLOCK_DB'] = CIBlock::GetByID($id)->Fetch();
$iblockOffer = CCatalogSKU::GetInfoByProductIBlock($id);
$arSelect = Array (
"ID",
"LID",
"IBLOCK_ID",
"IBLOCK_SECTION_ID",
"ACTIVE",
"ACTIVE_FROM",
"ACTIVE_TO",
"NAME",
"DETAIL_PICTURE",
"DETAIL_TEXT",
"DETAIL_PICTURE",
"LANG_DIR",
2013-08-20 13:42:13 +04:00
"DETAIL_PAGE_URL"
2013-08-19 15:14:36 +04:00
);
2013-09-22 20:03:35 +04:00
2013-08-20 13:42:13 +04:00
if (isset($this->articleProperties[$id]))
$arSelect[] = "PROPERTY_" . $this->articleProperties[$id];
2013-09-22 20:03:35 +04:00
2013-08-19 15:14:36 +04:00
$filter = Array (
"IBLOCK_ID" => $id,
"ACTIVE_DATE" => "Y",
"ACTIVE" => "Y",
"INCLUDE_SUBSECTIONS" => "Y"
);
2013-09-12 17:31:32 +04:00
$count = 0;
2013-08-19 15:14:36 +04:00
$dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect);
2013-09-12 17:31:32 +04:00
$stringOffers = "";
2013-08-19 15:14:36 +04:00
while ($product = $dbResProducts->GetNextElement()) {
2013-09-22 20:03:35 +04:00
2013-08-19 15:14:36 +04:00
$product = $product->GetFields();
2013-09-12 17:31:32 +04:00
// Get categories in InfoBlock
$categories = Array();
$dbResCategories = CIBlockElement::GetElementGroups($product['ID'], true);
while ($arResCategory = $dbResCategories->Fetch()) {
$categories[$arResCategory["ID"]] = array(
'ID' => $arResCategory["ID"],
'NAME' => $arResCategory["NAME"],
);
}
if (count($categories) == 0) {
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
$catId = $this->mainSection + $id;
$categories[$catId] = $allCategories[$catId];
}
2013-08-19 15:14:36 +04:00
2013-09-22 20:03:35 +04:00
2013-08-19 15:14:36 +04:00
$existOffer = false;
if (!empty($iblockOffer['IBLOCK_ID'])) {
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
$arFilterOffer = Array (
'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'],
'PROPERTY_' . $iblockOffer['SKU_PROPERTY_ID'] => $product["ID"]
);
$arSelectOffer = Array (
'ID',
"NAME",
"DETAIL_TEXT",
"DETAIL_PAGE_URL",
"DETAIL_PICTURE"
);
if (isset($this->articleProperties[$id]))
$arSelectOffer[] = "PROPERTY_" . $this->articleProperties[$id];
$rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer);
while ($arOffer = $rsOffers->GetNext()) {
$offer = CCatalogProduct::GetByID($arOffer['ID']);
$arOffer['QUANTITY'] = $offer["QUANTITY"];
$arOffer['PRODUCT_ID'] = $product["ID"];
$arOffer['DETAIL_PAGE_URL'] = $product["DETAIL_PAGE_URL"];
$arOffer['DETAIL_PICTURE'] = $product["DETAIL_PICTURE"];
$arOffer['PREVIEW_PICTURE'] = $product["PREVIEW_PICTURE"];
$arOffer['PRODUCT_NAME'] = $product["NAME"];
2013-08-20 13:42:13 +04:00
if (isset($this->articleProperties[$id]))
2013-09-12 17:31:32 +04:00
$arOffer['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"];
2013-08-19 15:14:36 +04:00
2013-09-12 17:31:32 +04:00
$dbPrice = GetCatalogProductPrice($arOffer["ID"],1);
$arOffer['PRICE'] = $dbPrice['PRICE'];
2013-08-19 15:14:36 +04:00
2013-09-12 17:31:32 +04:00
$stringOffers .= $this->BuildOffer($arOffer, $categories, $iblock);
$existOffer = true;
}
2013-08-19 15:14:36 +04:00
}
if (!$existOffer) {
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
$offer = CCatalogProduct::GetByID($product['ID']);
$product['QUANTITY'] = $offer["QUANTITY"];
2013-08-19 15:14:36 +04:00
2013-09-12 17:31:32 +04:00
$product['PRODUCT_ID'] = $product["ID"];
$product['PRODUCT_NAME'] = $product["NAME"];
if (isset($this->articleProperties[$id]))
$product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"];
2013-08-19 15:14:36 +04:00
2013-09-12 17:31:32 +04:00
$dbPrice = GetCatalogProductPrice($product["ID"],1);
$product['PRICE'] = $dbPrice['PRICE'];
$stringOffers .= $this->BuildOffer($product, $categories, $iblock);
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
$count++;
if ($count == 1000) {
$this->WriteOffers($stringOffers);
$stringOffers = "";
2013-08-19 15:14:36 +04:00
}
2013-09-22 20:03:35 +04:00
2013-08-19 15:14:36 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
if ($stringOffers != "") {
$this->WriteOffers($stringOffers);
$stringOffers = "";
}
2013-08-19 15:14:36 +04:00
}
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
2013-09-12 17:31:32 +04:00
protected function BuildOffer($arOffer, $categories, $iblock)
{
2013-08-19 15:14:36 +04:00
$offer = "";
$offer .= "<offer id=\"" .$this->PrepareValue($arOffer["ID"]) . "\" ".
"productId=\"" . $this->PrepareValue($arOffer["PRODUCT_ID"]) . "\" ".
"quantity=\"" . $this->PrepareValue(DoubleVal($arOffer['QUANTITY'])) . "\">\n";
2013-09-22 20:03:35 +04:00
2013-08-19 15:14:36 +04:00
$offer .= "<url>http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . $this->PrepareValue($arOffer['DETAIL_PAGE_URL']) . "</url>\n";
2013-08-19 15:14:36 +04:00
$offer .= "<price>" . $this->PrepareValue($arOffer['PRICE']) . "</price>\n";
2013-09-12 17:31:32 +04:00
foreach ($categories as $category)
$offer .= "<categoryId>" . $category['ID'] . "</categoryId>\n";
2013-08-19 15:14:36 +04:00
$detailPicture = intval($arOffer["DETAIL_PICTURE"]);
$previewPicture = intval($arOffer["PREVIEW_PICTURE"]);
if ($detailPicture > 0 || $previewPicture > 0)
{
$picture = $detailPicture;
if ($picture <= 0) {
$picture = $previewPicture;
}
if ($arFile = CFile::GetFileArray($picture))
{
if(substr($arFile["SRC"], 0, 1) == "/")
$strFile = "http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . implode("/", array_map("rawurlencode", explode("/", $arFile["SRC"])));
elseif(preg_match("/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $arFile["SRC"], $match))
$strFile = "http://" . $this->PrepareValue($match[2]) . '/' . implode("/", array_map("rawurlencode", explode("/", $this->PrepareValue($match[3]))));
else
$strFile = $arFile["SRC"];
$offer .= "<picture>" . $this->PrepareValue($strFile) . "</picture>\n";
}
}
$offer .= "<name>" . $this->PrepareValue($arOffer["NAME"]) . "</name>\n";
$offer .= "<xmlId>" . $this->PrepareValue($arOffer["EXTERNAL_ID"]) . "</xmlId>\n";
$offer .= "<productName>" . $this->PrepareValue($arOffer["PRODUCT_NAME"]) . "</productName>\n";
2013-08-20 13:42:13 +04:00
if (isset($arOffer["ARTICLE"]))
$offer .= "<article>" . $this->PrepareValue($arOffer["ARTICLE"]) . "</article>\n";
2013-08-19 15:14:36 +04:00
$offer.= "</offer>\n";
return $offer;
2013-09-12 17:31:32 +04:00
}
2013-09-22 20:03:35 +04:00
}