<?php

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

File:         class_newsletter.php
Description:  Class cNewsletter definition,
              handling of newsletter sending

*/

include "class_user.php";

if (!defined("CLASS_NEWSLETTER"))
{
    define ("CLASS_NEWSLETTER", "DEFINED");

    class cNewsletter
    {
        var $m_newsId;
        var $m_newsDate;
        var $m_newsTitle;
        var $m_newsText;
        var $m_userList;
        var $m_Email;
        var $m_Folder;
        var $m_userFolder;
        
        // PHP 4
        function cNewsletter()
        {
            $this->InitNews();
        }

        // PHP 5
        function __construct()
        {
            $this->InitNews();
        }

        function InitNews()
        {
            unset ($this->m_userList);
            $this->m_userList = array();
            $this->m_newsId = 0;
            $this->m_newsDate = "";
            $this->m_newsTitle = "";
            $this->m_newsText = "";
            $this->m_Email = "";
            $this->m_UnsubscribeUrl = "";
            $this->m_UnsubscribeText = "";
            
            $this->m_Folder = "";
            $this->m_userFolder = "";
        }
        
        // Getter / setter
        function SetUserList($userList)
        {
            $this->m_userList = $userList;
        }
        function GetUserList()
        {
            return $this->m_userList;
        }
        
        function SetNewsText($newsText)
        {
            $this->m_newsText = $newsText;
        }
        function GetNewsText()
        {
            return $this->m_newsText;
        }

        function SetNewsTitle($newsTitle)
        {
            $this->m_newsTitle = $newsTitle;
        }
        function GetNewsTitle()
        {
            return $this->m_newsTitle;
        }

        function SetEmail($email)
        {
            $this->m_Email = $email;
        }

        function SetUnsubscribeUrl($url)
        {
            $this->m_UnsubscribeUrl = $url;
        }
        function SetUnsubscribeText($text)
        {
            $this->m_UnsubscribeText = $text;
        }

        function SetFolder($folder)
        {
            $this->m_Folder = $folder;
        }
        function SetUserFolder($userFolder)
        {
            $this->m_userFolder = $userFolder;
        }

        
        // Main methods

        function CreateNewsletter()
        {
            $this->InitNews();
            $this->m_newsId = uniqid("new", FALSE);
            $this->m_newsDate = date("Y-m-d H:i:s");
        }

        function SendNewsLetter()
        {
            $newRow = "\r\n";
            $usrUser = new cUser();
            $usrUser->SetFolder($this->m_userFolder);
            $messageText;
            $mailSent = true;
            for ($i = 0; $i < count($this->m_userList); $i ++)
            {
                if ($usrUser->LoadUserFromId($this->m_userList[$i]) == false)
                    return false;
                // Add unsubscribe hint
                $messageText = $this->m_newsText . $newRow . $newRow;
                $messageText = $messageText . $this->m_UnsubscribeText . $newRow;
                $messageText = $messageText . $this->m_UnsubscribeUrl;

                if (mail($usrUser->GetEmail(), $this->m_newsTitle, $messageText, "From:" . $this->m_Email) == false)
                    $mailSent = false;
            }
            return $this->SaveNewsletter($mailSent);
        }

        function SaveNewsletter($mailSent)
        {
            $usrUser = new cUser();
            $usrUser->SetFolder($this->m_userFolder);
            $newRow = "\r\n";
            $fileName = $this->m_Folder . $this->m_newsId . ".mal";   // Save as mail!!
            $newsReceivers = "";
            // echo $fileName;

            if ($this->m_newsId == "0" || $this->m_Folder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("mail");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_newsId);
            $cStorage->AddData("Date", $this->m_newsDate);
            $cStorage->AddData("Title", $this->m_newsTitle);
            $cStorage->AddData("Message", $this->m_newsText);
            for ($i = 0; $i < count($this->m_userList); $i ++)
            {
                $usrUser->LoadUserFromId($this->m_userList[$i]);
                $newsReceivers = $newsReceivers . ($usrUser->GetFirstName() . ' ' . $usrUser->GetLastName() . '(' . $usrUser->GetEmail() . ')');
                if ($i < count($this->m_userList) - 1)
                    $newsReceivers = $newsReceivers . ', ';
            }
            $cStorage->AddData("Receiver", $newsReceivers);
            $cStorage->AddData("Sender", $this->m_Email);
            $cStorage->AddData("Sent", $mailSent, false);

            return $cStorage->SaveStorageToFile($fileName);
        }

        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
    }
}