Example #1
0
 private static function RegisterLiveFeedEvent(&$arFields)
 {
     $ID = isset($arFields['ID']) ? intval($arFields['ID']) : 0;
     if ($ID <= 0) {
         $arFields['ERROR'] = 'Could not find activity ID.';
         return false;
     }
     $ownerTypeID = isset($arFields['OWNER_TYPE_ID']) ? intval($arFields['OWNER_TYPE_ID']) : CCrmOwnerType::Undefined;
     if (!CCrmOwnerType::IsDefined($ownerTypeID)) {
         $arFields['ERROR'] = 'Could not find owner type ID.';
         return false;
     }
     $ownerID = isset($arFields['OWNER_ID']) ? intval($arFields['OWNER_ID']) : 0;
     if ($ownerID <= 0) {
         $arFields['ERROR'] = 'Could not find owner ID.';
         return false;
     }
     $arOwners = array(array("OWNER_TYPE_ID" => $ownerTypeID, "OWNER_ID" => $ownerID));
     $authorID = isset($arFields['AUTHOR_ID']) ? intval($arFields['AUTHOR_ID']) : 0;
     $editorID = isset($arFields['EDITOR_ID']) ? intval($arFields['EDITOR_ID']) : 0;
     $userID = $authorID > 0 ? $authorID : $editorID;
     if ($userID <= 0) {
         $userID = CCrmSecurityHelper::GetCurrentUserID();
     }
     // Params are not assigned - we will use current activity only.
     $liveFeedFields = array('ENTITY_TYPE_ID' => CCrmOwnerType::Activity, 'ENTITY_ID' => $ID, 'USER_ID' => $userID, 'MESSAGE' => '', 'TITLE' => '');
     $bindings = isset($arFields['BINDINGS']) && is_array($arFields['BINDINGS']) ? $arFields['BINDINGS'] : array();
     if (!empty($bindings)) {
         $liveFeedFields['PARENTS'] = $bindings;
         $liveFeedFields['PARENT_OPTIONS'] = array('ENTITY_TYPE_ID_KEY' => 'OWNER_TYPE_ID', 'ENTITY_ID_KEY' => 'OWNER_ID');
         $ownerInfoOptions = array('ENTITY_TYPE_ID_KEY' => 'OWNER_TYPE_ID', 'ENTITY_ID_KEY' => 'OWNER_ID', 'ADDITIONAL_DATA' => array('LEVEL' => 2));
         $additionalParents = array();
         foreach ($bindings as &$binding) {
             $ownerTypeID = isset($binding['OWNER_TYPE_ID']) ? intval($binding['OWNER_TYPE_ID']) : CCrmOwnerType::Undefined;
             $ownerID = isset($binding['OWNER_ID']) ? intval($binding['OWNER_ID']) : 0;
             if ($ownerTypeID != CCrmOwnerType::Undefined && $ownerID > 0) {
                 $arOwners[] = array("OWNER_TYPE_ID" => $ownerTypeID, "OWNER_ID" => $ownerID);
             }
             if ($ownerTypeID === CCrmOwnerType::Contact && $ownerID > 0) {
                 $owners = array();
                 if (CCrmOwnerType::TryGetOwnerInfos(CCrmOwnerType::Contact, $ownerID, $owners, $ownerInfoOptions)) {
                     $additionalParents = array_merge($additionalParents, $owners);
                 }
             }
         }
         unset($binding);
         if (!empty($additionalParents)) {
             $liveFeedFields['PARENTS'] = array_merge($bindings, $additionalParents);
         }
         $arOwners = array_unique($arOwners);
     }
     self::PrepareStorageElementIDs($arFields);
     $arStorageElementID = $arFields["STORAGE_ELEMENT_IDS"];
     if (!empty($arStorageElementID)) {
         if ($arFields["STORAGE_TYPE_ID"] == StorageType::WebDav) {
             $liveFeedFields["UF_SONET_LOG_DOC"] = $arStorageElementID;
         } else {
             if ($arFields["STORAGE_TYPE_ID"] == StorageType::Disk) {
                 $liveFeedFields["UF_SONET_LOG_DOC"] = array();
                 //We have to add prefix Bitrix\Disk\Uf\FileUserType::NEW_FILE_PREFIX to file ID
                 foreach ($arStorageElementID as $elementID) {
                     $liveFeedFields["UF_SONET_LOG_DOC"][] = "n{$elementID}";
                 }
             } else {
                 $liveFeedFields["UF_SONET_LOG_FILE"] = $arStorageElementID;
             }
         }
     }
     if ($arFields['TYPE_ID'] == CCrmActivityType::Task && isset($arFields["ASSOCIATED_ENTITY_ID"]) && intval($arFields["ASSOCIATED_ENTITY_ID"]) > 0 && CModule::IncludeModule("tasks")) {
         $dbTask = CTasks::GetByID($arFields["ASSOCIATED_ENTITY_ID"], false);
         if ($arTask = $dbTask->Fetch()) {
             $ufDocID = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("TASKS_TASK", "UF_TASK_WEBDAV_FILES", $arTask["ID"], LANGUAGE_ID);
             if ($ufDocID) {
                 $liveFeedFields["UF_SONET_LOG_DOC"] = $ufDocID;
             }
         }
     }
     $bConvert = false;
     if ($arTask) {
         $rsLog = CSocNetLog::GetList(array(), array("EVENT_ID" => "tasks", "SOURCE_ID" => $arTask["ID"]), array("ID"));
         if ($arLog = $rsLog->Fetch()) {
             $eventID = CCrmLiveFeed::ConvertTasksLogEvent(array("LOG_ID" => $arLog["ID"], "ACTIVITY_ID" => $ID));
             $bConvert = true;
         }
     }
     if (!$bConvert) {
         $eventID = CCrmLiveFeed::CreateLogEvent($liveFeedFields, CCrmLiveFeedEvent::Add);
     }
     if (!(is_int($eventID) && $eventID > 0) && isset($liveFeedFields['ERROR'])) {
         $arFields['ERROR'] = $liveFeedFields['ERROR'];
     } else {
         if ($arTask) {
             $arTaskParticipant = CTaskNotifications::GetRecipientsIDs($arTask, false);
             $arSocnetRights = CTaskNotifications::__UserIDs2Rights($arTaskParticipant);
             if (isset($arTask['GROUP_ID']) && intval($arTask['GROUP_ID']) > 0) {
                 $perm = CSocNetFeaturesPerms::GetOperationPerm(SONET_ENTITY_GROUP, $arTask['GROUP_ID'], "tasks", "view");
                 $arSocnetRights = array_merge($arSocnetRights, array('SG' . $arTask['GROUP_ID'], 'SG' . $arTask['GROUP_ID'] . '_' . $perm));
             }
             CSocNetLogRights::DeleteByLogID($eventID);
             CSocNetLogRights::Add($eventID, $arSocnetRights);
         }
         if (intval($arFields["RESPONSIBLE_ID"]) > 0 && $arFields["RESPONSIBLE_ID"] != $userID && CModule::IncludeModule("im")) {
             $bHasPermissions = false;
             $perms = CCrmPerms::GetUserPermissions($arFields["RESPONSIBLE_ID"]);
             foreach ($arOwners as $arOwner) {
                 if (CCrmActivity::CheckReadPermission($arOwner["OWNER_TYPE_ID"], $arOwner["OWNER_ID"], $perms)) {
                     $bHasPermissions = true;
                     break;
                 }
             }
             switch ($arFields['TYPE_ID']) {
                 case CCrmActivityType::Call:
                     $type = 'CALL';
                     break;
                 case CCrmActivityType::Meeting:
                     $type = 'MEETING';
                     break;
                 default:
                     $type = false;
             }
             if ($bHasPermissions && $type) {
                 $url = "/crm/stream/?log_id=#log_id#";
                 $url = str_replace(array("#log_id#"), array($eventID), $url);
                 $serverName = (CMain::IsHTTPS() ? "https" : "http") . "://" . (defined("SITE_SERVER_NAME") && strlen(SITE_SERVER_NAME) > 0 ? SITE_SERVER_NAME : COption::GetOptionString("main", "server_name", ""));
                 $arMessageFields = array("MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $arFields["RESPONSIBLE_ID"], "FROM_USER_ID" => $userID, "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "crm", "LOG_ID" => $eventID, "NOTIFY_EVENT" => "activity_add", "NOTIFY_TAG" => "CRM|ACTIVITY|" . $ID, "NOTIFY_MESSAGE" => GetMessage("CRM_ACTIVITY_" . $type . "_RESPONSIBLE_IM_NOTIFY", array("#title#" => '<a href="' . $url . '">' . htmlspecialcharsbx($arFields['SUBJECT']) . '</a>')), "NOTIFY_MESSAGE_OUT" => GetMessage("CRM_ACTIVITY_" . $type . "_RESPONSIBLE_IM_NOTIFY", array("#title#" => htmlspecialcharsbx($arFields['SUBJECT']))) . " (" . $serverName . $url . ")");
                 CIMNotify::Add($arMessageFields);
             }
         }
     }
     return $eventID;
 }
