<?php

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

File:         class_newslist.php

*/

include "class_news.php";

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

    class cNewsList
    {

        var $m_newslFolder;
        var $m_newslIds;

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

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

        function Init()
        {
            unset($this->m_newslIds);
            $this->m_newslIds = array();
            $this->m_newslFolder = "";
        }

        function SetFolder($newslFolder)
        {
            $this->m_newslFolder = $newslFolder;
        }

        function SetNewsList($newslArray)
        {
            $this->m_newslIds = $newslArray;
        }

        function GetNewsList()
        {
            if (isset($this->m_newslIds) == true)
                return $this->m_newslIds;
            else
                return array();
        }
        
        function LoadNewsList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_newslFolder);
            $strEntry = "";
            $fileName = "";
            $newsNews = new cNews;
            $newsNews->SetFolder($this->m_newslFolder);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.new')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    if ($filter == 0)
                        $this->m_newslIds[] = $fileName;
                    else
                    {
                        if ($newsNews->LoadNewsFromId($fileName) == true)
                        {
                            $addMe = false;
                            switch($filter)
                            {
                                case 1:   // unlocked
                                   if ($newsNews->IsLocked() == false)
                                       $addMe = true;
                                break;
                                case 2:   // locked
                                   if ($newsNews->IsLocked() == true)
                                       $addMe = true;
                                break;
                            }
                            if ($addMe == true)
                                $this->m_newslIds[] = $fileName;
                        }
                    }
                }
            }
            if ($sort == true)
                $this->SortByPubDate(true);
        }
        
        function SortByPubDate($bAscending = true)
        {
            if (isset($this->m_newslIds) == false)
                return;
            $newsNews = new cNews;
            $newsNews->SetFolder($this->m_newslFolder);
            $preArray = array();
            foreach ($this->m_newslIds as $value)
            {
                $newsNews->LoadNewsFromId($value);
                $preArray[] = $newsNews->GetPubDate() . '-|-' . $newsNews->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_newslIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_newslIds[] = $tmpArray[1];
            }
        }
    }
}
?>