Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. <?php
  2. use Bitrix\Main\Loader;
  3. use Bitrix\Iblock\PropertyTable;
  4. use Bitrix\Iblock\SectionTable;
  5. use Bitrix\Iblock\ElementTable;
  6.  
  7. define("STOP_STATISTICS", true);
  8. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
  9. Loader::includeModule("iblock");
  10.  
  11. class CSiteCatalogUploader
  12. {
  13. private $filename;
  14.  
  15. private $iblockId;
  16.  
  17. public function __construct($filename, $iblockId)
  18. {
  19. $this->filename = $filename;
  20. $this->iblockId = $iblockId;
  21.  
  22. Loader::includeModule("iblock");
  23. }
  24.  
  25. protected function translit($name)
  26. {
  27. return \CUtil::translit($name, "ru", ["change_case" => "U"]);
  28. }
  29.  
  30. protected function resolveProperties(array $row)
  31. {
  32. $result = [];
  33. foreach($row as $key => $val)
  34. {
  35. $arProperty = [
  36. "IBLOCK_ID" => $this->iblockId,
  37. "NAME" => $val,
  38. "CODE" => $this->translit($val),
  39. "PROPERTY_TYPE" => "S",
  40. "MULTIPLE" => "N",
  41. "ACTIVE" => "Y"
  42. ];
  43. $rs = PropertyTable::getList([
  44. "select" => ["ID"],
  45. "filter" => [
  46. "CODE" => $arProperty["CODE"],
  47. "IBLOCK_ID" => $arProperty["IBLOCK_ID"],
  48. ]
  49. ]);
  50. if($ar = $rs->Fetch())
  51. {
  52. $pid = $ar["ID"];
  53. // $ob = new CIBlockProperty();
  54. // $pid = $ob->update($pid, $arProperty);
  55. }
  56. else
  57. {
  58. $ob = new CIBlockProperty();
  59. $pid = $ob->add($arProperty);
  60. }
  61.  
  62. $result[] = "IP_PROP" . $pid;
  63. }
  64.  
  65. return $result;
  66. }
  67.  
  68. public function convert($outputFilename = null)
  69. {
  70. if(null == $outputFilename)
  71. $outputFilename = $this->filename . ".out";
  72.  
  73. if(file_exists($outputFilename))
  74. unlink($outputFilename);
  75.  
  76. $bFirstRow = true;
  77. $fileOut = new \SplFileObject($outputFilename, "w");
  78. $fileIn = new \SplFileObject($this->filename);
  79. while(!$fileIn->eof())
  80. {
  81. $data = $fileIn->fgetcsv(";");
  82. if($bFirstRow)
  83. {
  84. $bFirstRow = false;
  85. $data = $this->resolveProperties($data);
  86. }
  87. $fileOut->fputcsv($data, ";");
  88. }
  89.  
  90. unset($fileOut); unset($fileIn);
  91. }
  92.  
  93. public function clearProperties()
  94. {
  95. $rs = PropertyTable::getList([
  96. "select" => ["ID", "CODE"],
  97. "filter" => [
  98. "IBLOCK_ID" => $this->iblockId
  99. ]
  100. ]);
  101. while($ar = $rs->Fetch())
  102. {
  103. if(!in_array($ar["CODE"], ["MORE_PHOTO", "CML2_LINK", "PHOTO_CHECKSUM"]))
  104. CIBlockProperty::delete($ar["ID"]);
  105. }
  106. }
  107.  
  108. private function createSection($arFields)
  109. {
  110. $arSection = SectionTable::getRow([
  111. "select" => ["ID"],
  112. "filter" => $arFields
  113. ]);
  114. if(!$arSection)
  115. {
  116. if(!isset($arFields["CODE"]))
  117. $arFields["CODE"] = $this->translit($arFields["NAME"]);
  118.  
  119. $ob = new CIBlockSection();
  120. $arSection["ID"] = $ob->add($arFields);
  121. if(!$arSection["ID"])
  122. throw new Exception($ob->LAST_ERROR);
  123. }
  124.  
  125. return $arSection["ID"];
  126. }
  127.  
  128. private function createElement($arFields)
  129. {
  130. $arFilter = $arFields;
  131. if(isset($arFilter["PREVIEW_TEXT"]))
  132. unset($arFilter["PREVIEW_TEXT"]);
  133.  
  134. $arElement = ElementTable::getRow([
  135. "select" => ["ID"],
  136. "filter" => $arFilter
  137. ]);
  138. if(!$arElement)
  139. {
  140. // if(!isset($arFields["CODE"]))
  141. // $arFields["CODE"] = $this->translit($arFields["NAME"]);
  142.  
  143. $ob = new CIBlockElement();
  144. $arElement["ID"] = $ob->add($arFields);
  145. if(!$arElement["ID"])
  146. throw new Exception($ob->LAST_ERROR);
  147. }
  148.  
  149. return $arElement["ID"];
  150. }
  151.  
  152. /**
  153. * Рекурсивно копирует разделы в заданнй инфоблок
  154. *
  155. * @param int $targetIblockID Инфоблок назначения
  156. * @param bool $parentId
  157. * @param bool $sectionId
  158. * @return array
  159. */
  160. public function mapSectionsRecursive($targetIblockID, $parentId = false, $sectionId = false)
  161. {
  162. $result = [];
  163. $rs = SectionTable::getList([
  164. "select" => ["ID", "NAME", "IBLOCK_SECTION_ID"],
  165. "filter" => [
  166. "IBLOCK_SECTION_ID" => $parentId,
  167. "IBLOCK_ID" => $this->iblockId
  168. ],
  169. ]);
  170. while($ar = $rs->Fetch())
  171. {
  172. $result[$ar["ID"]] = $this->createSection([
  173. "NAME" => $ar["NAME"],
  174. "IBLOCK_SECTION_ID" => $sectionId,
  175. "IBLOCK_ID" => $targetIblockID
  176. ]);
  177.  
  178. $res = $this->mapSectionsRecursive($targetIblockID, $ar["ID"], $result[$ar["ID"]]);
  179. $result = array_replace($result, $res);
  180. }
  181.  
  182. return $result;
  183. }
  184.  
  185. public function createProducts($targetIblockID)
  186. {
  187. $arSectionMap = $this->mapSectionsRecursive($targetIblockID);
  188. $rsOffer = CIBlockElement::GetList(
  189. [],
  190. [
  191. "IBLOCK_ID" => $this->iblockId,
  192. "!PROPERTY_MODELNYY_RYAD" => false,
  193. "INCLUDE_SUBSECTIONS" => "Y"
  194. ],
  195. false,
  196. false,
  197. [
  198. "ID", "IBLOCK_ID",
  199. "PROPERTY_MODELNYY_RYAD",
  200. "PROPERTY_LINEYKA",
  201. "IBLOCK_SECTION_ID"
  202. ]
  203. );
  204. $arProducts = [];
  205. while($arOffer = $rsOffer->Fetch())
  206. {
  207. $arFields = [
  208. "IBLOCK_ID" => $targetIblockID,
  209. "ACTIVE" => "Y",
  210. "NAME" => $arOffer["PROPERTY_MODELNYY_RYAD_VALUE"],
  211. "PREVIEW_TEXT" => $arOffer["PROPERTY_LINEYKA_VALUE"],
  212. ];
  213. if($arOffer["IBLOCK_SECTION_ID"])
  214. $arFields["IBLOCK_SECTION_ID"] = $arSectionMap[$arOffer["IBLOCK_SECTION_ID"]];
  215.  
  216. $arProducts[$arFields["NAME"]] = $this->createElement($arFields);
  217.  
  218. CIBlockElement::SetPropertyValues($arOffer["ID"], $arOffer["IBLOCK_ID"], $arProducts[$arFields["NAME"]], "CML2_LINK");
  219.  
  220. $line = $this->createElement([
  221. "IBLOCK_ID" => 27,
  222. "ACTIVE" => "Y",
  223. "NAME" => $arOffer["PROPERTY_LINEYKA_VALUE"],
  224. ]);
  225. CIBlockElement::SetPropertyValues($arProducts[$arFields["NAME"]], $targetIblockID, $line, "LINEYKA_LINK");
  226. }
  227.  
  228. return $arProducts;
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement