1
0
mirror of synced 2024-11-22 13:26:10 +03:00

Before commit: Collect data about categories, then format this, collect data about offers, then format this, and then write all to file.

After commit: Collect all data, then format all data and write all
This commit is contained in:
m.korolev 2013-09-10 09:25:17 +04:00
parent fc469781b2
commit 0397aad602

View File

@ -22,11 +22,7 @@ class ICMLLoader {
$categories = $this->GetCategories(); $categories = $this->GetCategories();
$newCategories = array(); $offers = $this->GetOffers($categories);
$offers = $this->GetOffers($newCategories);
foreach ($newCategories as $newCategory) {
$categories[] = $this->BuildCategory($newCategory);
}
$this->PrepareFile(); $this->PrepareFile();
@ -76,7 +72,7 @@ class ICMLLoader {
{ {
@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, $this->BuildCategory($category) . "\n");
} }
@fwrite($this->fp, "</categories>\n"); @fwrite($this->fp, "</categories>\n");
} }
@ -84,7 +80,7 @@ class ICMLLoader {
{ {
@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, $this->BuildOffer($offer['OFFER'], $offer['CATEGORIES'], $offer['IBLOCK']) . "\n");
} }
@fwrite($this->fp, "</offers>\n"); @fwrite($this->fp, "</offers>\n");
} }
@ -117,7 +113,7 @@ class ICMLLoader {
$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[] = $arRes;
} }
if (count($categories) == 0) if (count($categories) == 0)
{ {
@ -125,7 +121,7 @@ class ICMLLoader {
$arRes['ID'] = $this->mainSection + $id; $arRes['ID'] = $this->mainSection + $id;
$arRes['IBLOCK_SECTION_ID'] = 0; $arRes['IBLOCK_SECTION_ID'] = 0;
$arRes['NAME'] = "Основной раздел каталога"; $arRes['NAME'] = "Основной раздел каталога";
$categories[] = $this->BuildCategory($arRes); $categories[] = $arRes;
} }
} }
return $categories; return $categories;
@ -145,7 +141,7 @@ class ICMLLoader {
} }
protected function GetOffers(&$newCategories) protected function GetOffers(&$allCategories)
{ {
$offers = Array(); $offers = Array();
foreach ($this->iblocks as $key => $id) foreach ($this->iblocks as $key => $id)
@ -187,14 +183,14 @@ class ICMLLoader {
$product = $product->GetFields(); $product = $product->GetFields();
$categoriesString = ""; $categories = Array();
$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"]
); );
$arSelectOffer = Array ( $arSelectOffer = Array (
'ID', 'ID',
@ -206,24 +202,28 @@ class ICMLLoader {
if (isset($this->articleProperties[$id])) if (isset($this->articleProperties[$id]))
$arSelectOffer[] = "PROPERTY_" . $this->articleProperties[$id]; $arSelectOffer[] = "PROPERTY_" . $this->articleProperties[$id];
$rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer);
while ($arOffer = $rsOffers->GetNext()) {
$dbResCategories = CIBlockElement::GetElementGroups($product['ID'], true); $dbResCategories = CIBlockElement::GetElementGroups($product['ID'], true);
while ($arResCategory = $dbResCategories->Fetch()) { while ($arResCategory = $dbResCategories->Fetch()) {
$categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n"; $categories[$arResCategory["ID"]] = array(
'ID' => $arResCategory["ID"],
'NAME' => $arResCategory["NAME"],
);
} }
if ($categoriesString == '') { if (count($categories) == 0) {
$catId = $this->mainSection + $id; $catId = $this->mainSection + $id;;
$categoriesString .= "<categoryId>" . ($catId) . "</categoryId>\n";
$category = array(); $category = array();
$category['ID'] = $catId; $category['ID'] = $catId;
$category['NAME'] = 'Основная группа инфоблока ' . $iblock['IBLOCK_DB']['NAME']; $category['NAME'] = 'Основная группа инфоблока ' . $iblock['IBLOCK_DB']['NAME'];
$newCategories[$catId] = $category; $categories[$catId] = $category;
$allCategories[$catId] = $category;
} }
$rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer);
while ($arOffer = $rsOffers->GetNext()) {
$offer = CCatalogProduct::GetByID($arOffer['ID']); $offer = CCatalogProduct::GetByID($arOffer['ID']);
$arOffer['QUANTITY'] = $offer["QUANTITY"]; $arOffer['QUANTITY'] = $offer["QUANTITY"];
@ -238,29 +238,36 @@ class ICMLLoader {
$dbPrice = GetCatalogProductPrice($arOffer["ID"],1); $dbPrice = GetCatalogProductPrice($arOffer["ID"],1);
$arOffer['PRICE'] = $dbPrice['PRICE']; $arOffer['PRICE'] = $dbPrice['PRICE'];
$offers[] = Array(
'OFFER' => $arOffer,
'CATEGORIES' => $categories,
'IBLOCK' => $iblock
);
$offers[] = $this->BuildOffer($arOffer, $categoriesString, $iblock);
$existOffer = true; $existOffer = true;
} }
} }
if (!$existOffer) { if (!$existOffer) {
$dbResCategories = CIBlockElement::GetElementGroups($product["ID"], true); $dbResCategories = CIBlockElement::GetElementGroups($product["ID"], true);
while ($arResCategory = $dbResCategories->Fetch()) { while ($arResCategory = $dbResCategories->Fetch()) {
$categoriesString .= "<categoryId>" . $arResCategory["ID"] . "</categoryId>\n"; $categories[$arResCategory["ID"]] = array(
'ID' => $arResCategory["ID"],
'NAME' => $arResCategory["NAME"],
);
} }
if ($categoriesString == '') { if (count($categories) == 0) {
$catId = $this->mainSection + $id; $catId = $this->mainSection + $id;
$categoriesString .= "<categoryId>" . ($catId) . "</categoryId>\n";
$category = array(); $category = array();
$category['ID'] = $catId; $category['ID'] = $catId;
$category['NAME'] = 'Основная группа инфоблока ' . $iblock['IBLOCK_DB']['NAME']; $category['NAME'] = 'Основная группа инфоблока ' . $iblock['IBLOCK_DB']['NAME'];
$newCategories[$catId] = $category; $categories[$catId] = $category;
$allCategories[$catId] = $category;
} }
$offer = CCatalogProduct::GetByID($product['ID']); $offer = CCatalogProduct::GetByID($product['ID']);
$product['QUANTITY'] = $offer["QUANTITY"]; $product['QUANTITY'] = $offer["QUANTITY"];
@ -272,7 +279,11 @@ class ICMLLoader {
$dbPrice = GetCatalogProductPrice($product["ID"],1); $dbPrice = GetCatalogProductPrice($product["ID"],1);
$product['PRICE'] = $dbPrice['PRICE']; $product['PRICE'] = $dbPrice['PRICE'];
$offers[] = $this->BuildOffer($product, $categoriesString, $iblock); $offers[] = Array(
'OFFER' => $product,
'CATEGORIES' => $categories,
'IBLOCK' => $iblock
);
} }
} }
} }
@ -280,7 +291,7 @@ class ICMLLoader {
} }
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"]) . "\" ".
@ -289,7 +300,8 @@ class ICMLLoader {
$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"]);