• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/classes/general/hrxml.php
  • Класс: CUserHRXMLImport
  • Вызов: CUserHRXMLImport::ImportSyncIndicativeData
private function ImportSyncIndicativeData($xml)
{
	$arUsers = array();
	define('INTR_SKIP_EVENT_ADD', 'Y');
	$arSections = array();
	$obSection = &$this->__section;
	$this->STRUCTURE_ROOT = $this->GetStructureRoot();
	foreach ($xml->IndicativeData as $data)
	{
		if (!isset ($data->IndicativePersonDossier))
			continue;
		$data = $data->IndicativePersonDossier;
		$terminated = false;
		if (isset($data->IndicativeEmployment->EmploymentLifecycle))
		{
			$employmentLifecycle = $data->IndicativeEmployment->EmploymentLifecycle;
			if (isset($employmentLifecycle->Termination->LastWorkedDate))
			{
				if ($employmentLifecycle->Termination->LastWorkedDate < date('Y-m-d'))
					$terminated = true;
			}
		}
		$tempUserData = array();
		$PersonID = false;
		if (isset ($data->IndicativePerson->PersonID))
			$PersonID = $this->GetGUID($data->IndicativePerson->PersonID);

		$EmployeeID = false;
		if (isset ($data->IndicativeEmployee->EmployeeID))
			$EmployeeID = $this->GetGUID($data->IndicativeEmployee->EmployeeID);

		if (isset($data->IndicativePerson->PersonName))
		{
			$arName = $this->GetCurrentName($data->IndicativePerson);
			if (isset($arName->LegalName))
				$tempUserData['NAME'] = (string)$arName->LegalName;
			if (isset($arName->FamilyName))
				$tempUserData['LAST_NAME'] = (string)$arName->FamilyName;
			if (isset($arName->MiddleName))
				$tempUserData['SECOND_NAME'] = (string)$arName->MiddleName;
		}

		if (isset($data->IndicativePerson->GenderCode))
			$tempUserData['PERSONAL_GENDER'] = mb_substr((string)$data->IndicativePerson->GenderCode, 0, 1);

		if (isset($data->IndicativePerson->BirthDate))
		{
			$tempUserData['PERSONAL_BIRTHDAY'] = explode('-', (string) $data->IndicativePerson->BirthDate);
			$tempUserData['PERSONAL_BIRTHDAY'] = $tempUserData['PERSONAL_BIRTHDAY'][2].'.'.$tempUserData['PERSONAL_BIRTHDAY'][1].'.'.$tempUserData['PERSONAL_BIRTHDAY'][0];
		}

		if (isset($data->IndicativePerson->Communication))
		{
			foreach ($data->IndicativePerson->Communication as $key => $value)
			{
				if ($value->ChannelCode == 'Email')
						$tempUserData['EMAIL'] = (string) $value->URI;
			}
		}

		if (isset($data->IndicativeDeployment->Job))
			$tempUserData['WORK_POSITION'] = (string)$data->IndicativeDeployment->Job->JobTitle;
		else
			$tempUserData['WORK_POSITION'] = '';

		$tempUserData = $this->ConvertCharset($tempUserData);
		$last = $this->STRUCTURE_ROOT;
		$lastXML_ID = '';

		if (isset($data->IndicativeDeployment->DeploymentOrganization))
		{
			if (isset($data->IndicativeDeployment->DeploymentOrganization->OrganizationIdentifiers))
			{
				$departments = $data->IndicativeDeployment->DeploymentOrganization->OrganizationIdentifiers;
				foreach ($departments as $department)
				{
					$arItem = array(
						'XML_ID' => $this->GetGUID($department->OrganizationID),
						'NAME' => (string)$department->OrganizationName,
						'IBLOCK_ID' => $this->DEPARTMENTS_IBLOCK_ID,
						'IBLOCK_SECTION_ID' => $last,
					);
					$arItem = $this->ConvertCharset($arItem);
					if (!array_key_exists($arItem['XML_ID'], $arSections))
					{
						$res = CIBlockSection::GetList(
							array(),
							array(
								'IBLOCK_ID' => $this->DEPARTMENTS_IBLOCK_ID,
								'XML_ID' => $arItem['XML_ID'],
								'SECTION_ID' => $last
							),
							false,
							array('ID')
						);
						if ($arElement = $res->Fetch())
						{
							$secID = $obSection->Update($arElement['ID'], $arItem);
							if ($secID > 0)
								$arSections[$arItem['XML_ID']] = $arElement['ID'];
							else
								$GLOBALS['APPLICATION']->ThrowException($obSection->LAST_ERROR);
						}
						else
						{
							$arItem['ACTIVE'] = 'Y';
							$secID = $obSection->Add($arItem);
							if ($secID > 0)
								$arSections[$arItem['XML_ID']] = $secID;
							else
								$GLOBALS['APPLICATION']->ThrowException($obSection->LAST_ERROR);
						}
					}
					$last = $arSections[$arItem['XML_ID']];
					$lastXML_ID = $arItem['XML_ID'];
				}
			}
		}


		if (array_key_exists($PersonID, $this->_users))
		{
			$arUser = $this->_users[$PersonID];
		}
		else
		{
			$rsUser = CUser::GetList(
				"ID",
				"desc",
				array('XML_ID' => $PersonID),
				array('FIELDS' => array('ID', 'ACTIVE'), 'SELECT' => array('UF_DEPARTMENT', 'UF_WORK_BINDING'))
			);
			if ($arUser = $rsUser->fetch())
			{
				$arUser['UF_WORK_BINDING'] = unserialize($arUser['UF_WORK_BINDING'], ["allowed_classes" => false]);
			}
			else
			{
				$arUser = array(
					'ACTIVE' => 'Y',
					'UF_1C' => 'Y',
					'XML_ID' => $PersonID,
					'UF_WORK_BINDING' => array(
						'PERSON_ID' => $PersonID,
						'DEPARTMENTS' => array(
							$EmployeeID => array(
								'DEPARTMENT' => $lastXML_ID,
								'JOB' => base64_encode($tempUserData['WORK_POSITION']),
							),
						),
					),
				);
			}
		}
		if ($terminated)
		{
			if (array_key_exists($EmployeeID, $arUser['UF_WORK_BINDING']['DEPARTMENTS']))
				unset ($arUser['UF_WORK_BINDING']['DEPARTMENTS'][$EmployeeID]);
		}
		else
		{
			$arUser['UF_WORK_BINDING']['DEPARTMENTS'][$EmployeeID] = array(
				'DEPARTMENT' => $lastXML_ID,
				'JOB' => base64_encode($tempUserData['WORK_POSITION']),
			);
		}
		if (isset($tempUserData['PERSONAL_BIRTHDAY']))
			$arUser['PERSONAL_BIRTHDAY'] = $tempUserData['PERSONAL_BIRTHDAY'];
		if (isset($tempUserData['PERSONAL_GENDER']))
			$arUser['PERSONAL_GENDER'] = $tempUserData['PERSONAL_GENDER'];
		if (isset($tempUserData['NAME']))
			$arUser['NAME'] = $tempUserData['NAME'];
		if (isset($tempUserData['LAST_NAME']))
			$arUser['LAST_NAME'] = $tempUserData['LAST_NAME'];
		if (isset($tempUserData['SECOND_NAME']))
			$arUser['SECOND_NAME'] = $tempUserData['SECOND_NAME'];
		if (isset($tempUserData['EMAIL']))
			$arUser['EMAIL'] = $tempUserData['EMAIL'];

		$arUser['UF_DEPARTMENT'] = array();
		$arUser['WORK_POSITION'] = array();
		foreach ($arUser['UF_WORK_BINDING']['DEPARTMENTS'] as $value)
		{
			if (empty($value['DEPARTMENT']) || $value['DEPARTMENT'] == $lastXML_ID)
			{
				if (!in_array($last, $arUser['UF_DEPARTMENT']))
					$arUser['UF_DEPARTMENT'][] = $last;
			}
			elseif (array_key_exists($value['DEPARTMENT'], $arSections))
			{
				if (!in_array($arSections[$value['DEPARTMENT']], $arUser['UF_DEPARTMENT']))
					$arUser['UF_DEPARTMENT'][] = $arSections[$value['DEPARTMENT']];
			}
			else
			{
				$res = CIBlockSection::GetList(
					array(),
					array('IBLOCK_ID' => $this->DEPARTMENTS_IBLOCK_ID, 'XML_ID' => $value['DEPARTMENT']),
					false,
					array('ID')
				);
				if ($arSection = $res->Fetch())
				{
					if (!in_array($arSection['ID'], $arUser['UF_DEPARTMENT']))
						$arUser['UF_DEPARTMENT'] = $arSection['ID'];
					$arSections[$value['DEPARTMENT']] = $arSection['ID'];
				}
			}
			$value['JOB'] = base64_decode($value['JOB']);
			if (!empty($value['JOB']) && !in_array($value['JOB'], $arUser['WORK_POSITION']))
				$arUser['WORK_POSITION'][] = $value['JOB'];
		}
		$arUser['WORK_POSITION'] = implode(' / ', $arUser['WORK_POSITION']);

		if ($terminated && empty($arUser['UF_DEPARTMENT']))
			$arUser['ACTIVE'] = 'N';

		$this->_users[$PersonID] = $arUser;
		$arUser['UF_WORK_BINDING'] = serialize($arUser['UF_WORK_BINDING']);
		$arUsers[] = $arUser;
	}

	foreach ($arUsers as $user)
	{
		$counter=array();
		$result = $this->ImportUser($user, $counter);
	}

	return true;
}