Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Bitrix\Main\Loader;
- use Bitrix\Iblock\PropertyTable;
- use Bitrix\Iblock\SectionTable;
- use Bitrix\Iblock\ElementTable;
- define("STOP_STATISTICS", true);
- require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
- Loader::includeModule("iblock");
- class CSiteCatalogUploader
- {
- private $filename;
- private $iblockId;
- public function __construct($filename, $iblockId)
- {
- $this->filename = $filename;
- $this->iblockId = $iblockId;
- Loader::includeModule("iblock");
- }
- protected function translit($name)
- {
- return \CUtil::translit($name, "ru", ["change_case" => "U"]);
- }
- protected function resolveProperties(array $row)
- {
- $result = [];
- foreach($row as $key => $val)
- {
- $arProperty = [
- "IBLOCK_ID" => $this->iblockId,
- "NAME" => $val,
- "CODE" => $this->translit($val),
- "PROPERTY_TYPE" => "S",
- "MULTIPLE" => "N",
- "ACTIVE" => "Y"
- ];
- $rs = PropertyTable::getList([
- "select" => ["ID"],
- "filter" => [
- "CODE" => $arProperty["CODE"],
- "IBLOCK_ID" => $arProperty["IBLOCK_ID"],
- ]
- ]);
- if($ar = $rs->Fetch())
- {
- $pid = $ar["ID"];
- // $ob = new CIBlockProperty();
- // $pid = $ob->update($pid, $arProperty);
- }
- else
- {
- $ob = new CIBlockProperty();
- $pid = $ob->add($arProperty);
- }
- $result[] = "IP_PROP" . $pid;
- }
- return $result;
- }
- public function convert($outputFilename = null)
- {
- if(null == $outputFilename)
- $outputFilename = $this->filename . ".out";
- if(file_exists($outputFilename))
- unlink($outputFilename);
- $bFirstRow = true;
- $fileOut = new \SplFileObject($outputFilename, "w");
- $fileIn = new \SplFileObject($this->filename);
- while(!$fileIn->eof())
- {
- $data = $fileIn->fgetcsv(";");
- if($bFirstRow)
- {
- $bFirstRow = false;
- $data = $this->resolveProperties($data);
- }
- $fileOut->fputcsv($data, ";");
- }
- unset($fileOut); unset($fileIn);
- }
- public function clearProperties()
- {
- $rs = PropertyTable::getList([
- "select" => ["ID", "CODE"],
- "filter" => [
- "IBLOCK_ID" => $this->iblockId
- ]
- ]);
- while($ar = $rs->Fetch())
- {
- if(!in_array($ar["CODE"], ["MORE_PHOTO", "CML2_LINK", "PHOTO_CHECKSUM"]))
- CIBlockProperty::delete($ar["ID"]);
- }
- }
- private function createSection($arFields)
- {
- $arSection = SectionTable::getRow([
- "select" => ["ID"],
- "filter" => $arFields
- ]);
- if(!$arSection)
- {
- if(!isset($arFields["CODE"]))
- $arFields["CODE"] = $this->translit($arFields["NAME"]);
- $ob = new CIBlockSection();
- $arSection["ID"] = $ob->add($arFields);
- if(!$arSection["ID"])
- throw new Exception($ob->LAST_ERROR);
- }
- return $arSection["ID"];
- }
- private function createElement($arFields)
- {
- $arFilter = $arFields;
- if(isset($arFilter["PREVIEW_TEXT"]))
- unset($arFilter["PREVIEW_TEXT"]);
- $arElement = ElementTable::getRow([
- "select" => ["ID"],
- "filter" => $arFilter
- ]);
- if(!$arElement)
- {
- // if(!isset($arFields["CODE"]))
- // $arFields["CODE"] = $this->translit($arFields["NAME"]);
- $ob = new CIBlockElement();
- $arElement["ID"] = $ob->add($arFields);
- if(!$arElement["ID"])
- throw new Exception($ob->LAST_ERROR);
- }
- return $arElement["ID"];
- }
- /**
- * Рекурсивно копирует разделы в заданнй инфоблок
- *
- * @param int $targetIblockID Инфоблок назначения
- * @param bool $parentId
- * @param bool $sectionId
- * @return array
- */
- public function mapSectionsRecursive($targetIblockID, $parentId = false, $sectionId = false)
- {
- $result = [];
- $rs = SectionTable::getList([
- "select" => ["ID", "NAME", "IBLOCK_SECTION_ID"],
- "filter" => [
- "IBLOCK_SECTION_ID" => $parentId,
- "IBLOCK_ID" => $this->iblockId
- ],
- ]);
- while($ar = $rs->Fetch())
- {
- $result[$ar["ID"]] = $this->createSection([
- "NAME" => $ar["NAME"],
- "IBLOCK_SECTION_ID" => $sectionId,
- "IBLOCK_ID" => $targetIblockID
- ]);
- $res = $this->mapSectionsRecursive($targetIblockID, $ar["ID"], $result[$ar["ID"]]);
- $result = array_replace($result, $res);
- }
- return $result;
- }
- public function createProducts($targetIblockID)
- {
- $arSectionMap = $this->mapSectionsRecursive($targetIblockID);
- $rsOffer = CIBlockElement::GetList(
- [],
- [
- "IBLOCK_ID" => $this->iblockId,
- "!PROPERTY_MODELNYY_RYAD" => false,
- "INCLUDE_SUBSECTIONS" => "Y"
- ],
- false,
- false,
- [
- "ID", "IBLOCK_ID",
- "PROPERTY_MODELNYY_RYAD",
- "PROPERTY_LINEYKA",
- "IBLOCK_SECTION_ID"
- ]
- );
- $arProducts = [];
- while($arOffer = $rsOffer->Fetch())
- {
- $arFields = [
- "IBLOCK_ID" => $targetIblockID,
- "ACTIVE" => "Y",
- "NAME" => $arOffer["PROPERTY_MODELNYY_RYAD_VALUE"],
- "PREVIEW_TEXT" => $arOffer["PROPERTY_LINEYKA_VALUE"],
- ];
- if($arOffer["IBLOCK_SECTION_ID"])
- $arFields["IBLOCK_SECTION_ID"] = $arSectionMap[$arOffer["IBLOCK_SECTION_ID"]];
- $arProducts[$arFields["NAME"]] = $this->createElement($arFields);
- CIBlockElement::SetPropertyValues($arOffer["ID"], $arOffer["IBLOCK_ID"], $arProducts[$arFields["NAME"]], "CML2_LINK");
- $line = $this->createElement([
- "IBLOCK_ID" => 27,
- "ACTIVE" => "Y",
- "NAME" => $arOffer["PROPERTY_LINEYKA_VALUE"],
- ]);
- CIBlockElement::SetPropertyValues($arProducts[$arFields["NAME"]], $targetIblockID, $line, "LINEYKA_LINK");
- }
- return $arProducts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement