function SendEditorReply($_messageId, $_email, $_qMessageId = "")
 {
     $tsData = array("", "");
     $reply = new TicketMessage($_messageId, false);
     $reply->Load();
     $pdm = PredefinedMessage::GetByLanguage(Server::$Groups[$this->Group]->PredefinedMessages, $this->Language);
     if ($pdm != null && $this->CreationType < 6) {
         if (!empty($pdm->EmailTicketReplyBodyPlaintext)) {
             $tsData[0] = $pdm->EmailTicketReplyBodyPlaintext;
         }
         if (!empty($pdm->EmailTicketReplyBodyHTML)) {
             $tsData[1] = $pdm->EmailTicketReplyBodyHTML;
         }
     }
     if (!empty($_qMessageId)) {
         $quote = new TicketMessage($_qMessageId, false);
         $quote->Load();
         $tsData[0] = $quote->TextReplace($tsData[0], $this->Group);
         $tsData[1] = $quote->TextReplace($tsData[1], $this->Group);
     } else {
         $quote = null;
     }
     $tsData[0] = Server::$Groups[$this->Group]->TextReplace($tsData[0], $this->Language);
     $tsData[1] = Server::$Groups[$this->Group]->TextReplace($tsData[1], $this->Language);
     $tsData[0] = Server::$Operators[CALLER_SYSTEM_ID]->TextReplace($tsData[0]);
     $tsData[1] = Server::$Operators[CALLER_SYSTEM_ID]->TextReplace($tsData[1]);
     $tsData[0] = $this->TextReplace($tsData[0]);
     $tsData[1] = $this->TextReplace($tsData[1]);
     for ($i = 0; $i < count($tsData); $i++) {
         if ($i == 1 && empty($tsData[1])) {
             continue;
         }
         $lb = $i == 0 ? "\r\n\r\n" : "<br><br>";
         if (empty($tsData[$i]) || strpos($tsData[$i], "%mailtext%") === false) {
             $tsData[$i] .= $lb . "%mailtext%";
         }
         if ($this->CreationType < 6) {
             if (empty($tsData[$i]) || strpos($tsData[$i], "%quote%") === false) {
                 $tsData[$i] .= $lb . "%quote%";
             }
             if (empty($tsData[$i]) || strpos($tsData[$i], "%ticket_hash%") === false) {
                 $tsData[$i] .= $lb . "%ticket_hash%";
             }
         }
         $qText = !empty($quote) ? "\r\n\r\n" . $quote->GetQuoteFormat($i == 0) : "";
         $tText = $i != 1 ? strip_tags($reply->Text) : $reply->Text;
         $tText = ($i == 0 ? $tText : nl2br($tText)) . $lb . $lb . "<!--lz_ref_link-->";
         $tsData[$i] = str_replace(array($this->GetHash(false), $this->GetHash(true)), "", $tsData[$i]);
         $tsData[$i] = $this->TextReplace($tsData[$i]);
         $tsData[$i] = Server::$Operators[$reply->SenderUserId]->TextReplace($tsData[$i]);
         $tsData[$i] = Server::$Groups[$this->Group]->TextReplace($tsData[$i], $this->Language);
         $tsData[$i] = Configuration::Replace($tsData[$i]);
         $tsData[$i] = str_replace("%mailtext%", $tText, $tsData[$i]);
         $tsData[$i] = str_replace("%quote%", trim($qText), $tsData[$i]);
         $tsData[$i] = str_replace("%ticket_hash%", $this->GetHash(true, $i == 1), $tsData[$i]);
         $tsData[$i] = Mailbox::FinalizeEmail($tsData[$i], $i == 1, $this->CreationType >= 6);
     }
     if (empty($reply->Subject)) {
         $reply->Subject = $pdm != null ? $pdm->SubjectTicketReply : "";
         $reply->Subject = str_replace("%ticket_hash%", $this->GetHash(true), $reply->Subject);
     }
     if ($this->CreationType >= 6 && !empty($this->ChannelId)) {
         $channel = SocialMediaChannel::GetChannelById($this->ChannelId);
         if ($channel != null) {
             $channel->SendReply($this, $reply, str_replace($this->GetHash(true), "", trim($tsData[0])), $quote);
         }
     }
     $mailbox = Mailbox::GetById(Server::$Groups[$this->Group]->TicketEmailOut, true);
     if ($mailbox != null) {
         if (!empty(Server::$Configuration->File["gl_scoo"])) {
             $_email .= "," . trim(Server::$Configuration->File["gl_scoo"]);
         }
         Communication::SendEmail($mailbox, str_replace(array(",,", ";"), ",", $_email), $mailbox->Email, $tsData[0], $tsData[1], Mailbox::GetSubject($reply->Subject, $_email, $this->Messages[0]->Fullname, $this->Group, "", $this->Messages[0]->Company, $this->Messages[0]->Phone, $this->Messages[0]->IP, $this->Messages[0]->Text, Server::$Groups[$this->Group]->GetDescription($this->Language), $this->Messages[0]->Customs), false, $reply->Attachments);
     }
 }
 function CreateArchiveEntry($_internalUser, $_visitor, $custom = "", $etpl = "", $_customsInTranscript = true, $pdm = null)
 {
     if (!empty($this->ArchiveCreated)) {
         return;
     }
     if (Server::$Inputs[111]->Active) {
         $custom .= strip_tags(Server::$Inputs[111]->Caption) . " %efullname%\r\n";
     }
     if (Server::$Inputs[112]->Active) {
         $custom .= strip_tags(Server::$Inputs[112]->Caption) . " %eemail%\r\n";
     }
     if (!empty($this->Company) && Server::$Inputs[113]->Active) {
         $custom .= strip_tags(Server::$Inputs[113]->Caption) . " " . trim($this->UserData->Company) . "\r\n";
     }
     if (!empty($this->Question) && Server::$Inputs[114]->Active) {
         $custom .= strip_tags(Server::$Inputs[114]->Caption) . " " . trim($this->UserData->Text) . "\r\n";
     }
     if (!empty($this->Phone) && Server::$Inputs[116]->Active) {
         $custom .= strip_tags(Server::$Inputs[116]->Caption) . " " . trim($this->UserData->Phone) . "\r\n";
     }
     if (!empty($this->ChatVoucherId)) {
         $custom .= strip_tags(LocalizationManager::$TranslationStrings["client_voucher_id"]) . " " . trim($this->ChatVoucherId) . "\r\n";
     }
     $customs = array();
     if (is_array($this->UserData->Customs)) {
         foreach ($this->UserData->Customs as $cind => $value) {
             if (Server::$Inputs[$cind]->Active && Server::$Inputs[$cind]->Custom) {
                 $customs[Server::$Inputs[$cind]->Name] = $value;
                 if (!isset(Server::$Groups[$this->DesiredChatGroup]->ChatInputsHidden[$cind]) && $_customsInTranscript) {
                     if (Server::$Inputs[$cind]->Type == "CheckBox") {
                         $custom .= strip_tags(Server::$Inputs[$cind]->Caption) . " " . (!empty($value) ? "<!--lang_client_yes-->" : "<!--lang_client_no-->") . "\r\n";
                     } else {
                         if (Server::$Inputs[$cind]->Type == "ComboBox") {
                             $custom .= strip_tags(Server::$Inputs[$cind]->Caption) . " " . Server::$Inputs[$cind]->GetClientValue($value) . "\r\n";
                         } else {
                             $custom .= strip_tags(Server::$Inputs[$cind]->Caption) . " " . $value . "\r\n";
                         }
                     }
                 }
             }
         }
     }
     $tsData = array("", "");
     if (!empty(Server::$Groups[$this->DesiredChatGroup]->PredefinedMessages)) {
         $pdm = PredefinedMessage::GetByLanguage(Server::$Groups[$this->DesiredChatGroup]->PredefinedMessages, $_visitor->Language);
         if (!empty($pdm->EmailChatTranscriptBodyPlaintext)) {
             $tsData[0] = $pdm->EmailChatTranscriptBodyPlaintext;
         }
         if (!empty($pdm->EmailChatTranscriptBodyHTML)) {
             $tsData[1] = $pdm->EmailChatTranscriptBodyHTML;
         }
     }
     for ($i = 0; $i < count($tsData); $i++) {
         $tsData[$i] = str_replace("%chat_id%", $this->ChatId, $tsData[$i]);
         $tsData[$i] = str_replace("%details%", $custom, $tsData[$i]);
         $tsData[$i] = str_replace("%external_name%", "%efullname%", $tsData[$i]);
         $tsData[$i] = str_replace("%external_email%", "%eemail%", $tsData[$i]);
         $tsData[$i] = str_replace("%external_company%", $this->UserData->Company, $tsData[$i]);
         $tsData[$i] = str_replace("%external_phone%", $this->UserData->Phone, $tsData[$i]);
         $tsData[$i] = str_replace("%question%", $this->UserData->Text, $tsData[$i]);
         $tsData[$i] = str_replace("%feedback_link%", Feedback::GetLink("cid=" . Encoding::Base64UrlEncode($this->ChatId)), $tsData[$i]);
         $tsData[$i] = Server::$Groups[$this->DesiredChatGroup]->TextReplace($tsData[$i], $_visitor->Language);
         if (!empty($this->OperatorId)) {
             $tsData[$i] = Server::$Operators[$this->OperatorId]->TextReplace($tsData[$i]);
         }
         $tsData[$i] = $_visitor->TextReplace($tsData[$i]);
         $tsData[$i] = Configuration::Replace($tsData[$i]);
     }
     $subject = $pdm != null ? $pdm->SubjectChatTranscript : "";
     $subject = Mailbox::GetSubject($subject, $this->UserData->Email, $this->UserData->Fullname, $this->DesiredChatGroup, $this->ChatId, $this->UserData->Company, $this->UserData->Phone, Communication::GetIP(), $this->UserData->Text, Server::$Groups[$this->DesiredChatGroup]->GetDescription($_visitor->Language), $this->UserData->Customs);
     $internal = $_internalUser != null && $_internalUser->IsBot ? $_internalUser->SystemId : "";
     $result = DBManager::Execute(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "';");
     if ($result && DBManager::GetRowCount($result) == 0) {
         if (Server::$Configuration->File["gl_adct"] == 1 || (!empty(Server::$Configuration->File["gl_rm_chats_time"]) || empty(Server::$Configuration->File["gl_rm_chats"]))) {
             $url = Visitor::GetLastURLFromVisitor($this->UserId);
             DBManager::Execute(true, "INSERT INTO `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` (`time`,`endtime`,`closed`,`chat_id`,`external_id`,`fullname`,`internal_id`,`group_id`,`area_code`,`html`,`plaintext`,`transcript_text`,`transcript_html`,`email`,`company`,`iso_language`,`iso_country`,`host`,`ip`,`gzip`,`transcript_sent`,`transcript_receiver`,`question`,`customs`,`subject`,`voucher_id`,`ref_url`) VALUES ('" . DBManager::RealEscape($this->FirstActive) . "',0,0,'" . DBManager::RealEscape($this->ChatId) . "','" . DBManager::RealEscape($this->UserId) . "','','" . DBManager::RealEscape($internal) . "','','" . DBManager::RealEscape($this->Code) . "','','','" . DBManager::RealEscape($tsData[0]) . "','" . DBManager::RealEscape($tsData[1]) . "','','','" . DBManager::RealEscape($_visitor->Language) . "','" . DBManager::RealEscape($_visitor->GeoCountryISO2) . "','" . DBManager::RealEscape($_visitor->Host) . "','" . DBManager::RealEscape($_visitor->IP) . "',0,0,'" . DBManager::RealEscape($this->UserData->Email) . "','','" . DBManager::RealEscape(@serialize($customs)) . "','" . DBManager::RealEscape($subject) . "','" . DBManager::RealEscape($this->ChatVoucherId) . "','" . DBManager::RealEscape($url) . "');");
         }
         $this->ArchiveCreated = 1;
         ChatRequest::AcceptAll($this->UserId);
     }
 }