Пример #1
0
 function AddComment_Tasks($arFields)
 {
     global $DB;
     if (!CModule::IncludeModule("forum")) {
         return false;
     }
     if (!CModule::IncludeModule("tasks")) {
         return false;
     }
     $ufFileID = array();
     $ufDocID = array();
     $dbResult = CSocNetLog::GetList(array(), array("ID" => $arFields["LOG_ID"]), false, false, array("ID", "SOURCE_ID", "SITE_ID"));
     if ($arLog = $dbResult->Fetch()) {
         $rsTask = CTasks::GetById($arLog["SOURCE_ID"]);
         if ($arTask = $rsTask->Fetch()) {
             // read shared cross-site FORUM_ID
             $forumID = COption::GetOptionString("tasks", "task_forum_id", 0, $siteId = '');
             if ($forumID) {
                 if (!$arTask["FORUM_TOPIC_ID"]) {
                     $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($res["NAME"] . " " . $res["LAST_NAME"]) : "";
                             $arUserStart["NAME"] = empty($sName) ? trim($res["LOGIN"]) : $sName;
                         }
                     }
                     $arUserStart["NAME"] = empty($arUserStart["NAME"]) ? $GLOBALS["FORUM_STATUS_NAME"]["guest"] : $arUserStart["NAME"];
                     $DB->StartTransaction();
                     $arTopicFields = array("TITLE" => $arTask["TITLE"], "FORUM_ID" => $forumID, "USER_START_ID" => $arUserStart["ID"], "USER_START_NAME" => $arUserStart["NAME"], "LAST_POSTER_NAME" => $arUserStart["NAME"], "PERMISSION_EXTERNAL" => 'E', "PERMISSION" => 'E', "APPROVED" => "Y", 'XML_ID' => 'TASK_' . $arTask['ID']);
                     $TID = CForumTopic::Add($arTopicFields);
                     if (intVal($TID) > 0) {
                         $arFieldsFirstMessage = array("POST_MESSAGE" => $arTopicFields['XML_ID'], "AUTHOR_ID" => $arTopicFields["USER_START_ID"], "AUTHOR_NAME" => $arTopicFields["USER_START_NAME"], "FORUM_ID" => $arTopicFields["FORUM_ID"], "TOPIC_ID" => $TID, "APPROVED" => "Y", "NEW_TOPIC" => "Y", "PARAM1" => 'TK', "PARAM2" => $arTask['ID'], "PERMISSION_EXTERNAL" => 'E', "PERMISSION" => 'E');
                         CForumMessage::Add($arFieldsFirstMessage, false, array("SKIP_INDEXING" => "Y", "SKIP_STATISTIC" => "N"));
                         $arTask["FORUM_TOPIC_ID"] = $TID;
                         $arTaskFields = array("FORUM_TOPIC_ID" => $TID);
                         $task = new CTasks();
                         $task->Update($arTask["ID"], $arTaskFields);
                     }
                     if (!$arTask["FORUM_TOPIC_ID"]) {
                         $DB->Rollback();
                     } else {
                         $DB->Commit();
                     }
                 } else {
                     // override forumId by fact forum, attached to the task
                     if ($arTopic = CForumTopic::getByID($arTask['FORUM_TOPIC_ID'])) {
                         $forumID = $arTopic['FORUM_ID'];
                     }
                 }
                 if ($forumID && $arTask["FORUM_TOPIC_ID"]) {
                     $userID = $GLOBALS["USER"]->GetID();
                     $arLogSites = array();
                     $rsLogSite = CSocNetLog::GetSite($arLog["ID"]);
                     while ($arLogSite = $rsLogSite->Fetch()) {
                         $arLogSites[] = $arLogSite["LID"];
                     }
                     $bCurrentUserIsAdmin = CSocNetUser::IsCurrentUserModuleAdmin($arLogSites);
                     if ($arFields["ENTITY_TYPE"] == SONET_ENTITY_GROUP) {
                         if (CSocNetFeaturesPerms::CanPerformOperation($userID, SONET_ENTITY_GROUP, $arFields["ENTITY_ID"], "tasks", "view", $bCurrentUserIsAdmin)) {
                             $strPermission = "I";
                         }
                     } else {
                         if (CSocNetFeaturesPerms::CanPerformOperation($userID, SONET_ENTITY_USER, $arFields["ENTITY_ID"], "tasks", "view", $bCurrentUserIsAdmin)) {
                             $strPermission = "I";
                         }
                     }
                     $arFieldsMessage = array("POST_MESSAGE" => $arFields["TEXT_MESSAGE"], "USE_SMILES" => "Y", "PERMISSION_EXTERNAL" => "Q", "PERMISSION" => $strPermission, "APPROVED" => "Y");
                     $MESSAGE_TYPE = 'REPLY';
                     $GLOBALS["USER_FIELD_MANAGER"]->EditFormAddFields("SONET_COMMENT", $arTmp);
                     if (is_array($arTmp)) {
                         if (array_key_exists("UF_SONET_COM_DOC", $arTmp)) {
                             $GLOBALS["UF_FORUM_MESSAGE_DOC"] = $arTmp["UF_SONET_COM_DOC"];
                         } elseif (array_key_exists("UF_SONET_COM_FILE", $arTmp)) {
                             $arFieldsMessage["FILES"] = array();
                             foreach ($arTmp["UF_SONET_COM_FILE"] as $file_id) {
                                 $arFieldsMessage["FILES"][] = array("FILE_ID" => $file_id);
                             }
                         }
                     }
                     $messageID = ForumAddMessage($MESSAGE_TYPE, $forumID, $arTask["FORUM_TOPIC_ID"], 0, $arFieldsMessage, $sError, $sNote);
                     // get UF DOC value and FILE_ID there
                     if ($messageID > 0) {
                         $dbAddedMessageFiles = CForumFiles::GetList(array("ID" => "ASC"), array("MESSAGE_ID" => $messageID));
                         while ($arAddedMessageFiles = $dbAddedMessageFiles->Fetch()) {
                             $ufFileID[] = $arAddedMessageFiles["FILE_ID"];
                         }
                         $ufDocID = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MESSAGE_DOC", $messageID, LANGUAGE_ID);
                     }
                     if ($messageID && ($arMessage = CForumMessage::GetByID($messageID))) {
                         $arLogFields = array("TASK_ID" => $arTask["ID"], "USER_ID" => $userID, "CREATED_DATE" => $arMessage["POST_DATE"], "FIELD" => "COMMENT", "TO_VALUE" => $messageID);
                         $log = new CTaskLog();
                         $log->Add($arLogFields);
                         // notification to IM
                         $arRecipientsIDs = CTaskNotifications::GetRecipientsIDs($arTask);
                         $arUnFollowers = array();
                         $rsUnFollower = CSocNetLogFollow::GetList(array("USER_ID" => $arRecipientsIDs, "CODE" => "L" . $arFields["LOG_ID"], "TYPE" => "N"), array("USER_ID"));
                         while ($arUnFollower = $rsUnFollower->Fetch()) {
                             $arUnFollowers[] = $arUnFollower["USER_ID"];
                         }
                         $arRecipientsIDs = array_diff($arRecipientsIDs, $arUnFollowers);
                         if (IsModuleInstalled("im") && CModule::IncludeModule("im") && sizeof($arRecipientsIDs)) {
                             $extranetSiteId = false;
                             if (CModule::IncludeModule('extranet') && method_exists('CExtranet', 'GetExtranetSiteID')) {
                                 $extranetSiteId = CExtranet::GetExtranetSiteID();
                             }
                             foreach ($arRecipientsIDs as $recipientUserID) {
                                 $arFilter = array("UF_DEPARTMENT" => false, "ID" => $recipientUserID);
                                 $rsUser = CUser::GetList($by = "last_name", $order = "asc", $arFilter, array("SELECT" => array("UF_DEPARTMENT")));
                                 $isExtranetUser = false;
                                 if ($arUser = $rsUser->Fetch()) {
                                     $isExtranetUser = true;
                                 }
                                 if ($isExtranetUser && $extranetSiteId !== false) {
                                     if ($arTask["GROUP_ID"]) {
                                         $pathTemplate = str_replace("#group_id#", $arTask["GROUP_ID"], COption::GetOptionString("tasks", "paths_task_group_entry", "/extranet/workgroups/group/#group_id#/tasks/task/view/#task_id#/", $extranetSiteId));
                                         $pathTemplate = str_replace("#GROUP_ID#", $arTask["GROUP_ID"], $pathTemplate);
                                     } else {
                                         $pathTemplate = COption::GetOptionString("tasks", "paths_task_user_entry", "/extranet/contacts/personal/user/#user_id#/tasks/task/view/#task_id#/", $extranetSiteId);
                                     }
                                 } else {
                                     if ($arTask["GROUP_ID"]) {
                                         $pathTemplate = str_replace("#group_id#", $arTask["GROUP_ID"], COption::GetOptionString("tasks", "paths_task_group_entry", "/workgroups/group/#group_id#/tasks/task/view/#task_id#/", $arLog["SITE_ID"]));
                                         $pathTemplate = str_replace("#GROUP_ID#", $arTask["GROUP_ID"], $pathTemplate);
                                     } else {
                                         $pathTemplate = COption::GetOptionString("tasks", "paths_task_user_entry", "/company/personal/user/#user_id#/tasks/task/view/#task_id#/", $arLog["SITE_ID"]);
                                     }
                                 }
                                 $messageUrl = CComponentEngine::MakePathFromTemplate($pathTemplate, array("user_id" => $recipientUserID, "task_id" => $arTask["ID"], "action" => "view"));
                                 if (strpos($messageUrl, "?") === false) {
                                     $messageUrl .= "?";
                                 } else {
                                     $messageUrl .= "&";
                                 }
                                 $messageUrl .= "MID=" . $messageID;
                                 $MESSAGE_SITE = preg_replace(array('|\\[\\/USER\\]|', '|\\[USER=\\d+\\]|', '|\\[DISK\\sFILE\\sID=[n]*\\d+\\]|', '|\\[DOCUMENT\\sID=\\d+\\]|'), '', $arFields['TEXT_MESSAGE']);
                                 if (strlen($MESSAGE_SITE) >= 100) {
                                     $dot = '...';
                                     $MESSAGE_SITE = substr($MESSAGE_SITE, 0, 99);
                                     if (substr($MESSAGE_SITE, -1) === '[') {
                                         $MESSAGE_SITE = substr($MESSAGE_SITE, 0, 98);
                                     }
                                     if (($lastLinkPosition = strrpos($MESSAGE_SITE, '[u')) !== false || ($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;
                                 }
                                 $rsUser = CUser::GetList($by = 'id', $order = 'asc', array('ID_EQUAL_EXACT' => (int) $userID), array('FIELDS' => array('PERSONAL_GENDER')));
                                 $strMsgAddComment = GetMessage("SONET_GL_EVENT_TITLE_TASK_COMMENT_MESSAGE_ADD");
                                 $strMsgEditComment = GetMessage("SONET_GL_EVENT_TITLE_TASK_COMMENT_MESSAGE_EDIT");
                                 if ($arUser = $rsUser->fetch()) {
                                     switch ($arUser['PERSONAL_GENDER']) {
                                         case "F":
                                         case "M":
                                             $strMsgAddComment = GetMessage("SONET_GL_EVENT_TITLE_TASK_COMMENT_MESSAGE_ADD" . '_' . $arUser['PERSONAL_GENDER']);
                                             $strMsgEditComment = GetMessage("SONET_GL_EVENT_TITLE_TASK_COMMENT_MESSAGE_EDIT" . '_' . $arUser['PERSONAL_GENDER']);
                                             break;
                                         default:
                                             break;
                                     }
                                 }
                                 $arMessageFields = array("TO_USER_ID" => $recipientUserID, "FROM_USER_ID" => $userID, "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "tasks", "NOTIFY_EVENT" => "comment", "NOTIFY_MESSAGE" => str_replace(array("#TASK_TITLE#", "#TASK_COMMENT_TEXT#"), array('[URL=' . tasksServerName() . $messageUrl . "#message" . $messageID . ']' . htmlspecialcharsbx($arTask["TITLE"]) . '[/URL]', '[COLOR=#000000]' . $MESSAGE_SITE . '[/COLOR]'), $MESSAGE_TYPE != "EDIT" ? $strMsgAddComment : $strMsgEditComment), "NOTIFY_MESSAGE_OUT" => str_replace(array("#TASK_TITLE#", "#TASK_COMMENT_TEXT#"), array(htmlspecialcharsbx($arTask["TITLE"]), $MESSAGE_SITE . ' #BR# ' . tasksServerName() . $messageUrl . "#message" . $messageID . ' '), $MESSAGE_TYPE != "EDIT" ? $strMsgAddComment : $strMsgEditComment) . "#BR##BR#" . $arFields["TEXT_MESSAGE"], "NOTIFY_TAG" => "TASKS|COMMENT|" . intval($arTask["ID"]) . "|" . intval($recipientUserID));
                                 CIMNotify::Add($arMessageFields);
                             }
                         }
                         CSocNetLog::Update($arFields["LOG_ID"], array('PARAMS' => serialize(array('TYPE' => 'comment'))));
                         $arFilesIds = array_merge($ufFileID, is_array($ufDocID) ? $ufDocID : array());
                         CTaskComments::fireOnAfterCommentAddEvent($messageID, $arTask['ID'], $arFields["TEXT_MESSAGE"], $arFilesIds);
                     }
                 }
             }
         }
     }
     if (!$messageID) {
         $sError = GetMessage("SONET_ADD_COMMENT_SOURCE_ERROR");
     }
     return array("SOURCE_ID" => $messageID, "MESSAGE" => $arFieldsMessage ? $arFieldsMessage["POST_MESSAGE"] : false, "RATING_TYPE_ID" => "FORUM_POST", "RATING_ENTITY_ID" => $messageID, "ERROR" => $sError, "NOTES" => $sNote, "UF" => array("FILE" => $ufFileID, "DOC" => $ufDocID), "URL" => $messageUrl);
 }