<?php

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

Filename:     class_pageimagelist.php
Description:  Class cPageImageList definition,
              handling of pageimage list

*/

include "class_pageimage.php";

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

    class cPageImageList
    {

        var $m_pgilFolder;
        var $m_pgilIds;

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

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

        function Init()
        {
            unset($this->m_pgilIds);
            $this->m_pgilIds = array();
            $this->m_pgilFolder = "";
        }

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

        function SetPageImageList($list)
        {
            $this->m_pgilIds = $list;
        }

        function GetPageImageList()
        {
            if (isset($this->m_pgilIds) == true)
                return $this->m_pgilIds;
            return array();
        }
        
        function LoadPageImageList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_pgilFolder);
            $strEntry = "";
            $fileName = "";
            $pgiPageImage = new cPageImage;
            $pgiPageImage->SetFolder($this->m_pgilFolder);
            unset ($this->m_pgilIds);

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

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

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

    }
}
?>