1
0
mirror of synced 2025-02-16 15:03:14 +03:00

catalog export fix

This commit is contained in:
Grisha Pomadchin 2013-09-22 20:03:35 +04:00
parent 0e5c9b5b6c
commit 6c2552881b
2 changed files with 46 additions and 41 deletions

View File

@ -1,33 +1,36 @@
<?php <?php
global $MESS;
IncludeModuleLangFile(__FILE__);
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;
if (count($this->iblocks) < count($this->articleProperties)) if (count($this->iblocks) < count($this->articleProperties))
return false; return false;
$this->PrepareFile(); $this->PrepareFile();
$this->PreWriteCatalog(); $this->PreWriteCatalog();
$categories = $this->GetCategories(); $categories = $this->GetCategories();
$this->WriteCategories($categories); $this->WriteCategories($categories);
$this->PreWriteOffers(); $this->PreWriteOffers();
$this->BuildOffers($categories); $this->BuildOffers($categories);
$this->PostWriteOffers(); $this->PostWriteOffers();
@ -36,9 +39,9 @@ class ICMLLoader {
$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);
@ -46,7 +49,7 @@ class ICMLLoader {
$newText = str_replace("&", "&#x26;", $newText); $newText = str_replace("&", "&#x26;", $newText);
return $newText; return $newText;
} }
protected function PrepareFile() protected function PrepareFile()
{ {
$fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename; $fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename;
@ -57,7 +60,7 @@ class ICMLLoader {
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");
@ -68,7 +71,7 @@ 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 = ""; $stringCategories = "";
@ -83,33 +86,33 @@ class ICMLLoader {
{ {
@fwrite($this->fp, "<offers>\n"); @fwrite($this->fp, "<offers>\n");
} }
protected function PostWriteOffers() protected function PostWriteOffers()
{ {
@fwrite($this->fp, "</offers>\n"); @fwrite($this->fp, "</offers>\n");
} }
protected function WriteOffers($offers) protected function WriteOffers($offers)
{ {
@fwrite($this->fp, $offers); @fwrite($this->fp, $offers);
} }
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,
@ -128,18 +131,18 @@ class ICMLLoader {
if (!$hasCategories) if (!$hasCategories)
{ {
$iblock = CIBlock::GetByID($id)->Fetch(); $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'] = "Основной раздел каталога " . $iblock['NAME']; $arRes['NAME'] = sprintf(GetMessage('ROOT_CATEGORY_FOR_CATALOG'), $iblock['NAME']);
$categories[$arRes['ID']] = $arRes; $categories[$arRes['ID']] = $arRes;
} }
} }
return $categories; return $categories;
} }
protected function BuildCategory($arCategory) protected function BuildCategory($arCategory)
{ {
return " return "
@ -150,12 +153,12 @@ class ICMLLoader {
. ">" . ">"
. $this->PrepareValue($arCategory["NAME"]) . $this->PrepareValue($arCategory["NAME"])
. "</category>\n"; . "</category>\n";
} }
protected function BuildOffers(&$allCategories) protected function BuildOffers(&$allCategories)
{ {
foreach ($this->iblocks as $key => $id) foreach ($this->iblocks as $key => $id)
{ {
$iblock['IBLOCK_DB'] = CIBlock::GetByID($id)->Fetch(); $iblock['IBLOCK_DB'] = CIBlock::GetByID($id)->Fetch();
@ -177,10 +180,10 @@ class ICMLLoader {
"LANG_DIR", "LANG_DIR",
"DETAIL_PAGE_URL" "DETAIL_PAGE_URL"
); );
if (isset($this->articleProperties[$id])) if (isset($this->articleProperties[$id]))
$arSelect[] = "PROPERTY_" . $this->articleProperties[$id]; $arSelect[] = "PROPERTY_" . $this->articleProperties[$id];
$filter = Array ( $filter = Array (
"IBLOCK_ID" => $id, "IBLOCK_ID" => $id,
@ -192,7 +195,7 @@ class ICMLLoader {
$dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect); $dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect);
$stringOffers = ""; $stringOffers = "";
while ($product = $dbResProducts->GetNextElement()) { while ($product = $dbResProducts->GetNextElement()) {
$product = $product->GetFields(); $product = $product->GetFields();
// Get categories in InfoBlock // Get categories in InfoBlock
@ -205,16 +208,16 @@ class ICMLLoader {
); );
} }
if (count($categories) == 0) { if (count($categories) == 0) {
$catId = $this->mainSection + $id; $catId = $this->mainSection + $id;
$categories[$catId] = $allCategories[$catId]; $categories[$catId] = $allCategories[$catId];
} }
$existOffer = false; $existOffer = false;
if (!empty($iblockOffer['IBLOCK_ID'])) { if (!empty($iblockOffer['IBLOCK_ID'])) {
$arFilterOffer = Array ( $arFilterOffer = Array (
'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'], 'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'],
'PROPERTY_' . $iblockOffer['SKU_PROPERTY_ID'] => $product["ID"] 'PROPERTY_' . $iblockOffer['SKU_PROPERTY_ID'] => $product["ID"]
@ -252,7 +255,7 @@ class ICMLLoader {
} }
} }
if (!$existOffer) { if (!$existOffer) {
$offer = CCatalogProduct::GetByID($product['ID']); $offer = CCatalogProduct::GetByID($product['ID']);
$product['QUANTITY'] = $offer["QUANTITY"]; $product['QUANTITY'] = $offer["QUANTITY"];
@ -266,15 +269,15 @@ class ICMLLoader {
$stringOffers .= $this->BuildOffer($product, $categories, $iblock); $stringOffers .= $this->BuildOffer($product, $categories, $iblock);
} }
$count++; $count++;
if ($count == 1000) { if ($count == 1000) {
$this->WriteOffers($stringOffers); $this->WriteOffers($stringOffers);
$stringOffers = ""; $stringOffers = "";
} }
} }
if ($stringOffers != "") { if ($stringOffers != "") {
$this->WriteOffers($stringOffers); $this->WriteOffers($stringOffers);
$stringOffers = ""; $stringOffers = "";
@ -282,15 +285,15 @@ class ICMLLoader {
} }
} }
protected function BuildOffer($arOffer, $categories, $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";
@ -329,5 +332,5 @@ class ICMLLoader {
$offer.= "</offer>\n"; $offer.= "</offer>\n";
return $offer; return $offer;
} }
} }

View File

@ -0,0 +1,2 @@
<?php
$MESS["ROOT_CATEGORY_FOR_CATALOG"] = "Корневой раздел для каталога \"%s\"";