Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexey-kar/1d8054b3ac6a1a1bb0e87b0829cc67e4 to your computer and use it in GitHub Desktop.
Save alexey-kar/1d8054b3ac6a1a1bb0e87b0829cc67e4 to your computer and use it in GitHub Desktop.
public function Task($messageId, $date = '')
{
if (!CModule::IncludeModule("tasks"))
return false;
$CIMMessage = new CIMMessage($this->user_id);
$message = $CIMMessage->GetMessage($messageId, true);
if (!$message)
return false;
$task = new \Bitrix\Tasks\Item\Task(0, $this->user_id);
$taskTitle = substr(trim(preg_replace(
array("/\n+/is".BX_UTF_PCRE_MODIFIER, '/\s+/is'.BX_UTF_PCRE_MODIFIER),
" ",
CTextParser::clearAllTags($message['MESSAGE'])
)), 0, 255);
$task->title = $taskTitle? $taskTitle: CTextParser::clearAllTags(GetMessage('IM_SHARE_CHAT_TASK', Array('#LINK#' => '')));
$task->description = $this->PrepareText($message)."\n";
$task['RESPONSIBLE_ID'] = $this->user_id;
if (
$message['AUTHOR_ID'] > 0 && $message['AUTHOR_ID'] != $this->user_id
&& !\Bitrix\Im\User::getInstance($message['AUTHOR_ID'])->isExtranet()
&& !\Bitrix\Im\User::getInstance($message['AUTHOR_ID'])->isBot()
)
{
$task['AUDITORS'] = Array($message['AUTHOR_ID']);
}
$task['CREATED_BY'] = $this->user_id;
if (!empty($message['FILES']))
{
$diskUf = \Bitrix\Tasks\Integration\Disk\UserField::getMainSysUFCode();
$task[$diskUf] = array_map(function($value){ return 'n'.$value;}, array_keys($message['FILES']));
}
$messageParams = Array();
if ($message['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
{
$messageParams = Array('LINK_ACTIVE' => Array((string)$this->user_id, (string)$message['AUTHOR_ID']));
}
else
{
$chat = \Bitrix\Im\Model\ChatTable::getById($message['CHAT_ID'])->fetch();
if ($chat['ENTITY_TYPE'] == 'LINES' && CModule::IncludeModule('crm'))
{
$fieldData = explode("|", $chat['ENTITY_DATA_1']);
if (isset($fieldData[0]) && $fieldData[0] == 'Y' && isset($fieldData[1]) && isset($fieldData[2]))
{
$crmType = \CCrmOwnerTypeAbbr::ResolveByTypeID(\CCrmOwnerType::ResolveID($fieldData[1]));
$task['UF_CRM_TASK'] = array($crmType.'_'.$fieldData[2]);
}
}
if ($chat['ENTITY_TYPE'] == 'SONET_GROUP')
{
$task['GROUP_ID'] = $chat['ENTITY_ID'];
}
else if ($chat['ENTITY_TYPE'] != 'SONET_GROUP')
{
$messageParams = Array('LINK_ACTIVE' => Array((string)$this->user_id, (string)$message['AUTHOR_ID']));
}
}
$date = intval($date);
if ($date > 0)
{
$task['DEADLINE'] = Bitrix\Main\Type\DateTime::createFromTimestamp($date);
}
else
{
$results = \Bitrix\Main\Text\DateConverter::decode(CTextParser::clearAllTags($message['MESSAGE']), 1000);
if (!empty($results))
{
$task['DEADLINE'] = $results[0]->getDate();
}
}
$result = $task->save();
if(!$result->isSuccess())
{
return false;
}
$link = CTaskNotifications::getNotificationPath(array('ID' => $this->user_id), $task->getId());
$this->SendMessage($message, GetMessage('IM_SHARE_CHAT_TASK', Array('#LINK#' => $link)), $messageParams);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment