• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/classes/general/hrxml.php
  • Класс: CUserHRXMLImport
  • Вызов: CUserHRXMLImport::ImportOrganizationChart
private function ImportOrganizationChart($xml)
{
	$arSections = array();
	$this->STRUCTURE_ROOT = $this->GetStructureRoot();
	foreach ($xml->OrganizationChart as $value)
	{
		$arUnitFields = array(
			'IBLOCK_ID' => $this->DEPARTMENTS_IBLOCK_ID,
			'XML_ID' => (string) $value->OrganizationUnit->OrganizationUnitID,
		);
		if (!array_key_exists($arUnitFields['XML_ID'], $arSections))
		{
			$rsUnit = CIBlockSection::GetList(array(), $arUnitFields, false, array('ID'));
			if ($arUnit = $rsUnit->GetNext())
				$arSections[$arUnitFields['XML_ID']] = $arUnit['ID'];
		}
		$arUnitFields['NAME'] = (string) $value->OrganizationUnit->OrganizationUnitName;
		if (isset($value->OrganizationUnit->ParentOrganizationUnit))
		{
			$arFields = array(
				'IBLOCK_ID' => $this->DEPARTMENTS_IBLOCK_ID,
				'XML_ID' => (string) $value->OrganizationUnit->ParentOrganizationUnit->OrganizationUnitID
			);
			if (!array_key_exists($arFields['XML_ID'], $arSections) || empty($arSections[$arFields['XML_ID']]))
			{
				$rsParent = CIBlockSection::GetList(array(), $arFields, false, array('ID'));
				if ($arParent = $rsParent->GetNext())
				{
					$arSections[$arFields['XML_ID']] = $arParent['ID'];
				}
				else
				{
					$sec = new CIBlockSection;
					$arFields['NAME'] = (string) $value->OrganizationUnit->ParentOrganizationUnit->OrganizationName;
					$arFields['IBLOCK_SECTION_ID'] = $this->STRUCTURE_ROOT;
					$arFields = $this->ConvertCharset($arFields);
					$id = $sec->Add($arFields);
					if ($id)
						$arSections[$arFields['XML_ID']] = $id;
				}
			}
			$arUnitFields['IBLOCK_SECTION_ID'] = $arSections[$arFields['XML_ID']];
		}
		else
		{
			$arUnitFields['IBLOCK_SECTION_ID'] = $this->STRUCTURE_ROOT;
		}

		$arUnitFields = $this->ConvertCharset($arUnitFields);
		$sec = new CIBlockSection;
		if (array_key_exists($arUnitFields['XML_ID'], $arSections))
		{
			$sec->Update($arSections[$arUnitFields['XML_ID']], $arUnitFields);
			if (!empty($sec->LAST_ERROR))
			{
				$this->warnings[] = GetMessage('IBLOCK_HR_NOT_UPDATED_DEPARTMENT').' "'
					.$arUnitFields['NAME'].'"('.$arFields['XML_ID'].')'."rn".$sec->LAST_ERROR;
			}
		}
		else
		{
			$id = $sec->Add($arUnitFields);
			if (!empty($sec->LAST_ERROR))
			{
				$this->warnings[] = GetMessage('IBLOCK_HR_NOT_ADD_DEPARTMENT').' "'
					.$arUnitFields['NAME'].'"('.$arFields['XML_ID'].')'."rn".$sec->LAST_ERROR;
			}
			else
			{
				$arSections[$arUnitFields['XML_ID']] = $id;
			}
		}
	}
}