• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/classes/general/cml2.php
  • Класс: CIBlockCMLImport
  • Вызов: CIBlockCMLImport::ImportSection
function ImportSection($xml_tree_id, $IBLOCK_ID, $parent_section_id)
{
	/** @var CUserTypeManager $USER_FIELD_MANAGER */
	global $USER_FIELD_MANAGER;
	/** @var CDatabase $DB */
	global $DB;

	static $arUserFields;
	if($parent_section_id === false)
	{
		$arUserFields = array();
		foreach($USER_FIELD_MANAGER->GetUserFields("IBLOCK_".$IBLOCK_ID."_SECTION") as $FIELD_ID => $arField)
		{
			if($arField["XML_ID"] == '')
				$arUserFields[$FIELD_ID] = $arField;
			else
				$arUserFields[$arField["XML_ID"]] = $arField;
		}
	}

	$this->next_step["section_sort"] += 10;
	$arSection = array(
		"IBLOCK_SECTION_ID" => $parent_section_id,
		"ACTIVE" => "Y",
	);
	$rsS = $this->_xml_file->GetList(
		array("ID" => "asc"),
		array("PARENT_ID" => $xml_tree_id)
	);
	$XML_SECTIONS_PARENT = false;
	$XML_PROPERTIES_PARENT = false;
	$XML_SECTION_PROPERTIES = false;
	$deletedStatus = false;
	while($arS = $rsS->Fetch())
	{
		if(isset($arS["VALUE_CLOB"]))
			$arS["VALUE"] = $arS["VALUE_CLOB"];

		if($arS["NAME"]==$this->mess["IBLOCK_XML2_ID"])
			$arSection["XML_ID"] = $arS["VALUE"];
		elseif($arS["NAME"]==$this->mess["IBLOCK_XML2_NAME"])
			$arSection["NAME"] = $arS["VALUE"];
		elseif($arS["NAME"]==$this->mess["IBLOCK_XML2_DESCRIPTION"])
		{
			$arSection["DESCRIPTION"] = $arS["VALUE"];
			$arSection["DESCRIPTION_TYPE"] = "html";
		}
		elseif($arS["NAME"]==$this->mess["IBLOCK_XML2_GROUPS"])
			$XML_SECTIONS_PARENT = $arS["ID"];
		elseif($arS["NAME"]==$this->mess["IBLOCK_XML2_PROPERTIES_VALUES"])
			$XML_PROPERTIES_PARENT = $arS["ID"];
		elseif($arS["NAME"]==$this->mess["IBLOCK_XML2_BX_SORT"])
			$arSection["SORT"] = intval($arS["VALUE"]);
		elseif($arS["NAME"]==$this->mess["IBLOCK_XML2_BX_CODE"])
			$arSection["CODE"] = $arS["VALUE"];
		elseif($arS["NAME"] == $this->mess["IBLOCK_XML2_BX_PICTURE"])
		{
			if($arS["VALUE"] <> '')
				$arSection["PICTURE"] = $this->MakeFileArray($arS["VALUE"]);
			else
				$arSection["PICTURE"] = $this->MakeFileArray($this->_xml_file->GetAllChildrenArray($arS["ID"]));
		}
		elseif($arS["NAME"] == $this->mess["IBLOCK_XML2_BX_DETAIL_PICTURE"])
		{
			if($arS["VALUE"] <> '')
				$arSection["DETAIL_PICTURE"] = $this->MakeFileArray($arS["VALUE"]);
			else
				$arSection["DETAIL_PICTURE"] = $this->MakeFileArray($this->_xml_file->GetAllChildrenArray($arS["ID"]));
		}
		elseif($arS["NAME"] == $this->mess["IBLOCK_XML2_BX_ACTIVE"])
			$arSection["ACTIVE"] = ($arS["VALUE"]=="true") || intval($arS["VALUE"])? "Y": "N";
		elseif($arS["NAME"] == $this->mess["IBLOCK_XML2_SECTION_PROPERTIES"])
			$XML_SECTION_PROPERTIES = $arS["ID"];
		elseif($arS["NAME"] == $this->mess["IBLOCK_XML2_STATUS"])
			$deletedStatus = $arS["VALUE"] === $this->mess["IBLOCK_XML2_DELETED"];
		elseif($arS["NAME"] == $this->mess["IBLOCK_XML2_INHERITED_TEMPLATES"])
		{
			$arSection["IPROPERTY_TEMPLATES"] = array();
			$arTemplates = $this->_xml_file->GetAllChildrenArray($arS["ID"]);
			foreach($arTemplates as $TEMPLATE)
			{
				$id = $TEMPLATE[$this->mess["IBLOCK_XML2_ID"]];
				$template = $TEMPLATE[$this->mess["IBLOCK_XML2_VALUE"]];
				if($id <> '' && $template <> '')
					$arSection["IPROPERTY_TEMPLATES"][$id] = $template;
			}
		}
		elseif($arS["NAME"] == $this->mess["IBLOCK_XML2_DELETE_MARK"])
		{
			$arSection["ACTIVE"] = ($arS["VALUE"]=="true") || intval($arS["VALUE"])? "N": "Y";
		}
	}

	if ($deletedStatus)
	{
		$obSection = new CIBlockSection;
		$rsSection = $obSection->GetList(array(), array(
			"IBLOCK_ID" => $IBLOCK_ID,
			"XML_ID" => $arSection["XML_ID"],
		), false, array("ID"));
		if($arDBSection = $rsSection->Fetch())
		{
			$obSection->Update($arDBSection["ID"], array(
				"ACTIVE" => "N",
			));
			$this->_xml_file->Add(array("PARENT_ID" => 0, "LEFT_MARGIN" => $arDBSection["ID"]));
		}
		return true;
	}

	if($XML_PROPERTIES_PARENT)
	{
		$rs = $this->_xml_file->GetList(
			array("ID" => "asc"),
			array("PARENT_ID" => $XML_PROPERTIES_PARENT),
			array("ID")
		);
		while($ar = $rs->Fetch())
		{
			$arXMLProp = $this->_xml_file->GetAllChildrenArray($ar["ID"]);
			if(
				array_key_exists($this->mess["IBLOCK_XML2_ID"], $arXMLProp)
				&& array_key_exists($arXMLProp[$this->mess["IBLOCK_XML2_ID"]], $arUserFields)
			)
			{
				$arUserField = $arUserFields[$arXMLProp[$this->mess["IBLOCK_XML2_ID"]]];
				unset($arXMLProp[$this->mess["IBLOCK_XML2_ID"]]);

				$arProp = array();
				$i = 0;
				foreach($arXMLProp as $value)
				{
					if($arUserField["USER_TYPE"]["BASE_TYPE"] === "file")
						$arProp["n".($i++)] = $this->MakeFileArray($value);
					elseif($arUserField["USER_TYPE"]["BASE_TYPE"] === "enum")
						$arProp["n".($i++)] = $this->GetSectionEnumByXML_ID($arUserField["ID"], $value);
					else
						$arProp["n".($i++)] = $value;
				}

				if($arUserField["MULTIPLE"] == "N")
					$arSection[$arUserField["FIELD_NAME"]] = array_pop($arProp);
				else
					$arSection[$arUserField["FIELD_NAME"]] = $arProp;
			}
		}
	}

	$obSection = new CIBlockSection;
	$rsSection = $obSection->GetList(array(), array("IBLOCK_ID"=>$IBLOCK_ID, "XML_ID"=>$arSection["XML_ID"]), false);
	if($arDBSection = $rsSection->Fetch())
	{
		if(
			!array_key_exists("CODE", $arSection)
			&& is_array($this->translit_on_update)
		)
		{
			$CODE = CUtil::translit($arSection["NAME"], LANGUAGE_ID, $this->translit_on_update);
			$CODE = $this->CheckSectionCode($IBLOCK_ID, $arDBSection["ID"], $CODE);
			if ($CODE !== false)
				$arSection["CODE"] = $CODE;
		}

		$bChanged = false;
		foreach($arSection as $key=>$value)
		{
			if(is_array($arDBSection[$key]) || ($arDBSection[$key] != $value))
			{
				$bChanged = true;
				break;
			}
		}

		if($bChanged)
		{
			foreach($arUserFields as $arField1)
			{
				if($arField1["USER_TYPE"]["BASE_TYPE"] == "file")
				{
					$sectionUF = $USER_FIELD_MANAGER->GetUserFields("IBLOCK_".$IBLOCK_ID."_SECTION", $arDBSection["ID"]);
					foreach($sectionUF as $arField2)
					{
						if(
							$arField2["USER_TYPE"]["BASE_TYPE"] == "file"
							&& isset($arSection[$arField2["FIELD_NAME"]])
						)
						{
							if($arField2["MULTIPLE"] == "Y" && is_array($arField2["VALUE"]))
							{
								foreach($arField2["VALUE"] as $old_file_id)
									$arSection[$arField2["FIELD_NAME"]][] = array("del"=>true,"old_id"=>$old_file_id);
							}
							elseif($arField2["MULTIPLE"] == "N" && $arField2["VALUE"] > 0)
							{
								$arSection[$arField2["FIELD_NAME"]]["old_id"] = $arField2["VALUE"];
							}
						}
					}
					break;
				}
			}

			if (!isset($arSection['MODIFIED_BY']) || (int)$arSection['MODIFIED_BY'] <= 0)
			{
				if ($this->currentUserId > 0)
				{
					$arSection['MODIFIED_BY'] = $this->currentUserId;
				}
			}

			$res = $obSection->Update($arDBSection["ID"], $arSection);
			if(!$res)
			{
				$this->LAST_ERROR = $obSection->LAST_ERROR;
				return $this->LAST_ERROR;
			}
		}
		else
		{
			$strUpdate = "TIMESTAMP_X = ".$DB->CurrentTimeFunction();
			if ($this->currentUserId > 0)
				$strUpdate .= ", MODIFIED_BY=".$this->currentUserId;
			$DB->Query("UPDATE b_iblock_section SET ".$strUpdate." WHERE ID=".$arDBSection["ID"]);
			unset($strUpdate);
		}

		$arSection["ID"] = $arDBSection["ID"];
	}
	else
	{
		if(!array_key_exists("CODE", $arSection) && is_array($this->translit_on_add))
		{
			$CODE = CUtil::translit($arSection["NAME"], LANGUAGE_ID, $this->translit_on_add);
			$CODE = $this->CheckSectionCode($IBLOCK_ID, 0, $CODE);
			if ($CODE !== false)
				$arSection["CODE"] = $CODE;
		}

		$arSection["IBLOCK_ID"] = $IBLOCK_ID;
		if(!isset($arSection["SORT"]))
			$arSection["SORT"] = $this->next_step["section_sort"];

		$arSection["ID"] = $obSection->Add($arSection);
		if(!$arSection["ID"])
		{
			$this->LAST_ERROR = $obSection->LAST_ERROR;
			return $this->LAST_ERROR;
		}
	}

	if($XML_SECTION_PROPERTIES)
	{
		$this->ImportSectionProperties($XML_SECTION_PROPERTIES, $IBLOCK_ID, $arSection["ID"]);
	}

	//Clear seo cache
	if(isset($bChanged) && $bChanged)
	{
		$ipropValues = new BitrixIblockInheritedPropertySectionValues($this->next_step["IBLOCK_ID"], $arSection["ID"]);
		$ipropValues->clearValues();
	}

	if($arSection["ID"])
	{
		$this->_xml_file->Add(array("PARENT_ID" => 0, "LEFT_MARGIN" => $arSection["ID"]));
	}

	if($XML_SECTIONS_PARENT)
	{
		$rs = $this->_xml_file->GetList(
			array("ID" => "asc"),
			array("PARENT_ID" => $XML_SECTIONS_PARENT),
			array("ID")
		);
		while($ar = $rs->Fetch())
		{
			$result = $this->ImportSection($ar["ID"], $IBLOCK_ID, $arSection["ID"]);
			if($result !== true)
				return $result;
		}
	}

	return true;
}