CCrmLiveFeedComponent::ProcessLogEventEditPOSTCrmEmailUsers

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmLiveFeedComponent
  4. ProcessLogEventEditPOSTCrmEmailUsers
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/livefeed.php
  • Класс: \CCrmLiveFeedComponent
  • Вызов: CCrmLiveFeedComponent::ProcessLogEventEditPOSTCrmEmailUsers
static function ProcessLogEventEditPOSTCrmEmailUsers(&$arPOST, &$arCrmEmailEntities = array())
{
	$arResult = array();

	if (
		isset($arPOST['SPERM'])
		&& isset($arPOST['SPERM']['UE'])
		&& is_array($arPOST['SPERM']['UE'])
		&& CModule::IncludeModule('mail')
	)
	{
		foreach ($arPOST['SPERM']['UE'] as $key => $userEmail)
		{
			if (!check_email($userEmail))
			{
				continue;
			}

			$bFound = false;

			if (
				isset($arPOST["INVITED_USER_CRM_ENTITY"])
				&& !empty($arPOST["INVITED_USER_CRM_ENTITY"][$userEmail])
			)
			{
				$arFilter = array(
					array(
						'LOGIC' => 'OR',
						'=EMAIL' => $userEmail,
						'UF_USER_CRM_ENTITY' => $arPOST["INVITED_USER_CRM_ENTITY"][$userEmail]
					)
				);
			}
			else
			{
				$arFilter = array(
					'=EMAIL' => $userEmail,
				);
			}

			$rsUser = \Bitrix\Main\UserTable::getList(array(
				'order' => array('ID' => 'ASC'),
				'filter' => $arFilter,
				'select' => array('ID', 'UF_USER_CRM_ENTITY')
			));

			while ($arEmailUser = $rsUser->fetch())
			{
				if (intval($arEmailUser["ID"]) > 0)
				{
					$arPOST["SPERM"]["U"][] = "U".$arEmailUser["ID"];

					if (!empty($arEmailUser["UF_USER_CRM_ENTITY"]))
					{
						$res = self::resolveLFEntityFromUF($arEmailUser["UF_USER_CRM_ENTITY"]);
						if (!empty($res))
						{
							[$k, $v] = $res;

							if ($k && $v)
							{
								if (!isset($arPOST["SPERM"][$k]))
								{
									$arPOST["SPERM"][$k] = array();
								}
								$arPOST["SPERM"][$k][] = $k.$v;

								if (!isset($arCrmEmailEntities[$k]))
								{
									$arCrmEmailEntities[$k] = array();
								}
								$arCrmEmailEntities[$k][] = array(
									"ID" => $v,
									"EMAIL" => $userEmail
								);
							}
						}
					}
					$bFound = true;
				}
			}

			if ($bFound)
			{
				continue;
			}

			$userFields = array(
				'EMAIL' => $userEmail,
				'NAME' => (
					isset($arPOST["INVITED_USER_NAME"])
					&& isset($arPOST["INVITED_USER_NAME"][$userEmail])
						? $arPOST["INVITED_USER_NAME"][$userEmail]
						: ''
				),
				'LAST_NAME' => (
					isset($arPOST["INVITED_USER_LAST_NAME"])
					&& isset($arPOST["INVITED_USER_LAST_NAME"][$userEmail])
						? $arPOST["INVITED_USER_LAST_NAME"][$userEmail]
						: ''
				),
				'UF' => array(
					'UF_USER_CRM_ENTITY' => $arPOST["INVITED_USER_CRM_ENTITY"][$userEmail]
				)
			);

			$res = self::resolveLFEntityFromUF($arPOST["INVITED_USER_CRM_ENTITY"][$userEmail]);
			if (!empty($res))
			{
				[$k, $v] = $res;
				if ($k && $v)
				{
					if (
						$k == CCrmLiveFeedEntity::Contact
						&& ($contact = \CCrmContact::GetByID($v))
						&& intval($contact['PHOTO']) > 0
					)
					{
						$userFields['PERSONAL_PHOTO_ID'] = intval($contact['PHOTO']);
					}
				}
			}

			// invite email user by email
			$invitedUserId = \Bitrix\Mail\User::create($userFields);

			if (
				intval($invitedUserId) <= 0
				&& $invitedUserId->LAST_ERROR <> ''
			)
			{
				$strError = $invitedUserId->LAST_ERROR;
			}

			if (
				!$strError
				&& intval($invitedUserId) > 0
			)
			{
				if (!isset($arPOST["SPERM"]["U"]))
				{
					$arPOST["SPERM"]["U"] = array();
				}
				$arPOST["SPERM"]["U"][] = "U".$invitedUserId;

				$res = self::resolveLFEntityFromUF($arPOST["INVITED_USER_CRM_ENTITY"][$userEmail]);
				if (!empty($res))
				{
					[$k, $v] = $res;

					if ($k && $v)
					{
						if (!isset($arPOST["SPERM"][$k]))
						{
							$arPOST["SPERM"][$k] = array();
						}
						$arPOST["SPERM"][$k][] = $k.$v;

						if (!isset($arCrmEmailEntities[$k]))
						{
							$arCrmEmailEntities[$k] = array();
						}
						$arCrmEmailEntities[$k][] = array(
							"ID" => $v,
							"EMAIL" => $userEmail
						);
					}
				}

				if (Loader::includeModule('intranet') && class_exists('\Bitrix\Intranet\Integration\Mail\EmailUser'))
				{
					\Bitrix\Intranet\Integration\Mail\EmailUser::invite($invitedUserId);
				}
			}
			else
			{
				$arResult["ERROR_MESSAGE"] .= $strError;
			}
		}
		unset($arPOST["SPERM"]["UE"]);
	}

	if (
		isset($arPOST['SPERM'])
		&& isset($arPOST['SPERM']['U'])
		&& is_array($arPOST['SPERM']['U'])
	)
	{
		$arUserId = array();
		foreach ($arPOST['SPERM']['U'] as $key => $code)
		{
			if (preg_match('/^U(\d+)$/', $code, $matches))
			{
				$arUserId[] = intval($matches[1]);
			}
		}
		if (!empty($arUserId))
		{
			$rsUser = \Bitrix\Main\UserTable::getList(array(
				'order' => array(),
				'filter' => array(
					'ID' => $arUserId
				),
				'select' => array('ID', 'EMAIL', 'UF_USER_CRM_ENTITY')
			));

			while ($arEmailUser = $rsUser->fetch())
			{
				if (!empty($arEmailUser['UF_USER_CRM_ENTITY']))
				{
					$res = self::resolveLFEntityFromUF($arEmailUser["UF_USER_CRM_ENTITY"]);
					if (!empty($res))
					{
						[$k, $v] = $res;

						if ($k && $v)
						{
							if (!isset($arPOST["SPERM"][$k]))
							{
								$arPOST["SPERM"][$k] = array();
							}
							$arPOST["SPERM"][$k][] = $k.$v;

							if (!isset($arCrmEmailEntities[$k]))
							{
								$arCrmEmailEntities[$k] = array();
							}
							$arCrmEmailEntities[$k][] = array(
								"ID" => $v,
								"EMAIL" => $arEmailUser['EMAIL']
							);
						}
					}
				}
			}
		}
	}

	return $arResult;
}

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