static function SaveToArchive($_chatId, $_externalFullname, $_externalId, $_internalId, $_groupId, $_email, $_company, $_phone, $_host, $_ip, $_question, $_transcriptSent = false, $_waitingtime, $_startResult, $_endResult)
 {
     $result = DBManager::Execute(true, "SELECT `voucher_id`,`endtime`,`transcript_text`,`transcript_html`,`iso_language`,`time` FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `chat_id`='" . DBManager::RealEscape($_chatId) . "' LIMIT 1;");
     $row = DBManager::FetchArray($result);
     LocalizationManager::AutoLoad($row["iso_language"], true);
     CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_CHATS);
     $_externalFullname = empty($_externalFullname) ? LocalizationManager::$TranslationStrings["client_guest"] . " " . Visitor::GetNoName($_externalId . $_ip) : $_externalFullname;
     $filter = new Chat();
     $filter->Generate($_chatId, $_externalFullname, true, true, $_question, $row["time"]);
     $filter->PlainText = Server::Replace($filter->PlainText, true, false, false);
     $filter->HTML = Server::Replace($filter->HTML, true, false, false);
     $tsData = array($row["transcript_text"], $row["transcript_html"]);
     for ($i = 0; $i < count($tsData); $i++) {
         if ($i == 1 && empty($tsData[$i])) {
             continue;
         }
         $tsData[$i] = Server::Replace($tsData[$i], true, false, false);
         if (!empty($filter->PlainText)) {
             $tText = ($i == 0 ? $filter->PlainText : nl2br(htmlentities($filter->PlainText, ENT_QUOTES, "UTF-8"))) . "<!--lz_ref_link-->";
             $tsData[$i] = str_replace("%localdate%", date("r", $filter->FirstPost), $tsData[$i]);
             if (strpos($tsData[$i], "%transcript%") === false && strpos($tsData[$i], "%mailtext%") === false) {
                 $tsData[$i] .= $tText;
             } else {
                 if (strpos($tsData[$i], "%transcript%") !== false) {
                     $tsData[$i] = str_replace("%transcript%", $tText, $tsData[$i]);
                 } else {
                     if (strpos($tsData[$i], "%mailtext%") !== false) {
                         $tsData[$i] = str_replace("%mailtext%", $tText, $tsData[$i]);
                     }
                 }
             }
         } else {
             $tsData[$i] = "";
         }
         $tsData[$i] = str_replace("%company_logo_url%", Server::$Configuration->File["gl_cali"], $tsData[$i]);
         $tsData[$i] = str_replace(array("%email%", "%eemail%"), $_email, $tsData[$i]);
         $tsData[$i] = str_replace(array("%fullname%", "%efullname%"), $_externalFullname, $tsData[$i]);
         $tsData[$i] = str_replace("%rating%", Feedback::GetRatingAVG($_chatId), $tsData[$i]);
     }
     $name = !empty($_externalFullname) ? ",`fullname`='" . DBManager::RealEscape($_externalFullname) . "'" : "";
     DBManager::Execute(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `external_id`='" . DBManager::RealEscape($_externalId) . "',`closed`='" . DBManager::RealEscape(time()) . "'" . $name . ",`internal_id`='" . DBManager::RealEscape($_internalId) . "',`group_id`='" . DBManager::RealEscape($_groupId) . "',`html`='" . DBManager::RealEscape($filter->HTML) . "',`plaintext`='" . DBManager::RealEscape($filter->PlainText) . "',`transcript_text`='" . DBManager::RealEscape($tsData[0]) . "',`transcript_html`='" . DBManager::RealEscape($tsData[1]) . "',`fullname`='" . DBManager::RealEscape($_externalFullname) . "',`email`='" . DBManager::RealEscape($_email) . "',`company`='" . DBManager::RealEscape($_company) . "',`phone`='" . DBManager::RealEscape($_phone) . "',`host`='" . DBManager::RealEscape($_host) . "',`ip`='" . DBManager::RealEscape($_ip) . "',`gzip`=0,`wait`='" . DBManager::RealEscape($_waitingtime) . "',`accepted`='" . DBManager::RealEscape($_startResult) . "',`ended`='" . DBManager::RealEscape($_endResult) . "',`transcript_sent`='" . DBManager::RealEscape(empty(Server::$Configuration->File["gl_soct"]) && empty(Server::$Configuration->File["gl_scct"]) && empty(Server::$Configuration->File["gl_scto"]) && empty(Server::$Configuration->File["gl_sctg"]) || empty($tsData) || $filter->ElementCount < 3 || $_transcriptSent ? "1" : "0") . "',`question`='" . DBManager::RealEscape(cutString($_question, 255)) . "' WHERE `chat_id`='" . DBManager::RealEscape($_chatId) . "' AND `closed`=0 LIMIT 1;");
     $result = DBManager::Execute(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` WHERE `channel_id`='" . DBManager::RealEscape($_chatId) . "';");
     if ($result && ($rowc = DBManager::FetchArray($result))) {
         $Ticket = new Ticket($rowc["ticket_id"], true);
         $Ticket->LinkChat($rowc["channel_id"], $rowc["id"]);
     }
 }
function processTicketActions($count = 0)
{
    $temporaryIds = array();
    $updateRequiredTickets = $updateRequiredEmails = false;
    while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"])) {
        $type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"];
        if ($type == "SetTicketStatus") {
            $Ticket = new Ticket();
            $Ticket->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"];
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]) {
                $Ticket->Log(0, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
            }
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"]) && $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]) {
                $Ticket->Log(2, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
            }
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->Log(3, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
            }
            $TicketEditor = new TicketEditor($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"]);
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"])) {
                $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
            }
            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
            $TicketEditor->Save();
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->SetGroup($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
            }
            $Ticket->Editor = $TicketEditor;
            $Ticket->LoadMessages();
            $time = SystemTime::GetUniqueMessageTime(DATABASE_TICKETS, "last_update");
            $Ticket->SetLastUpdate($time);
            $updateRequiredTickets = true;
        } else {
            if ($type == "AddTicketEditorReply") {
                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                $Ticket->Load(false, false);
                $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                $Ticket->Messages[0]->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"];
                $Ticket->Messages[0]->ChannelId = getId(32);
                $Ticket->Messages[0]->Hash = $Ticket->GetHash(false);
                $Ticket->Messages[0]->SenderUserId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
                $Ticket->Messages[0]->Type = 1;
                $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                $Ticket->Messages[0]->Text = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
                if ($Ticket->CreationType >= 6 && !empty($Ticket->ChannelId)) {
                    $twchannel = SocialMediaChannel::GetChannelById($Ticket->ChannelId);
                    if ($Ticket->CreationType == 7 && $twchannel->StreamType == 1) {
                        $Ticket->Messages[0]->Text = $twchannel->AddScreenName($Ticket->Messages[0]->Text, $Ticket->Messages[0]->Email);
                    } else {
                        if ($Ticket->CreationType == 6) {
                            $Ticket->Messages[0]->Email = $twchannel->PageId;
                        }
                    }
                }
                $Ticket->Messages[0]->Subject = cutString($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"], 255, false);
                $Ticket->Messages[0]->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], time());
                $acount = 8;
                while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount])) {
                    $Ticket->Messages[0]->ApplyAttachment($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount++]);
                }
                $Ticket->SendEditorReply($Ticket->Messages[0]->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"]);
                if (!empty(Server::$Configuration->File["gl_ctor"])) {
                    $Ticket->LoadStatus();
                    $Ticket->Editor->Status = TICKET_STATUS_CLOSED;
                    $Ticket->Editor->Save();
                }
                $Ticket->SetLastUpdate(time());
                $updateRequiredTickets = true;
            } else {
                if ($type == "SetTicketLanguage") {
                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->SetLanguage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->Log(1, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                    $Ticket->SetLastUpdate(time());
                    $updateRequiredTickets = true;
                } else {
                    if ($type == "DeleteTicketFromServer") {
                        $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                        $Ticket->Destroy();
                        $Ticket->Log(7, CALLER_SYSTEM_ID, 0, 1);
                        $Ticket->SetLastUpdate(time());
                        $updateRequiredTickets = true;
                    } else {
                        if ($type == "AddComment") {
                            $Ticket = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                            $Ticket->AddComment(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                            $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], "");
                            $Ticket->SetLastUpdate(time());
                            $updateRequiredTickets = true;
                        } else {
                            if ($type == "LinkChat") {
                                if (!empty($temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]])) {
                                    $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] = $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]];
                                }
                                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                $Ticket->LinkChat($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], getId(32));
                                $Ticket->Log(5, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                                $Ticket->SetLastUpdate(time());
                                $updateRequiredTickets = true;
                            } else {
                                if ($type == "LinkTicket") {
                                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                    $TicketSub = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                    $counts[$Ticket->Id] = Ticket::GetMessageCount($Ticket->Id);
                                    $counts[$TicketSub->Id] = Ticket::GetMessageCount($TicketSub->Id);
                                    if ($counts[$Ticket->Id] > $counts[$TicketSub->Id]) {
                                        $Ticket->LinkTicket($TicketSub->Id, getId(32));
                                    } else {
                                        $TicketSub->LinkTicket($Ticket->Id, getId(32));
                                    }
                                    $Ticket->SetLastUpdate(time());
                                    $updateRequiredTickets = true;
                                } else {
                                    if ($type == "EditMessage") {
                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                        $ticket->LoadStatus();
                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                        $message->Load();
                                        $message->ChangeValue($ticket, 10, CALLER_SYSTEM_ID, $message->Fullname, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                        $message->ChangeValue($ticket, 11, CALLER_SYSTEM_ID, $message->Email, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                                        $message->ChangeValue($ticket, 12, CALLER_SYSTEM_ID, $message->Company, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                        $message->ChangeValue($ticket, 13, CALLER_SYSTEM_ID, $message->Phone, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                                        $message->ChangeValue($ticket, 14, CALLER_SYSTEM_ID, $message->Subject, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"]);
                                        $message->ChangeValue($ticket, 15, CALLER_SYSTEM_ID, $message->Text, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"]);
                                        $message->ApplyCustomFromPost($count, true, $ticket, CALLER_SYSTEM_ID);
                                        $message->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], true);
                                        $ticket->SetLastUpdate(time(), false);
                                        $updateRequiredTickets = true;
                                        if ($ticket->Editor != null) {
                                            $ticket->Editor->Save();
                                        }
                                    } else {
                                        if ($type == "CreateTicket") {
                                            $Ticket = new Ticket(CacheManager::GetObjectId("ticket_id", DATABASE_TICKETS), "");
                                            $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_11"]] = $Ticket->Id;
                                            $Ticket->Messages[0]->Id = $Ticket->Id;
                                            $Ticket->Messages[0]->ChannelId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                                            $Ticket->CreationType = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"];
                                            $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                                            $Ticket->Language = strtoupper(trim($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_10"]));
                                            $Ticket->Messages[0]->Fullname = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
                                            $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
                                            $Ticket->Messages[0]->Text = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                                            $Ticket->Messages[0]->Company = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"];
                                            $Ticket->Messages[0]->Phone = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_8"];
                                            $Ticket->Messages[0]->Type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_9"];
                                            $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_15"];
                                            $Ticket->Messages[0]->ApplyCustomFromPost($count);
                                            $cid = 0;
                                            while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid])) {
                                                $value = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid++];
                                                if (strpos($value, "[att]") === 0) {
                                                    $Ticket->Messages[0]->ApplyAttachment(base64_decode(str_replace("[att]", "", $value)));
                                                } else {
                                                    if (strpos($value, "[com]") === 0) {
                                                        $Ticket->Messages[0]->AddComment(CALLER_SYSTEM_ID, $Ticket->Id, base64_decode(str_replace("[com]", "", $value)));
                                                    }
                                                }
                                            }
                                            $Ticket->Messages[0]->LoadAttachments();
                                            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"])) {
                                                $email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"], false, "");
                                                $email->Load();
                                                $email->LoadAttachments();
                                                foreach ($email->Attachments as $rid => $res) {
                                                    if (empty($Ticket->Messages[0]->Attachments[$rid])) {
                                                        KnowledgeBase::Process(CALLER_SYSTEM_ID, $rid, "", RESOURCE_TYPE_FILE_INTERNAL, "", true, 100, 1, 0);
                                                    }
                                                }
                                                $email->Destroy();
                                                if (!empty($email->Created)) {
                                                    $Ticket->Messages[0]->Created = $email->Created;
                                                }
                                            }
                                            $Ticket->Save();
                                            $TicketEditor = new TicketEditor($Ticket->Id);
                                            $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_13"];
                                            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_12"];
                                            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_14"];
                                            $TicketEditor->Save();
                                            $Ticket->Log(6, CALLER_SYSTEM_ID, $Ticket->Id, "");
                                            $Ticket->SetLastUpdate(time());
                                            $updateRequiredTickets = true;
                                        } else {
                                            if ($type == "SetEmailStatus") {
                                                $Email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                $Email->SetStatus();
                                                $updateRequiredEmails = true;
                                            } else {
                                                if ($type == "ForwardMessage") {
                                                    $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                    $message->Load();
                                                    $message->Forward($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                                    $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"], "");
                                                    $ticket->Log(9, CALLER_SYSTEM_ID, $message->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                } else {
                                                    if ($type == "MoveMessageIntoTicket") {
                                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                                        $message->Load(true);
                                                        $message->ChannelId = getId(32);
                                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                        $ticket->Load();
                                                        $ticket->Id = $message->Id = CacheManager::GetObjectId("ticket_id", DATABASE_TICKETS);
                                                        $ticket->Messages = array();
                                                        $ticket->Messages[0] = $message;
                                                        $ticket->Save();
                                                        $ticket->Log(8, CALLER_SYSTEM_ID, $ticket->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                                                        $message->SaveAttachments();
                                                        $message->SaveComments($ticket->Id);
                                                        $updateRequiredTickets = true;
                                                    } else {
                                                        if ($type == "DeleteAttachment") {
                                                            KnowledgeBase::Process(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "", RESOURCE_TYPE_FILE_INTERNAL, "", true, "100", "1");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $count++;
    }
    if ($updateRequiredTickets) {
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_TICKETS);
    }
    if ($updateRequiredEmails) {
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_EMAILS);
    }
}
function processTicketActions($count = 0)
{
    global $GROUPS, $CONFIG;
    $temporaryIds = array();
    while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"])) {
        $type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"];
        if ($type == "SetTicketStatus") {
            $Ticket = new Ticket();
            $Ticket->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"];
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]) {
                $Ticket->Log(0, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
            }
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"]) && $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]) {
                $Ticket->Log(2, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
            }
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->Log(3, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
            }
            $TicketEditor = new TicketEditor($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"]);
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"])) {
                $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
            }
            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
            $TicketEditor->Save();
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->SetGroup($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
            }
            $Ticket->Editor = $TicketEditor;
            $Ticket->LoadMessages();
            $Ticket->SetLastUpdate(time());
        } else {
            if ($type == "AddTicketEditorReply") {
                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                $Ticket->Messages[0]->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                $Ticket->Messages[0]->ChannelId = getId(32);
                $Ticket->Messages[0]->Hash = $Ticket->GetHash(false);
                $Ticket->Messages[0]->SenderUserId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
                $Ticket->Messages[0]->Type = 1;
                $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                $Ticket->Messages[0]->Text = Mailbox::FinalizeEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"];
                $Ticket->Messages[0]->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], time());
                $acount = 7;
                $att = array();
                while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount])) {
                    $att[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount]] = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount];
                    $Ticket->Messages[0]->ApplyAttachment($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount++]);
                }
                $mailbox = Mailbox::GetById($GROUPS[$Ticket->Group]->TicketEmailOut, true);
                $Ticket->SendEditorReply($mailbox, $Ticket->Messages[0]->Text, getPredefinedMessage($GROUPS[$Ticket->Group]->PredefinedMessages, $Ticket->Language), $att, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                if (!empty($CONFIG["gl_ctor"])) {
                    $Ticket->LoadStatus();
                    $Ticket->Editor->Status = TICKET_STATUS_CLOSED;
                    $Ticket->Editor->Save();
                }
                $Ticket->SetLastUpdate(time());
            } else {
                if ($type == "SetTicketLanguage") {
                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->SetLanguage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->Log(1, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                    $Ticket->SetLastUpdate(time());
                } else {
                    if ($type == "DeleteTicketFromServer") {
                        $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                        $Ticket->Destroy();
                        $Ticket->Log(7, CALLER_SYSTEM_ID, 0, 1);
                        $Ticket->SetLastUpdate(time());
                    } else {
                        if ($type == "AddComment") {
                            $Ticket = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                            $Ticket->AddComment(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                            $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], "");
                            $Ticket->SetLastUpdate(time());
                        } else {
                            if ($type == "LinkChat") {
                                if (!empty($temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]])) {
                                    $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] = $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]];
                                }
                                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                $Ticket->LinkChat($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], getId(32));
                                $Ticket->Log(5, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                                $Ticket->SetLastUpdate(time());
                            } else {
                                if ($type == "LinkTicket") {
                                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                    $TicketSub = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                    $counts[$Ticket->Id] = Ticket::GetMessageCount($Ticket->Id);
                                    $counts[$TicketSub->Id] = Ticket::GetMessageCount($TicketSub->Id);
                                    if ($counts[$Ticket->Id] > $counts[$TicketSub->Id]) {
                                        $Ticket->LinkTicket($TicketSub->Id, getId(32));
                                    } else {
                                        $TicketSub->LinkTicket($Ticket->Id, getId(32));
                                    }
                                    $Ticket->SetLastUpdate(time());
                                } else {
                                    if ($type == "EditMessage") {
                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                        $message->Load();
                                        $message->ChangeValue($ticket, 10, CALLER_SYSTEM_ID, $message->Fullname, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                        $message->ChangeValue($ticket, 11, CALLER_SYSTEM_ID, $message->Email, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                                        $message->ChangeValue($ticket, 12, CALLER_SYSTEM_ID, $message->Company, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                        $message->ChangeValue($ticket, 13, CALLER_SYSTEM_ID, $message->Phone, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                                        $message->ChangeValue($ticket, 14, CALLER_SYSTEM_ID, $message->Subject, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"]);
                                        $message->ChangeValue($ticket, 15, CALLER_SYSTEM_ID, $message->Text, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"]);
                                        $message->ApplyCustomFromPost($count, true, $ticket, CALLER_SYSTEM_ID);
                                        $message->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], true);
                                        $ticket->SetLastUpdate(time(), false);
                                    } else {
                                        if ($type == "CreateTicket") {
                                            $Ticket = new Ticket(getObjectId("ticket_id", DATABASE_TICKETS), "");
                                            $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_11"]] = $Ticket->Id;
                                            $Ticket->Messages[0]->Id = $Ticket->Id;
                                            $Ticket->Messages[0]->ChannelId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                                            $Ticket->CreationType = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"];
                                            $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                                            $Ticket->Language = strtoupper(trim($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_10"]));
                                            $Ticket->Messages[0]->Fullname = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
                                            $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
                                            $Ticket->Messages[0]->Text = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                                            $Ticket->Messages[0]->Company = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"];
                                            $Ticket->Messages[0]->Phone = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_8"];
                                            $Ticket->Messages[0]->Type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_9"];
                                            $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_15"];
                                            $Ticket->Messages[0]->ApplyCustomFromPost($count);
                                            $cid = 0;
                                            while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid])) {
                                                $value = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid++];
                                                if (strpos($value, "[att]") === 0) {
                                                    $Ticket->Messages[0]->ApplyAttachment(base64_decode(str_replace("[att]", "", $value)));
                                                } else {
                                                    if (strpos($value, "[com]") === 0) {
                                                        $Ticket->Messages[0]->AddComment(CALLER_SYSTEM_ID, $Ticket->Id, base64_decode(str_replace("[com]", "", $value)));
                                                    }
                                                }
                                            }
                                            $Ticket->Messages[0]->LoadAttachments();
                                            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"])) {
                                                $email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"], false, "");
                                                $email->LoadAttachments();
                                                foreach ($email->Attachments as $rid => $res) {
                                                    if (empty($Ticket->Messages[0]->Attachments[$rid])) {
                                                        processResource(CALLER_SYSTEM_ID, $rid, "", RESOURCE_TYPE_FILE_INTERNAL, "", true, 100, 1, 0);
                                                    }
                                                }
                                                $email->Destroy();
                                            }
                                            $Ticket->Save();
                                            $TicketEditor = new TicketEditor($Ticket->Id);
                                            $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_13"];
                                            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_12"];
                                            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_14"];
                                            $TicketEditor->Save();
                                            $Ticket->Log(6, CALLER_SYSTEM_ID, $Ticket->Id, "");
                                            $Ticket->SetLastUpdate(time());
                                        } else {
                                            if ($type == "SetEmailStatus") {
                                                $Email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                $Email->SetStatus();
                                            } else {
                                                if ($type == "ForwardMessage") {
                                                    $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                    $message->Load();
                                                    $message->Forward($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                                    $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"], "");
                                                    $ticket->Log(9, CALLER_SYSTEM_ID, $message->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                } else {
                                                    if ($type == "MoveMessageIntoTicket") {
                                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                                        $message->Load(true);
                                                        $message->ChannelId = getId(32);
                                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                        $ticket->Load();
                                                        $ticket->Id = $message->Id = getObjectId("ticket_id", DATABASE_TICKETS);
                                                        $ticket->Messages = array();
                                                        $ticket->Messages[0] = $message;
                                                        $ticket->Save();
                                                        $ticket->Log(8, CALLER_SYSTEM_ID, $ticket->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                                                        $message->SaveAttachments();
                                                        $message->SaveComments($ticket->Id);
                                                    } else {
                                                        if ($type == "DeleteAttachment") {
                                                            processResource(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "", RESOURCE_TYPE_FILE_INTERNAL, "", true, "100", "1");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $count++;
    }
    if ($count > 0) {
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_TICKETS);
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_EMAILS);
    }
}
function closeArchiveEntry($_chatId, $_externalFullname, $_externalId, $_internalId, $_groupId, $_email, $_company, $_phone, $_host, $_ip, $_question, $_transcriptSent = false, $_waitingtime, $_startResult, $_endResult)
{
    global $CONFIG, $LZLANG;
    $result = queryDB(true, "SELECT `voucher_id`,`endtime`,`transcript_text`,`iso_language`,`time` FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `chat_id`='" . DBManager::RealEscape($_chatId) . "' LIMIT 1;");
    $row = DBManager::FetchArray($result);
    languageSelect($row["iso_language"], true);
    $etpl = $row["transcript_text"];
    $etpl = applyReplacements($etpl, true, false, false);
    CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_CHATS);
    $_externalFullname = empty($_externalFullname) ? $LZLANG["client_guest"] . " " . getNoName($_externalId . $_ip) : $_externalFullname;
    $filter = new Chat();
    $filter->Generate($_chatId, $_externalFullname, true, true, $_question, $row["time"]);
    $filter->PlainText = applyReplacements($filter->PlainText, true, false, false);
    $filter->HTML = applyReplacements($filter->HTML, true, false, false);
    if (!empty($filter->PlainText)) {
        $etpl = str_replace("%localdate%", date("r", $filter->FirstPost), $etpl);
        if (strpos($etpl, "%transcript%") === false && strpos($etpl, "%mailtext%") === false) {
            $etpl .= $filter->PlainText;
        } else {
            if (strpos($etpl, "%transcript%") !== false) {
                $etpl = str_replace("%transcript%", $filter->PlainText, $etpl);
            } else {
                if (strpos($etpl, "%mailtext%") !== false) {
                    $etpl = str_replace("%mailtext%", $filter->PlainText, $etpl);
                }
            }
        }
    } else {
        $etpl = "";
    }
    $etpl = str_replace(array("%email%", "%eemail%"), $_email, $etpl);
    $etpl = str_replace(array("%fullname%", "%efullname%"), $_externalFullname, $etpl);
    $etpl = str_replace("%rating%", getRatingAVGFromChatId($_chatId), $etpl);
    $name = !empty($_externalFullname) ? ",`fullname`='" . DBManager::RealEscape($_externalFullname) . "'" : "";
    queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `external_id`='" . DBManager::RealEscape($_externalId) . "',`closed`='" . DBManager::RealEscape(time()) . "'" . $name . ",`internal_id`='" . DBManager::RealEscape($_internalId) . "',`group_id`='" . DBManager::RealEscape($_groupId) . "',`html`='" . DBManager::RealEscape($filter->HTML) . "',`plaintext`='" . DBManager::RealEscape($filter->PlainText) . "',`transcript_text`='" . DBManager::RealEscape($etpl) . "',`email`='" . DBManager::RealEscape($_email) . "',`company`='" . DBManager::RealEscape($_company) . "',`phone`='" . DBManager::RealEscape($_phone) . "',`host`='" . DBManager::RealEscape($_host) . "',`ip`='" . DBManager::RealEscape($_ip) . "',`gzip`=0,`wait`='" . DBManager::RealEscape($_waitingtime) . "',`accepted`='" . DBManager::RealEscape($_startResult) . "',`ended`='" . DBManager::RealEscape($_endResult) . "',`transcript_sent`='" . DBManager::RealEscape(empty($CONFIG["gl_soct"]) && empty($CONFIG["gl_scct"]) && empty($CONFIG["gl_scto"]) && empty($CONFIG["gl_sctg"]) || empty($etpl) || $filter->ElementCount == 0 || $_transcriptSent ? "1" : "0") . "',`question`='" . DBManager::RealEscape(cutString($_question, 255)) . "' WHERE `chat_id`='" . DBManager::RealEscape($_chatId) . "' AND `closed`=0 LIMIT 1;");
    $result = queryDB(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` WHERE `channel_id`='" . DBManager::RealEscape($_chatId) . "';");
    if ($result && ($rowc = DBManager::FetchArray($result))) {
        $Ticket = new Ticket($rowc["ticket_id"], true);
        $Ticket->LinkChat($rowc["channel_id"], $rowc["id"]);
    }
}