static function SendChatTranscripts($_custom = false)
 {
     Server::InitDataBlock(array("INTERNAL", "INPUTS"));
     Chat::CloseChats();
     $defmailbox = Mailbox::GetDefaultOutgoing();
     $result = DBManager::Execute(false, "SELECT `voucher_id`,`subject`,`customs`,`internal_id`,`transcript_text`,`transcript_html`,`transcript_receiver`,`email`,`chat_id`,`fullname`,`group_id` FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `endtime`>0 AND `closed`>0 AND `transcript_sent`=0 LIMIT 1;");
     if ($result) {
         while ($row = DBManager::FetchArray($result)) {
             DBManager::Execute(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `transcript_sent`=1 WHERE `chat_id`='" . DBManager::RealEscape($row["chat_id"]) . "' LIMIT 1;");
             if (empty($row["transcript_html"]) && empty($row["transcript_text"])) {
                 continue;
             }
             $tData = array($row["transcript_text"], $row["transcript_html"]);
             for ($i = 0; $i < count($tData); $i++) {
                 if ($i == 1 && empty($tData[$i])) {
                     continue;
                 }
                 $tData[$i] = str_replace(array("%fullname%", "%efullname%"), $row["fullname"], $tData[$i]);
                 $tData[$i] = str_replace(array("%email%", "%eemail%"), $row["email"], $tData[$i]);
                 $tData[$i] = str_replace("%rating%", Feedback::GetRatingAVG($row["chat_id"]), $tData[$i]);
                 $subject = $row["subject"];
                 $customs = @unserialize($row["customs"]);
                 $fakeSender = "";
                 foreach (Server::$Inputs as $index => $input) {
                     if ($input->Active && $input->Custom && !isset(Server::$Groups[$row["group_id"]]->TicketInputsHidden[$index])) {
                         $cv = "";
                         if ($input->Type == "CheckBox") {
                             $cv = !empty($customs[$input->Name]) ? "<!--lang_client_yes-->" : "<!--lang_client_no-->";
                         } else {
                             if (!empty($customs[$input->Name]) || $input->Type == "ComboBox") {
                                 $cv = $input->GetClientValue(@$customs[$input->Name]);
                             }
                         }
                         $tData[$i] = str_replace("%custom" . $index . "%", $cv, $tData[$i]);
                     }
                 }
                 $tData[$i] = Server::Replace($tData[$i]);
                 $tData[$i] = Mailbox::FinalizeEmail($tData[$i], $i == 1);
             }
             $mailbox = null;
             if (!empty($row["group_id"]) && isset(Server::$Groups[$row["group_id"]]) && !empty(Server::$Groups[$row["group_id"]]->ChatEmailOut)) {
                 $mailbox = Mailbox::GetById(Server::$Groups[$row["group_id"]]->ChatEmailOut);
             }
             $mailbox = !empty($mailbox) ? $mailbox : $defmailbox;
             if ($mailbox != null && (!empty(Server::$Configuration->File["gl_soct"]) || $_custom) && !empty($row["transcript_receiver"])) {
                 Communication::SendEmail($mailbox, $row["transcript_receiver"], $mailbox->Email, $tData[0], $tData[1], $subject);
             }
             if (!empty(Server::$Configuration->File["gl_scto"]) && !$_custom) {
                 Server::InitDataBlock(array("INTERNAL"));
                 $receivers = array();
                 $resulti = DBManager::Execute(true, "SELECT `user_id` FROM `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "` WHERE `chat_id`='" . DBManager::RealEscape($row["chat_id"]) . "' AND `ltime`=0;");
                 if ($resulti) {
                     while ($rowi = DBManager::FetchArray($resulti)) {
                         if (!empty(Server::$Operators[$rowi["user_id"]]) && !in_array($rowi["user_id"], $receivers)) {
                             $receivers[] = $rowi["user_id"];
                         } else {
                             continue;
                         }
                         Communication::SendEmail($mailbox, Server::$Operators[$receivers[count($receivers) - 1]]->Email, $mailbox->Email, $tData[0], $tData[1], $subject);
                     }
                 }
             }
             if (!empty(Server::$Configuration->File["gl_sctg"]) && !$_custom) {
                 Server::InitDataBlock(array("GROUPS"));
                 Communication::SendEmail($mailbox, Server::$Groups[$row["group_id"]]->Email, $mailbox->Email, $tData[0], $tData[1], $subject);
             }
             if (!empty($mailbox) && !empty(Server::$Configuration->File["gl_scct"])) {
                 if (!empty(Server::$Configuration->File["gl_uvec"])) {
                     if (Mailbox::IsValidEmail($row["transcript_receiver"])) {
                         $fakeSender = $row["transcript_receiver"];
                     } else {
                         if (Mailbox::IsValidEmail($row["email"])) {
                             $fakeSender = $row["email"];
                         }
                     }
                 }
                 Communication::SendEmail($mailbox, Server::$Configuration->File["gl_scct"], $mailbox->Email, $tData[0], $tData[1], $subject, false, null, $fakeSender);
             }
             if (!empty($row["voucher_id"])) {
                 $ticket = new CommercialChatVoucher(null, $row["voucher_id"]);
                 $ticket->Load();
                 $ticket->SendStatusEmail();
             }
         }
     }
     if (!empty(Server::$Configuration->File["gl_rm_chats"]) && Server::$Configuration->File["gl_rm_chats_time"] == 0) {
         DBManager::Execute(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `transcript_sent` = '1';");
     }
 }
function sendChatTranscripts($_custom = false)
{
    global $CONFIG, $INTERNAL, $GROUPS, $INPUTS;
    initData(array("INTERNAL", "INPUTS"));
    closeChats();
    $defmailbox = Mailbox::GetDefaultOutgoing();
    $result = queryDB(false, "SELECT `voucher_id`,`subject`,`customs`,`internal_id`,`transcript_text`,`transcript_receiver`,`email`,`chat_id`,`fullname`,`group_id` FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `endtime`>0 AND `closed`>0 AND `transcript_sent`=0 LIMIT 1;");
    if ($result) {
        while ($row = DBManager::FetchArray($result)) {
            if (empty($row["transcript_text"])) {
                continue;
            }
            queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `transcript_sent`=1 WHERE `chat_id`='" . DBManager::RealEscape($row["chat_id"]) . "' LIMIT 1;");
            $rcvs = str_replace(array("%fullname%", "%efullname%"), $row["fullname"], $row["transcript_text"]);
            $rcvs = str_replace(array("%email%", "%eemail%"), $row["email"], $rcvs);
            $rcvs = str_replace("%rating%", getRatingAVGFromChatId($row["chat_id"]), $rcvs);
            $subject = $row["subject"];
            $customs = @unserialize($row["customs"]);
            $fakeSender = "";
            foreach ($INPUTS as $index => $input) {
                if ($input->Active && $input->Custom && !isset($GROUPS[$row["group_id"]]->TicketInputsHidden[$index])) {
                    $cv = "";
                    if ($input->Type == "CheckBox") {
                        $cv = !empty($customs[$input->Name]) ? "<!--lang_client_yes-->" : "<!--lang_client_no-->";
                    } else {
                        if (!empty($customs[$input->Name]) || $input->Type == "ComboBox") {
                            $cv = $input->GetClientValue(@$customs[$input->Name]);
                        }
                    }
                    $rcvs = str_replace("%custom" . $index . "%", $cv, $rcvs);
                }
            }
            $rcvs = applyReplacements($rcvs);
            $rcvs = Mailbox::FinalizeEmail($rcvs . "\r\n\r\n<!--lz_ref_link-->");
            $mailbox = null;
            if (!empty($row["group_id"]) && isset($GROUPS[$row["group_id"]]) && !empty($GROUPS[$row["group_id"]]->ChatEmailOut)) {
                $mailbox = Mailbox::GetById($GROUPS[$row["group_id"]]->ChatEmailOut);
            }
            $mailbox = !empty($mailbox) ? $mailbox : $defmailbox;
            if ((!empty($CONFIG["gl_soct"]) || $_custom) && !empty($row["transcript_receiver"])) {
                sendMail($mailbox, $row["transcript_receiver"], $mailbox->Email, $rcvs, $subject);
            }
            if (!empty($CONFIG["gl_scto"]) && !$_custom) {
                initData(array("INTERNAL"));
                $receivers = array();
                $resulti = queryDB(true, "SELECT `sender`,`receiver` FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `chat_id`='" . DBManager::RealEscape($row["chat_id"]) . "';");
                if ($resulti) {
                    while ($rowi = DBManager::FetchArray($resulti)) {
                        if (!empty($INTERNAL[$rowi["sender"]]) && !in_array($rowi["sender"], $receivers)) {
                            $receivers[] = $rowi["sender"];
                        } else {
                            if (!empty($INTERNAL[$rowi["receiver"]]) && !in_array($rowi["receiver"], $receivers)) {
                                $receivers[] = $rowi["receiver"];
                            } else {
                                continue;
                            }
                        }
                        sendMail($mailbox, $INTERNAL[$receivers[count($receivers) - 1]]->Email, $mailbox->Email, $rcvs, $subject);
                    }
                }
            }
            if (!empty($CONFIG["gl_sctg"]) && !$_custom) {
                initData(array("GROUPS"));
                sendMail($mailbox, $GROUPS[$row["group_id"]]->Email, $mailbox->Email, $rcvs, $subject);
            }
            if (!empty($mailbox) && !empty($CONFIG["gl_scct"])) {
                if (!empty($CONFIG["gl_uvec"])) {
                    if (Mailbox::IsValidEmail($row["transcript_receiver"])) {
                        $fakeSender = $row["transcript_receiver"];
                    } else {
                        if (Mailbox::IsValidEmail($row["email"])) {
                            $fakeSender = $row["email"];
                        }
                    }
                }
                sendMail($mailbox, $CONFIG["gl_scct"], $mailbox->Email, $rcvs, $subject, false, null, $fakeSender);
            }
            if (!empty($row["voucher_id"])) {
                $ticket = new CommercialChatVoucher(null, $row["voucher_id"]);
                $ticket->Load();
                $ticket->SendStatusEmail();
            }
        }
    }
    if (!empty($CONFIG["gl_rm_chats"]) && $CONFIG["gl_rm_chats_time"] == 0) {
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `transcript_sent` = '1';");
    }
}