CAllCrmActivity::FindLeadCommunications

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

	if($needle === '')
	{
		return array();
	}

	global $DB;
	$fieldMultiTableName = CCrmActivity::FIELD_MULTI_TABLE_NAME;
	$leadTableName = CCrmLead::TABLE_NAME;
	$result = array();

	$needleSql = $DB->ForSqlLike($needle.'%');
	$firstNameSql = '';
	$lastNameSql = '';

	$nameParts = array();
	\Bitrix\Crm\Format\PersonNameFormatter::tryParseName(
		$needle,
		\Bitrix\Crm\Format\PersonNameFormatter::getFormatID(),
		$nameParts
	);

	if(isset($nameParts['NAME'])
		&& $nameParts['NAME'] !== ''
		&& isset($nameParts['LAST_NAME'])
		&& $nameParts['LAST_NAME'] !== ''
	)
	{
		$firstNameSql = $DB->ForSqlLike($nameParts['NAME'].'%');
		$lastNameSql = $DB->ForSqlLike($nameParts['LAST_NAME'].'%');
	}

	if($communicationType === '')
	{
		//Search by TITLE and FULL_NAME
		if($firstNameSql !== '' && $lastNameSql !== '')
		{
			$sql  = "SELECT L.ID AS ELEMENT_ID, '' AS VALUE_TYPE, '' AS VALUE, L.NAME, L.SECOND_NAME, L.LAST_NAME, L.HONORIFIC, L.TITLE AS LEAD_TITLE FROM {$leadTableName} L WHERE L.TITLE LIKE '{$needleSql}' OR (L.NAME LIKE '{$firstNameSql}' AND L.LAST_NAME LIKE '{$lastNameSql}')";
		}
		else
		{
			$sql  = "SELECT L.ID AS ELEMENT_ID, '' AS VALUE_TYPE, '' AS VALUE, L.NAME, L.SECOND_NAME, L.LAST_NAME, L.HONORIFIC, L.TITLE AS LEAD_TITLE FROM {$leadTableName} L WHERE L.TITLE LIKE '{$needleSql}' OR L.NAME LIKE '{$needleSql}' OR L.LAST_NAME LIKE '{$needleSql}'";
		}
		if($top > 0)
		{
			$sql = $DB->TopSql($sql, $top);
		}

		$dbRes = $DB->Query(
			$sql,
			false,
			'FILE: '.__FILE__.'
LINE: '.__LINE__ ); while($arRes = $dbRes->Fetch()) { $result[] = CAllCrmActivity::ReadLeadCommunication($arRes, $communicationType); } return $result; } //Search by Name if($firstNameSql !== '' && $lastNameSql !== '') { $sql = "SELECT FM.ELEMENT_ID, FM.VALUE_TYPE, FM.VALUE, L.NAME, L.SECOND_NAME, L.LAST_NAME, L.HONORIFIC, L.TITLE AS LEAD_TITLE FROM {$fieldMultiTableName} FM INNER JOIN {$leadTableName} L ON FM.ELEMENT_ID = L.ID AND FM.ENTITY_ID = 'LEAD' AND FM.TYPE_ID = '{$DB->ForSql($communicationType)}' AND L.TITLE LIKE '{$needleSql}' OR (L.NAME LIKE '{$firstNameSql}' AND L.LAST_NAME LIKE '{$lastNameSql}')"; } else { $sql = "SELECT FM.ELEMENT_ID, FM.VALUE_TYPE, FM.VALUE, L.NAME, L.SECOND_NAME, L.LAST_NAME, L.HONORIFIC, L.TITLE AS LEAD_TITLE FROM {$fieldMultiTableName} FM INNER JOIN {$leadTableName} L ON FM.ELEMENT_ID = L.ID AND FM.ENTITY_ID = 'LEAD' AND FM.TYPE_ID = '{$DB->ForSql($communicationType)}' AND (L.TITLE LIKE '{$needleSql}' OR L.NAME LIKE '{$needleSql}' OR L.LAST_NAME LIKE '{$needleSql}')"; } if($top > 0) { $sql = $DB->TopSql($sql, $top); } $dbRes = $DB->Query( $sql, false, 'FILE: '.__FILE__.'
LINE: '.__LINE__ ); while($arRes = $dbRes->Fetch()) { $result[] = CAllCrmActivity::ReadLeadCommunication($arRes, $communicationType); } //Search by VALUE $sql = "SELECT FM.ELEMENT_ID, FM.VALUE_TYPE, FM.VALUE, L.NAME, L.SECOND_NAME, L.LAST_NAME, L.HONORIFIC, L.TITLE AS LEAD_TITLE FROM {$fieldMultiTableName} FM INNER JOIN {$leadTableName} L ON FM.ELEMENT_ID = L.ID AND FM.ENTITY_ID = 'LEAD' AND FM.TYPE_ID = '{$DB->ForSql($communicationType)}' AND FM.VALUE LIKE '{$needleSql}'"; if($top > 0) { $sql = $DB->TopSql($sql, $top); } $dbRes = $DB->Query( $sql, false, 'FILE: '.__FILE__.'
LINE: '.__LINE__ ); while($arRes = $dbRes->Fetch()) { $result[] = CAllCrmActivity::ReadLeadCommunication($arRes, $communicationType); } return $result; }

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