CAllCrmActivity::RegisterLiveFeedEvent

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmActivity
  4. RegisterLiveFeedEvent
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_activity.php
  • Класс: \CAllCrmActivity
  • Вызов: CAllCrmActivity::RegisterLiveFeedEvent
static function RegisterLiveFeedEvent(&$arFields)
{
	static $contextUserId = false;

	$ID = isset($arFields['ID']) ? intval($arFields['ID']) : 0;
	if($ID <= 0)
	{
		$arFields['ERROR'] = 'Could not find activity ID.';
		return false;
	}

	$ownerTypeID = isset($arFields['OWNER_TYPE_ID']) ? intval($arFields['OWNER_TYPE_ID']) : CCrmOwnerType::Undefined;
	if(!CCrmOwnerType::IsDefined($ownerTypeID))
	{
		$arFields['ERROR'] = 'Could not find owner type ID.';
		return false;
	}

	$ownerID = isset($arFields['OWNER_ID']) ? intval($arFields['OWNER_ID']) : 0;
	if($ownerID <= 0)
	{
		$arFields['ERROR'] = 'Could not find owner ID.';
		return false;
	}

	$arOwners = array(
		array(
			"OWNER_TYPE_ID" => $ownerTypeID,
			"OWNER_ID" => $ownerID
		)
	);

	$authorID = isset($arFields['AUTHOR_ID']) ? intval($arFields['AUTHOR_ID']) : 0;
	$editorID = isset($arFields['EDITOR_ID']) ? intval($arFields['EDITOR_ID']) : 0;
	$userID = $authorID > 0 ? $authorID : $editorID;
	if($userID <= 0)
	{
		$userID = CCrmSecurityHelper::GetCurrentUserID();
	}

	// Params are not assigned - we will use current activity only.
	$liveFeedFields = array(
		'ENTITY_TYPE_ID' => CCrmOwnerType::Activity,
		'ENTITY_ID' => $ID,
		'USER_ID' => $userID,
		'MESSAGE' => '',
		'TITLE' => '',
		'LOG_RIGHTS' => (!empty($arFields["RESPONSIBLE_ID"]) && intval($arFields["RESPONSIBLE_ID"]) > 0 ? array('U'.$arFields["RESPONSIBLE_ID"]) : false)
	);

	$bindings = isset($arFields['BINDINGS']) && is_array($arFields['BINDINGS']) ? $arFields['BINDINGS'] : array();
	if(!empty($bindings))
	{
		$liveFeedFields['PARENTS'] = $bindings;
		$liveFeedFields['PARENT_OPTIONS'] = array(
			'ENTITY_TYPE_ID_KEY' => 'OWNER_TYPE_ID',
			'ENTITY_ID_KEY' => 'OWNER_ID'
		);

		$ownerInfoOptions = array(
			'ENTITY_TYPE_ID_KEY' => 'OWNER_TYPE_ID',
			'ENTITY_ID_KEY' => 'OWNER_ID',
			'ADDITIONAL_DATA' => array('LEVEL' => 2)
		);

		$additionalParents = array();
		foreach($bindings as &$binding)
		{
			$ownerTypeID = isset($binding['OWNER_TYPE_ID']) ? intval($binding['OWNER_TYPE_ID']) : CCrmOwnerType::Undefined;
			$ownerID = isset($binding['OWNER_ID']) ? intval($binding['OWNER_ID']) : 0;

			if (
				$ownerTypeID != CCrmOwnerType::Undefined
				&& $ownerID > 0
			)
			{
				$arOwners[] = array(
					"OWNER_TYPE_ID" => $ownerTypeID,
					"OWNER_ID" => $ownerID
				);
			}

			if(
				$ownerTypeID === CCrmOwnerType::Contact
				&& $ownerID > 0
			)
			{
				$owners = array();
				if(CCrmOwnerType::TryGetOwnerInfos(CCrmOwnerType::Contact, $ownerID, $owners, $ownerInfoOptions))
				{
					$additionalParents = array_merge($additionalParents, $owners);
				}
			}
		}
		unset($binding);
		if(!empty($additionalParents))
		{
			$liveFeedFields['PARENTS'] = array_merge($bindings, $additionalParents);
		}

	}

	self::PrepareStorageElementIDs($arFields);
	$arStorageElementID = $arFields["STORAGE_ELEMENT_IDS"];
	if (!empty($arStorageElementID))
	{
		if ($arFields["STORAGE_TYPE_ID"] == StorageType::WebDav)
		{
			$liveFeedFields["UF_SONET_LOG_DOC"] = $arStorageElementID;
		}
		else if ($arFields["STORAGE_TYPE_ID"] == StorageType::Disk)
		{
			$liveFeedFields["UF_SONET_LOG_DOC"] = array();
			//We have to add prefix Bitrix\Disk\Uf\FileUserType::NEW_FILE_PREFIX to file ID
			foreach($arStorageElementID as $elementID)
			{
				$liveFeedFields["UF_SONET_LOG_DOC"][] = "n{$elementID}";
			}
		}
		else
		{
			$liveFeedFields["UF_SONET_LOG_FILE"] = $arStorageElementID;
		}
	}

	if ($arFields["TYPE_ID"] == CCrmActivityType::Email)
	{
		if ($contextUserId === false)
		{
			$res = \Bitrix\Main\UserGroupTable::getList(array(
				'order' => array(
					'USER_ID' => 'ASC'
				),
				'filter' => array(
					'GROUP_ID' => 1,
					'=USER.ACTIVE' => 'Y'
				),
				'select' => array('USER_ID'),
				'limit' => 1
			));
			if ($userGroupFields = $res->fetch())
			{
				// hack: for UF CheckFields(), agent call
				$contextUserId = $liveFeedFields['CONTEXT_USER_ID'] = $userGroupFields['USER_ID'];
			}
		}
		else
		{
			$liveFeedFields['CONTEXT_USER_ID'] = $contextUserId;
		}
	}

	$eventID = 0;
	$associatedEntityId = isset($arFields['ASSOCIATED_ENTITY_ID']) ? (int)$arFields['ASSOCIATED_ENTITY_ID'] : 0;
	$provider = self::GetActivityProvider($arFields);
	if ($provider !== null && Settings\Crm::isLiveFeedRecordsGenerationEnabled())
	{
		$eventID = $provider::createLiveFeedLog($associatedEntityId, $arFields, $liveFeedFields);
	}

	if ($eventID === 0)
	{
		$arOptions = [];
		if (isset($arFields['PROVIDER_ID']))
		{
			$arOptions['ACTIVITY_PROVIDER_ID'] = $arFields['PROVIDER_ID'];
		}
		$eventID = CCrmLiveFeed::CreateLogEvent($liveFeedFields, CCrmLiveFeedEvent::Add, $arOptions);
	}

	if ($eventID === false && isset($liveFeedFields['ERROR']))
	{
		$arFields['ERROR'] = $liveFeedFields['ERROR'];
	}
	else
	{
		if (
			intval($arFields["RESPONSIBLE_ID"]) > 0
			&& $arFields["RESPONSIBLE_ID"] != $userID
			&& CModule::IncludeModule("im")
		)
		{
			$bHasPermissions = false;
			$perms = CCrmPerms::GetUserPermissions($arFields["RESPONSIBLE_ID"]);
			foreach ($arOwners as $arOwner)
			{
				if (CCrmActivity::CheckReadPermission($arOwner["OWNER_TYPE_ID"], $arOwner["OWNER_ID"], $perms))
				{
					$bHasPermissions = true;
					break;
				}
			}

			switch ($arFields['TYPE_ID'])
			{
				case CCrmActivityType::Call:
					$type = 'CALL';
					break;
				case CCrmActivityType::Meeting:
					$type = 'MEETING';
					break;
				default:
					$type = false;
			}

			if ($type)
			{
				$url = CCrmOwnerType::GetEntityShowPath(CCrmOwnerType::Activity, $ID);

				$arMessageFields = array(
					"MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
					"TO_USER_ID" => $arFields["RESPONSIBLE_ID"],
					"FROM_USER_ID" => $userID,
					"NOTIFY_TYPE" => IM_NOTIFY_FROM,
					"NOTIFY_MODULE" => "crm",
					"LOG_ID" => $eventID,
					"NOTIFY_EVENT" => "changeAssignedBy",
					"NOTIFY_TAG" => "CRM|ACTIVITY|".$ID,
					"NOTIFY_MESSAGE" => GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => ''.htmlspecialcharsbx($arFields['SUBJECT']).'')),
					"NOTIFY_MESSAGE_OUT" => GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($arFields['SUBJECT'])))." (".CCrmUrlUtil::ToAbsoluteUrl($url).")"
				);

				if(!$bHasPermissions)
				{
					//TODO: Add  message 'Need for permissions'
					$arMessageFields["NOTIFY_MESSAGE"] = GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($arFields['SUBJECT'])));
					$arMessageFields["NOTIFY_MESSAGE_OUT"] = GetMessage("CRM_ACTIVITY_".$type."_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($arFields['SUBJECT'])));
				}

				CIMNotify::Add($arMessageFields);
			}
		}
	}

	return $eventID;
}

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