1
0
mirror of synced 2024-11-25 23:06:08 +03:00

Writing offers to file has been paginate

This commit is contained in:
m.korolev 2013-09-11 15:37:50 +04:00
parent 0397aad602
commit 6db7bc93e1

View File

@ -20,16 +20,17 @@ 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($categories);
$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();
@ -70,21 +71,29 @@ class ICMLLoader {
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, $this->BuildCategory($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, $this->BuildOffer($offer['OFFER'], $offer['CATEGORIES'], $offer['IBLOCK']) . "\n");
} }
protected function PostWriteOffers()
{
@fwrite($this->fp, "</offers>\n"); @fwrite($this->fp, "</offers>\n");
} }
protected function WriteOffers($offers)
{
@fwrite($this->fp, $offers);
}
protected function PostWriteCatalog() protected function PostWriteCatalog()
{ {
@fwrite($this->fp, "</shop>\n"); @fwrite($this->fp, "</shop>\n");
@ -109,19 +118,22 @@ 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[] = $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[] = $arRes; $categories[$arRes['ID']] = $arRes;
} }
} }
return $categories; return $categories;
@ -137,13 +149,12 @@ class ICMLLoader {
:"") :"")
. ">" . ">"
. $this->PrepareValue($arCategory["NAME"]) . $this->PrepareValue($arCategory["NAME"])
. "</category>"; . "</category>\n";
} }
protected function GetOffers(&$allCategories) protected function BuildOffers(&$allCategories)
{ {
$offers = Array();
foreach ($this->iblocks as $key => $id) foreach ($this->iblocks as $key => $id)
{ {
@ -177,17 +188,33 @@ 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();
// Get categories in InfoBlock
$categories = Array(); $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 ( $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"]
@ -202,24 +229,6 @@ class ICMLLoader {
if (isset($this->articleProperties[$id])) if (isset($this->articleProperties[$id]))
$arSelectOffer[] = "PROPERTY_" . $this->articleProperties[$id]; $arSelectOffer[] = "PROPERTY_" . $this->articleProperties[$id];
$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;;
$category = array();
$category['ID'] = $catId;
$category['NAME'] = 'Основная группа инфоблока ' . $iblock['IBLOCK_DB']['NAME'];
$categories[$catId] = $category;
$allCategories[$catId] = $category;
}
$rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer); $rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer);
while ($arOffer = $rsOffers->GetNext()) { while ($arOffer = $rsOffers->GetNext()) {
@ -238,36 +247,12 @@ class ICMLLoader {
$dbPrice = GetCatalogProductPrice($arOffer["ID"],1); $dbPrice = GetCatalogProductPrice($arOffer["ID"],1);
$arOffer['PRICE'] = $dbPrice['PRICE']; $arOffer['PRICE'] = $dbPrice['PRICE'];
$offers[] = Array( $stringOffers .= $this->BuildOffer($arOffer, $categories, $iblock);
'OFFER' => $arOffer,
'CATEGORIES' => $categories,
'IBLOCK' => $iblock
);
$existOffer = true; $existOffer = true;
} }
} }
if (!$existOffer) { if (!$existOffer) {
$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;
$category = array();
$category['ID'] = $catId;
$category['NAME'] = 'Основная группа инфоблока ' . $iblock['IBLOCK_DB']['NAME'];
$categories[$catId] = $category;
$allCategories[$catId] = $category;
}
$offer = CCatalogProduct::GetByID($product['ID']); $offer = CCatalogProduct::GetByID($product['ID']);
$product['QUANTITY'] = $offer["QUANTITY"]; $product['QUANTITY'] = $offer["QUANTITY"];
@ -279,15 +264,23 @@ class ICMLLoader {
$dbPrice = GetCatalogProductPrice($product["ID"],1); $dbPrice = GetCatalogProductPrice($product["ID"],1);
$product['PRICE'] = $dbPrice['PRICE']; $product['PRICE'] = $dbPrice['PRICE'];
$offers[] = Array( $stringOffers .= $this->BuildOffer($product, $categories, $iblock);
'OFFER' => $product,
'CATEGORIES' => $categories,
'IBLOCK' => $iblock
);
} }
$count++;
if ($count == 1000) {
$this->WriteOffers($stringOffers);
$stringOffers = "";
} }
}
if ($stringOffers != "") {
$this->WriteOffers($stringOffers);
$stringOffers = "";
}
} }
return $offers;
} }
@ -297,6 +290,7 @@ class ICMLLoader {
$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";
@ -336,6 +330,4 @@ class ICMLLoader {
return $offer; return $offer;
} }
} }