static function GetCaption($typeID, $ID, $checkRights = true, array $options = null)
{
$typeID = (int)$typeID;
$ID = (int)$ID;
if($ID <= 0)
{
return '';
}
$key = "{$typeID}_{$ID}";
if(isset(self::$CAPTIONS[$key]))
{
return self::$CAPTIONS[$key];
}
if($options === null)
{
$options = array();
}
switch($typeID)
{
case self::Lead:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes))
{
$dbRes = CCrmLead::GetListEx(array(), array('=ID' => $ID, 'CHECK_PERMISSIONS' => ($checkRights ? 'Y' : 'N')), false, false, array('TITLE', 'HONORIFIC', 'NAME', 'SECOND_NAME', 'LAST_NAME'));
$arRes = $dbRes ? $dbRes->Fetch() : null;
}
if(!$arRes)
{
return (self::$CAPTIONS[$key] = '');
}
else
{
$caption = isset($arRes['TITLE']) ? $arRes['TITLE'] : '';
if($caption === '')
{
$caption = CCrmLead::PrepareFormattedName(
array(
'HONORIFIC' => isset($arRes['HONORIFIC']) ? $arRes['HONORIFIC'] : '',
'NAME' => isset($arRes['NAME']) ? $arRes['NAME'] : '',
'SECOND_NAME' => isset($arRes['SECOND_NAME']) ? $arRes['SECOND_NAME'] : '',
'LAST_NAME' => isset($arRes['LAST_NAME']) ? $arRes['LAST_NAME'] : ''
)
);
}
return (self::$CAPTIONS[$key] = $caption);
}
}
case self::Contact:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes))
{
$dbRes = CCrmContact::GetListEx(array(), array('=ID' => $ID, 'CHECK_PERMISSIONS' => ($checkRights ? 'Y' : 'N')), false, false, array('HONORIFIC', 'NAME', 'SECOND_NAME', 'LAST_NAME'));
$arRes = $dbRes ? $dbRes->Fetch() : null;
}
if(!$arRes)
{
return (self::$CAPTIONS[$key] = '');
}
else
{
return (self::$CAPTIONS[$key] =
CCrmContact::PrepareFormattedName(
array(
'HONORIFIC' => isset($arRes['HONORIFIC']) ? $arRes['HONORIFIC'] : '',
'NAME' => isset($arRes['NAME']) ? $arRes['NAME'] : '',
'SECOND_NAME' => isset($arRes['SECOND_NAME']) ? $arRes['SECOND_NAME'] : '',
'LAST_NAME' => isset($arRes['LAST_NAME']) ? $arRes['LAST_NAME'] : ''
)
)
);
}
}
case self::Company:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes))
{
$dbRes = CCrmCompany::GetListEx(array(), array('=ID' => $ID, 'CHECK_PERMISSIONS' => ($checkRights ? 'Y' : 'N')), false, false, array('TITLE'));
$arRes = $dbRes ? $dbRes->Fetch() : null;
}
return (self::$CAPTIONS[$key] = $arRes ? $arRes['TITLE'] : '');
}
case self::Deal:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes))
{
$dbRes = CCrmDeal::GetListEx(array(), array('=ID' => $ID, 'CHECK_PERMISSIONS' => ($checkRights ? 'Y' : 'N')), false, false, array('TITLE'));
$arRes = $dbRes ? $dbRes->Fetch() : null;
}
return (self::$CAPTIONS[$key] = $arRes ? $arRes['TITLE'] : '');
}
case self::Invoice:
{
$dbRes = CCrmInvoice::GetList(array(), array('ID' => $ID), false, false, array('ORDER_TOPIC'));
$arRes = $dbRes ? $dbRes->Fetch() : null;
return (self::$CAPTIONS[$key] = $arRes ? $arRes['ORDER_TOPIC'] : '');
}
case self::Quote:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes))
{
$dbRes = CCrmQuote::GetList(array(), array('=ID' => $ID, 'CHECK_PERMISSIONS' => ($checkRights ? 'Y' : 'N')), false, false, array('QUOTE_NUMBER', 'TITLE'));
$arRes = $dbRes ? $dbRes->Fetch() : null;
}
$quoteTitle = empty($arRes['QUOTE_NUMBER']) ? '' : $arRes['QUOTE_NUMBER'];
$quoteTitle = empty($arRes['TITLE']) ?
$quoteTitle : (empty($quoteTitle) ? $arRes['TITLE'] : $quoteTitle.' - '.$arRes['TITLE']);
$quoteTitle = empty($quoteTitle) ? '' : str_replace(array(';', ','), ' ', $quoteTitle);
return $quoteTitle;
}
case self::Order:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes))
{
// todo: 'CHECK_PERMISSIONS' => ($checkRights ? 'Y' : 'N')
$dbRes = Bitrix\Crm\Order\Order::getList(array(
'filter' => array('=ID' => $ID),
'select' => array('ACCOUNT_NUMBER', 'ORDER_TOPIC')
));
$arRes = $dbRes->fetch();
}
$orderTitle = empty($arRes['ACCOUNT_NUMBER']) ? '' : $arRes['ACCOUNT_NUMBER'];
$orderTitle = empty($arRes['ORDER_TOPIC']) ?
$orderTitle : (empty($orderTitle) ? $arRes['ORDER_TOPIC'] : $orderTitle.' - '.$arRes['ORDER_TOPIC']);
$orderTitle = empty($orderTitle) ? '' : str_replace(array(';', ','), ' ', $orderTitle);
return $orderTitle;
}
case self::StoreDocument:
{
if (\Bitrix\Main\Loader::includeModule('catalog'))
{
$documentData = StoreDocumentTable::getList(
[
'select' => ['ID', 'DOC_TYPE', 'TITLE'],
'filter' => ['ID' => $ID],
]
)->fetch();
if ($documentData)
{
if (!empty($documentData['TITLE']))
{
return $documentData['TITLE'];
}
if (!empty($documentData['DOC_TYPE']))
{
$documentTitle = StoreDocumentTable::getTypeList(true)[$documentData['DOC_TYPE']];
return $documentTitle;
}
}
return '';
}
break;
}
case self::AgentContractDocument:
{
if (\Bitrix\Main\Loader::includeModule('catalog'))
{
$documentData = AgentContractTable::getList(
[
'select' => ['ID', 'TITLE'],
'filter' => ['ID' => $ID],
]
)->fetch();
if ($documentData && !empty($documentData['TITLE']))
{
return $documentData['TITLE'];
}
return '';
}
break;
}
case self::ShipmentDocument:
{
if
(
\Bitrix\Main\Loader::includeModule('catalog')
&& \Bitrix\Main\Loader::includeModule('sale')
)
{
$documentData = \Bitrix\Sale\Internals\ShipmentTable::getList(
[
'select' => ['ID', 'ORDER_ID', 'ACCOUNT_NUMBER'],
'filter' => ['ID' => $ID],
]
)->fetch();
if ($documentData)
{
if (!empty($documentData['ACCOUNT_NUMBER']))
{
$accountNumber = $documentData['ACCOUNT_NUMBER'];
}
else
{
$accountNumber = '';
}
return GetMessage('CRM_OWNER_TYPE_SHIPMENT_DOCUMENT', [
"#ACCOUNT_NUMBER#" => $accountNumber
]);
}
return '';
}
break;
}
case self::DealCategory:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes) && (!$checkRights || Bitrix\Crm\Category\DealCategory::checkReadPermission()))
{
$arRes = Bitrix\Crm\Category\DealCategory::get($ID);
}
if(!$arRes)
{
return (self::$CAPTIONS[$key] = '');
}
else
{
$caption = isset($arRes['NAME']) ? $arRes['NAME'] : '';
return (self::$CAPTIONS[$key] = $caption);
}
}
case self::CustomActivityType:
{
$arRes = isset($options['FIELDS']) ? $options['FIELDS'] : null;
if(!is_array($arRes))
{
$arRes = Bitrix\Crm\Activity\CustomType::get($ID);
}
if(!$arRes)
{
return (self::$CAPTIONS[$key] = '');
}
else
{
$caption = isset($arRes['NAME']) ? $arRes['NAME'] : '';
return (self::$CAPTIONS[$key] = $caption);
}
}
}
if (static::isUseDynamicTypeBasedApproach($typeID))
{
$factory = Container::getInstance()->getFactory($typeID);
if ($factory)
{
$item = $factory->getItem($ID);
if (!$item)
{
self::$CAPTIONS[$key] = '';
return '';
}
if ($checkRights && !Container::getInstance()->getUserPermissions()->canReadItem($item))
{
self::$CAPTIONS[$key] = '';
return '';
}
self::$CAPTIONS[$key] = $item->getHeading();
return self::$CAPTIONS[$key];
}
}
return '';
}