CAllCrmActivity::PrepareClientInfos

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmActivity
  4. PrepareClientInfos
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_activity.php
  • Класс: \CAllCrmActivity
  • Вызов: CAllCrmActivity::PrepareClientInfos
static function PrepareClientInfos($IDs, $arOptions = null)
{
	$nameTemplate = is_array($arOptions) && isset($arOptions['NAME_TEMPLATE'])
		&& is_string($arOptions['NAME_TEMPLATE']) && $arOptions['NAME_TEMPLATE'] !== ''
		? $arOptions['NAME_TEMPLATE'] : \Bitrix\Crm\Format\PersonNameFormatter::getFormat();

	$result = array();
	if(!is_array(self::$CLIENT_INFOS) || empty(self::$CLIENT_INFOS))
	{
		$selectIDs = $IDs;
	}
	else
	{
		$selectIDs = array();
		foreach($IDs as $ID)
		{
			if(!isset(self::$CLIENT_INFOS[$ID]))
			{
				$selectIDs[] = $ID;
			}
			else
			{
				$info = self::$CLIENT_INFOS[$ID];
				if(isset($info['NAME_DATA']) && $nameTemplate !== $info['NAME_DATA']['NAME_TEMPLATE'])
				{
					$info['NAME_DATA']['NAME_TEMPLATE'] = $nameTemplate;
					$ownerTypeID = isset($info['ENTITY_TYPE_ID']) ? $info['ENTITY_TYPE_ID'] : CCrmOwnerType::Undefined;
					if($ownerTypeID === CCrmOwnerType::Lead)
					{
						$info['TITLE'] = CCrmLead::PrepareFormattedName(
							array(
								'HONORIFIC' => isset($info['NAME_DATA']['HONORIFIC']) ? $info['NAME_DATA']['HONORIFIC'] : '',
								'NAME' => isset($info['NAME_DATA']['NAME']) ? $info['NAME_DATA']['NAME'] : '',
								'LAST_NAME' => isset($info['NAME_DATA']['LAST_NAME']) ? $info['NAME_DATA']['LAST_NAME'] : '',
								'SECOND_NAME' => isset($info['NAME_DATA']['SECOND_NAME']) ? $info['NAME_DATA']['SECOND_NAME'] : ''
							),
							$nameTemplate
						);
					}
					else//if($ownerTypeID === CCrmOwnerType::Contact)
					{
						$info['TITLE'] = CCrmContact::PrepareFormattedName(
							array(
								'HONORIFIC' => isset($info['NAME_DATA']['HONORIFIC']) ? $info['NAME_DATA']['HONORIFIC'] : '',
								'NAME' => isset($info['NAME_DATA']['NAME']) ? $info['NAME_DATA']['NAME'] : '',
								'LAST_NAME' => isset($info['NAME_DATA']['LAST_NAME']) ? $info['NAME_DATA']['LAST_NAME'] : '',
								'SECOND_NAME' => isset($info['NAME_DATA']['SECOND_NAME']) ? $info['NAME_DATA']['SECOND_NAME'] : ''
							),
							$nameTemplate
						);
					}
				}
				$result[$ID] = $info;
			}
		}
	}

	if(!empty($selectIDs))
	{
		global $DB;
		$condition = implode(',', $selectIDs);
		$dbResult = $DB->Query("SELECT A.ID ACTIVITY_ID, A.OWNER_TYPE_ID, A.OWNER_ID, C3.ENTITY_ID, C3.ENTITY_TYPE_ID, C3.ENTITY_SETTINGS
			FROM b_crm_act A LEFT OUTER JOIN(
				SELECT C2.ID, C2.ACTIVITY_ID, C2.ENTITY_ID, C2.ENTITY_TYPE_ID, C2.ENTITY_SETTINGS
					FROM (SELECT ACTIVITY_ID, MIN(ID) ID FROM b_crm_act_comm WHERE ACTIVITY_ID IN({$condition}) GROUP BY ACTIVITY_ID) C1
						INNER JOIN b_crm_act_comm C2 ON C1.ID = C2.ID) C3 ON C3.ACTIVITY_ID = A.ID
			WHERE A.ID IN({$condition})");

		if(is_object($dbResult))
		{
			if(self::$CLIENT_INFOS === null)
			{
				self::$CLIENT_INFOS = array();
			}

			while($comm = $dbResult->Fetch())
			{
				$ID = intval($comm['ACTIVITY_ID']);
				$entityID = isset($comm['ENTITY_ID']) ? intval($comm['ENTITY_ID']) : 0;
				$entityTypeID = isset($comm['ENTITY_TYPE_ID']) ? intval($comm['ENTITY_TYPE_ID']) : 0;

				$isExists = ($entityID > 0 && $entityTypeID > 0);
				if(!$isExists)
				{
					$entityID = isset($comm['OWNER_ID']) ? intval($comm['OWNER_ID']) : 0;
					$entityTypeID = isset($comm['OWNER_TYPE_ID']) ? intval($comm['OWNER_TYPE_ID']) : 0;
				}

				if($entityID <= 0 || $entityTypeID <= 0 || $entityTypeID === CCrmOwnerType::Deal)
				{
					continue;
				}

				$info = array(
					'ENTITY_ID' => $entityID,
					'ENTITY_TYPE_ID' => $entityTypeID,
					'TITLE' => '',
					'SHOW_URL' => CCrmOwnerType::GetEntityShowPath($entityTypeID, $entityID, false)
				);

				$settings = isset($comm['ENTITY_SETTINGS']) ? unserialize($comm['ENTITY_SETTINGS'], ['allowed_classes' => false]) : array();
				if(empty($settings))
				{
					$customComm = array('ENTITY_ID' => $entityID, 'ENTITY_TYPE_ID' => $entityTypeID);
					self::PrepareCommunicationSettings($customComm);
					if(isset($customComm['ENTITY_SETTINGS']))
					{
						$settings = $customComm['ENTITY_SETTINGS'];
						if($isExists)
						{
							self::SaveEntityCommunicationSettings($entityTypeID, $entityID, $settings);
						}
						else
						{
							self::SaveCommunications(
								$ID,
								array(
									array(
										'ENTITY_ID' => $entityID,
										'ENTITY_TYPE_ID' => $entityTypeID,
										'ENTITY_SETTINGS' => $settings
									)
								)
							);
						}
					}
				}

				if($entityTypeID === CCrmOwnerType::Lead)
				{
					$info['TITLE'] = isset($settings['LEAD_TITLE']) ? $settings['LEAD_TITLE'] : '';
				}
				elseif($entityTypeID === CCrmOwnerType::Company)
				{
					$info['TITLE'] = isset($settings['COMPANY_TITLE']) ? $settings['COMPANY_TITLE'] : '';
				}
				elseif($entityTypeID === CCrmOwnerType::Contact)
				{
					$info['TITLE'] = CCrmContact::PrepareFormattedName(
						array(
							'HONORIFIC' => isset($settings['HONORIFIC']) ? $settings['HONORIFIC'] : '',
							'NAME' => isset($settings['NAME']) ? $settings['NAME'] : '',
							'LAST_NAME' => isset($settings['LAST_NAME']) ? $settings['LAST_NAME'] : '',
							'SECOND_NAME' => isset($settings['SECOND_NAME']) ? $settings['SECOND_NAME'] : ''
						),
						$nameTemplate
					);

					$info['NAME_DATA'] = array(
						'NAME_TEMPLATE' => $nameTemplate,
						'HONORIFIC' => isset($settings['HONORIFIC']) ? $settings['HONORIFIC'] : '',
						'NAME' => isset($settings['NAME']) ? $settings['NAME'] : '',
						'LAST_NAME' => isset($settings['LAST_NAME']) ? $settings['LAST_NAME'] : '',
						'SECOND_NAME' => isset($settings['SECOND_NAME']) ? $settings['SECOND_NAME'] : ''
					);
				}

				$result[$ID] = self::$CLIENT_INFOS[$ID] = $info;
			}
		}
	}
	return $result;
}

Добавить комментарий