• Модуль: webdav
  • Путь к файлу: ~/bitrix/modules/webdav/classes/iblock.php
  • Класс: CWebDavIblock
  • Вызов: CWebDavIblock::GetObject
function GetObject($options = array(), $replace_symbols = false, $force = false)
{
	$options = (is_array($options) ? $options : array());
	if (!isset($options['check_permissions']))
		$options['check_permissions'] = true;

	if(array_key_exists("path", $options))
	{
		$path = $this->_udecode($options["path"]);
	}
	else
	{
		if(array_key_exists('section_id', $options))
		{
			$path = 'section' . $options['section_id'];
		}
		elseif(array_key_exists('element_id', $options))
		{
			$path = 'element' . $options['element_id'];
		}
		else
		{
			$path = $this->_path;
		}
	}

	$arSelectFields = array("ID", "NAME", "IBLOCK_ID", "IBLOCK_SECTION_ID", "PREVIEW_TEXT",
		"CREATED_BY", 'MODIFIED_BY', 'DATE_CREATE', "TIMESTAMP_X", "PROPERTY_" . static::PROPERTY_VERSION, "PROPERTY_".$this->file_prop, "PROPERTY_WEBDAV_INFO",
		"WF_NEW", "WF_PARENT_ELEMENT_ID", "WF_STATUS_ID",
		"IBLOCK_CODE", "BP_PUBLISHED");

	$contextCache = '';
	if(!empty($this->attributes['user_id']))
	{
		$contextCache = $this->attributes['user_id'] . (empty($options['section_id'])? '' : 'section' . $options['section_id']);
	}
	elseif(!empty($this->attributes['group_id']))
	{
		$contextCache = $this->attributes['group_id'] . (empty($options['section_id'])? '' : 'section' . $options['section_id']);
	}

	$id = $this->_get_element_cacheID($path, $replace_symbols).($options['check_permissions']?'Y':'N').'_'.$contextCache;

	//todo!!!!!
	if (!$force && isset($this->CACHE[__METHOD__][$id]))
	{
		$arParams = array_merge($this->arParams, $this->CACHE[__METHOD__][$id]);
		return $arParams;
	}

	/**
	 * Analize path part "folder#section_id#/#element_id#"
	 */
	$arKeys = array();
	if (!empty($this->short_path_template) &&
		preg_match_all("/(?<=#)([a-z_]+)(?=#)/is", mb_strtolower($this->short_path_template), $arKeys))
	{
		$arKeys = (is_array($arKeys) ? $arKeys[0] : false);
		if (!empty($arKeys) && is_array($arKeys))
		{
			$pageTemplateReg = "'^".preg_replace("'#[^#]+?#'", "([^/]*)", $this->short_path_template)."'";
			$arValues = array();
			if (preg_match($pageTemplateReg, $path, $arValues))
			{
				array_shift($arValues);
				$arValues = @array_combine($arKeys, $arValues);
				if (is_array($arValues))
				{
					if (!empty($arValues["element_id"]) && intval($arValues["element_id"]) > 0)
						$options["element_id"] = (int)$arValues["element_id"];
					elseif (!empty($arValues["section_id"]) && intval($arValues["section_id"]) > 0)
						$options["section_id"] = (int)$arValues["section_id"];
				}
			}
		}
	}

	$params = array(
		"item_id" => (is_array($this->arRootSection) ? $this->arRootSection["ID"] : 0),
		"not_found" => false,
		"is_dir" => false,
		"is_file" => false,
		"parent_id" => false,
		"basename" => "");

	$arFilter = array("IBLOCK_ID" => $this->IBLOCK_ID);
	if($this->arRootSection && is_array($this->arRootSection))
	{
		$arFilter["RIGHT_MARGIN"] = intval($this->arRootSection["RIGHT_MARGIN"]) - 1;
		$arFilter["LEFT_MARGIN"] = intval($this->arRootSection["LEFT_MARGIN"]) + 1;
	}
	if ($options["show_history"] == "Y")
		$arFilter["SHOW_HISTORY"] = "Y";
	$arFilter['CHECK_PERMISSIONS'] = ($options['check_permissions'] ? 'Y' : 'N');

	$arPath = explode("/", trim($path, "/"));
	$arNewPath = array();
	foreach ($arPath as $p)
	{
		if (trim($p) <> '')
			$arNewPath[] = $p;
	}
	$arPath = $arNewPath;

	$arElement = array();

	foreach ($arPath as $pathID => $pathName)
	{
		foreach($this->meta_names as $metaName => $metaArr)
		{
			if ($pathName == $metaArr["alias"])
				$arPath[$pathID] = $metaArr["name"];
		}
	}

	if (is_set($options, "section_id"))
	{
		if (empty($options["section_id"]) || ($this->arRootSection && $options["section_id"] == $this->arRootSection["ID"]))
		{
			$params["is_dir"] = true;
			$params["dir_array"] = array("ID" => 0, "DEPTH_LEVEL" => 0, "LEFT_MARGIN" => 0, "RIGHT_MARGIN" => $this->INT_MAX);
		}
		else
		{
			$arFilter["ID"] = $options["section_id"];
			//todo check if section_id in symlink section
			if(1)
			{
				if($this->arParams['is_dir'] && !empty($this->arParams['dir_array']['IBLOCK_ID']))
				{
					$arFilter['IBLOCK_ID'] = $this->arParams['dir_array']['IBLOCK_ID'];
				}
				$iblockId = CWebDavSymlinkHelper::getIblockIdForSectionId($options["section_id"]);
				if($iblockId)
				{
					$arFilter['IBLOCK_ID'] = $iblockId;
				}
				unset($arFilter["RIGHT_MARGIN"], $arFilter["LEFT_MARGIN"]);
			}
			$rs = CIBlockSection::GetList(array(), $arFilter, false, self::getUFNamesForSectionLink());
			if ($arr = $rs->Fetch())
			{
				$params["item_id"] = intval($arr["ID"]);
				$params["parent_id"] = intval($arr["IBLOCK_SECTION_ID"]);
				$params["is_dir"] = true;
				$params["dir_array"] = $arr;
				$params["basename"] = $arr['NAME'];
			}
			else
			{
				$params["not_found"] = true;
			}
		}
	}
	elseif (is_set($options, "element_id"))
	{
		$arFilter = array(
			"SHOW_NEW" => "Y",
			"SHOW_HISTORY" => "Y",
			"ID" => $options["element_id"],
			"IBLOCK_ID" => $this->IBLOCK_ID,
			"CHECK_PERMISSIONS" => ($options['check_permissions'] ? 'Y' : 'N')
		);

		list($contextType, $contextEntityId) = $this->getContextData();
		if(CWebDavSymlinkHelper::isLinkElement($contextType, $contextEntityId, $options["element_id"]))
		{
			$parentSectionData = CWebDavSymlinkHelper::getLinkDataOfElement($contextType, $contextEntityId, $options["element_id"]);
			if(!empty($parentSectionData[self::UF_LINK_IBLOCK_ID]))
			{
				$arFilter['IBLOCK_ID'] = $parentSectionData[self::UF_LINK_IBLOCK_ID];
			}
		}

		$db_res = CIBlockElement::GetList(
			null,
			$arFilter,
			false,
			false,
			$arSelectFields
		);
		if (!($db_res && $arElement = $db_res->Fetch()))
		{
			$params["not_found"] = true;
		}
		else
		{
			$params["is_file"] = true;
			$params["parent_id"] = intval($arElement["IBLOCK_SECTION_ID"]);
			$params["item_id"] = $options["element_id"];
			$params["basename"] = $arElement['NAME'];
			if ($this->e_rights)
			{
				$arElement['E_RIGHTS'] = $this->GetPermission('ELEMENT', $arElement['ID']);
			}
			if ($this->arRootSection && $this->arRootSection["ID"] != $arElement["IBLOCK_SECTION_ID"])
			{
				$arFilter["ID"] = $arElement["IBLOCK_SECTION_ID"];
				$rs = CIBlockSection::GetList(array(), $arFilter, false, array('ID'));
				if ($arr = $rs->Fetch())
				{
					$params["item_id"] = intval($arr["ID"]);
				}
				else
				{
					$params["not_found"] = true;
				}
			}
		}
	}
	elseif (count($arPath) > 0)
	{
		$basename = array_pop($arPath);
		$params['basename'] = ($replace_symbols == true ? $this->CorrectName($basename) : $basename);

		foreach ($arPath as $dir)
		{
			$arFilter["SECTION_ID"] = $params["item_id"];
			$arFilter["=NAME"] = $dir;

			$rs = CIBlockSection::GetList(array(), $arFilter, false, array_merge(array('ID'), self::getUFNamesForSectionLink()));
			if ($rs && $arr = $rs->Fetch())
			{
				$params["item_id"] = intval($arr["ID"]);
				if(!empty($arr[self::UF_LINK_SECTION_ID]))
				{
					$params["item_id"] = intval($arr[self::UF_LINK_SECTION_ID]);
					$arFilter['IBLOCK_ID'] = $arr[self::UF_LINK_IBLOCK_ID];
					unset($arFilter["RIGHT_MARGIN"], $arFilter["LEFT_MARGIN"]);
				}
			}
			else
			{
				$params["not_found"] = true;
				break;
			}
		}

		if ($params["not_found"] == false)
		{
			$params["parent_id"] = intval($params["item_id"]);

			$arFilter["SECTION_ID"] =  $params["item_id"];
			$arFilter["=NAME"] = $params["basename"];

			$rs = CIBlockSection::GetList(array(), $arFilter, false, self::getUFNamesForSectionLink());
			if ($arr = $rs->Fetch())
			{
				$params["item_id"] = intval($arr["ID"]);
				$params["is_dir"] = true;
				$params["dir_array"] = $arr;
			}
			else
			{
				$arOrder = array("ID" => "ASC");
				$arFilter = array(
					"IBLOCK_ID" => empty($arFilter['IBLOCK_ID'])? $this->IBLOCK_ID : $arFilter['IBLOCK_ID'], //set iblockId to iblockId in prev.lookup (symlink logic)
					"SECTION_ID" =>  $params["item_id"],
					"CHECK_PERMISSIONS" => ($options['check_permissions'] ? 'Y' : 'N'),
					"=NAME" => $params["basename"],
					"SHOW_HISTORY" => "N",
					"SHOW_NEW" => "Y");

				if ($this->workflow == 'workflow')
				{
					$arOrderWF = array("ID" => "DESC");
					$arFilterWF = $arFilter;
					$arFilterWF["SHOW_HISTORY"] = "Y";
					$arFilterWF[">WF_PARENT_ELEMENT_ID"] = 0;

					$db_res = CIBlockElement::GetList($arOrderWF, $arFilterWF, false, false, $arSelectFields);
					if ($db_res && $res = $db_res->Fetch())
					{
						$arBuff = array();
						do
						{
							$res["LAST_ID"] = CIBlockElement::WF_GetLast($res["ID"]);
							if (!in_array($res["LAST_ID"], $arBuff))
							{
								$db_res = CIBlockElement::GetList($arOrderWF, array("ID" => $res["LAST_ID"], "SHOW_HISTORY" => "Y"), false, false, $arSelectFields);
								if ($db_res && $res = $db_res->Fetch()):
									if ($res["NAME"] == $params["basename"] && (intval($res["IBLOCK_SECTION_ID"]) == intval($params["item_id"]))):
										$original = $arElement = $res;
										if ($res["WF_PARENT_ELEMENT_ID"] > 0)
										{
											$arFilterWF = array("ID" => $arElement["WF_PARENT_ELEMENT_ID"], "SHOW_HISTORY" => "Y");
											$db_res = CIBlockElement::GetList($arOrderWF, $arFilterWF, false, false, $arSelectFields);
											if ($db_res)
												$original = $db_res->Fetch();
										}
										$arElement["REAL_ELEMENT_ID"] = $arElement["ID"];
										$arElement["ID"] = $original["ID"];
										$arElement["PROPERTY_WEBDAV_INFO_VALUE"] = $original["PROPERTY_WEBDAV_INFO_VALUE"];
										$arElement["PROPERTY_WEBDAV_INFO_VALUE_ID"] = $original["PROPERTY_WEBDAV_INFO_VALUE_ID"];
										$arElement["original"] = $original;
										break;
									endif;
								endif;
								$arBuff[] = $res["LAST_ID"];
							}
						} while ($res = $db_res->Fetch());
					}
				}

				if (empty($arElement))
				{
					$db_res = CIBlockElement::GetList($arOrder, $arFilter, false, false, $arSelectFields);
					if ($db_res && $res = $db_res->Fetch()):
						$arElement = $res;
					endif;
				}

				if (!empty($arElement))
				{
					$params["is_file"] = true;
				}
				else
				{
					$params["not_found"] = true;
				}
			}
		}
	}
	else
	{
		$params["is_dir"] = true;
	}

	if (!$params["not_found"] && !empty($arElement) && $params["is_file"])
	{
		$params["element_id"] = $params["item_id"] = $arElement["ID"];
		$params["original"] = $arElement["original"];
		unset($arElement["original"]);
		$params["element_array"] = $arElement;
		$params["element_name"] = $arElement["NAME"];
		$params["file_name"] = $arElement["NAME"];
		$params["file_extention"] = mb_strtolower(strrchr($arElement["NAME"], '.'));

		$arFile = CFile::GetFileArray($arElement["PROPERTY_".$this->file_prop."_VALUE"]);

		$params["file_id"] = $arFile["ID"];
		$params["file_mimetype"] = $arFile["CONTENT_TYPE"];
		$params["file_size"] = $arFile["FILE_SIZE"];
		$params["fullpath"] = $arFile["SRC"];
		$params["file_array"] = $arFile;
	}
	elseif ($params["not_found"])
	{
		$params["item_id"] = false;
	}

	$this->CACHE[__METHOD__][$id] = $params;
	return $params;
}