Example #2
0
 /**
  * WARNING! This method is transitional and can be changed without 
  * any notifications! Don't use it.
  * 
  * @deprecated
  */
 public static function __deprecated_Add($commentText, $forumTopicId, $forumId, $nameTemplate, $arTask, $permissions, $commentId, $givenUserId, $imageWidth, $imageHeight, $arSmiles, $arForum, $messagesPerPage, $arUserGroupArray, $backPage, $strMsgAddComment, $strMsgEditComment, $strMsgNewTask, $componentName, &$outForumTopicId, &$arErrorCodes, &$outStrUrl, $arFieldsAdditional)
 {
     global $DB;
     if (is_array($arTask)) {
         if (!array_key_exists('~TITLE', $arTask)) {
             $arTmpTask = $arTask;
             foreach ($arTmpTask as $key => $value) {
                 if (substr($key, 0, 1) !== '~') {
                     $arTask['~' . $key] = $arTmpTask[$key];
                 }
             }
         }
     }
     $MID = 0;
     $TID = 0;
     if ($forumTopicId > 0 && CForumTopic::GetByID($forumTopicId) === false) {
         $forumTopicId = false;
     }
     if ($forumTopicId <= 0) {
         $arUserStart = array("ID" => intVal($arTask["CREATED_BY"]), "NAME" => $GLOBALS["FORUM_STATUS_NAME"]["guest"]);
         if ($arUserStart["ID"] > 0) {
             $res = array();
             $db_res = CForumUser::GetListEx(array(), array("USER_ID" => $arTask["CREATED_BY"]));
             if ($db_res && ($res = $db_res->Fetch())) {
                 $res["FORUM_USER_ID"] = intVal($res["ID"]);
                 $res["ID"] = $res["USER_ID"];
             } else {
                 $db_res = CUser::GetByID($arTask["CREATED_BY"]);
                 if ($db_res && ($res = $db_res->Fetch())) {
                     $res["SHOW_NAME"] = COption::GetOptionString("forum", "USER_SHOW_NAME", "Y");
                     $res["USER_PROFILE"] = "N";
                 }
             }
             if (!empty($res)) {
                 $arUserStart = $res;
                 $sName = $res["SHOW_NAME"] == "Y" ? trim(CUser::FormatName($nameTemplate, $res)) : "";
                 $arUserStart["NAME"] = empty($sName) ? trim($res["LOGIN"]) : $sName;
             }
         }
         $arUserStart["NAME"] = empty($arUserStart["NAME"]) ? $GLOBALS["FORUM_STATUS_NAME"]["guest"] : $arUserStart["NAME"];
         $DB->StartTransaction();
         $arFields = array("TITLE" => $arTask["~TITLE"], "FORUM_ID" => $forumId, "USER_START_ID" => $arUserStart["ID"], "USER_START_NAME" => $arUserStart["NAME"], "LAST_POSTER_NAME" => $arUserStart["NAME"], "APPROVED" => "Y", "PERMISSION_EXTERNAL" => $permissions, "PERMISSION" => $permissions, "NAME_TEMPLATE" => $nameTemplate, 'XML_ID' => 'TASK_' . $arTask['ID']);
         $TID = CForumTopic::Add($arFields);
         if (intVal($TID) <= 0) {
             $arErrorCodes[] = array('code' => 'topic is not created');
         } else {
             $arFields = array("FORUM_TOPIC_ID" => $TID);
             $task = new CTasks();
             $task->Update($arTask["ID"], $arFields);
         }
         if (!empty($arErrorCodes)) {
             $DB->Rollback();
             return false;
         } else {
             $DB->Commit();
         }
     }
     $arFieldsG = array("POST_MESSAGE" => $commentText, "AUTHOR_NAME" => '', "AUTHOR_EMAIL" => $GLOBALS['USER']->GetEmail(), "USE_SMILES" => NULL, "PARAM2" => $arTask['ID'], "TITLE" => $arTask["~TITLE"], "PERMISSION_EXTERNAL" => $permissions, "PERMISSION" => $permissions);
     // UF_* forwarding
     if (is_array($arFieldsAdditional)) {
         foreach ($arFieldsAdditional as $field => $value) {
             if (strlen($field) && substr($field, 0, 3) == 'UF_') {
                 $arFieldsG[$field] = $value;
                 $GLOBALS[$field] = $value;
                 // strange behaviour required for ForumMessageAdd() to handle UF_* properly
             }
         }
     }
     if (!empty($_FILES["REVIEW_ATTACH_IMG"])) {
         $arFieldsG["ATTACH_IMG"] = $_FILES["REVIEW_ATTACH_IMG"];
     } else {
         $arFiles = array();
         if (!empty($_REQUEST["FILES"])) {
             foreach ($_REQUEST["FILES"] as $key) {
                 $arFiles[$key] = array("FILE_ID" => $key);
                 if (!in_array($key, $_REQUEST["FILES_TO_UPLOAD"])) {
                     $arFiles[$key]["del"] = "Y";
                 }
             }
         }
         if (!empty($_FILES)) {
             $res = array();
             foreach ($_FILES as $key => $val) {
                 if (substr($key, 0, strLen("FILE_NEW")) == "FILE_NEW" && !empty($val["name"])) {
                     $arFiles[] = $_FILES[$key];
                 }
             }
         }
         if (!empty($arFiles)) {
             $arFieldsG["FILES"] = $arFiles;
         }
     }
     $TOPIC_ID = $forumTopicId > 0 ? $forumTopicId : $TID;
     $MESSAGE_ID = 0;
     $MESSAGE_TYPE = $TOPIC_ID > 0 ? "REPLY" : "NEW";
     if (COption::GetOptionString("tasks", "task_comment_allow_edit") && ($MESSAGE_ID = intval($commentId))) {
         $MESSAGE_TYPE = "EDIT";
     }
     $strErrorMessage = '';
     $strOKMessage = '';
     $MID = ForumAddMessage($MESSAGE_TYPE, $forumId, $TOPIC_ID, $MESSAGE_ID, $arFieldsG, $strErrorMessage, $strOKMessage, false, $_POST["captcha_word"], 0, $_POST["captcha_code"], $nameTemplate);
     if ($MID <= 0 || !empty($strErrorMessage)) {
         $arErrorCodes[] = array('code' => 'message is not added 2', 'title' => empty($strErrorMessage) ? NULL : $strErrorMessage);
     } else {
         $arMessage = CForumMessage::GetByID($MID);
         if ($forumTopicId <= 0) {
             $forumTopicId = $TID = intVal($arMessage["TOPIC_ID"]);
         }
         $outForumTopicId = intVal($forumTopicId);
         if ($componentName !== null) {
             ForumClearComponentCache($componentName);
         }
         // NOTIFICATION
         $arTask["ACCOMPLICES"] = $arTask["AUDITORS"] = array();
         $rsMembers = CTaskMembers::GetList(array(), array("TASK_ID" => $arTask["ID"]));
         while ($arMember = $rsMembers->Fetch()) {
             if ($arMember["TYPE"] == "A") {
                 $arTask["ACCOMPLICES"][] = $arMember["USER_ID"];
             } elseif ($arMember["TYPE"] == "U") {
                 $arTask["AUDITORS"][] = $arMember["USER_ID"];
             }
         }
         $arEmailUserIDs = array($arTask["RESPONSIBLE_ID"], $arTask["CREATED_BY"]);
         $arEmailUserIDs = array_unique(array_merge($arEmailUserIDs, $arTask["ACCOMPLICES"], $arTask["AUDITORS"]));
         $currentUserPos = array_search($givenUserId, $arEmailUserIDs);
         if ($currentUserPos !== false) {
             unset($arEmailUserIDs[$currentUserPos]);
         }
         $parser = new CTextParser();
         $parser->imageWidth = $imageWidth;
         $parser->imageHeight = $imageHeight;
         $parser->smiles = $arSmiles;
         $parser->allow = array("HTML" => $arForum["ALLOW_HTML"], "ANCHOR" => $arForum["ALLOW_ANCHOR"], "BIU" => $arForum["ALLOW_BIU"], "IMG" => "N", "VIDEO" => "N", "LIST" => $arForum["ALLOW_LIST"], "QUOTE" => $arForum["ALLOW_QUOTE"], "CODE" => $arForum["ALLOW_CODE"], "FONT" => $arForum["ALLOW_FONT"], "SMILES" => "N", "UPLOAD" => $arForum["ALLOW_UPLOAD"], "NL2BR" => $arForum["ALLOW_NL2BR"], "TABLE" => "Y");
         $arAllow = NULL;
         $MESSAGE = HTMLToTxt($parser->convertText($commentText, $arAllow));
         // remove [ url] for socialnetwork log
         $MESSAGE = preg_replace("/(\\s\\[\\s(http:\\/\\/|https:\\/\\/|ftp:\\/\\/))(.*?)(\\s\\])/is", "", $MESSAGE);
         $parser->allow = array("HTML" => 'Y', "ANCHOR" => 'Y', "BIU" => 'Y', "IMG" => "Y", "VIDEO" => "Y", "LIST" => 'N', "QUOTE" => 'Y', "CODE" => 'Y', "FONT" => 'Y', "SMILES" => "N", "UPLOAD" => 'N', "NL2BR" => 'N', "TABLE" => "Y");
         $message_notify = $parser->convertText($commentText);
         $arRecipientsIDs = CTaskNotifications::GetRecipientsIDs($arTask);
         // Instant Messages
         if (IsModuleInstalled("im") && CModule::IncludeModule("im") && sizeof($arRecipientsIDs)) {
             $pageNumber = CForumMessage::GetMessagePage($MID, $messagesPerPage, $arUserGroupArray);
             // There are different links for extranet users
             $isExtranetEnabled = false;
             if (CModule::IncludeModule("extranet")) {
                 $isExtranetEnabled = true;
             }
             if ($isExtranetEnabled) {
                 $arSites = array();
                 $dbSite = CSite::GetList($by = "sort", $order = "desc", array("ACTIVE" => "Y"));
                 while ($arSite = $dbSite->Fetch()) {
                     if (strlen(trim($arSite["DIR"])) > 0) {
                         $arSites[$arSite['ID']]['DIR'] = $arSite['DIR'];
                     } else {
                         $arSites[$arSite['ID']]['DIR'] = '/';
                     }
                     if (strlen(trim($arSite["SERVER_NAME"])) > 0) {
                         $arSites[$arSite['ID']]['SERVER_NAME'] = $arSite["SERVER_NAME"];
                     } else {
                         $arSites[$arSite['ID']]['SERVER_NAME'] = COption::GetOptionString("main", "server_name", $_SERVER["HTTP_HOST"]);
                     }
                     $arSites[$arSite['ID']]['urlPrefix'] = $arSites[$arSite['ID']]['SERVER_NAME'] . $arSites[$arSite['ID']]['DIR'];
                     // remove last '/'
                     if (strlen($arSites[$arSite['ID']]['urlPrefix']) > 0 && substr($arSites[$arSite['ID']]['urlPrefix'], -1) === '/') {
                         $arSites[$arSite['ID']]['urlPrefix'] = substr($arSites[$arSite['ID']]['urlPrefix'], 0, -1);
                     }
                 }
                 $extranet_site_id = CExtranet::GetExtranetSiteID();
                 $intranet_site_id = CSite::GetDefSite();
                 $arIntranetUsers = CExtranet::GetIntranetUsers();
             } else {
                 if ($arTask["GROUP_ID"]) {
                     $pathTemplateWoExtranet = str_replace("#group_id#", $arTask["GROUP_ID"], COption::GetOptionString("tasks", "paths_task_group_entry", "/workgroups/group/#group_id#/tasks/task/view/#task_id#/", $arFields["SITE_ID"]));
                 } else {
                     $pathTemplateWoExtranet = COption::GetOptionString("tasks", "paths_task_user_entry", "/company/personal/user/#user_id#/tasks/task/view/#task_id#/", $arFields["SITE_ID"]);
                 }
             }
             foreach ($arRecipientsIDs as $userID) {
                 $urlPrefixForUser = tasksServerName();
                 if ($isExtranetEnabled) {
                     if (!in_array($userID, $arIntranetUsers) && $extranet_site_id) {
                         $userSiteId = $extranet_site_id;
                     } else {
                         $userSiteId = $intranet_site_id;
                     }
                     if (isset($arSites[$userSiteId]['SERVER_NAME'])) {
                         $urlPrefixForUser = tasksServerName($arSites[$userSiteId]['SERVER_NAME']);
                     }
                     if ($arTask["GROUP_ID"]) {
                         $pathTemplate = str_replace('#group_id#', $arTask['GROUP_ID'], CTasksTools::GetOptionPathTaskGroupEntry($userSiteId, ''));
                     } else {
                         $pathTemplate = CTasksTools::GetOptionPathTaskUserEntry($userSiteId, '');
                     }
                 } else {
                     $pathTemplate = $pathTemplateWoExtranet;
                 }
                 $NOTIFY_MESSAGE_TITLE_TEMPLATE = '';
                 $messageUrl = '';
                 if (strlen($pathTemplate) > 0) {
                     $groupId = 0;
                     if (isset($arTask['GROUP_ID'])) {
                         $groupId = (int) $arTask['GROUP_ID'];
                     }
                     $messageUrl = $urlPrefixForUser . CComponentEngine::MakePathFromTemplate($pathTemplate, array("user_id" => $userID, "task_id" => $arTask["ID"], "action" => "view", "USER_ID" => $userID, "TASK_ID" => $arTask["ID"], "ACTION" => "view", 'GROUP_ID' => $groupId, 'group_id' => $groupId));
                     if ($pageNumber > 1) {
                         $messageUrl .= (strpos($messageUrl, "?") === false ? "?" : "&") . "MID=" . $MID;
                     }
                     $NOTIFY_MESSAGE_TITLE_TEMPLATE = '[URL=' . $messageUrl . "#message" . $MID . ']' . $arTask["~TITLE"] . '[/URL]';
                 } else {
                     $NOTIFY_MESSAGE_TITLE_TEMPLATE = $arTask["~TITLE"];
                 }
                 $MESSAGE_SITE = trim(htmlspecialcharsbx(strip_tags(str_replace(array("\r\n", "\n", "\r"), ' ', htmlspecialcharsback($message_notify)))));
                 $MESSAGE_EMAIL = $MESSAGE_SITE;
                 // full message to email
                 if (strlen($MESSAGE_SITE) >= 100) {
                     $dot = '...';
                     $MESSAGE_SITE = substr($MESSAGE_SITE, 0, 99);
                     if (($lastLinkPosition = strrpos($MESSAGE_SITE, 'http://')) !== false || ($lastLinkPosition = strrpos($MESSAGE_SITE, 'https://')) !== false || ($lastLinkPosition = strrpos($MESSAGE_SITE, 'ftp://')) !== false || ($lastLinkPosition = strrpos($MESSAGE_SITE, 'ftps://')) !== false) {
                         if (strpos($MESSAGE_SITE, ' ', $lastLinkPosition) === false) {
                             $MESSAGE_SITE = substr($MESSAGE_SITE, 0, $lastLinkPosition);
                         }
                     }
                     $MESSAGE_SITE .= $dot;
                 }
                 $arMessageFields = array("TO_USER_ID" => $userID, "FROM_USER_ID" => $givenUserId, "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "tasks", "NOTIFY_EVENT" => "comment", "NOTIFY_MESSAGE" => str_replace(array("#TASK_TITLE#", "#TASK_COMMENT_TEXT#"), array($NOTIFY_MESSAGE_TITLE_TEMPLATE, '[COLOR=#000000]' . $MESSAGE_SITE . '[/COLOR]'), $MESSAGE_TYPE != "EDIT" ? $strMsgAddComment : $strMsgEditComment), "NOTIFY_MESSAGE_OUT" => str_replace(array("#TASK_TITLE#", "#TASK_COMMENT_TEXT#"), array($arTask["~TITLE"], $MESSAGE_EMAIL . ' #BR# ' . $messageUrl . "#message" . $MID . ' '), $MESSAGE_TYPE != "EDIT" ? $strMsgAddComment : $strMsgEditComment));
                 CIMNotify::Add($arMessageFields);
             }
         }
         $strURL = !empty($backPage) ? $backPage : $GLOBALS['APPLICATION']->GetCurPageParam("", array("IFRAME", "MID", "SEF_APPLICATION_CUR_PAGE_URL", BX_AJAX_PARAM_ID, "result"));
         $strURL = ForumAddPageParams($strURL, array("MID" => $MID, "result" => $arForum["MODERATION"] != "Y" || CForumNew::CanUserModerateForum($forumId, $arUserGroupArray) ? "reply" : "not_approved"), false, false);
         $outStrUrl = $strURL;
         // sonet log
         if (CModule::IncludeModule("socialnetwork")) {
             $dbRes = CSocNetLog::GetList(array("ID" => "DESC"), array("EVENT_ID" => "tasks", "SOURCE_ID" => $arTask["ID"]), false, false, array("ID", "ENTITY_TYPE", "ENTITY_ID", "TMP_ID"));
             if ($arRes = $dbRes->Fetch()) {
                 $log_id = $arRes["TMP_ID"];
                 $entity_type = $arRes["ENTITY_TYPE"];
                 $entity_id = $arRes["ENTITY_ID"];
             } else {
                 $entity_type = $arTask["GROUP_ID"] ? SONET_ENTITY_GROUP : SONET_ENTITY_USER;
                 $entity_id = $arTask["GROUP_ID"] ? $arTask["GROUP_ID"] : $arTask["CREATED_BY"];
                 $rsUser = CUser::GetByID($arTask["CREATED_BY"]);
                 if ($arUser = $rsUser->Fetch()) {
                     $arSoFields = array("ENTITY_TYPE" => $entity_type, "ENTITY_ID" => $entity_id, "EVENT_ID" => "tasks", "LOG_DATE" => $arTask["CREATED_DATE"], "TITLE_TEMPLATE" => "#TITLE#", "TITLE" => htmlspecialcharsBack($arTask["~TITLE"]), "MESSAGE" => "", "TEXT_MESSAGE" => $strMsgNewTask, "MODULE_ID" => "tasks", "CALLBACK_FUNC" => false, "SOURCE_ID" => $arTask["ID"], "ENABLE_COMMENTS" => "Y", "USER_ID" => $arTask["CREATED_BY"], "URL" => CTaskNotifications::GetNotificationPath($arUser, $arTask["ID"]), "PARAMS" => serialize(array("TYPE" => "create")));
                     $log_id = CSocNetLog::Add($arSoFields, false);
                     if (intval($log_id) > 0) {
                         CSocNetLog::Update($log_id, array("TMP_ID" => $log_id));
                         $arRights = CTaskNotifications::__UserIDs2Rights(CTaskNotifications::GetRecipientsIDs($arTask, false));
                         if ($arTask["GROUP_ID"]) {
                             $arRights[] = "S" . SONET_ENTITY_GROUP . $arTask["GROUP_ID"];
                         }
                         CSocNetLogRights::Add($log_id, $arRights);
                     }
                 }
             }
             if (intval($log_id) > 0) {
                 $sText = COption::GetOptionString("forum", "FILTER", "Y") == "Y" ? $arMessage["POST_MESSAGE_FILTER"] : $arMessage["POST_MESSAGE"];
                 CSocNetLog::Update($log_id, array('PARAMS' => serialize(array('TYPE' => 'comment'))));
                 $arFieldsForSocnet = array("ENTITY_TYPE" => $entity_type, "ENTITY_ID" => $entity_id, "EVENT_ID" => "tasks_comment", "MESSAGE" => $sText, "TEXT_MESSAGE" => $parser->convert4mail($sText), "URL" => str_replace("?IFRAME=Y", "", str_replace("&IFRAME=Y", "", str_replace("IFRAME=Y&", "", $strURL))), "MODULE_ID" => "tasks", "SOURCE_ID" => $MID, "LOG_ID" => $log_id, "RATING_TYPE_ID" => "FORUM_POST", "RATING_ENTITY_ID" => $MID);
                 if ($MESSAGE_TYPE == "EDIT") {
                     $dbRes = CSocNetLogComments::GetList(array("ID" => "DESC"), array("EVENT_ID" => array("tasks_comment"), "SOURCE_ID" => $MID), false, false, array("ID"));
                     while ($arRes = $dbRes->Fetch()) {
                         CSocNetLogComments::Update($arRes["ID"], $arFieldsForSocnet);
                     }
                 } else {
                     $arFieldsForSocnet['USER_ID'] = $givenUserId;
                     $arFieldsForSocnet['=LOG_DATE'] = $GLOBALS['DB']->CurrentTimeFunction();
                     $ufFileID = array();
                     $dbAddedMessageFiles = CForumFiles::GetList(array("ID" => "ASC"), array("MESSAGE_ID" => $MID));
                     while ($arAddedMessageFiles = $dbAddedMessageFiles->Fetch()) {
                         $ufFileID[] = $arAddedMessageFiles["FILE_ID"];
                     }
                     if (count($ufFileID) > 0) {
                         $arFieldsForSocnet["UF_SONET_COM_FILE"] = $ufFileID;
                     }
                     $ufDocID = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MESSAGE_DOC", $MID, LANGUAGE_ID);
                     if ($ufDocID) {
                         $arFieldsForSocnet["UF_SONET_COM_DOC"] = $ufDocID;
                     }
                     $ufDocVer = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MESSAGE_VER", $MID, LANGUAGE_ID);
                     if ($ufDocVer) {
                         $arFieldsForSocnet["UF_SONET_COM_VER"] = $ufDocVer;
                     }
                     $comment_id = CSocNetLogComments::Add($arFieldsForSocnet, false, false);
                     CSocNetLog::CounterIncrement($comment_id, false, false, "LC");
                 }
             }
         }
         $occurAsUserId = CTasksTools::getOccurAsUserId();
         if (!$occurAsUserId) {
             $occurAsUserId = $arMessage["AUTHOR_ID"] ? $arMessage["AUTHOR_ID"] : 1;
         }
         // Tasks log
         $arLogFields = array("TASK_ID" => $arTask["ID"], "USER_ID" => $occurAsUserId, "CREATED_DATE" => $arMessage["EDIT_DATE"] ? ConvertTimeStamp(MakeTimeStamp($arMessage["EDIT_DATE"], CSite::GetDateFormat()), "FULL") : $arMessage["POST_DATE"], "FIELD" => "COMMENT", "TO_VALUE" => $MID);
         $log = new CTaskLog();
         $log->Add($arLogFields);
     }
     return $MID;
     // Message id
 }
Example #3
0
 /**
  * WARNING! This method is transitional and can be changed without 
  * any notifications! Don't use it.
  * 
  * @deprecated
  */
 public static function __deprecated_Add($commentText, $forumTopicId, $forumId, $nameTemplate, $arTask, $permissions, $commentId, $givenUserId, $imageWidth, $imageHeight, $arSmiles, $arForum, $messagesPerPage, $arUserGroupArray, $backPage, $strMsgAddComment, $strMsgEditComment, $strMsgNewTask, $componentName, &$outForumTopicId, &$arErrorCodes, &$outStrUrl, $arFieldsAdditional = array())
 {
     global $DB;
     if (is_array($arTask)) {
         if (!array_key_exists('~TITLE', $arTask)) {
             $arTmpTask = $arTask;
             foreach ($arTmpTask as $key => $value) {
                 if (substr($key, 0, 1) !== '~') {
                     $arTask['~' . $key] = $arTmpTask[$key];
                 }
             }
         }
     }
     $MID = 0;
     $TID = 0;
     if ($forumTopicId > 0 && CForumTopic::GetByID($forumTopicId) === false) {
         $forumTopicId = false;
     }
     if ($forumTopicId <= 0) {
         $arUserStart = array("ID" => intVal($arTask["CREATED_BY"]), "NAME" => $GLOBALS["FORUM_STATUS_NAME"]["guest"]);
         if ($arUserStart["ID"] > 0) {
             $res = array();
             $db_res = CForumUser::GetListEx(array(), array("USER_ID" => $arTask["CREATED_BY"]));
             if ($db_res && ($res = $db_res->Fetch())) {
                 $res["FORUM_USER_ID"] = intVal($res["ID"]);
                 $res["ID"] = $res["USER_ID"];
             } else {
                 $db_res = CUser::GetByID($arTask["CREATED_BY"]);
                 if ($db_res && ($res = $db_res->Fetch())) {
                     $res["SHOW_NAME"] = COption::GetOptionString("forum", "USER_SHOW_NAME", "Y");
                     $res["USER_PROFILE"] = "N";
                 }
             }
             if (!empty($res)) {
                 $arUserStart = $res;
                 $sName = $res["SHOW_NAME"] == "Y" ? trim(CUser::FormatName($nameTemplate, $res)) : "";
                 $arUserStart["NAME"] = empty($sName) ? trim($res["LOGIN"]) : $sName;
             }
         }
         $arUserStart["NAME"] = empty($arUserStart["NAME"]) ? $GLOBALS["FORUM_STATUS_NAME"]["guest"] : $arUserStart["NAME"];
         $DB->StartTransaction();
         $arFields = array("TITLE" => $arTask["~TITLE"], "FORUM_ID" => $forumId, "USER_START_ID" => $arUserStart["ID"], "USER_START_NAME" => $arUserStart["NAME"], "LAST_POSTER_NAME" => $arUserStart["NAME"], "APPROVED" => "Y", "PERMISSION_EXTERNAL" => $permissions, "PERMISSION" => $permissions, "NAME_TEMPLATE" => $nameTemplate, 'XML_ID' => 'TASK_' . $arTask['ID']);
         $TID = CForumTopic::Add($arFields);
         if (intVal($TID) <= 0) {
             $arErrorCodes[] = array('code' => 'topic is not created');
         } else {
             $arFields = array("FORUM_TOPIC_ID" => $TID);
             $task = new CTasks();
             $task->Update($arTask["ID"], $arFields);
         }
         if (!empty($arErrorCodes)) {
             $DB->Rollback();
             return false;
         } else {
             $DB->Commit();
         }
     }
     $arFieldsG = array("POST_MESSAGE" => $commentText, "AUTHOR_NAME" => '', "AUTHOR_EMAIL" => $GLOBALS['USER']->GetEmail(), "USE_SMILES" => NULL, "PARAM2" => $arTask['ID'], "TITLE" => $arTask["~TITLE"], "PERMISSION_EXTERNAL" => $permissions, "PERMISSION" => $permissions);
     // UF_* forwarding
     if (is_array($arFieldsAdditional)) {
         foreach ($arFieldsAdditional as $field => $value) {
             if (strlen($field) && substr($field, 0, 3) == 'UF_') {
                 $arFieldsG[$field] = $value;
                 $GLOBALS[$field] = $value;
                 // strange behaviour required for ForumMessageAdd() to handle UF_* properly
             }
         }
     }
     if (!empty($_FILES["REVIEW_ATTACH_IMG"])) {
         $arFieldsG["ATTACH_IMG"] = $_FILES["REVIEW_ATTACH_IMG"];
     } else {
         $arFiles = array();
         if (!empty($_REQUEST["FILES"])) {
             foreach ($_REQUEST["FILES"] as $key) {
                 $arFiles[$key] = array("FILE_ID" => $key);
                 if (!in_array($key, $_REQUEST["FILES_TO_UPLOAD"])) {
                     $arFiles[$key]["del"] = "Y";
                 }
             }
         }
         if (!empty($_FILES)) {
             $res = array();
             foreach ($_FILES as $key => $val) {
                 if (substr($key, 0, strLen("FILE_NEW")) == "FILE_NEW" && !empty($val["name"])) {
                     $arFiles[] = $_FILES[$key];
                 }
             }
         }
         if (!empty($arFiles)) {
             $arFieldsG["FILES"] = $arFiles;
         }
     }
     $TOPIC_ID = $forumTopicId > 0 ? $forumTopicId : $TID;
     $MESSAGE_ID = 0;
     $MESSAGE_TYPE = $TOPIC_ID > 0 ? "REPLY" : "NEW";
     if (COption::GetOptionString("tasks", "task_comment_allow_edit") && ($MESSAGE_ID = intval($commentId))) {
         $MESSAGE_TYPE = "EDIT";
     }
     $strErrorMessage = '';
     $strOKMessage = '';
     $MID = ForumAddMessage($MESSAGE_TYPE, $forumId, $TOPIC_ID, $MESSAGE_ID, $arFieldsG, $strErrorMessage, $strOKMessage, false, $_POST["captcha_word"], 0, $_POST["captcha_code"], $nameTemplate);
     if ($MID <= 0 || !empty($strErrorMessage)) {
         $arErrorCodes[] = array('code' => 'message is not added 2', 'title' => empty($strErrorMessage) ? NULL : $strErrorMessage);
     } else {
         $arMessage = CForumMessage::GetByID($MID);
         if ($forumTopicId <= 0) {
             $forumTopicId = $TID = intVal($arMessage["TOPIC_ID"]);
         }
         $outForumTopicId = intVal($forumTopicId);
         if ($componentName !== null) {
             ForumClearComponentCache($componentName);
         }
         // NOTIFICATION
         $arTask["ACCOMPLICES"] = $arTask["AUDITORS"] = array();
         $rsMembers = CTaskMembers::GetList(array(), array("TASK_ID" => $arTask["ID"]));
         while ($arMember = $rsMembers->Fetch()) {
             if ($arMember["TYPE"] == "A") {
                 $arTask["ACCOMPLICES"][] = $arMember["USER_ID"];
             } elseif ($arMember["TYPE"] == "U") {
                 $arTask["AUDITORS"][] = $arMember["USER_ID"];
             }
         }
         $arEmailUserIDs = array($arTask["RESPONSIBLE_ID"], $arTask["CREATED_BY"]);
         $arEmailUserIDs = array_unique(array_merge($arEmailUserIDs, $arTask["ACCOMPLICES"], $arTask["AUDITORS"]));
         $currentUserPos = array_search($givenUserId, $arEmailUserIDs);
         if ($currentUserPos !== false) {
             unset($arEmailUserIDs[$currentUserPos]);
         }
         $parser = new CTextParser();
         $parser->imageWidth = $imageWidth;
         $parser->imageHeight = $imageHeight;
         $parser->smiles = $arSmiles;
         $parser->allow = array("HTML" => $arForum["ALLOW_HTML"], "ANCHOR" => $arForum["ALLOW_ANCHOR"], "BIU" => $arForum["ALLOW_BIU"], "IMG" => "N", "VIDEO" => "N", "LIST" => $arForum["ALLOW_LIST"], "QUOTE" => $arForum["ALLOW_QUOTE"], "CODE" => $arForum["ALLOW_CODE"], "FONT" => $arForum["ALLOW_FONT"], "SMILES" => "N", "UPLOAD" => $arForum["ALLOW_UPLOAD"], "NL2BR" => $arForum["ALLOW_NL2BR"], "TABLE" => "Y");
         $arAllow = NULL;
         $MESSAGE = HTMLToTxt($parser->convertText($commentText, $arAllow));
         // remove [ url] for socialnetwork log
         $MESSAGE = preg_replace("/(\\s\\[\\s(http:\\/\\/|https:\\/\\/|ftp:\\/\\/))(.*?)(\\s\\])/is", "", $MESSAGE);
         $parser->allow = array("HTML" => 'Y', "ANCHOR" => 'Y', "BIU" => 'Y', "IMG" => "Y", "VIDEO" => "Y", "LIST" => 'N', "QUOTE" => 'Y', "CODE" => 'Y', "FONT" => 'Y', "SMILES" => "N", "UPLOAD" => 'N', "NL2BR" => 'N', "TABLE" => "Y");
         $message_notify = $parser->convertText($commentText);
         $arRecipientsIDs = CTaskNotifications::GetRecipientsIDs($arTask);
         // Instant Messages
         if (IsModuleInstalled("im") && CModule::IncludeModule("im") && sizeof($arRecipientsIDs)) {
             // this entire class is deprecated. disable edit message, add only here
             if ($MESSAGE_TYPE != 'EDIT') {
                 CTaskComments::sendAddMessage(array('ID' => $MID, 'POST_MESSAGE' => $message_notify), $arTask, $givenUserId, $arRecipientsIDs, array());
             }
         }
         $strURL = !empty($backPage) ? $backPage : $GLOBALS['APPLICATION']->GetCurPageParam("", array("IFRAME", "MID", "SEF_APPLICATION_CUR_PAGE_URL", BX_AJAX_PARAM_ID, "result"));
         $strURL = ForumAddPageParams($strURL, array("MID" => $MID, "result" => $arForum["MODERATION"] != "Y" || CForumNew::CanUserModerateForum($forumId, $arUserGroupArray) ? "reply" : "not_approved"), false, false);
         $outStrUrl = $strURL;
         // sonet log
         if (CModule::IncludeModule("socialnetwork")) {
             $dbRes = CSocNetLog::GetList(array("ID" => "DESC"), array("EVENT_ID" => "tasks", "SOURCE_ID" => $arTask["ID"]), false, false, array("ID", "ENTITY_TYPE", "ENTITY_ID", "TMP_ID"));
             if ($arRes = $dbRes->Fetch()) {
                 $log_id = $arRes["TMP_ID"];
                 $entity_type = $arRes["ENTITY_TYPE"];
                 $entity_id = $arRes["ENTITY_ID"];
             } else {
                 $entity_type = $arTask["GROUP_ID"] ? SONET_ENTITY_GROUP : SONET_ENTITY_USER;
                 $entity_id = $arTask["GROUP_ID"] ? $arTask["GROUP_ID"] : $arTask["CREATED_BY"];
                 $rsUser = CUser::GetByID($arTask["CREATED_BY"]);
                 if ($arUser = $rsUser->Fetch()) {
                     $arSoFields = array("ENTITY_TYPE" => $entity_type, "ENTITY_ID" => $entity_id, "EVENT_ID" => "tasks", "LOG_DATE" => $arTask["CREATED_DATE"], "TITLE_TEMPLATE" => "#TITLE#", "TITLE" => htmlspecialcharsBack($arTask["~TITLE"]), "MESSAGE" => "", "TEXT_MESSAGE" => $strMsgNewTask, "MODULE_ID" => "tasks", "CALLBACK_FUNC" => false, "SOURCE_ID" => $arTask["ID"], "ENABLE_COMMENTS" => "Y", "USER_ID" => $arTask["CREATED_BY"], "URL" => CTaskNotifications::GetNotificationPath($arUser, $arTask["ID"]), "PARAMS" => serialize(array("TYPE" => "create")));
                     $log_id = CSocNetLog::Add($arSoFields, false);
                     if (intval($log_id) > 0) {
                         CSocNetLog::Update($log_id, array("TMP_ID" => $log_id));
                         $arRights = CTaskNotifications::__UserIDs2Rights(CTaskNotifications::GetRecipientsIDs($arTask, false));
                         if ($arTask["GROUP_ID"]) {
                             $arRights[] = "S" . SONET_ENTITY_GROUP . $arTask["GROUP_ID"];
                         }
                         CSocNetLogRights::Add($log_id, $arRights);
                     }
                 }
             }
             if (intval($log_id) > 0) {
                 $sText = COption::GetOptionString("forum", "FILTER", "Y") == "Y" ? $arMessage["POST_MESSAGE_FILTER"] : $arMessage["POST_MESSAGE"];
                 CSocNetLog::Update($log_id, array('PARAMS' => serialize(array('TYPE' => 'comment'))));
                 $arFieldsForSocnet = array("ENTITY_TYPE" => $entity_type, "ENTITY_ID" => $entity_id, "EVENT_ID" => "tasks_comment", "MESSAGE" => $sText, "TEXT_MESSAGE" => $parser->convert4mail($sText), "URL" => str_replace("?IFRAME=Y", "", str_replace("&IFRAME=Y", "", str_replace("IFRAME=Y&", "", $strURL))), "MODULE_ID" => "tasks", "SOURCE_ID" => $MID, "LOG_ID" => $log_id, "RATING_TYPE_ID" => "FORUM_POST", "RATING_ENTITY_ID" => $MID);
                 if ($MESSAGE_TYPE == "EDIT") {
                     $dbRes = CSocNetLogComments::GetList(array("ID" => "DESC"), array("EVENT_ID" => array("tasks_comment"), "SOURCE_ID" => $MID), false, false, array("ID"));
                     while ($arRes = $dbRes->Fetch()) {
                         CSocNetLogComments::Update($arRes["ID"], $arFieldsForSocnet);
                     }
                 } else {
                     $arFieldsForSocnet['USER_ID'] = $givenUserId;
                     $arFieldsForSocnet['=LOG_DATE'] = $GLOBALS['DB']->CurrentTimeFunction();
                     $ufFileID = array();
                     $dbAddedMessageFiles = CForumFiles::GetList(array("ID" => "ASC"), array("MESSAGE_ID" => $MID));
                     while ($arAddedMessageFiles = $dbAddedMessageFiles->Fetch()) {
                         $ufFileID[] = $arAddedMessageFiles["FILE_ID"];
                     }
                     if (count($ufFileID) > 0) {
                         $arFieldsForSocnet["UF_SONET_COM_FILE"] = $ufFileID;
                     }
                     $ufDocID = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MESSAGE_DOC", $MID, LANGUAGE_ID);
                     if ($ufDocID) {
                         $arFieldsForSocnet["UF_SONET_COM_DOC"] = $ufDocID;
                     }
                     $ufDocVer = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MESSAGE_VER", $MID, LANGUAGE_ID);
                     if ($ufDocVer) {
                         $arFieldsForSocnet["UF_SONET_COM_VER"] = $ufDocVer;
                     }
                     if (isset($arFieldsAdditional["ANCILLARY"]) && $arFieldsAdditional["ANCILLARY"]) {
                         CSocNetLogComments::Add($arFieldsForSocnet, false, false, false);
                     } else {
                         $comment_id = CSocNetLogComments::Add($arFieldsForSocnet, false, false);
                         CSocNetLog::CounterIncrement($comment_id, false, false, "LC");
                     }
                 }
             }
         }
         $occurAsUserId = CTasksTools::getOccurAsUserId();
         if (!$occurAsUserId) {
             $occurAsUserId = $arMessage["AUTHOR_ID"] ? $arMessage["AUTHOR_ID"] : 1;
         }
         // Tasks log
         $arLogFields = array("TASK_ID" => $arTask["ID"], "USER_ID" => $occurAsUserId, "CREATED_DATE" => $arMessage["EDIT_DATE"] ? ConvertTimeStamp(MakeTimeStamp($arMessage["EDIT_DATE"], CSite::GetDateFormat()), "FULL") : $arMessage["POST_DATE"], "FIELD" => "COMMENT", "TO_VALUE" => $MID);
         $log = new CTaskLog();
         $log->Add($arLogFields);
     }
     return $MID;
     // Message id
 }
 protected static function SendMessageToSocNet($arFields, $bSpawnedByAgent, $arChanges = null, $arTask = null, array $parameters = array())
 {
     global $USER, $DB;
     $effectiveUserId = self::getEffectiveUserId($arFields, array(), $bSpawnedByAgent, $parameters);
     if (!CModule::IncludeModule('socialnetwork')) {
         return null;
     }
     $bCrmTask = isset($arTask) && isset($arTask["UF_CRM_TASK"]) && (is_array($arTask["UF_CRM_TASK"]) && (isset($arTask["UF_CRM_TASK"][0]) && strlen($arTask["UF_CRM_TASK"][0]) > 0) || !is_array($arTask["UF_CRM_TASK"]) && strlen($arTask["UF_CRM_TASK"]) > 0);
     $arLogFilter = false;
     if (!$bCrmTask) {
         $arLogFilter = array("EVENT_ID" => "tasks", "SOURCE_ID" => $arTask["ID"]);
     } elseif (CModule::IncludeModule("crm")) {
         $dbCrmActivity = CCrmActivity::GetList(array(), array('TYPE_ID' => CCrmActivityType::Task, 'ASSOCIATED_ENTITY_ID' => $arTask["ID"], 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID'));
         if ($arCrmActivity = $dbCrmActivity->Fetch()) {
             $arLogFilter = array("EVENT_ID" => "crm_activity_add", "ENTITY_ID" => $arCrmActivity["ID"]);
         }
     }
     if (!$arLogFilter) {
         return null;
     }
     static $arCheckedUsers = array();
     // users that checked for their existing
     static $cachedSiteTimeFormat = -1;
     // select "real" author
     $occurAsUserId = CTasksTools::getOccurAsUserId();
     if (!$occurAsUserId) {
         $occurAsUserId = $effectiveUserId;
     }
     if ($cachedSiteTimeFormat === -1) {
         $cachedSiteTimeFormat = CSite::GetDateFormat('FULL', SITE_ID);
     }
     static $cachedAllSitesIds = -1;
     if ($cachedAllSitesIds === -1) {
         $cachedAllSitesIds = array();
         $dbSite = CSite::GetList($by = 'sort', $order = 'desc', array('ACTIVE' => 'Y'));
         while ($arSite = $dbSite->Fetch()) {
             $cachedAllSitesIds[] = $arSite['ID'];
         }
     }
     // Check that user exists
     if (!in_array((int) $arFields["CREATED_BY"], $arCheckedUsers, true)) {
         $rsUser = CUser::GetList($by = 'ID', $order = 'ASC', array('ID' => $arFields["CREATED_BY"]), array('FIELDS' => array('ID')));
         if (!($arUser = $rsUser->Fetch())) {
             return false;
         }
         $arCheckedUsers[] = (int) $arFields["CREATED_BY"];
     }
     if (is_array($arChanges)) {
         if (count($arChanges) == 0) {
             $rsSocNetLogItems = CSocNetLog::GetList(array("ID" => "DESC"), $arLogFilter, false, false, array("ID", "ENTITY_TYPE", "ENTITY_ID"));
             while ($arRes = $rsSocNetLogItems->Fetch()) {
                 $authorUserId = false;
                 if (isset($arFields['CREATED_BY'])) {
                     $authorUserId = (int) $arFields['CREATED_BY'];
                 } elseif (isset($arTask['CREATED_BY'])) {
                     $authorUserId = (int) $arTask['CREATED_BY'];
                 }
                 // Add author to list of users that view log about task in livefeed
                 // But only when some other person change task
                 // or if added FORUM_TOPIC_ID
                 if ($authorUserId !== $effectiveUserId || $arTask['FORUM_TOPIC_ID'] == 0 && isset($arFields['FORUM_TOPIC_ID']) && $arFields['FORUM_TOPIC_ID'] > 0) {
                     $authorGroupCode = 'U' . $authorUserId;
                     $rsRights = CSocNetLogRights::GetList(array(), array('LOG_ID' => $arRes['ID'], 'GROUP_CODE' => $authorGroupCode));
                     // If task's author hasn't rights yet, give them
                     if (!($arRights = $rsRights->fetch())) {
                         CSocNetLogRights::Add($arRes["ID"], array($authorGroupCode));
                     }
                 }
             }
             return null;
         } elseif (count($arChanges) == 1 && isset($arChanges['STATUS'])) {
             return null;
             // if only status changes - don't send message, because it will be sent by SendStatusMessage()
         }
     }
     if ($bSpawnedByAgent === 'Y') {
         $bSpawnedByAgent = true;
     } elseif ($bSpawnedByAgent === 'N') {
         $bSpawnedByAgent = false;
     }
     if (!is_bool($bSpawnedByAgent)) {
         return false;
     }
     $taskId = false;
     if (is_array($arFields) && isset($arFields['ID']) && $arFields['ID'] > 0) {
         $taskId = $arFields['ID'];
     } elseif (is_array($arTask) && isset($arTask['ID']) && $arTask['ID'] > 0) {
         $taskId = $arTask['ID'];
     }
     // We will mark this to false, if we send update message and log item already exists
     $bSocNetAddNewItem = true;
     $logDate = $DB->CurrentTimeFunction();
     $curTimeTimestamp = time() + CTimeZone::GetOffset();
     if (!$bCrmTask) {
         $arSoFields = array('EVENT_ID' => 'tasks', 'TITLE' => $arFields['TITLE'], 'MESSAGE' => '', 'MODULE_ID' => 'tasks');
     } else {
         $arSoFields = array();
     }
     // If changes and task data given => we are prepare "update" message,
     // or "add" message otherwise
     if (is_array($arChanges) && is_array($arTask)) {
         // Prepare "update" message here
         if (strlen($arFields["CHANGED_DATE"]) > 0) {
             $createdDateTimestamp = MakeTimeStamp($arFields["CHANGED_DATE"], $cachedSiteTimeFormat);
             if ($createdDateTimestamp > $curTimeTimestamp) {
                 $logDate = $DB->CharToDateFunction($arFields["CHANGED_DATE"], "FULL", SITE_ID);
             }
         }
         $arChangesFields = array_keys($arChanges);
         $arSoFields['TEXT_MESSAGE'] = str_replace('#CHANGES#', implode(', ', CTaskNotifications::__Fields2Names($arChangesFields)), GetMessage('TASKS_SONET_TASK_CHANGED_MESSAGE'));
         if (!$bCrmTask) {
             if ($arFields["GROUP_ID"] === NULL && $arTask['GROUP_ID'] || $arFields['GROUP_ID']) {
                 $arSoFields["ENTITY_TYPE"] = SONET_ENTITY_GROUP;
                 $arSoFields["ENTITY_ID"] = $arFields["GROUP_ID"] ? $arFields["GROUP_ID"] : $arTask['GROUP_ID'];
             } else {
                 $arSoFields["ENTITY_TYPE"] = SONET_ENTITY_USER;
                 $arSoFields["ENTITY_ID"] = $arFields["CREATED_BY"] ? $arFields["CREATED_BY"] : $arTask["CREATED_BY"];
             }
         }
         $arSoFields['PARAMS'] = serialize(array('TYPE' => 'modify', 'CHANGED_FIELDS' => $arChangesFields, 'CREATED_BY' => $arFields["CREATED_BY"] ? $arFields["CREATED_BY"] : $arTask["CREATED_BY"], 'CHANGED_BY' => $occurAsUserId ? $occurAsUserId : $arFields['CHANGED_BY'], 'PREV_REAL_STATUS' => isset($arTask['REAL_STATUS']) ? $arTask['REAL_STATUS'] : false));
         // Determine, does item exists in sonet log
         $rsSocNetLogItems = CSocNetLog::GetList(array("ID" => "DESC"), $arLogFilter, false, false, array("ID", "ENTITY_TYPE", "ENTITY_ID"));
         if ($rsSocNetLogItems->Fetch()) {
             $bSocNetAddNewItem = false;
             // item already exists, update it, not create.
         }
     } else {
         if (strlen($arFields["CREATED_DATE"]) > 0) {
             $createdDateTimestamp = MakeTimeStamp($arFields["CREATED_DATE"], $cachedSiteTimeFormat);
             if ($createdDateTimestamp > $curTimeTimestamp) {
                 $logDate = $DB->CharToDateFunction($arFields["CREATED_DATE"], "FULL", SITE_ID);
             }
         }
         $arSoFields['TEXT_MESSAGE'] = GetMessage('TASKS_SONET_NEW_TASK_MESSAGE');
         if ($arFields["GROUP_ID"]) {
             $arSoFields["ENTITY_TYPE"] = SONET_ENTITY_GROUP;
             $arSoFields["ENTITY_ID"] = $arFields["GROUP_ID"];
         } else {
             $arSoFields["ENTITY_TYPE"] = SONET_ENTITY_USER;
             $arSoFields["ENTITY_ID"] = $arFields["CREATED_BY"];
         }
         $arParamsLog = array('TYPE' => 'create', 'CREATED_BY' => $arFields["CREATED_BY"] ? $arFields["CREATED_BY"] : $arTask["CREATED_BY"], 'PREV_REAL_STATUS' => isset($arTask['REAL_STATUS']) ? $arTask['REAL_STATUS'] : false);
         if ($occurAsUserId) {
             $arParamsLog["CREATED_BY"] = $occurAsUserId;
         }
         $arSoFields['PARAMS'] = serialize($arParamsLog);
     }
     // rating entity id (ilike)
     $arSoFields["RATING_ENTITY_ID"] = $taskId;
     $arSoFields["RATING_TYPE_ID"] = "TASK";
     // Do we need add new item to socnet?
     // We adds new item, if it is not exists.
     $logID = false;
     if (IsModuleInstalled("webdav") || IsModuleInstalled("disk")) {
         $ufDocID = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("TASKS_TASK", "UF_TASK_WEBDAV_FILES", $taskId, LANGUAGE_ID);
         if ($ufDocID) {
             $arSoFields["UF_SONET_LOG_DOC"] = $ufDocID;
         }
     }
     if ($bSocNetAddNewItem) {
         $arSoFields['=LOG_DATE'] = $logDate;
         $arSoFields['CALLBACK_FUNC'] = false;
         $arSoFields['SOURCE_ID'] = $taskId;
         $arSoFields['ENABLE_COMMENTS'] = 'Y';
         $arSoFields['URL'] = CTaskNotifications::GetNotificationPath(array('ID' => (int) $arFields["CREATED_BY"]), $taskId, false);
         $arSoFields['USER_ID'] = $arFields['CREATED_BY'];
         $arSoFields['TITLE_TEMPLATE'] = '#TITLE#';
         // Set all sites because any user from any site may be
         // added to task in future. For example, new auditor, etc.
         $arSoFields['SITE_ID'] = $cachedAllSitesIds;
         $logID = CSocNetLog::Add($arSoFields, false);
         if (intval($logID) > 0) {
             CSocNetLog::Update($logID, array("TMP_ID" => $logID));
             $arTaskParticipant = CTaskNotifications::GetRecipientsIDs($arFields, false);
             // Exclude author
             $arLogCanViewedBy = array_diff($arTaskParticipant, array($arFields['CREATED_BY']));
             $arRights = CTaskNotifications::__UserIDs2Rights($arLogCanViewedBy);
             if (isset($arFields['GROUP_ID'])) {
                 $arRights = array_merge($arRights, self::prepareRightsCodesForViewInGroupLiveFeed($logID, $arFields['GROUP_ID']));
             }
             CSocNetLogRights::Add($logID, $arRights);
             CSocNetLog::SendEvent($logID, "SONET_NEW_EVENT", $logID);
         }
     } else {
         $arSoFields['=LOG_DATE'] = $logDate;
         $arSoFields['=LOG_UPDATE'] = $logDate;
         // All tasks posts in live feed should be from director
         if (isset($arFields['CREATED_BY'])) {
             $arSoFields['USER_ID'] = $arFields['CREATED_BY'];
         } elseif (isset($arTask['CREATED_BY'])) {
             $arSoFields['USER_ID'] = $arTask['CREATED_BY'];
         } elseif ($occurAsUserId) {
             $arSoFields['USER_ID'] = $occurAsUserId;
         } else {
             unset($arSoFields['USER_ID']);
         }
         $rsSocNetLogItems = CSocNetLog::GetList(array("ID" => "DESC"), $arLogFilter, false, false, array("ID", "ENTITY_TYPE", "ENTITY_ID"));
         while ($arRes = $rsSocNetLogItems->Fetch()) {
             CSocNetLog::Update($arRes["ID"], $arSoFields);
             $arTaskParticipant = CTaskNotifications::GetRecipientsIDs($arFields, false, true);
             $bAuthorMustBeExcluded = false;
             $authorUserId = false;
             if (isset($arFields['CREATED_BY'])) {
                 $authorUserId = (int) $arFields['CREATED_BY'];
             } elseif (isset($arTask['CREATED_BY'])) {
                 $authorUserId = (int) $arTask['CREATED_BY'];
             }
             // Get current rights
             $rsRights = CSocNetLogRights::GetList(array(), array('LOG_ID' => $arRes['ID']));
             $arCurrentRights = array();
             while ($arRights = $rsRights->fetch()) {
                 $arCurrentRights[] = $arRights['GROUP_CODE'];
             }
             // If author changes the task and author doesn't have
             // access to task yet, don't give access to him.
             if ($authorUserId === $effectiveUserId) {
                 $authorGroupCode = 'U' . $authorUserId;
                 // If task's author hasn't rights yet, still exclude him
                 if (!in_array($authorGroupCode, $arCurrentRights, true)) {
                     $bAuthorMustBeExcluded = true;
                 }
             }
             if ($bAuthorMustBeExcluded) {
                 $arLogCanViewedBy = array_diff($arTaskParticipant, array($authorUserId));
             } else {
                 $arLogCanViewedBy = $arTaskParticipant;
             }
             $arNewRights = CTaskNotifications::__UserIDs2Rights($arLogCanViewedBy);
             $bGroupChanged = false;
             if (isset($arFields['GROUP_ID'], $arTask['GROUP_ID']) && $arFields['GROUP_ID'] && $arFields['GROUP_ID'] != $arTask['GROUP_ID']) {
                 $bGroupChanged = true;
             }
             // If rights really changed, update them
             if (count(array_diff($arCurrentRights, $arNewRights)) || count(array_diff($arNewRights, $arCurrentRights)) || $bGroupChanged) {
                 if (isset($arFields['GROUP_ID'])) {
                     $arNewRights = array_merge($arNewRights, self::prepareRightsCodesForViewInGroupLiveFeed($logID, $arFields['GROUP_ID']));
                 } elseif (isset($arTask['GROUP_ID'])) {
                     $arNewRights = array_merge($arNewRights, self::prepareRightsCodesForViewInGroupLiveFeed($logID, $arTask['GROUP_ID']));
                 }
                 CSocNetLogRights::DeleteByLogID($arRes["ID"], true);
                 CSocNetLogRights::Add($arRes["ID"], $arNewRights);
             }
         }
     }
     return $logID;
 }