<?php

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

File:         send_newsletter.php
Description:  Administrative module send newsletters

*/

    include "../modules/mod_admin.php";
    include "../modules/class_userlist.php";
    include "../modules/class_newsletter.php";

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

    // Cancel newsletter?
    if (isset($_POST['newslett_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }

    // Send newsletter?
    if (isset($_POST['newslett_Send']))
    {
        if (isset($_POST['news_content']) == true && $_POST['news_content'] != "")
        {
            $userList = new cUserList;
            $userList->SetFolder($USER_FOLDER);
            $userList->LoadNewsletterUserList();
            $newsLetter = new cNewsletter;
            $newsLetter->CreateNewsletter();
            $newsLetter->SetUserFolder($USER_FOLDER);
            $newsLetter->SetUserList($userList->GetUserList());
            $newsLetter->SetNewsText($_POST['news_content']);
            $newsLetter->SetNewsTitle($SITE_TITLE . ': ' . get_translation('admin_newsletter_from') . ' ' . date("Y-m-d H:i:s"));
            $newsLetter->SetEmail($SITE_CONTACT_ADDRESS);
            $newsLetter->SetUnsubscribeText(get_translation('admin_newsletter_unsubscribe'));
            $newsLetter->SetUnsubscribeUrl($_SERVER['SERVER_NAME']);
            $newsLetter->SetFolder($MAIL_FOLDER);
            if ($newsLetter->SendNewsletter() == true)
            {
                log_message ('User ' . get_user_name() . ' wrote a newsletter');
                header('location: ' . $ADMIN_MAIN_SITE);
                return;
            }
            else
                $errorMessage = get_translation('admin_newsletter_errorsend');
        }
        else
            $errorMessage = get_translation('admin_newsletter_errortext');
    }

    // Display
    print_admin_header(get_translation('admin_newsletter_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-8', true, get_translation('admin_backtomain'));
    if ($errorMessage != "")
        print_error_message($errorMessage);
        
    echo_r ('<form method="post" action="">');

    // Newsletter content
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td>' . get_translation('admin_newsletter_text') . ': </td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td>');
    if (isset($_POST['news_content']) == true)
        echo_r ('      <textarea cols="100" rows="15" name="news_content" wrap="off">' . $_POST['news_content'] . '</textarea>');
    else
        echo_r ('      <textarea cols="100" rows="15" name="news_content" wrap="off"></textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');


    echo_r('<br>');

    print_bottom_links("newslett_Send", get_translation('admin_send'), "newslett_Cancel", get_translation('admin_cancel'), false);
    echo_r('</form>');

    print_admin_footer();
?>