<?php

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

File:         class_pagelist.php

*/

include "class_page.php";

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

    class cPageList
    {

        var $m_pagelFolder;
        var $m_pagelIds;

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

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

        function Init()
        {
            unset($this->m_pagelIds);
            $this->m_pagelIds = array();
            $this->m_pagelFolder = "";
        }

        function SetFolder($pagelFolder)
        {
            $this->m_pagelFolder = $pagelFolder;
        }

        function SetPageList($pagelArray)
        {
            $this->m_pagelIds = $pagelArray;
        }

        function GetPageList()
        {
            if (isset($this->m_pagelIds) == true)
                return $this->m_pagelIds;
            else
                return array();
        }
        
        function LoadPageList($filter = 0)
        {
            $rDir = dir($this->m_pagelFolder);
            $strEntry = "";
            $fileName = "";
            $pagPage = new cPage;
            $pagPage->SetFolder($this->m_pagelFolder);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.pag')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    if ($filter == 0)
                        $this->m_pagelIds[] = $fileName;
                    else
                    {
                        if ($pagPage->LoadPageFromId($fileName) == true)
                        {
                            $addMe = false;
                            switch($filter)
                            {
                                case 1:   // unlocked
                                   if ($pagPage->IsLocked() == false)
                                       $addMe = true;
                                break;
                                case 2:   // locked
                                   if ($pagPage->IsLocked() == true)
                                       $addMe = true;
                                break;
                            }
                            if ($addMe == true)
                                $this->m_pagelIds[] = $fileName;
                        }
                    }
                }
            }
        }
        
        function SortPageList($sort = -1)
        {
            if (isset($this->m_pagelIds) == false)
                return;

            $pagPage = new cPage;
            $pagPage->SetFolder($this->m_pagelFolder);
            $preArray = array();
            if ($sort == 0 || $sort < -6 || $sort > 6) // undefined, then default it
                $sort = -1;
            foreach ($this->m_pagelIds as $value)
            {
                $pagPage->LoadPageFromId($value);
                if ($sort == -1 || $sort == 1)  // Date or Id
                    $preArray[] = $pagPage->GetId() . '-|-' . $pagPage->GetId();
                else if ($sort == -2 || $sort == 2)  // Title
                    $preArray[] = $pagPage->GetTitle() . '-|-' . $pagPage->GetId();
                else if ($sort == -3 || $sort == 3)  // Nav title
                    $preArray[] = $pagPage->GetNavigationTitle() . '-|-' . $pagPage->GetId();
                else if ($sort == -4 || $sort == 4)  // Nav position
                {
                    if (is_numeric($pagPage->GetNavigationPosition()) == true)
                        $preArray[] = $pagPage->GetNavigationLevel() . sprintf("%09d", $pagPage->GetNavigationPosition()) . '-|-' . $pagPage->GetId();
                    else
                        $preArray[] = $pagPage->GetNavigationLevel() . $pagPage->GetNavigationPosition() . '-|-' . $pagPage->GetId();
                }
                else if ($sort == -5 || $sort == 5)  // Modified
                    $preArray[] = $pagPage->GetModified() . '-|-' . $pagPage->GetId();
                else  // Type
                    $preArray[] = $pagPage->GetType() . '-|-' . $pagPage->GetId();
            }
            if ($sort > 0)  // ascending
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_pagelIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_pagelIds[] = $tmpArray[1];
            }
        }
    }
}
?>