<?php

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

File:         class_videolist.php

*/

include "class_video.php";

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

    class cVideoList
    {

        var $m_vidlFolder;
        var $m_vidlIds;

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

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

        function Init()
        {
            unset($this->m_vidlIds);
            $this->m_vidlIds = array();
            $this->m_vidlFolder = "";
        }

        function SetFolder($vidlFolder)
        {
            $this->m_vidlFolder = $vidlFolder;
        }

        function SetVideoList($vidlArray)
        {
            $this->m_vidlIds = $vidlArray;
        }

        function GetVideoList()
        {
            if (isset($this->m_vidlIds) == true)
                return $this->m_vidlIds;
            else
                return array();
        }
        
        function LoadVideoList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_vidlFolder);
            $strEntry = "";
            $fileName = "";
            $vidVideo = new cVideo;
            $vidVideo->SetFolder($this->m_vidlFolder);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.vid')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    if ($filter == 0)
                        $this->m_vidlIds[] = $fileName;
                    else
                    {
                        if ($vidVideo->LoadVideoFromId($fileName) == true)
                        {
                            $addMe = false;
                            switch($filter)
                            {
                                case 1:   // unlocked
                                   if ($vidVideo->IsLocked() == false)
                                       $addMe = true;
                                break;
                                case 2:   // locked
                                   if ($vidVideo->IsLocked() == true)
                                       $addMe = true;
                                break;
                            }
                            if ($addMe == true)
                                $this->m_vidlIds[] = $fileName;
                        }
                    }
                }
            }
            if ($sort == true)
                $this->SortByPosition(true);
        }
        
        function CheckName($nameDE)
        {
            if (empty($this->m_vidlIds) == true)
                return true; // empty
            $this->LoadVideoList();
            $vidVideo = new cVideo;
            $vidVideo->SetFolder($this->m_vidlFolder);
            for ($i = 0; $i < count($this->m_vidlIds); $i ++)
            {
                $vidVideo->LoadVideoFromId($this->m_vidlIds[$i]);
                if ( $vidVideo->GetNameDE() == $nameDE)
                    return false; // Double entry!!
            }
            return true;
        }

        function SortByPosition($bAscending = true)
        {
            if (isset($this->m_vidlIds) == false)
                return false;
            $vidVideo = new cVideo;
            $vidVideo->SetFolder($this->m_vidlFolder);
            $preArray = array();
            foreach ($this->m_vidlIds as $value)
            {
                $vidVideo->LoadVideoFromId($value);
                $preArray[] = sprintf("%04d", $vidVideo->GetPosition()) . '-|-' . $vidVideo->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_vidlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_vidlIds[] = $tmpArray[1];
            }
        }
    }
}
?>