- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/lib/controller/mail/message.php
- Класс: Bitrix\Crm\Controller\Mail\Message
- Вызов: Message::getChainAction
public function getChainAction(int $id): array
{
/**
* todo: Develop the method:
* Write a function for obtaining a chain in parts (By parent id/ for long chains).
* The chain can be taken both up and down from the original message.
*/
if (!$this->checkModules())
{
return [];
}
if (!\CCrmPerms::IsAccessEnabled())
{
$this->addError(new Error(Loc::getMessage('CRM_MAIL_CONTROLLER_PERMISSION_DENIED')));
return [];
}
$activities = $this->getActivities(
[
'ID' => $id,
],
self::SUPPORTED_ACTIVITY_TYPE,
[
'THREAD_ID',
],
limit: 1
);
if (count($activities) === 0 || !$this->checkActivityPermission(self::PERMISSION_READ, $activities))
{
return [];
}
$activity = $activities[0];
$threadId = $activity['THREAD_ID'];
$select = [
'SETTINGS',
'PARENT_ID',
'THREAD_ID',
'SUBJECT',
'START_TIME',
'DIRECTION',
];
$order = [
'START_TIME' => 'DESC',
];
/*
We need to make two selections and sort the messages by date
in order to limit the number of messages in long chains(in the future)
while preserving the open email in the middle of the chain.
*/
$messageBeforeCurrent = $this->getActivities(
[
'=THREAD_ID' => $threadId,
'<=PARENT_ID' => $id,
],
self::SUPPORTED_ACTIVITY_TYPE,
$select,
$order
);
$messageAfterCurrent = $this->getActivities(
[
'=THREAD_ID' => $threadId,
'>PARENT_ID' => $id,
],
self::SUPPORTED_ACTIVITY_TYPE,
$select,
$order
);
$chainActivities = array_merge($messageBeforeCurrent, $messageAfterCurrent);
/*
We need to sort the messages by time again.
For example, message number 5 may be a response to the first message, not the fourth.
*/
usort($chainActivities, function($a, $b) {
return $a['START_TIME']->getTimestamp() <=> $b['START_TIME']->getTimestamp();
});
$chain = [];
$lastIncomingId = null;
$lastIncomingKey = null;
foreach ($chainActivities as $key => $item)
{
$buildItem = [
'ID' => $item['ID'],
'OWNER_ID' => $item['OWNER_ID'],
'SUBJECT' => $item['SUBJECT'],
'DATE' => $this->getMessageDate(
$item
),
'HEADER' => $this->getHeader(
$item
),
'OWNER_TYPE' => \CCrmOwnerType::ResolveName($item['OWNER_TYPE_ID']),
'DIRECTION' => $item['DIRECTION'],
];
if ($item['DIRECTION'] === strval(\CCrmActivityDirection::Incoming))
{
$lastIncomingId = $item['ID'];
$lastIncomingKey = $key;
}
if ($item['ID'] == $id)
{
$buildItem['DESCRIPTION'] = $this->getMessageBody($id)['HTML'];
$buildItem['FILES'] = $this->getMessageFilesLinkMessages($id)['FILES'];
}
$chain[] = $buildItem;
}
/*
Upload the content of the last read message to be able to transfer it to the sending component for citation
*/
if (!is_null($lastIncomingKey))
{
$chain[$lastIncomingKey]['DESCRIPTION'] = $this->getMessageBody($lastIncomingId)['HTML'];
$chain[$lastIncomingKey]['FILES'] = $this->getMessageFilesLinkMessages($lastIncomingId)['FILES'];
}
return [
'list' => $chain,
'properties' => [
'lastIncomingId' => $lastIncomingId,
],
];
}