<?php

/*  Alternate WebKit
(C) 2012 by Alternate Tools
-----------------------------------------------------

File:         edit_mail.php
Description:  Administrative module to view mails/delete them

*/

    include "../modules/class_mail.php";
    include "../modules/mod_config.php";
    include "../modules/mod_admin.php";
    include "../modules/class_textfile.php";

    $cFile = new cTextFile;
    $b_NotFinished = true;
    $b_ErrorOccured = false;
    $mailMail = new cMail;
    $mailMail->SetFolder($MAIL_FOLDER);
//    $mailMail->SetFolderDeleted($MAIL_FOLDER_DELETED);
    $mailId = "";
    $mailEdtAction = "view";
    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }

    // Check action
    if (isset ($_POST['mail_EdtAction']) == true)
    {
        if ($_POST['mail_EdtAction'] == "delete")
            $mailEdtAction = "delete";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "delete")
            $mailEdtAction = "delete";
    }

    if (isset ($_POST['mail_Id']) == true)
        $mailId = $_POST['mail_Id'];
    else if (isset ($_GET['id']) == true)
        $mailId = $_GET['id'];
    if ($mailMail->LoadMailFromId($mailId) == false)
    {
        print_error_message(get_translation('admin_mailedt_errorload') . ' ' . $mailId);
        return;
    }

    // Mail edit cancelled?
    if (isset ($_POST['mail_Cancel']) || isset ($_POST['mail_Ok']))
    {
        header ('Location: ' . $ADMIN_LIST_MAILS_SITE);
        return;
    }

    // Delete the mail
    if (isset ($_POST['mail_Delete']))
    {
        if ($mailMail->DeleteMailFinalX() == false)
            $errorString = get_translation('admin_mailedt_errordelete') . ' ' . $mailIdd;
        else
        {
            log_message ('User ' . get_user_name() . ' deleted mail' . $mailMail->GetId());
            header('location: ' . $ADMIN_LIST_MAILS_SITE);
            return;
        }
    }

    // Display the input dialog for edit mail
    $titleString = get_translation('admin_mailedt_deletehead');
    if ($mailEdtAction == "view")  // Edit
        $titleString = get_translation('admin_mailedt_viewhead');
    print_admin_header($titleString, true, get_translation('admin_login_logout'), false, true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_MAILS_SITE, get_translation('admin_maill_button'));
    if ($errorString != "")
        print_error_message($errorString);
        
    if ($mailEdtAction == "delete")
    {
        echo_r(get_translation('admin_mailedt_deletequest') . ':<br>');
    }
    // Mail
    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    echo_r('<table width="100%">');
    echo_r('<tr>');
    echo_r('  <td><b>' . get_translation('admin_mail_title') . ':</b></td>');
    echo_r('  <td><b>' . $mailMail->GetTitle() . '</b></td>');
    echo_r('</tr>');
    echo_r('<tr>');
    echo_r('  <td width="30%">' . get_translation('admin_mail_id') . ':</td>');
    echo_r('  <td>' . $mailMail->GetId() . '</td>');
    echo_r('</tr>');
    echo_r('<tr>');
    echo_r('  <td>' . get_translation('admin_mail_date') . ':</td>');
    echo_r('  <td>' . $mailMail->GetDate() . '</td>');
    echo_r('</tr>');
    echo_r('<tr>');
    echo_r('  <td>' . get_translation('admin_mail_receiver') . ':</td>');
    echo_r('  <td>' . $mailMail->GetReceiver() . '</td>');
    echo_r('</tr>');
    echo_r('<tr>');
    echo_r('  <td>' . get_translation('admin_mail_message') . ':</td>');
    echo_r('  <td><textarea cols="80" rows="10" wrap="off" readonly>' . str_replace("<br>", $LINE_FEED, $mailMail->GetMessage()) . '</textarea></td>');
    echo_r('</tr>');
    echo_r('<tr>');
    echo_r('  <td>' . get_translation('admin_mail_sender') . ':</td>');
    $sender = str_replace("<", "&lt;", $mailMail->GetSender());
    $sender = str_replace(">", "&gt;", $sender);
    echo_r('  <td>' . $sender . '</td>');
    echo_r('</tr>');
    echo_r('<tr>');
    echo_r('  <td>' . get_translation('admin_mail_sent') . ':</td>');
    if ($mailMail->IsSent() == false)
        echo_r('<td><b>' . get_translation('admin_mail_stateunsent') . '!</b></td>');
    else
        echo_r('<td>' . get_translation('admin_mail_statesent') . '.</td>');
    echo_r('</tr>');
    echo_r('</table>');
    if ($mailEdtAction == "view")  // View
        print_bottom_links("mail_Ok", get_translation('admin_ok'), "", "", false);
    else // Delete
        print_bottom_links("mail_Delete", get_translation('admin_yes'),
                           "mail_Cancel", get_translation('admin_cancel'), false);
    echo_r('<input type="hidden" name="mail_Id" value="' . $mailId . '">');
    echo_r('<input type="hidden" name="mail_EdtAction" value="' . $mailEdtAction . '">');
    echo_r('</form>');

    print_admin_footer();

?>