<?php

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

File:         class_maillist.php
Description:  Handling of mail lists
*/

include "class_mail.php";

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

    class cMailList
    {

        var $m_maillFolder;
        var $m_maillIds;

        function cCategoryList()  // PHP 4
        {
            $this->Init();
        }

        function __construct()
        {
            $this->Init();
        }

        function Init()
        {
            unset($this->m_maillIds);
            $this->m_maillIds = array();
            $this->m_maillFolder = "";
        }

        function SetFolder($maillFolder)
        {
            $this->m_maillFolder = $maillFolder;
        }

        function SetMailList($maillArray)
        {
            $this->m_maillIds = $maillArray;
        }

        function GetMailList()
        {
            if (isset($this->m_maillIds) == true)
                return $this->m_maillIds;
            else
                return array();
        }
        
        function LoadMailList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_maillFolder);
            $strEntry = "";
            $fileName = "";
            $mailMail = new cMail;
            $mailMail->SetFolder($this->m_maillFolder);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.mal')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    if ($filter == 0)
                        $this->m_maillIds[] = $fileName;
                    else
                    {
                        if ($mailMail->LoadMailFromId($fileName) == true)
                        {
                            $addMe = false;
                            switch($filter)
                            {
                                case 1:   // sent
                                   if ($mailMail->IsSent() == true)
                                       $addMe = true;
                                break;
                                case 2:   // unsent
                                   if ($mailMail->IsSent() == false)
                                       $addMe = true;
                                break;
                                case 3:  // this year
                                   if (substr($mailMail->GetDate(), 0, 4) == date("Y"))
                                       $addMe = true;
                                break;
                                case 4:  // this month
                                   if (substr($mailMail->GetDate(), 0, 7) == date("Y-m"))
                                       $addMe = true;
                                break;
                            }
                            if ($addMe == true)
                                $this->m_maillIds[] = $fileName;
                        }
                    }
                }
            }
            if ($sort == true)
                $this->SortByReceiver(true);
        }
        
        function SortByReceiver($bAscending = true)
        {
            $mailMail = new cMail;
            $mailMail->SetFolder($this->m_maillFolder);
            $preArray = array();
            if (count($this->m_maillIds) < 2)
                return;
            foreach ($this->m_maillIds as $value)
            {
                $mailMail->LoadMailFromId($value);
                $preArray[] = $mailMail->GetReceiver() . '-|-' . $mailMail->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_maillIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_maillIds[] = $tmpArray[1];
            }
        }
    }
}
?>