Esempio n. 1
0
 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 (strlen($arField["XML_ID"]) <= 0) {
                 $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 (strlen($arS["VALUE"]) > 0) {
                 $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 (strlen($arS["VALUE"]) > 0) {
                 $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 (strlen($id) > 0 && strlen($template) > 0) {
                     $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)) {
             $arSection["CODE"] = CUtil::translit($arSection["NAME"], LANGUAGE_ID, $this->translit_on_update);
             //Check if name was not changed in a way to update CODE
             if (substr($arDBSection["CODE"], 0, strlen($arSection["CODE"])) === $arSection["CODE"]) {
                 unset($arSection["CODE"]);
             } else {
                 $arSection["CODE"] = $this->CheckSectionCode($IBLOCK_ID, $arSection["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;
                 }
             }
             $res = $obSection->Update($arDBSection["ID"], $arSection);
             if (!$res) {
                 $this->LAST_ERROR = $obSection->LAST_ERROR;
                 return $this->LAST_ERROR;
             }
         } else {
             $DB->Query("UPDATE b_iblock_section SET TIMESTAMP_X = " . $DB->CurrentTimeFunction() . " WHERE ID=" . $arDBSection["ID"]);
         }
         $arSection["ID"] = $arDBSection["ID"];
     } else {
         if (!array_key_exists("CODE", $arSection) && is_array($this->translit_on_add)) {
             $arSection["CODE"] = $this->CheckSectionCode($IBLOCK_ID, CUtil::translit($arSection["NAME"], LANGUAGE_ID, $this->translit_on_add));
         }
         $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"]);
     }
     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;
 }
Esempio n. 2
0
	function ImportSection($xml_tree_id, $IBLOCK_ID, $parent_section_id)
	{
		/** @var CUserTypeManager $USER_FIELD_MANAGER */
		global $USER_FIELD_MANAGER;

		static $arUserFields;
		if($parent_section_id === false)
		{
			$arUserFields = array();
			foreach($USER_FIELD_MANAGER->GetUserFields("IBLOCK_".$IBLOCK_ID."_SECTION") as $FIELD_ID => $arField)
			{
				if(strlen($arField["XML_ID"]) <= 0)
					$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"]==GetMessage("IBLOCK_XML2_ID"))
				$arSection["XML_ID"] = $arS["VALUE"];
			elseif($arS["NAME"]==GetMessage("IBLOCK_XML2_NAME"))
				$arSection["NAME"] = $arS["VALUE"];
			elseif($arS["NAME"]==GetMessage("IBLOCK_XML2_DESCRIPTION"))
			{
				$arSection["DESCRIPTION"] = $arS["VALUE"];
				$arSection["DESCRIPTION_TYPE"] = "html";
			}
			elseif($arS["NAME"]==GetMessage("IBLOCK_XML2_GROUPS"))
				$XML_SECTIONS_PARENT = $arS["ID"];
			elseif($arS["NAME"]==GetMessage("IBLOCK_XML2_PROPERTIES_VALUES"))
				$XML_PROPERTIES_PARENT = $arS["ID"];
			elseif($arS["NAME"]==GetMessage("IBLOCK_XML2_BX_SORT"))
				$arSection["SORT"] = intval($arS["VALUE"]);
			elseif($arS["NAME"]==GetMessage("IBLOCK_XML2_BX_CODE"))
				$arSection["CODE"] = $arS["VALUE"];
			elseif($arS["NAME"] == GetMessage("IBLOCK_XML2_BX_PICTURE"))
			{
				if(strlen($arS["VALUE"]) > 0)
					$arSection["PICTURE"] = $this->MakeFileArray($arS["VALUE"]);
				else
					$arSection["PICTURE"] = $this->MakeFileArray($this->_xml_file->GetAllChildrenArray($arS["ID"]));
			}
			elseif($arS["NAME"] == GetMessage("IBLOCK_XML2_BX_DETAIL_PICTURE"))
			{
				if(strlen($arS["VALUE"]) > 0)
					$arSection["DETAIL_PICTURE"] = $this->MakeFileArray($arS["VALUE"]);
				else
					$arSection["DETAIL_PICTURE"] = $this->MakeFileArray($this->_xml_file->GetAllChildrenArray($arS["ID"]));
			}
			elseif($arS["NAME"] == GetMessage("IBLOCK_XML2_BX_ACTIVE"))
				$arSection["ACTIVE"] = ($arS["VALUE"]=="true") || intval($arS["VALUE"])? "Y": "N";
			elseif($arS["NAME"] == GetMessage("IBLOCK_XML2_SECTION_PROPERTIES"))
				$XML_SECTION_PROPERTIES = $arS["ID"];
			elseif($arS["NAME"] == GetMessage("IBLOCK_XML2_STATUS"))
				$deletedStatus = $arS["VALUE"] === GetMessage("IBLOCK_XML2_DELETED");
		}

		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(GetMessage("IBLOCK_XML2_ID"), $arXMLProp)
					&& array_key_exists($arXMLProp[GetMessage("IBLOCK_XML2_ID")], $arUserFields)
				)
				{
					$FIELD_NAME = $arUserFields[$arXMLProp[GetMessage("IBLOCK_XML2_ID")]]["FIELD_NAME"];
					$MULTIPLE = $arUserFields[$arXMLProp[GetMessage("IBLOCK_XML2_ID")]]["MULTIPLE"];
					$IS_FILE = $arUserFields[$arXMLProp[GetMessage("IBLOCK_XML2_ID")]]["USER_TYPE"]["BASE_TYPE"] === "file";

					unset($arXMLProp[GetMessage("IBLOCK_XML2_ID")]);
					$arProp = array();
					$i = 0;
					foreach($arXMLProp as $value)
					{
						if($IS_FILE)
							$arProp["n".($i++)] = $this->MakeFileArray($value);
						else
							$arProp["n".($i++)] = $value;
					}

					if($MULTIPLE == "N")
						$arSection[$FIELD_NAME] = array_pop($arProp);
					else
						$arSection[$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))
			{
				$arSection["CODE"] = CUtil::translit($arSection["NAME"], LANGUAGE_ID, $this->translit_on_update);
				//Check if name was not changed in a way to update CODE
				if(substr($arDBSection["CODE"], 0, strlen($arSection["CODE"])) === $arSection["CODE"])
					unset($arSection["CODE"]);
				else
					$arSection["CODE"] = $this->CheckSectionCode($IBLOCK_ID, $arSection["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;
					}
				}

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

			$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"]);
		}

		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;
	}