diff --git a/intaro.intarocrm/classes/general/ICMLLoader.php b/intaro.intarocrm/classes/general/ICMLLoader.php index cb5715c5..18715118 100644 --- a/intaro.intarocrm/classes/general/ICMLLoader.php +++ b/intaro.intarocrm/classes/general/ICMLLoader.php @@ -1,18 +1,18 @@ iblocks) < count($this->articleProperties)) return false; - $categories = $this->GetCategories(); - - $offers = $this->GetOffers(); - $this->PrepareFile(); $this->PreWriteCatalog(); - + + $categories = $this->GetCategories(); + $this->WriteCategories($categories); - $this->WriteOffers($offers); + + $this->PreWriteOffers(); + $this->BuildOffers($categories); + $this->PostWriteOffers(); $this->PostWriteCatalog(); $this->CloseFile(); return true; - - } - - protected function PrepareValue($text) + + } + + protected function PrepareValue($text) { $newText = $this->application->ConvertCharset($text, LANG_CHARSET, $this->encoding); $newText = strip_tags($newText); $newText = str_replace("&", "&", $newText); return $newText; } - - protected function PrepareFile() - { + + protected function PrepareFile() + { $fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename; CheckDirPath($fullFilename); @@ -55,10 +56,10 @@ class ICMLLoader { return false; else return true; - } - - protected function PreWriteCatalog() - { + } + + protected function PreWriteCatalog() + { @fwrite($this->fp, "PrepareValue(Date("Y-m-d H:i:s")) . "\">\n"); @fwrite($this->fp, "\n"); @@ -66,39 +67,47 @@ class ICMLLoader { @fwrite($this->fp, "".$this->PrepareValue(COption::GetOptionString("main", "site_name", ""))."\n"); - } - - protected function WriteCategories($categories) - { + } + + protected function WriteCategories($categories) + { + $stringCategories = ""; @fwrite($this->fp, "\n"); foreach ($categories as $category) { - @fwrite($this->fp, $category . "\n"); + $stringCategories .= $this->BuildCategory($category); } + @fwrite($this->fp, $stringCategories); @fwrite($this->fp, "\n"); - } - protected function WriteOffers($offers) - { + } + protected function PreWriteOffers() + { @fwrite($this->fp, "\n"); - foreach ($offers as $offer) { - @fwrite($this->fp, $offer . "\n"); - } + } + + protected function PostWriteOffers() + { @fwrite($this->fp, "\n"); - } - - protected function PostWriteCatalog() - { + } + + protected function WriteOffers($offers) + { + @fwrite($this->fp, $offers); + } + + protected function PostWriteCatalog() + { @fwrite($this->fp, "\n"); @fwrite($this->fp, "\n"); - } - - protected function CloseFile() - { + } + + protected function CloseFile() + { @fclose($this->fp); - } - - - protected function GetCategories() - { + } + + + protected function GetCategories() + { $categories = array(); foreach ($this->iblocks as $id) { @@ -109,27 +118,30 @@ class ICMLLoader { "GLOBAL_ACTIVE" => "Y" ); - $dbRes = CIBlockSection::GetList(array("left_margin" => "asc"), $filter); + $hasCategories = false; 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['ID'] = $this->mainSection + $id; $arRes['IBLOCK_SECTION_ID'] = 0; - $arRes['NAME'] = "Основной раздел каталога"; - $categories[] = $this->BuildCategory($arRes); + $arRes['NAME'] = "Основной раздел каталога " . $iblock['NAME']; + $categories[$arRes['ID']] = $arRes; } } return $categories; - } - - protected function BuildCategory($arCategory) - { + } + + protected function BuildCategory($arCategory) + { return " PrepareValue($arCategory["ID"]) . "\"" . ( intval($arCategory["IBLOCK_SECTION_ID"] ) > 0 ? @@ -137,13 +149,12 @@ class ICMLLoader { :"") . ">" . $this->PrepareValue($arCategory["NAME"]) - . ""; - - } - - protected function GetOffers() - { - $offers = Array(); + . "\n"; + + } + + protected function BuildOffers(&$allCategories) + { foreach ($this->iblocks as $key => $id) { @@ -177,97 +188,114 @@ class ICMLLoader { "ACTIVE" => "Y", "INCLUDE_SUBSECTIONS" => "Y" ); - + $count = 0; $dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect); + $stringOffers = ""; while ($product = $dbResProducts->GetNextElement()) { - + $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; if (!empty($iblockOffer['IBLOCK_ID'])) { - $arFilterOffer = Array ( - 'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'], - 'PROPERTY_'.$iblockOffer['SKU_PROPERTY_ID'] => $product["ID"] - ); - $arSelectOffer = Array ( - 'ID', - "NAME", - "DETAIL_TEXT", - "DETAIL_PAGE_URL", - "DETAIL_PICTURE" - ); + + + $arFilterOffer = Array ( + 'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'], + 'PROPERTY_' . $iblockOffer['SKU_PROPERTY_ID'] => $product["ID"] + ); + $arSelectOffer = Array ( + 'ID', + "NAME", + "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])) - $arSelectOffer[] = "PROPERTY_" . $this->articleProperties[$id]; + $arOffer['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"]; - $rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer); - while ($arOffer = $rsOffers->GetNext()) { + $dbPrice = GetCatalogProductPrice($arOffer["ID"],1); + $arOffer['PRICE'] = $dbPrice['PRICE']; - $dbResCategories = CIBlockElement::GetElementGroups($arOffer['ID'], true); - while ($arResCategory = $dbResCategories->Fetch()) { - $categoriesString .= "" . $arResCategory["ID"] . "\n"; - } - if ($categoriesString == '') - $categoriesString .= "" . ($this->mainSection + $id) . "\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; - } + $stringOffers .= $this->BuildOffer($arOffer, $categories, $iblock); + $existOffer = true; + } } if (!$existOffer) { - $dbResCategories = CIBlockElement::GetElementGroups($product["ID"], true); - while ($arResCategory = $dbResCategories->Fetch()) { - $categoriesString .= "" . $arResCategory["ID"] . "\n"; - } - if ($categoriesString == '') - $categoriesString .= "" . ($this->mainSection + $id) . "\n"; + + $offer = CCatalogProduct::GetByID($product['ID']); + $product['QUANTITY'] = $offer["QUANTITY"]; - $offer = CCatalogProduct::GetByID($product['ID']); - $product['QUANTITY'] = $offer["QUANTITY"]; + $product['PRODUCT_ID'] = $product["ID"]; + $product['PRODUCT_NAME'] = $product["NAME"]; + if (isset($this->articleProperties[$id])) + $product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"]; - $product['PRODUCT_ID'] = $product["ID"]; - $product['PRODUCT_NAME'] = $product["NAME"]; - if (isset($this->articleProperties[$id])) - $product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$id] . "_VALUE"]; + $dbPrice = GetCatalogProductPrice($product["ID"],1); + $product['PRICE'] = $dbPrice['PRICE']; - $dbPrice = GetCatalogProductPrice($product["ID"],1); - $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 .= "PrepareValue($arOffer["ID"]) . "\" ". "productId=\"" . $this->PrepareValue($arOffer["PRODUCT_ID"]) . "\" ". "quantity=\"" . $this->PrepareValue(DoubleVal($arOffer['QUANTITY'])) . "\">\n"; + $offer .= "http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . $this->PrepareValue($arOffer['DETAIL_PAGE_URL']) . "\n"; $offer .= "" . $this->PrepareValue($arOffer['PRICE']) . "\n"; - $offer .= $categoriesString; + foreach ($categories as $category) + $offer .= "" . $category['ID'] . "\n"; $detailPicture = intval($arOffer["DETAIL_PICTURE"]); $previewPicture = intval($arOffer["PREVIEW_PICTURE"]); @@ -300,8 +328,6 @@ class ICMLLoader { $offer.= "\n"; return $offer; - } - - - + } + } \ No newline at end of file diff --git a/intaro.intarocrm/export/export_run.php b/intaro.intarocrm/export/export_run.php new file mode 100644 index 00000000..ea2d7464 --- /dev/null +++ b/intaro.intarocrm/export/export_run.php @@ -0,0 +1,19 @@ +iblocks = $IBLOCK_EXPORT; +$loader->articleProperties = $IBLOCK_PROPERTY_ARTICLE; +$loader->filename = $SETUP_FILE_NAME; +$loader->application = $APPLICATION; +$loader->Load(); \ No newline at end of file