示例#1
0
 function AddMessage($ticketID, $arFields, &$arrFILES, $checkRights = "Y")
 {
     if (strlen($arFields["MESSAGE"]) > 0 || is_array($arFields["FILES"]) && count($arFields["FILES"]) > 0) {
         $err_mess = CTicket::err_mess() . "<br>Function: AddMessage<br>Line: ";
         global $DB, $USER;
         $bAdmin = "N";
         $bSupportTeam = "N";
         $bSupportClient = "N";
         if ($checkRights == "Y") {
             $bAdmin = CTicket::IsAdmin() ? "Y" : "N";
             $bSupportTeam = CTicket::IsSupportTeam() ? "Y" : "N";
             $bSupportClient = CTicket::IsSupportClient() ? "Y" : "N";
             $uid = intval($USER->GetID());
         } else {
             $bAdmin = "Y";
             $bSupportTeam = "Y";
             $bSupportClient = "Y";
             //if (is_object($USER)) $uid = intval($USER->GetID()); else $uid = -1;
             $uid = 0;
         }
         if ($bAdmin != "Y" && $bSupportTeam != "Y" && $bSupportClient != "Y") {
             return false;
         }
         $ticketID = intval($ticketID);
         if ($ticketID <= 0) {
             return 0;
         }
         $strSql = "SELECT RESPONSIBLE_USER_ID, LAST_MESSAGE_USER_ID, REOPEN, SITE_ID, TITLE FROM b_ticket WHERE ID='{$ticketID}'";
         $rsTicket = $DB->Query($strSql, false, $err_mess . __LINE__);
         $arTicket = $rsTicket->Fetch();
         $currentResponsibleUserID = $arTicket["RESPONSIBLE_USER_ID"];
         $siteID = $arTicket["SITE_ID"];
         $tTitle = $arTicket["TITLE"];
         $strSql = "SELECT max(C_NUMBER) MAX_NUMBER FROM b_ticket_message WHERE TICKET_ID='{$ticketID}'";
         $z = $DB->Query($strSql, false, $err_mess . __LINE__);
         $zr = $z->Fetch();
         $maxNumber = intval($zr['MAX_NUMBER']);
         if ((strlen(trim($arFields["MESSAGE_AUTHOR_SID"])) > 0 || intval($arFields["MESSAGE_AUTHOR_USER_ID"]) > 0 || intval($arFields["MESSAGE_CREATED_USER_ID"]) > 0) && ($bSupportTeam == "Y" || $bAdmin == "Y")) {
             $ownerUserID = intval($arFields["MESSAGE_AUTHOR_USER_ID"]);
             $ownerSid = "'" . $DB->ForSql($arFields["MESSAGE_AUTHOR_SID"], 2000) . "'";
             $ownerGuestID = intval($arFields["MESSAGE_AUTHOR_GUEST_ID"]) > 0 ? intval($arFields["MESSAGE_AUTHOR_GUEST_ID"]) : "null";
             $createdUserID = intval($arFields["MESSAGE_CREATED_USER_ID"]) > 0 ? intval($arFields["MESSAGE_CREATED_USER_ID"]) : intval($uid);
             $createdGuestID = intval($arFields["MESSAGE_CREATED_GUEST_ID"]) > 0 ? intval($arFields["MESSAGE_CREATED_GUEST_ID"]) : intval($_SESSION["SESS_GUEST_ID"]);
         } else {
             $ownerUserID = intval($uid);
             $ownerSid = "null";
             $ownerGuestID = intval($_SESSION["SESS_GUEST_ID"]);
             $createdUserID = intval($uid);
             $createdGuestID = intval($_SESSION["SESS_GUEST_ID"]);
         }
         if (intval($ownerGuestID) <= 0) {
             $ownerGuestID = "null";
         }
         $MessageBySupportTeam = "null";
         if ($ownerUserID <= 0) {
             $ownerUserID = "null";
         } else {
             $MessageBySupportTeam = "'N'";
             if (CTicket::IsSupportTeam($ownerUserID) || CTicket::IsAdmin($ownerUserID)) {
                 $MessageBySupportTeam = "'Y'";
             }
         }
         if ($createdUserID <= 0) {
             $createdUserID = "null";
         }
         if (intval($createdGuestID) <= 0) {
             $createdGuestID = "null";
         }
         $createdModuleName = strlen($arFields["MESSAGE_CREATED_MODULE_NAME"]) > 0 ? "'" . $DB->ForSql($arFields["MESSAGE_CREATED_MODULE_NAME"], 255) . "'" : "'support'";
         $externalID = intval($arFields["EXTERNAL_ID"]) > 0 ? intval($arFields["EXTERNAL_ID"]) : "null";
         $externalField1 = $arFields["EXTERNAL_FIELD_1"];
         if (is_set($arFields, "HIDDEN")) {
             $hidden = $arFields["HIDDEN"] == "Y" ? "Y" : "N";
         } elseif (is_set($arFields, "IS_HIDDEN")) {
             $hidden = $arFields["IS_HIDDEN"] == "Y" ? "Y" : "N";
         }
         $hidden = $hidden == "Y" ? "Y" : "N";
         $notChangeStatus = is_set($arFields, "NOT_CHANGE_STATUS") && $arFields["NOT_CHANGE_STATUS"] == "Y" ? "Y" : "N";
         $changeLastMessageDate = true;
         if ($arTicket["LAST_MESSAGE_USER_ID"] == $uid && $arTicket["REOPEN"] != "Y") {
             $changeLastMessageDate = false;
         }
         $TASK_TIME = intval($arFields["TASK_TIME"]) > 0 ? intval($arFields["TASK_TIME"]) : "null";
         if (is_set($arFields, "LOG")) {
             $log = $arFields["LOG"] == "Y" ? "Y" : "N";
         } elseif (is_set($arFields, "IS_LOG")) {
             $log = $arFields["IS_LOG"] == "Y" ? "Y" : "N";
         }
         $log = $log == "Y" ? "Y" : "N";
         if (is_set($arFields, "OVERDUE")) {
             $overdue = $arFields["OVERDUE"] == "Y" ? "Y" : "N";
         } elseif (is_set($arFields, "IS_OVERDUE")) {
             $overdue = $arFields["IS_OVERDUE"] == "Y" ? "Y" : "N";
         }
         $overdue = $overdue == "Y" ? "Y" : "N";
         $arFieldsI = array("TIMESTAMP_X" => $DB->GetNowFunction(), "DAY_CREATE" => $DB->CurrentDateFunction(), "C_NUMBER" => $maxNumber + 1, "TICKET_ID" => $ticketID, "IS_HIDDEN" => "'" . $hidden . "'", "IS_LOG" => "'" . $log . "'", "IS_OVERDUE" => "'" . $overdue . "'", "MESSAGE" => "'" . $DB->ForSql($arFields["MESSAGE"]) . "'", "MESSAGE_SEARCH" => "'" . $DB->ForSql(ToUpper($arFields["MESSAGE"])) . "'", "EXTERNAL_ID" => $externalID, "EXTERNAL_FIELD_1" => strlen($externalField1) > 0 ? "'" . $DB->ForSql($externalField1) . "'" : "null", "OWNER_USER_ID" => $ownerUserID, "OWNER_GUEST_ID" => $ownerGuestID, "OWNER_SID" => $ownerSid, "SOURCE_ID" => intval($arFields["MESSAGE_SOURCE_ID"]), "CREATED_USER_ID" => $createdUserID, "CREATED_GUEST_ID" => $createdGuestID, "CREATED_MODULE_NAME" => $createdModuleName, "MODIFIED_USER_ID" => $createdUserID, "MODIFIED_GUEST_ID" => $createdGuestID, "MESSAGE_BY_SUPPORT_TEAM" => $MessageBySupportTeam, "TASK_TIME" => $TASK_TIME, "NOT_CHANGE_STATUS" => "'" . $notChangeStatus . "'");
         CTimeZone::Disable();
         $arFieldsI["DATE_CREATE"] = $DB->CharToDateFunction(GetTime(time(), "FULL"));
         CTimeZone::Enable();
         if ($hidden != "Y" && $log != "Y" && $changeLastMessageDate == false) {
             if ($MessageBySupportTeam == "'Y'" || $maxNumber <= 0 && array_key_exists('SOURCE_SID', $arFields) && $arFields['SOURCE_SID'] === 'email') {
                 $arFieldsI["NOT_CHANGE_STATUS"] = "'N'";
             } else {
                 $arFieldsI["NOT_CHANGE_STATUS"] = "'Y'";
             }
         }
         if (intval($currentResponsibleUserID) > 0) {
             $arFieldsI["CURRENT_RESPONSIBLE_USER_ID"] = $currentResponsibleUserID;
         }
         $mid = $DB->Insert("b_ticket_message", $arFieldsI, $err_mess . __LINE__);
         if (intval($mid) > 0) {
             $not_image_extension_suffix = COption::GetOptionString("support", "NOT_IMAGE_EXTENSION_SUFFIX");
             $not_image_upload_dir = COption::GetOptionString("support", "NOT_IMAGE_UPLOAD_DIR");
             $max_size = COption::GetOptionString("support", "SUPPORT_MAX_FILESIZE");
             // сохраняем приаттаченные файлы
             $arFILES = $arFields["FILES"];
             if (is_array($arFILES) && count($arFILES) > 0) {
                 while (list($key, $arFILE) = each($arFILES)) {
                     if (strlen($arFILE["name"]) > 0) {
                         if ($bSupportTeam != "Y" && $bAdmin != "Y") {
                             $max_file_size = intval($max_size) * 1024;
                         }
                         $fes = "";
                         $upload_dir = "support";
                         if (!CFile::IsImage($arFILE["name"], $arFILE["type"])) {
                             $fes = $not_image_extension_suffix;
                             $arFILE["name"] .= $fes;
                             $upload_dir = $not_image_upload_dir;
                         }
                         if (!array_key_exists("MODULE_ID", $arFILE) || strlen($arFILE["MODULE_ID"]) <= 0) {
                             $arFILE["MODULE_ID"] = "support";
                         }
                         $fid = intval(CFile::SaveFile($arFILE, $upload_dir, $max_file_size));
                         if ($fid > 0) {
                             $md5 = md5(uniqid(mt_rand(), true) . time());
                             $arFILE["HASH"] = $md5;
                             $arFILE["FILE_ID"] = $fid;
                             $arFILE["MESSAGE_ID"] = $mid;
                             $arFILE["TICKET_ID"] = $ticketID;
                             $arFILE["EXTENSION_SUFFIX"] = $fes;
                             $arFields_fi = array("HASH" => "'" . $DB->ForSql($md5, 255) . "'", "MESSAGE_ID" => $mid, "FILE_ID" => $fid, "TICKET_ID" => $ticketID, "EXTENSION_SUFFIX" => strlen($fes) > 0 ? "'" . $DB->ForSql($fes, 255) . "'" : "null");
                             $link_id = $DB->Insert("b_ticket_message_2_file", $arFields_fi, $err_mess . __LINE__);
                             if (intval($link_id) > 0) {
                                 $arFILE["LINK_ID"] = $link_id;
                                 $arrFILES[] = $arFILE;
                             }
                         }
                     }
                 }
             }
             // если это не было скрытым сообщением или сообщение лога, то
             if ($notChangeStatus != "Y" && $hidden != "Y" && $log != "Y") {
                 // обновим ряд параметров обращения
                 if (!isset($arFields["AUTO_CLOSE_DAYS"])) {
                     $RESET_AUTO_CLOSE = "Y";
                 }
                 CTicket::UpdateLastParams($ticketID, $RESET_AUTO_CLOSE, $changeLastMessageDate, true);
                 // при необходимости создадим или удалим агенты-напоминальщики
                 //CTicketReminder::Update($ticketID);
             }
             if ($log != "Y") {
                 CSupportSearch::writeWordsInTable($mid, $siteID, $tTitle . " " . $arFields["MESSAGE"]);
             }
             //если была установлена галочка "не изменять статус обращени" - пересчитаем количество собщений
             if ($notChangeStatus == "Y" || $hidden == "Y") {
                 CTicket::UpdateMessages($ticketID);
             }
         }
     }
     return $mid;
 }