Livefeed::addActivity

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. Livefeed
  4. addActivity
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/activity/provider/livefeed.php
  • Класс: Bitrix\Crm\Activity\Provider\Livefeed
  • Вызов: Livefeed::addActivity
static function addActivity($fields)
{
	global $USER_FIELD_MANAGER;
	$result = false;

	$type = (
		!empty($fields["TYPE"])
			? $fields["TYPE"]
			: 'ENTRY'
	);

	$communications = (
		!empty($fields["COMMUNICATIONS"])
		&& is_array($fields["COMMUNICATIONS"])
			? $fields["COMMUNICATIONS"]
			: array()
	);

	$bindings = (
		!empty($fields["BINDINGS"])
		&& is_array($fields["BINDINGS"])
			? $fields["BINDINGS"]
			: array()
	);

	$message = (
		!empty($fields["MESSAGE"])
			? $fields["MESSAGE"]
			: ''
	);

	$title = (
		!empty($fields["TITLE"])
			? $fields["TITLE"]
			: ''
	);

	$userId = (
		!empty($fields["USER_ID"])
			? intval($fields["USER_ID"])
			: 0
	);

	$responsibleUserId = (
		!empty($fields["RESPONSIBLE_USER_ID"])
			? intval($fields["RESPONSIBLE_USER_ID"])
			: $userId
	);

	$entityId = (
		!empty($fields["ENTITY_ID"])
			? intval($fields["ENTITY_ID"])
			: 0
	);

	$parentId = (
		!empty($fields["PARENT_ID"])
			? intval($fields["PARENT_ID"])
			: 0
	);

	if (
		empty($communications)
		|| empty($message)
		|| $userId <= 0
		|| $responsibleUserId <= 0
		|| $entityId <= 0
	)
	{
		return $result;
	}

	if (!empty($bindings))
	{
		switch($type)
		{
			case 'COMMENT_IN':
			case 'BLOG_COMMENT_IN':
				$subject = Loc::getMessage('CRM_ACTIVITY_PROVIDER_LIVEFEED_TITLE_COMMENT_IN');
				$endTime = '';
				$completed = 'N';
				$direction = \CCrmActivityDirection::Incoming;
				$providerTypeId = self::PROVIDER_TYPE_ID_COMMENT;
				$ufEntity = ($type == 'BLOG_COMMENT_IN' ? 'BLOG_COMMENT' : 'SONET_COMMENT');
				$ufEntityId = $entityId;
				$entityId = ($type == 'BLOG_COMMENT_IN' ? self::getLogCommentId($entityId, 'blog_comment') : $entityId);
				$ufCode = ($type == 'BLOG_COMMENT_IN' ? 'UF_BLOG_COMMENT_FILE' : 'UF_SONET_COM_DOC');
				break;
			case 'COMMENT_OUT':
			case 'BLOG_COMMENT_OUT':
				$subject = Loc::getMessage('CRM_ACTIVITY_PROVIDER_LIVEFEED_TITLE_COMMENT_OUT');
				$endTime = '';
				$completed = 'Y';
				$direction = \CCrmActivityDirection::Outgoing;
				$providerTypeId = self::PROVIDER_TYPE_ID_COMMENT;
				$ufEntity = ($type == 'BLOG_COMMENT_OUT' ? 'BLOG_COMMENT' : 'SONET_COMMENT');
				$ufEntityId = $entityId;
				$entityId = ($type == 'BLOG_COMMENT_OUT' ? self::getLogCommentId($entityId, 'blog_comment') : $entityId);
				$ufCode = ($type == 'BLOG_COMMENT_OUT' ? 'UF_BLOG_COMMENT_FILE' : 'UF_SONET_COM_DOC');
				break;
			case 'ENTRY':
			case 'BLOG_POST':
				$subject = (!empty($title) ? $title : Loc::getMessage('CRM_ACTIVITY_PROVIDER_LIVEFEED_TITLE_ENTRY'));
				$endTime = null;
				$completed = 'Y';
				$direction = \CCrmActivityDirection::Outgoing;
				$providerTypeId = self::PROVIDER_TYPE_ID_ENTRY;
				$ufEntity = ($type == 'BLOG_POST' ? 'BLOG_POST' : 'SONET_LOG');
				$ufEntityId = ($type == 'BLOG_POST' ? self::getSourceEntryId($entityId) : $entityId);
				$ufCode = ($type == 'BLOG_POST' ? 'UF_BLOG_POST_FILE' : 'UF_SONET_LOG_DOC');
				break;
			default:
		}

		$uf = ($ufEntityId > 0 ? $USER_FIELD_MANAGER->getUserFields($ufEntity, $ufEntityId, LANGUAGE_ID, 0) : null);
		$fileIdList = array();

		if (
			is_array($uf)
			&& isset($uf[$ufCode])
			&& !empty($uf[$ufCode]["VALUE"])
			&& Loader::includeModule('disk')
		)
		{
			foreach ($uf[$ufCode]["VALUE"] as $attachedObjectId)
			{
				$attachedObject = \Bitrix\Disk\AttachedObject::loadById($attachedObjectId);
				if ($attachedObject)
				{
					$fileIdList[] = $attachedObject->getObjectId();
				}
			}
		}

		$activityFields = array(
			'TYPE_ID' =>  \CCrmActivityType::Provider,
			'PROVIDER_ID' => self::PROVIDER_ID,
			'SUBJECT' => $subject,
			'START_TIME' => convertTimeStamp((time() + \CTimeZone::getOffset()), 'FULL', SITE_ID),
			'END_TIME' => $endTime,
			'COMPLETED' => $completed,
			'PRIORITY' => \CCrmActivityPriority::Medium,
			'DESCRIPTION' => $message,
			'DESCRIPTION_TYPE' => \CCrmContentType::BBCode,
			'LOCATION' => '',
			'DIRECTION' => $direction,
			'NOTIFY_TYPE' => \CCrmActivityNotifyType::None,
			'BINDINGS' => $bindings,
			'SETTINGS' => array(),
			'AUTHOR_ID' => $userId,
			'RESPONSIBLE_ID' => $responsibleUserId,
			'PROVIDER_TYPE_ID' => $providerTypeId,
			'ASSOCIATED_ENTITY_ID' => $entityId,
			'PARENT_ID' => $parentId
		);

		if (!empty($fileIdList))
		{
			$activityFields['STORAGE_TYPE_ID'] = \CCrmActivity::getDefaultStorageTypeID();
			$activityFields['STORAGE_ELEMENT_IDS'] = $fileIdList;
		}

		$activityId = \CCrmActivity::add($activityFields, false, true, array('REGISTER_SONET_EVENT' => false));
		if($activityId > 0)
		{
			\CCrmActivity::saveCommunications($activityId, $communications, $activityFields, true, false);
			$result = $activityId;
		}
	}

	return $result;
}

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