<?php

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

Filename:     class_pagefilelist.php
Description:  Class cPageFileist definition,
              handling of pagefile list

*/

include "class_pagefile.php";

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

    class cPageFileList
    {

        var $m_pgflFolder;
        var $m_pgflIds;

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

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

        function Init()
        {
            unset($this->m_pgflIds);
            $this->m_pgflIds = array();
            $this->m_pgflFolder = "";
        }

        function SetFolder($folder)
        {
            $this->m_pgflFolder = $folder;
        }

        function SetPageFileList($list)
        {
            $this->m_pgflIds = $list;
        }

        function GetPageFileList()
        {
            if (isset($this->m_pgflIds) == true)
                return $this->m_pgflIds;
            return array();
        }
        
        function LoadPageFileList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_pgflFolder);
            $strEntry = "";
            $fileName = "";
            $pgfPageFile = new cPageFile;
            $pgfPageFile->SetFolder($this->m_pgflFolder);
            unset ($this->m_pgflIds);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.pgf')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    if ($filter == 0)
                        $this->m_pgflIds[] = $fileName;
                    else
                    {
                        if ($pgfPageFile->LoadPageFileFromId($fileName) == true)
                        {
                            $addMe = false;
                            switch($filter)
                            {
                                case 1:   // unlocked
                                   if ($pgfPageFile->IsLocked() == false)
                                       $addMe = true;
                                break;
                                case 2:   // locked
                                   if ($pgfPageFile->IsLocked() == true)
                                       $addMe = true;
                                break;
                            }
                            if ($addMe == true)
                                $this->m_pgflIds[] = $fileName;
                        }
                    }
                }
            }
            if ($sort == true)
                $this->SortByTitle(true);
        }

        function SortByTitle($bAscending = true)
        {
            if (empty($this->m_pgflIds) == true)
                return; // empty
            $pgfPageFile = new cPageFile;
            $pgfPageFile->SetFolder($this->m_pgflFolder);
            $preArray = array();
            foreach ($this->m_pgflIds as $value)
            {
                $pgfPageFile->LoadPageFileFromId($value);
                $preArray[] = $pgfPageFile->GetTitle() . '-|-' . $pgfPageFile->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_pgflIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_pgflIds[] = $tmpArray[1];
            }
        }

        function SortByOriginName($bAscending = true)
        {
            if (empty($this->m_pgflIds) == true)
                return; // empty
            $pgfPageFile = new cPageFile;
            $pgfPageFile->SetFolder($this->m_pgflFolder);
            $preArray = array();
            foreach ($this->m_pgflIds as $value)
            {
                $pgfPageFile->LoadPageFileFromId($value);
                $preArray[] = $pgfPageFile->GetOriginName() . '-|-' . $pgfPageFile->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_pgflIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_pgflIds[] = $tmpArray[1];
            }
        }

    }
}
?>