• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/lib/componenthelper.php
  • Класс: BitrixSocialnetworkComponentHelper
  • Вызов: ComponentHelper::processUserEmail
static function processUserEmail($params, &$errorText): array
{
	$result = [];

	if (
		!is_array($params)
		|| empty($params['EMAIL'])
		|| !check_email($params['EMAIL'])
		|| !Loader::includeModule('mail')
	)
	{
		return $result;
	}

	$userEmail = $params['EMAIL'];

	if (
		empty($userEmail)
		|| !check_email($userEmail)
	)
	{
		return $result;
	}

	$res = CUser::getList(
		'ID',
		'ASC',
		[
			'=EMAIL' => $userEmail,
			'!EXTERNAL_AUTH_ID' => array_diff(BitrixMainUserTable::getExternalUserTypes(), [ 'email' ]),
		],
		[
			'FIELDS' => [ 'ID', 'EXTERNAL_AUTH_ID', 'ACTIVE' ]
		]
	);

	$userId = false;

	while (
		($emailUser = $res->fetch())
		&& !$userId
	)
	{
		if (
			(int)$emailUser["ID"] > 0
			&& (
				$emailUser["ACTIVE"] === "Y"
				|| $emailUser["EXTERNAL_AUTH_ID"] === "email"
			)
		)
		{
			if ($emailUser["ACTIVE"] === "N") // email only
			{
				$user = new CUser;
				$user->update($emailUser["ID"], [
					'ACTIVE' => 'Y'
				]);
			}

			$userId = $emailUser['ID'];
		}
	}

	if ($userId)
	{
		$result = [
			'U'.$userId
		];
	}

	if (!$userId)
	{
		$userFields = array(
			'EMAIL' => $userEmail,
			'NAME' => ($params["NAME"] ?? ''),
			'LAST_NAME' => ($params["LAST_NAME"] ?? '')
		);

		if (
			!empty($params["CRM_ENTITY"])
			&& Loader::includeModule('crm')
		)
		{
			$userFields['UF'] = [
				'UF_USER_CRM_ENTITY' => $params["CRM_ENTITY"],
			];
			$res = CCrmLiveFeedComponent::resolveLFEntityFromUF($params["CRM_ENTITY"]);
			if (!empty($res))
			{
				[ $k, $v ] = $res;
				if ($k && $v)
				{
					$result[] = $k.$v;

					if (
						$k === CCrmLiveFeedEntity::Contact
						&& ($contact = CCrmContact::getById($v))
						&& (int)$contact['PHOTO'] > 0
					)
					{
						$userFields['PERSONAL_PHOTO_ID'] = (int)$contact['PHOTO'];
					}
				}
			}
		}
		elseif (
			!empty($params["CREATE_CRM_CONTACT"])
			&& $params["CREATE_CRM_CONTACT"] === 'Y'
			&& Loader::includeModule('crm')
			&& ($contactId = CCrmLiveFeedComponent::createContact($userFields))
		)
		{
			$userFields['UF'] = [
				'UF_USER_CRM_ENTITY' => 'C_'.$contactId
			];
			$result[] = "CRMCONTACT".$contactId;
		}

		// invite extranet user by email
		$userId = BitrixMailUser::create($userFields);

		$errorMessage = false;

		if (
			is_object($userId)
			&& $userId->LAST_ERROR <> ''
		)
		{
			$errorMessage = $userId->LAST_ERROR;
		}

		if (
			!$errorMessage
			&& (int)$userId > 0
		)
		{
			$result[] = "U".$userId;
		}
		else
		{
			$errorText = $errorMessage;
		}
	}

	if (
		!is_object($userId)
		&& (int)$userId > 0
	)
	{
		BitrixMainUISelectorEntities::save([
			'context' => (isset($params['CONTEXT']) && $params['CONTEXT'] <> '' ? $params['CONTEXT'] : 'BLOG_POST'),
			'code' => 'U'.$userId
		]);

		if (Loader::includeModule('intranet') && class_exists('BitrixIntranetIntegrationMailEmailUser'))
		{
			BitrixIntranetIntegrationMailEmailUser::invite($userId);
		}
	}

	return $result;
}