CCrmLiveFeedComponent::processCrmBlogPostRights

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmLiveFeedComponent
  4. processCrmBlogPostRights
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/livefeed.php
  • Класс: \CCrmLiveFeedComponent
  • Вызов: CCrmLiveFeedComponent::processCrmBlogPostRights
static function processCrmBlogPostRights($logId, $logFields = array(), $blogPostFields = array(), $action = 'new')
{
	global $USER;

	$logId = intval($logId);
	if (
		$logId > 0
		&& Main\Loader::includeModule('socialnetwork')
	)
	{
		$bOldCrm = ($logFields['ENTITY_TYPE'] == CCrmLiveFeedEntity::Contact);
		$bNewCrm = false;

		$bindings = array();
		$communications = array();
		$sonetRights = array();
		$logRightsToAdd = array();

		$rsLogRights = CSocNetLogRights::GetList(
			array(),
			array(
				'LOG_ID' => $logId
			)
		);
		while($arLogRights = $rsLogRights->fetch())
		{
			$sonetRights[] = $arLogRights['GROUP_CODE'];
		}

		foreach ($sonetRights as $rightCode)
		{
			if (preg_match('/^CRMCONTACT(\d+)$/', $rightCode, $matches))
			{
				$entityId = intval($matches[1]);
				$bNewCrm = true;

				$bindings = self::mergeBindings($bindings, array(array(
					'OWNER_ID' => $entityId,
					'OWNER_TYPE_ID' => CCrmOwnerType::Contact
				)));

				$rsUser = \Bitrix\Main\UserTable::getList(array(
					'order' => array(),
					'filter' => array(
						'UF_USER_CRM_ENTITY' => 'C_'.$entityId,
						'=EXTERNAL_AUTH_ID' => 'email'
					),
					'select' => array('ID', 'EMAIL')
				));

				if ($arUser = $rsUser->fetch())
				{
					$communications = self::mergeCommunications($communications, array(array(
						'ID' => 0,
						'TYPE' => 'EMAIL',
						'VALUE' => $arUser['EMAIL'],
						'ENTITY_ID' => $entityId,
						'ENTITY_TYPE_ID' => CCrmOwnerType::Contact
					)));
				}
			}
			elseif (preg_match('/^U(\d+)$/', $rightCode, $matches))
			{
				$rsUser = \Bitrix\Main\UserTable::getList(array(
					'order' => array(),
					'filter' => array(
						'ID' => intval($matches[1]),
						'=EXTERNAL_AUTH_ID' => 'email'
					),
					'select' => array('ID', 'EMAIL', 'UF_USER_CRM_ENTITY')
				));
				if (
					($arUser = $rsUser->fetch())
					&& !empty($arUser["UF_USER_CRM_ENTITY"])
					&& preg_match('/^C_(\d+)$/', $arUser["UF_USER_CRM_ENTITY"], $matches2)
				)
				{
					$bNewCrm = true;
					$entityId = intval($matches2[1]);

					$bindings = self::mergeBindings($bindings, array(array(
						'OWNER_ID' => $entityId,
						'OWNER_TYPE_ID' => CCrmOwnerType::Contact
					)));

					$communications = self::mergeCommunications($communications, array(array(
						'ID' => 0,
						'TYPE' => 'EMAIL',
						'VALUE' => $arUser['EMAIL'],
						'ENTITY_ID' => $entityId,
						'ENTITY_TYPE_ID' => CCrmOwnerType::Contact
					)));

					$newRight = CCrmLiveFeedEntity::Contact.$entityId;
					if (!in_array($newRight, $sonetRights))
					{
						$logRightsToAdd[] = $newRight;
					}
				}
			}
		}

		if ($bNewCrm && !$bOldCrm)
		{
			CSocNetLog::Update($logId, array(
				"ENTITY_TYPE" => CCrmLiveFeedEntity::Contact,
				"ENTITY_ID" => $entityId
			));
		}

		if (
			$action === 'new'
			&& $bNewCrm
		)
		{
			CSocNetLogRights::DeleteByLogID($logId);
			CSocNetLogRights::Add($logId, array_merge($sonetRights, $logRightsToAdd));
		}
		elseif (
			$action === 'edit'
			&& ($bNewCrm || $bOldCrm)
		)
		{
			CSocNetLogRights::DeleteByLogID($logId);
			CSocNetLogRights::Add($logId, array_merge($sonetRights, $logRightsToAdd));

			if ($bOldCrm && !$bNewCrm)
			{
				$arActivityIdToDelete = array();
				$rsActivity = CCrmActivity::GetList(
					array(),
					array("ASSOCIATED_ENTITY_ID" => $logId),
					false,
					false,
					array("ID")
				);
				while($arActivity = $rsActivity->fetch())
				{
					$arActivityIdToDelete[] = $arActivity["ID"];
				}

				foreach($arActivityIdToDelete as $activityId)
				{
					CCrmActivity::Delete($activityId, false);
				}

				CSocNetLog::Update($logId, array(
					"ENTITY_TYPE" => SONET_ENTITY_USER,
					"ENTITY_ID" => $logFields['USER_ID']
				));
			}
		}
		elseif (
			$action == 'share'
			&& ($bNewCrm || $bOldCrm)
		)
		{
			CSocNetLogRights::DeleteByLogID($logId);
			CSocNetLogRights::Add($logId, array_merge($sonetRights, $logRightsToAdd));
		}

		if ($bNewCrm && !$bOldCrm)
		{
			\Bitrix\Crm\Activity\Provider\Livefeed::addActivity(array(
				"TYPE" => "BLOG_POST",
				"COMMUNICATIONS" => $communications,
				"BINDINGS" => $bindings,
				"TITLE" => ($blogPostFields['MICRO'] === 'Y' ? '' : $blogPostFields['TITLE']),
				"MESSAGE" => $blogPostFields['DETAIL_TEXT'],
				"USER_ID" => (isset($blogPostFields['AUTHOR_ID']) ? $blogPostFields['AUTHOR_ID'] : $USER->getId()),
				"ENTITY_ID" => $logId
			));

			CCrmLiveFeed::CounterIncrement(array(
				"USER_ID" => (is_array($logFields["USER_ID"]) && isset($logFields["USER_ID"]) ? $logFields["USER_ID"] : $USER->getId()),
				"ENTITY_TYPE" => CCrmLiveFeedEntity::Contact,
				"ENTITY_ID" => $entityId,
				"LOG_ID" => $logId,
				"EVENT_ID" => (is_array($logFields["EVENT_ID"]) && isset($logFields["EVENT_ID"]) ? $logFields["EVENT_ID"] : 'blog_post')
			));
		}
		elseif ($bOldCrm && $bNewCrm)
		{
			$rsActivity = CCrmActivity::GetList(
				array(),
				array(
					'ASSOCIATED_ENTITY_ID' => $logId
				),
				false,
				array('nTopCount' => 1)
			);
			if ($arActivity = $rsActivity->fetch())
			{
				CCrmActivity::SaveBindings($arActivity['ID'], self::mergeBindings($bindings, CCrmActivity::GetBindings($arActivity['ID'])), false, false);
				CCrmActivity::SaveCommunications($arActivity['ID'], self::mergeCommunications($communications, CCrmActivity::GetCommunications($arActivity['ID'])), $arActivity, true, false);
			}
		}
	}
}

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