<?php

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

File:         class_themelist.php
Description:  Class cThemeList definition
              to load theme list

*/

include "class_theme.php";

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

    class cThemeList
    {

        var $m_thmlFolder;
        var $m_thmlIds;

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

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

        function Init()
        {
            unset($this->m_thmlIds);
            $this->m_thmlIds = array();
            $this->m_thmlFolder = "";
        }

        function SetFolder($themelFolder)
        {
            $this->m_thmlFolder = $themelFolder;
        }

        function SetThemeList($themelArray)
        {
            $this->m_thmlIds = $themelArray;
        }

        function GetThemeList()
        {
            if (isset($this->m_thmlIds) == true)
                return $this->m_thmlIds;
            else
                return array();
        }
        
        function LoadThemeList($filter = 0)
        {
            $rDir = dir($this->m_thmlFolder);
            $strEntry = "";
            $fileName = "";
            $thmTheme = new cTheme;
            $thmTheme->SetFolder($this->m_thmlFolder);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -6 ) == '.wkthm')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 6);
                    if ($filter == 0)
                        $this->m_thmlIds[] = $fileName;
                    else
                    {
                        if ($thmTheme->LoadThemeFromId($fileName) == true)
                        {
                            $addMe = false;
                            switch($filter)
                            {
                                case 1:   // unlocked
                                   if ($thmTheme->IsLocked() == false)
                                       $addMe = true;
                                break;
                                case 2:   // locked
                                   if ($thmTheme->IsLocked() == true)
                                       $addMe = true;
                                break;
                            }
                            if ($addMe == true)
                                $this->m_thmlIds[] = $fileName;
                        }
                    }
                }
            }
        }
        
        function SortThemeList($sort = -1)
        {
            if (isset($this->m_thmlIds) == false)
                return;

            $thmTheme = new cTheme;
            $thmTheme->SetFolder($this->m_thmlFolder);
            $preArray = array();
            if ($sort == 0 || $sort < -6 || $sort > 6) // undefined, then default it
                $sort = -1;
            foreach ($this->m_thmlIds as $value)
            {
                $thmTheme->LoadThemeFromId($value);
                if ($sort == -1 || $sort == 1)  // Id
                    $preArray[] = $thmTheme->GetId() . '-|-' . $thmTheme->GetId();
                else if ($sort == -2 || $sort == 2)  // Date
                    $preArray[] = $thmTheme->GetDate() . '-|-' . $thmTheme->GetId();
                else if ($sort == -3 || $sort == 3)  // Name
                    $preArray[] = $thmTheme->GetName() . '-|-' . $thmTheme->GetId();
                else if ($sort == -4 || $sort == 4)  // Description
                    $preArray[] = $thmTheme->GetDescription() . '-|-' . $thmTheme->GetId();
            }
            if ($sort > 0)  // ascending
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_thmlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_thmlIds[] = $tmpArray[1];
            }
        }
    }
}
?>