1
0
mirror of synced 2024-11-22 05:16:09 +03:00

bug fix ICMLLoader.php

This commit is contained in:
m.korolev 2013-08-19 15:14:36 +04:00
parent 6bb71edd15
commit 2a66f0a67b

View File

@ -9,303 +9,271 @@ class ICMLLoader {
public $encoding = 'utf-8'; public $encoding = 'utf-8';
protected $fp; protected $fp;
public static function AgentLoad3( $filename)
{
echo $filename;
$arFilename = "'" . $filename . "'";
return "ICMLLoader::AgentLoad( " . $arFilename . ");";
}
public static function AgentLoad($iblocks, $filename)
{
if (!CModule::IncludeModule("iblock")) {
//handle err
self::eventLog('ICMLLoader::AgentLoad', 'iblock', 'module not found');
return true;
}
if (!CModule::IncludeModule("catalog")) {
//handle err
self::eventLog('ICMLLoader::AgentLoad', 'catalog', 'module not found');
return true;
}
global $APPLICATION, $USER;
if(!isset($USER)) {
$USER = new CUser;
}
$loader = new ICMLLoader();
$loader->iblocks = json_decode($iblocks, true);
$loader->filename = $filename;
$loader->application = $APPLICATION;
$loader->Load();
$arIblocks = "'" . $iblocks . "'";
$arFilename = "'" . $filename . "'";
return "ICMLLoader::AgentLoad(" . $arIblocks . ", " . $arFilename . ");";
}
public function Load() public function Load()
{ {
$categories = $this->GetCategories(); global $USER;
if(!isset($USER))
$offers = $this->GetOffers(); $USER = new CUser;
$this->PrepareFile(); $categories = $this->GetCategories();
$this->PreWriteCatalog(); $offers = $this->GetOffers();
$this->WriteCategories($categories); $this->PrepareFile();
$this->WriteOffers($offers);
$this->PreWriteCatalog();
$this->PostWriteCatalog();
$this->WriteCategories($categories);
$this->CloseFile(); $this->WriteOffers($offers);
$this->PostWriteCatalog();
$this->CloseFile();
} }
protected function PrepareValue($text) protected function PrepareValue($text)
{ {
return $this->application->ConvertCharset($text, LANG_CHARSET, $this->encoding); return $this->application->ConvertCharset($text, LANG_CHARSET, $this->encoding);
} }
protected function PrepareFile() protected function PrepareFile()
{ {
$fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename; $fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename;
CheckDirPath($fullFilename); CheckDirPath($fullFilename);
if (!$this->fp = @fopen($fullFilename, "w")) if (!$this->fp = @fopen($fullFilename, "w"))
return false; return false;
else else
return true; return true;
} }
protected function PreWriteCatalog() protected function PreWriteCatalog()
{ {
@fwrite($this->fp, "<yml_catalog date=\"" . $this->PrepareValue(Date("Y-m-d H:i:s")) . "\">\n"); @fwrite($this->fp, "<yml_catalog date=\"" . $this->PrepareValue(Date("Y-m-d H:i:s")) . "\">\n");
@fwrite($this->fp, "<shop>\n"); @fwrite($this->fp, "<shop>\n");
@fwrite($this->fp, "<name>". $this->PrepareValue(COption::GetOptionString("main", "site_name", ""))."</name>\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"); @fwrite($this->fp, "<company>".$this->PrepareValue(COption::GetOptionString("main", "site_name", ""))."</company>\n");
} }
protected function WriteCategories($categories) protected function WriteCategories($categories)
{ {
@fwrite($this->fp, "<categories>\n"); @fwrite($this->fp, "<categories>\n");
foreach ($categories as $category) { foreach ($categories as $category) {
@fwrite($this->fp, $category . "\n"); @fwrite($this->fp, $category . "\n");
} }
@fwrite($this->fp, "</categories>\n"); @fwrite($this->fp, "</categories>\n");
} }
protected function WriteOffers($offers) protected function WriteOffers($offers)
{ {
@fwrite($this->fp, "<offers>\n"); @fwrite($this->fp, "<offers>\n");
foreach ($offers as $offer) { foreach ($offers as $offer) {
@fwrite($this->fp, $offer . "\n"); @fwrite($this->fp, $offer . "\n");
} }
@fwrite($this->fp, "</offers>\n"); @fwrite($this->fp, "</offers>\n");
} }
protected function PostWriteCatalog() protected function PostWriteCatalog()
{ {
@fwrite($this->fp, "</shop>\n"); @fwrite($this->fp, "</shop>\n");
@fwrite($this->fp, "</yml_catalog>\n"); @fwrite($this->fp, "</yml_catalog>\n");
} }
protected function CloseFile() protected function CloseFile()
{ {
@fclose($this->fp); @fclose($this->fp);
} }
protected function GetCategories() protected function GetCategories()
{ {
$categories = array(); $categories = array();
foreach ($this->iblocks as $id) foreach ($this->iblocks as $id)
{ {
$filter = Array( $filter = Array(
"IBLOCK_ID" => $id, "IBLOCK_ID" => $id,
"ACTIVE" => "Y", "ACTIVE" => "Y",
"IBLOCK_ACTIVE" => "Y", "IBLOCK_ACTIVE" => "Y",
"GLOBAL_ACTIVE" => "Y" "GLOBAL_ACTIVE" => "Y"
); );
$dbRes = CIBlockSection::GetList(array("left_margin" => "asc"), $filter); $dbRes = CIBlockSection::GetList(array("left_margin" => "asc"), $filter);
while ($arRes = $dbRes->Fetch()) while ($arRes = $dbRes->Fetch())
{ {
$categories[] = $this->BuildCategory($arRes); $categories[] = $this->BuildCategory($arRes);
} }
} }
return $categories; return $categories;
} }
protected function BuildCategory($arCategory) protected function BuildCategory($arCategory)
{ {
return " return "
<category id=\"" . $this->PrepareValue($arCategory["ID"]) . "\"" <category id=\"" . $this->PrepareValue($arCategory["ID"]) . "\""
. ( intval($arCategory["IBLOCK_SECTION_ID"] ) > 0 ? . ( intval($arCategory["IBLOCK_SECTION_ID"] ) > 0 ?
" parentId=\"" . $this->PrepareValue($arCategory["IBLOCK_SECTION_ID"]) . "\"" " parentId=\"" . $this->PrepareValue($arCategory["IBLOCK_SECTION_ID"]) . "\""
:"") :"")
. ">" . ">"
. $this->PrepareValue($arCategory["NAME"]) . $this->PrepareValue($arCategory["NAME"])
. "</category>"; . "</category>";
} }
protected function GetOffers() protected function GetOffers()
{ {
$offers = Array(); $offers = Array();
foreach ($this->iblocks as $key => $id) foreach ($this->iblocks as $key => $id)
{ {
$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",
"DETAIL_PAGE_URL",
"PROPERTY_" . $this->articleProperties[$key]
);
$filter = Array ( $iblock['IBLOCK_DB'] = CIBlock::GetByID($id)->Fetch();
"IBLOCK_ID" => $id, $iblockOffer = CCatalogSKU::GetInfoByProductIBlock($id);
"ACTIVE_DATE" => "Y",
"ACTIVE" => "Y",
"INCLUDE_SUBSECTIONS" => "Y" $arSelect = Array (
); "ID",
"LID",
$dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect); "IBLOCK_ID",
while ($product = $dbResProducts->GetNextElement()) { "IBLOCK_SECTION_ID",
"ACTIVE",
$product = $product->GetFields(); "ACTIVE_FROM",
"ACTIVE_TO",
$categoriesString = ""; "NAME",
"DETAIL_PICTURE",
"DETAIL_TEXT",
$existOffer = false; "DETAIL_PICTURE",
if (!empty($iblockOffer['IBLOCK_ID'])) { "LANG_DIR",
$arFilterOffer = Array ( "DETAIL_PAGE_URL",
'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'], "PROPERTY_" . $this->articleProperties[$key]
'PROPERTY_'.$iblockOffer['SKU_PROPERTY_ID'] => $product["ID"] );
);
$arSelectOffer = Array ( $filter = Array (
'ID', "IBLOCK_ID" => $id,
"NAME", "ACTIVE_DATE" => "Y",
"DETAIL_TEXT", "ACTIVE" => "Y",
"DETAIL_PAGE_URL", "INCLUDE_SUBSECTIONS" => "Y"
"DETAIL_PICTURE", );
"PROPERTY_" . $this->articleProperties[$key]
); $dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect);
while ($product = $dbResProducts->GetNextElement()) {
$rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer);
while ($arOffer = $rsOffers->GetNext()) { $product = $product->GetFields();
$dbResCategories = CIBlockElement::GetElementGroups($arOffer['ID'], true); $categoriesString = "";
while ($arResCategory = $dbResCategories->Fetch()) {
$categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n";
} $existOffer = false;
$offer = CCatalogProduct::GetByID($arOffer['ID']); if (!empty($iblockOffer['IBLOCK_ID'])) {
$arOffer['QUANTITY'] = $offer["QUANTITY"]; $arFilterOffer = Array (
'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'],
$arOffer['PRODUCT_ID'] = $product["ID"]; 'PROPERTY_'.$iblockOffer['SKU_PROPERTY_ID'] => $product["ID"]
$arOffer['DETAIL_PAGE_URL'] = $product["DETAIL_PAGE_URL"]; );
$arOffer['DETAIL_PICTURE'] = $product["DETAIL_PICTURE"]; $arSelectOffer = Array (
$arOffer['PREVIEW_PICTURE'] = $product["PREVIEW_PICTURE"]; 'ID',
$arOffer['PRODUCT_NAME'] = $product["NAME"]; "NAME",
$arOffer['ARTICLE'] = $arOffer["PROPERTY_" . $this->articleProperties[$key] . "_VALUE"]; "DETAIL_TEXT",
"DETAIL_PAGE_URL",
$dbPrice = GetCatalogProductPrice($arOffer["ID"],1); "DETAIL_PICTURE",
$arOffer['PRICE'] = $dbPrice['PRICE']; "PROPERTY_" . $this->articleProperties[$key]
);
$rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer);
$offers[] = $this->BuildOffer($arOffer, $categoriesString, $iblock); while ($arOffer = $rsOffers->GetNext()) {
$existOffer = true;
} $dbResCategories = CIBlockElement::GetElementGroups($arOffer['ID'], true);
} while ($arResCategory = $dbResCategories->Fetch()) {
if (!$existOffer) { $categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n";
$dbResCategories = CIBlockElement::GetElementGroups($product["ID"], true); }
while ($arResCategory = $dbResCategories->Fetch()) { $offer = CCatalogProduct::GetByID($arOffer['ID']);
$categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n"; $arOffer['QUANTITY'] = $offer["QUANTITY"];
}
$arOffer['PRODUCT_ID'] = $product["ID"];
$arOffer['DETAIL_PAGE_URL'] = $product["DETAIL_PAGE_URL"];
$offer = CCatalogProduct::GetByID($product['ID']); $arOffer['DETAIL_PICTURE'] = $product["DETAIL_PICTURE"];
$product['QUANTITY'] = $offer["QUANTITY"]; $arOffer['PREVIEW_PICTURE'] = $product["PREVIEW_PICTURE"];
$arOffer['PRODUCT_NAME'] = $product["NAME"];
$product['PRODUCT_ID'] = $product["ID"]; $arOffer['ARTICLE'] = $arOffer["PROPERTY_" . $this->articleProperties[$key] . "_VALUE"];
$product['PRODUCT_NAME'] = $product["NAME"];
$product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$key] . "_VALUE"]; $dbPrice = GetCatalogProductPrice($arOffer["ID"],1);
$arOffer['PRICE'] = $dbPrice['PRICE'];
$dbPrice = GetCatalogProductPrice($product["ID"],1);
$product['PRICE'] = $dbPrice['PRICE'];
$offers[] = $this->BuildOffer($product, $categoriesString, $iblock); $offers[] = $this->BuildOffer($arOffer, $categoriesString, $iblock);
} $existOffer = true;
} }
} }
return $offers; if (!$existOffer) {
$dbResCategories = CIBlockElement::GetElementGroups($product["ID"], true);
while ($arResCategory = $dbResCategories->Fetch()) {
$categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n";
}
$offer = CCatalogProduct::GetByID($product['ID']);
$product['QUANTITY'] = $offer["QUANTITY"];
$product['PRODUCT_ID'] = $product["ID"];
$product['PRODUCT_NAME'] = $product["NAME"];
$product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$key] . "_VALUE"];
$dbPrice = GetCatalogProductPrice($product["ID"],1);
$product['PRICE'] = $dbPrice['PRICE'];
$offers[] = $this->BuildOffer($product, $categoriesString, $iblock);
}
}
}
return $offers;
} }
protected function BuildOffer($arOffer, $categoriesString, $iblock) protected function BuildOffer($arOffer, $categoriesString, $iblock)
{ {
$offer = ""; $offer = "";
$offer .= "<offer id=\"" .$this->PrepareValue($arOffer["ID"]) . "\" ". $offer .= "<offer id=\"" .$this->PrepareValue($arOffer["ID"]) . "\" ".
"productId=\"" . $this->PrepareValue($arOffer["PRODUCT_ID"]) . "\" ". "productId=\"" . $this->PrepareValue($arOffer["PRODUCT_ID"]) . "\" ".
"quantity=\"" . $this->PrepareValue(DoubleVal($arOffer['QUANTITY'])) . "\">\n"; "quantity=\"" . $this->PrepareValue(DoubleVal($arOffer['QUANTITY'])) . "\">\n";
$offer .= "<url>http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . $this->PrepareValue($arOffer['DETAIL_PAGE_URL']) . "</url>\n"; $offer .= "<url>http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . $this->PrepareValue($arOffer['DETAIL_PAGE_URL']) . "</url>\n";
$offer .= "<price>" . $this->PrepareValue($arOffer['PRICE']) . "</price>\n"; $offer .= "<price>" . $this->PrepareValue($arOffer['PRICE']) . "</price>\n";
$offer .= $categoriesString; $offer .= $categoriesString;
$detailPicture = intval($arOffer["DETAIL_PICTURE"]);
$previewPicture = intval($arOffer["PREVIEW_PICTURE"]);
if ($detailPicture > 0 || $previewPicture > 0) $detailPicture = intval($arOffer["DETAIL_PICTURE"]);
{ $previewPicture = intval($arOffer["PREVIEW_PICTURE"]);
$picture = $detailPicture;
if ($picture <= 0) {
$picture = $previewPicture;
}
if ($arFile = CFile::GetFileArray($picture)) if ($detailPicture > 0 || $previewPicture > 0)
{ {
if(substr($arFile["SRC"], 0, 1) == "/") $picture = $detailPicture;
$strFile = "http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . implode("/", array_map("rawurlencode", explode("/", $arFile["SRC"]))); if ($picture <= 0) {
elseif(preg_match("/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $arFile["SRC"], $match)) $picture = $previewPicture;
$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 .= "<description>" . (strip_tags( html_entity_decode(str_replace("&nbsp;", ' ', $this->PrepareValue($arOffer["DETAIL_TEXT"]))))) ."</description>\n";
$offer .= "<xmlId>" . $this->PrepareValue($arOffer["EXTERNAL_ID"]) . "</xmlId>\n"; if ($arFile = CFile::GetFileArray($picture))
$offer .= "<productName>" . $this->PrepareValue($arOffer["PRODUCT_NAME"]) . "</productName>\n"; {
$offer .= "<article>" . $this->PrepareValue($arOffer["ARTICLE"]) . "</article>\n"; if(substr($arFile["SRC"], 0, 1) == "/")
$strFile = "http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . implode("/", array_map("rawurlencode", explode("/", $arFile["SRC"])));
$offer.= "</offer>\n"; elseif(preg_match("/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $arFile["SRC"], $match))
return $offer; $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";
$offer .= "<article>" . $this->PrepareValue($arOffer["ARTICLE"]) . "</article>\n";
$offer.= "</offer>\n";
return $offer;
} }