1
0
mirror of synced 2024-11-22 21:36:10 +03:00

update icml

This commit is contained in:
Grisha Pomadchin 2013-09-12 17:31:32 +04:00
parent ebbc5d2763
commit 90ea4b3eab
2 changed files with 181 additions and 136 deletions

View File

@ -2,17 +2,17 @@
class ICMLLoader { class ICMLLoader {
public $iblocks; public $iblocks;
public $filename; public $filename;
public $articleProperties; public $articleProperties;
public $application; public $application;
public $encoding = 'utf-8'; public $encoding = 'utf-8';
protected $fp; protected $fp;
protected $mainSection = 1000000; protected $mainSection = 1000000;
public function Load() public function Load()
{ {
global $USER; global $USER;
if(!isset($USER)) if(!isset($USER))
$USER = new CUser; $USER = new CUser;
@ -20,25 +20,26 @@ class ICMLLoader {
if (count($this->iblocks) < count($this->articleProperties)) if (count($this->iblocks) < count($this->articleProperties))
return false; return false;
$categories = $this->GetCategories();
$offers = $this->GetOffers();
$this->PrepareFile(); $this->PrepareFile();
$this->PreWriteCatalog(); $this->PreWriteCatalog();
$categories = $this->GetCategories();
$this->WriteCategories($categories); $this->WriteCategories($categories);
$this->WriteOffers($offers);
$this->PreWriteOffers();
$this->BuildOffers($categories);
$this->PostWriteOffers();
$this->PostWriteCatalog(); $this->PostWriteCatalog();
$this->CloseFile(); $this->CloseFile();
return true; return true;
} }
protected function PrepareValue($text) protected function PrepareValue($text)
{ {
$newText = $this->application->ConvertCharset($text, LANG_CHARSET, $this->encoding); $newText = $this->application->ConvertCharset($text, LANG_CHARSET, $this->encoding);
$newText = strip_tags($newText); $newText = strip_tags($newText);
@ -46,8 +47,8 @@ class ICMLLoader {
return $newText; return $newText;
} }
protected function PrepareFile() protected function PrepareFile()
{ {
$fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename; $fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename;
CheckDirPath($fullFilename); CheckDirPath($fullFilename);
@ -55,10 +56,10 @@ class ICMLLoader {
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");
@ -66,39 +67,47 @@ class ICMLLoader {
@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)
{ {
$stringCategories = "";
@fwrite($this->fp, "<categories>\n"); @fwrite($this->fp, "<categories>\n");
foreach ($categories as $category) { foreach ($categories as $category) {
@fwrite($this->fp, $category . "\n"); $stringCategories .= $this->BuildCategory($category);
} }
@fwrite($this->fp, $stringCategories);
@fwrite($this->fp, "</categories>\n"); @fwrite($this->fp, "</categories>\n");
} }
protected function WriteOffers($offers) protected function PreWriteOffers()
{ {
@fwrite($this->fp, "<offers>\n"); @fwrite($this->fp, "<offers>\n");
foreach ($offers as $offer) { }
@fwrite($this->fp, $offer . "\n");
}
@fwrite($this->fp, "</offers>\n");
}
protected function PostWriteCatalog() protected function PostWriteOffers()
{ {
@fwrite($this->fp, "</offers>\n");
}
protected function WriteOffers($offers)
{
@fwrite($this->fp, $offers);
}
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)
{ {
@ -109,27 +118,30 @@ class ICMLLoader {
"GLOBAL_ACTIVE" => "Y" "GLOBAL_ACTIVE" => "Y"
); );
$dbRes = CIBlockSection::GetList(array("left_margin" => "asc"), $filter); $dbRes = CIBlockSection::GetList(array("left_margin" => "asc"), $filter);
$hasCategories = false;
while ($arRes = $dbRes->Fetch()) while ($arRes = $dbRes->Fetch())
{ {
$categories[] = $this->BuildCategory($arRes); $categories[$arRes['ID']] = $arRes;
$hasCategories = true;
} }
if (count($categories) == 0) if (!$hasCategories)
{ {
$iblock = CIBlock::GetByID($id)->Fetch();
$arRes = Array(); $arRes = Array();
$arRes['ID'] = $this->mainSection + $id; $arRes['ID'] = $this->mainSection + $id;
$arRes['IBLOCK_SECTION_ID'] = 0; $arRes['IBLOCK_SECTION_ID'] = 0;
$arRes['NAME'] = "Основной раздел каталога"; $arRes['NAME'] = "Основной раздел каталога " . $iblock['NAME'];
$categories[] = $this->BuildCategory($arRes); $categories[$arRes['ID']] = $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 ?
@ -137,13 +149,12 @@ class ICMLLoader {
:"") :"")
. ">" . ">"
. $this->PrepareValue($arCategory["NAME"]) . $this->PrepareValue($arCategory["NAME"])
. "</category>"; . "</category>\n";
} }
protected function GetOffers() protected function BuildOffers(&$allCategories)
{ {
$offers = Array();
foreach ($this->iblocks as $key => $id) foreach ($this->iblocks as $key => $id)
{ {
@ -177,97 +188,114 @@ class ICMLLoader {
"ACTIVE" => "Y", "ACTIVE" => "Y",
"INCLUDE_SUBSECTIONS" => "Y" "INCLUDE_SUBSECTIONS" => "Y"
); );
$count = 0;
$dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect); $dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect);
$stringOffers = "";
while ($product = $dbResProducts->GetNextElement()) { while ($product = $dbResProducts->GetNextElement()) {
$product = $product->GetFields(); $product = $product->GetFields();
$categoriesString = ""; // 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) {
$catId = $this->mainSection + $id;
$categories[$catId] = $allCategories[$catId];
}
$existOffer = false; $existOffer = false;
if (!empty($iblockOffer['IBLOCK_ID'])) { if (!empty($iblockOffer['IBLOCK_ID'])) {
$arFilterOffer = Array (
'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'],
'PROPERTY_'.$iblockOffer['SKU_PROPERTY_ID'] => $product["ID"] $arFilterOffer = Array (
); 'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'],
$arSelectOffer = Array ( 'PROPERTY_' . $iblockOffer['SKU_PROPERTY_ID'] => $product["ID"]
'ID', );
"NAME", $arSelectOffer = Array (
"DETAIL_TEXT", 'ID',
"DETAIL_PAGE_URL", "NAME",
"DETAIL_PICTURE" "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"];
if (isset($this->articleProperties[$id])) if (isset($this->articleProperties[$id]))
$arSelectOffer[] = "PROPERTY_" . $this->articleProperties[$id]; $arOffer['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"];
$rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer); $dbPrice = GetCatalogProductPrice($arOffer["ID"],1);
while ($arOffer = $rsOffers->GetNext()) { $arOffer['PRICE'] = $dbPrice['PRICE'];
$dbResCategories = CIBlockElement::GetElementGroups($arOffer['ID'], true); $stringOffers .= $this->BuildOffer($arOffer, $categories, $iblock);
while ($arResCategory = $dbResCategories->Fetch()) { $existOffer = true;
$categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n"; }
}
if ($categoriesString == '')
$categoriesString .= "<categoryId>" . ($this->mainSection + $id) . "</categoryId>\n";
$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"];
if (isset($this->articleProperties[$id]))
$arOffer['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"];
$dbPrice = GetCatalogProductPrice($arOffer["ID"],1);
$arOffer['PRICE'] = $dbPrice['PRICE'];
$offers[] = $this->BuildOffer($arOffer, $categoriesString, $iblock);
$existOffer = true;
}
} }
if (!$existOffer) { if (!$existOffer) {
$dbResCategories = CIBlockElement::GetElementGroups($product["ID"], true);
while ($arResCategory = $dbResCategories->Fetch()) {
$categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n";
}
if ($categoriesString == '')
$categoriesString .= "<categoryId>" . ($this->mainSection + $id) . "</categoryId>\n";
$offer = CCatalogProduct::GetByID($product['ID']); $offer = CCatalogProduct::GetByID($product['ID']);
$product['QUANTITY'] = $offer["QUANTITY"]; $product['QUANTITY'] = $offer["QUANTITY"];
$product['PRODUCT_ID'] = $product["ID"]; $product['PRODUCT_ID'] = $product["ID"];
$product['PRODUCT_NAME'] = $product["NAME"]; $product['PRODUCT_NAME'] = $product["NAME"];
if (isset($this->articleProperties[$id])) if (isset($this->articleProperties[$id]))
$product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"]; $product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"];
$dbPrice = GetCatalogProductPrice($product["ID"],1); $dbPrice = GetCatalogProductPrice($product["ID"],1);
$product['PRICE'] = $dbPrice['PRICE']; $product['PRICE'] = $dbPrice['PRICE'];
$offers[] = $this->BuildOffer($product, $categoriesString, $iblock); $stringOffers .= $this->BuildOffer($product, $categories, $iblock);
} }
$count++;
if ($count == 1000) {
$this->WriteOffers($stringOffers);
$stringOffers = "";
}
} }
if ($stringOffers != "") {
$this->WriteOffers($stringOffers);
$stringOffers = "";
}
} }
return $offers; }
}
protected function BuildOffer($arOffer, $categoriesString, $iblock) protected function BuildOffer($arOffer, $categories, $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; foreach ($categories as $category)
$offer .= "<categoryId>" . $category['ID'] . "</categoryId>\n";
$detailPicture = intval($arOffer["DETAIL_PICTURE"]); $detailPicture = intval($arOffer["DETAIL_PICTURE"]);
$previewPicture = intval($arOffer["PREVIEW_PICTURE"]); $previewPicture = intval($arOffer["PREVIEW_PICTURE"]);
@ -300,8 +328,6 @@ class ICMLLoader {
$offer.= "</offer>\n"; $offer.= "</offer>\n";
return $offer; return $offer;
} }
} }

View File

@ -0,0 +1,19 @@
<?php
set_time_limit(0);
global $APPLICATION;
if (!CModule::IncludeModule("iblock"))
return;
if (!CModule::IncludeModule("catalog"))
return;
if (!CModule::IncludeModule("intaro.intarocrm"))
return;
$loader = new ICMLLoader();
$loader->iblocks = $IBLOCK_EXPORT;
$loader->articleProperties = $IBLOCK_PROPERTY_ARTICLE;
$loader->filename = $SETUP_FILE_NAME;
$loader->application = $APPLICATION;
$loader->Load();