<?php

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

File:         class_downloadlist.php

*/

include "class_download.php";

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

    class cDownloadList
    {

        var $m_dldlFolder;
        var $m_dldlIds;

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

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

        function Init()
        {
            unset($this->m_dldlIds);
            $this->m_dldlIds = array();
            $this->m_dldlFolder = "";
        }

        function SetFolder($dldlFolder)
        {
            $this->m_dldlFolder = $dldlFolder;
        }

        function SetDownloadList($dldlArray)
        {
            $this->m_dldlIds = $dldlArray;
        }

        function GetDownloadList()
        {
            if (isset($this->m_dldlIds) == true)
                return $this->m_dldlIds;
            return array();
        }
        
        function LoadDownloadList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_dldlFolder);
            $strEntry = "";
            $fileName = "";
            $dldDownload = new cDownload;
            $dldDownload->SetFolder($this->m_dldlFolder);
            unset ($this->m_dldlIds);

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

        function LoadDownloadListFromCatId($catId, $filter = 0, $sort = true)
        {
            $this->LoadDownloadList($filter, $sort);
            $tmpArray = array();
            $dldDownload = new cDownload;
            $dldDownload->SetFolder($this->m_dldlFolder);
            if (isset($this->m_dldlIds) == false)
                $this->m_dldlIds = array();
            for ($i = 0; $i < count($this->m_dldlIds); $i ++)
            {
                if ($dldDownload->LoadDownloadFromId($this->m_dldlIds[$i]) == true)
                {
                    if ($dldDownload->GetCategoryId() == $catId)
                        $tmpArray[] = $dldDownload->GetId();
                }
            }
            unset($this->m_dldlIds);
            $this->m_dldlIds = $tmpArray;
        }

        function CheckInternalTitle($title)
        {
            if (empty($this->m_dldlIds) == true)
                return true; // empty
            $this->LoadDownloadList();
            $dldDownload = new cDownload;
            $dldDownload->SetFolder($this->m_dldlFolder);
            foreach ($this->m_dldlIds as $value)
            {
                $dldDownload->LoadDownloadFromId($value);
                if ($dldDownload->GetInternalTitle() == $title)
                    return false; // Double entry!!
            }
            return true;
        }
        
        function SortByPosition($bAscending = true)
        {
            if (empty($this->m_dldlIds) == true)
                return; // empty
            $dldDownload = new cDownload;
            $dldDownload->SetFolder($this->m_dldlFolder);
            $preArray = array();
            foreach ($this->m_dldlIds as $value)
            {
                $dldDownload->LoadDownloadFromId($value);
                $preArray[] = sprintf("%04d", $dldDownload->GetPosition()) . '-|-' . $dldDownload->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_dldlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_dldlIds[] = $tmpArray[1];
            }
        }
        function SortByNavTitle($bAscending = true)
        {
            if (isset($this->m_dldlIds) == false)
                return;
            $dldDownload = new cDownload;
            $dldDownload->SetFolder($this->m_dldlFolder);
            $preArray = array();
            foreach ($this->m_dldlIds as $value)
            {
                $dldDownload->LoadDownloadFromId($value);
                $preArray[] = $dldDownload->GetNavTitle() . '-|-' . $dldDownload->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_dldlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_dldlIds[] = $tmpArray[1];
            }
        }
        function SortByInternalTitle($bAscending = true)
        {
            if (isset($this->m_dldlIds) == false)
                return;
            $dldDownload = new cDownload;
            $dldDownload->SetFolder($this->m_dldlFolder);
            $preArray = array();
            foreach ($this->m_dldlIds as $value)
            {
                $dldDownload->LoadDownloadFromId($value);
                $preArray[] = sprintf("%04d", $dldDownload->GetInternalTitle()) . '-|-' . $dldDownload->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_dldlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_dldlIds[] = $tmpArray[1];
            }
        }
    }
}
?>