<?php

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

File:         list_mails.php
Description:  Administrative overview of all system-emails

*/

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

    // Check privileges
    check_login();
    if (user_is_customer() || user_is_reporter() || isset ($_POST['maill_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }

    // View mail?
    if (isset ($_POST['maill_EditMail']))
    {
        header ('Location: ' . $ADMIN_EDIT_MAILS_SITE . '?id=' . $_POST['maill_MailId'] . '&action=view');
        return;
    }
    // Delete mail?
    if (isset ($_POST['maill_DeleteMail']))
    {
        header ('Location: ' . $ADMIN_EDIT_MAILS_SITE . '?id=' . $_POST['maill_MailId'] . '&action=delete');
        return;
    }

    // Display
    print_admin_header(get_translation('admin_maill_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-11', true, get_translation('admin_backtomain'));

    $mail = new cMail;
    $mailList = new cMailList;
    $mailFilter = $FILTER_ALL;
    if (isset ($_GET['filter']) && $_GET['filter'] != "")
        $mailFilter = $_GET['filter'];
    else if (isset ($_POST['maill_filter']) && $_POST['maill_filter'] != "")
        $mailFilter = $_POST['maill_filter'];

    $mail->SetFolder($MAIL_FOLDER);
    $mailList->SetFolder($MAIL_FOLDER);

    $mailList->LoadMailList($mailFilter);
    $list = $mailList->GetMailList();
    rsort($list);

    echo_r ('<form method="post" action="">');
    echo_r ('<table border="0" width="100%">');
    echo_r ('<tr>');
    echo_r ('  <td width="10%">' . get_translation('admin_filter') . '</td>');
    echo_r ('  <td>');
    echo_r ('  <select name="maill_filter" onChange="submit();">');
    echo   ('  <option value="' . $FILTER_ALL. '"');
    if ($mailFilter == $FILTER_ALL)
        echo (' selected');
    echo_r ('>' . get_translation('admin_all') . '</option>');
    echo   ('  <option value="' . $FILTER_SENT . '"');
    if ($mailFilter == $FILTER_DONE)
        echo (' selected');
    echo_r ('>' . get_translation('admin_mail_statesent') . '</option>');
    echo   ('  <option value="' . $FILTER_UNSENT . '"');
    if ($mailFilter == $FILTER_UNDONE)
        echo (' selected');
    echo_r ('>' . get_translation('admin_mail_stateunsent') . '</option>');
    echo   ('  <option value="' . $FILTER_THISYEAR . '"');
    if ($mailFilter == $FILTER_THISYEAR)
        echo (' selected');
    echo_r ('>' . get_translation('admin_thisyear') . '</option>');
    echo   ('  <option value="' . $FILTER_THISMONTH . '"');
    if ($mailFilter == $FILTER_THISMONTH)
        echo (' selected');
    echo_r ('>' . get_translation('admin_thismonth') . '</option>');
    echo_r ('  </select>');
    echo_r ('  </td>');
    echo_r ('</tr>');
    echo_r ('</table>');
    echo_r ('</form>');

    // No entries?
    if (count($list) < 1)
        echo_r ('<p>&nbsp;' . get_translation('admin_noentries') . '</p>');
    foreach ($list as $value)
    {
        $mail->LoadMailFromId($value);
        
        echo_r('<hr>');
        echo_r('<table width="100%" border="0">');
        echo_r('  <tr>');
        echo_r('    <td width="16%">');
        echo  ('      <a href="' . $ADMIN_EDIT_MAILS_SITE . '?id=' . $mail->GetId() . '">');
        if ($mail->IsSent() == true)
            echo_r('<img src="' . $MAIL_IMAGE . '" title="' . get_translation('admin_view') .'" alt=""></a>');
        else
            echo_r('<img src="' . $MAIL_FAILED_IMAGE . '" title="' . get_translation('admin_view') .'" alt=""></a>');
        echo_r('    </td>');
        echo_r('    <td width="32%">');
        echo_r('       ' . get_translation('admin_mail_id') . ': ' . $mail->GetId() . '<br>');
        echo_r('       ' . get_translation('admin_mail_date') . ': ' . $mail->GetDate());
        echo_r('    </td>');
        echo_r('    <td width="32%">');
        echo_r('       ' . get_translation('admin_mail_receiver') . ': ' . $mail->GetReceiver() . '<br>');
        echo_r('       ' . get_translation('admin_mail_title') . ': ' . $mail->GetTitle());
        echo_r('    </td>');
        echo_r('    <td width="20%">');
        echo_r('    <form method="post" action="">');
        echo_r('    <input type="hidden" value="' . $mail->GetId() . '" name="maill_MailId">  ');
        echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_view') . '" name="maill_EditMail">  ');
        echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_delete') . '" name="maill_DeleteMail">  ');
        echo_r('    </form>');
        echo_r('    </td>');
        echo_r('  </tr>');
        echo_r('</table>');
    }
    print_bottom_links("maill_Cancel", get_translation('admin_ok'));

    print_admin_footer();
?>