<?php

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

    filename: class_youtubegallery.php
*/


if (!defined ("CLASS_YOUTUBEGALLERY"))
{
    define ("CLASS_YOUTUBEGALLERY", "defined");
    include "class_storagefile.php";

    class cYouTubeGallery
    {
        var $m_yolId;
        var $m_yolDate;
        var $m_yolYouTubeLinks;
        var $m_yolDescriptions;
        var $m_yolMaxYVideos;
        var $m_yolShowDescriptions;
        var $m_yolShowRelated;
        var $m_yolSortOrder;
        var $m_yolLocked;
        var $m_yolFolder;

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

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

        function Init()
        {
            $this->m_yolId = "0";
            $this->m_yolDate = "";
            unset($this->m_yolYouTubeLinks);
            $this->m_yolYouTubeLinks = array();
            unset($this->m_yolDescriptions);
            $this->m_yolDescriptions = array();
            $this->m_yolMaxYVideos = 1;
            $this->m_yolSortOrder = 1;
            $this->m_yolShowDescriptions = 1;
            $this->m_yolShowRelated = 0;
            $this->BuildArray();
            $this->m_yolLocked = "0";

            $this->m_yolFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_yolId;
        }
        
        function GetDate()
        {
            return $this->m_yolDate;
        }
        
        function SetYouTubeLink($index, $url)
        {
            if ($index >= $this->m_yolMaxYVideos)
                return;
            $this->m_yolYouTubeLinks[$index] = $url;
        }
        function GetYouTubeLink($index)
        {
            if ($index >= $this->m_yolMaxYVideos)
                return;
            return $this->m_yolYouTubeLinks[$index];
        }
        
        function SetDescription($index, $description)
        {
            if ($index >= $this->m_yolMaxYVideos)
                return;
            $this->m_yolDescriptions[$index] = $description;
        }
        function GetDescription($index)
        {
            if ($index >= $this->m_yolMaxYVideos)
                return;
            return $this->m_yolDescriptions[$index];
        }

        function SetYouTubeLinks($links)
        {
            $this->m_yolYouTubeLinks = $links;
        }
        function GetYouTubeLinks()
        {
            return $this->m_yolYouTubeLinks;
        }
        
        function SetDescriptions($descriptions)
        {
            $this->m_yolDescriptions = $descriptions;
        }
        function GetDescriptions()
        {
            return $this->m_yolDescriptions;
        }

        function SetMaxYVideos($count)
        {
            if ($count <= 0)
                $count = 1;
            unset($this->m_yolYouTubeLinks);
            unset($this->m_yolDescriptions);
            $this->m_yolMaxYVideos = $count;
            $this->BuildArray();
        }
        function ResizeMaxYVideos($count)
        {
            if ($count <= 0)
                $count = 1;
            if ($count == $this->m_yolMaxYVideos)
                return;
            if ($count > $this->m_yolMaxYVideos)  // Enlarge
            {
                $this->m_yolMaxYVideos = $count;
                $this->BuildArray();
            }
            else  // Truncate
            {
                $newYVideos = array();
                $newDescriptions = array();
                for ($i = 0; $i < $count ; $i ++)
                {
                    $newYVideos[] = $this->m_yolYouTubeLinks[$i];
                    $newDescriptions[] = $this->m_yolDescriptions[$i];
                }
                $this->m_yolYouTubeLinks = $newYVideos;
                $this->m_yolDescriptions = $newDescriptions;
                $this->m_yolMaxYVideos = $count;
            }

        }
        function BuildArray()
        {
            for ($i = 0; $i < $this->m_yolMaxYVideos; $i ++)
            {
                if (isset($this->m_yolYouTubeLinks[$i]) == false)
                    $this->m_yolYouTubeLinks[$i] = "";
                if (isset($this->m_yolDescriptions[$i]) == false)
                    $this->m_yolDescriptions[$i] = "";
            }
        }
        function GetMaxYVideos()
        {
            return $this->m_yolMaxYVideos;
        }

        function SetSortOrder($sortOrder)
        {
            $this->m_yolSortOrder = $sortOrder;
        }

        function GetSortOrder()
        {
            return $this->m_yolSortOrder;
        }

        function SortForFrontend()
        {
            $newVideos = array();
            $newDescriptions = array();
            if ($this->m_yolSortOrder == -1)  // reverse order
            {
                for ($i = ($this->m_yolMaxYVideos - 1); $i >= 0; $i --)
                {
                    $newVideos[] = $this->m_yolYouTubeLinks[$i];
                    $newDescriptions[] = $this->m_yolDescriptions[$i];
                }
                $this->m_yolYouTubeLinks = $newVideos;
                $this->m_yolDescriptions = $newDescriptions;
            }
            else if ($this->m_yolSortOrder == -2 || $this->m_yolSortOrder == 2)  // sort by description
            {
                $preArray = array();
                for ($i = 0; $i < $this->m_yolMaxYVideos; $i++)
                    $preArray[] = $this->m_yolDescriptions[$i] . '-|-' . $this->m_yolYouTubeLinks[$i];
                if ($this->m_yolSortOrder == 2)  // ascending
                    sort($preArray);
                else
                    rsort($preArray);
                foreach ($preArray as $value)
                {
                    $tmpArray = explode('-|-', $value);
                    $newVideos[] = $tmpArray[1];
                    $newDescriptions[] = $tmpArray[0];
                }
                $this->m_yolYouTubeLinks = $newVideos;
                $this->m_yolDescriptions = $newDescriptions;
            }
        }

        function ShowDescriptions()
        {
            return ($this->m_yolShowDescriptions == "1");
        }

        function SetShowDescriptions($bShowDescriptions)
        {
            $this->m_yolShowDescriptions = "1";
            if ($bShowDescriptions == false)
                $this->m_yolShowDescriptions = "0";
        }

        function ShowRelated()
        {
            return ($this->m_yolShowRelated == "1");
        }

        function SetShowRelated($bShowRelated)
        {
            $this->m_yolShowRelated = "1";
            if ($bShowRelated == false)
                $this->m_yolShowRelated = "0";
        }

        function IsLocked()
        {
            return ($this->m_yolLocked == "1");
        }

        function SetLocked($bLock)
        {
            $this->m_yolLocked = "0";
            if ($bLock == true)
                $this->m_yolLocked = "1";
            return $this->SaveYouTubeGallery();
        }

        function SetFolder($folder)
        {
            $this->m_yolFolder = $folder;
        }
        
        // Main methods
        function CreateYouTubeGallery()
        {
            $this->Init();
            $this->m_yolId = uniqid("yol", FALSE);
            $this->m_yolDate = date("Y-m-d H:i:s");
        }
        function CreateYouTubeGalleryById($galId)
        {
            $this->Init();
            $this->m_yolId = $galId;
            $this->m_yolDate = date("Y-m-d H:i:s");
        }

        function DeleteYouTubeGalleryFinal()
        {
            return unlink($this->m_yolFolder . $this->m_yolId . ".yol");
        }

        function DeleteYouTubeGalleryFinalFromId($galId)
        {
            $this->m_yolId = $galId;
            return DeleteYouTubeGalleryFinal();
        }


        function LoadYouTubeGalleryFromId($galId)
        {
            $this->m_yolId = $galId;
            return $this->LoadYouTubeGallery();
        }

        function LoadYouTubeGallery()
        {
            $fileName = $this->m_yolFolder . $this->m_yolId  . ".yol";
            // echo  $fileName . '<br>';

            if ($this->m_yolId == "0" || $this->m_yolFolder == "")
                return false;


            $cStorage = new cStorageFile;
            if ($cStorage->LoadStorageFromFile($fileName) == false)
                return false;

            if ($cStorage->GetData("Id") == "")
                return false;

            $this->m_yolId = $cStorage->GetData("Id");
            $this->m_yolDate = $cStorage->GetData("Date");
            $this->m_yolYouTubeLinks = explode("-|-", $cStorage->GetData("YVideoLinks"));
            $this->m_yolDescriptions = explode("-|-", $cStorage->GetData("Descriptions"));
            $this->m_yolMaxYVideos = $cStorage->GetData("MaxYVideos");
            $this->m_yolSortOrder = $cStorage->GetData("SortOrder");
            $this->m_yolShowDescriptions = $cStorage->GetData("ShowDescriptions");
            $this->m_yolShowRelated = $cStorage->GetData("ShowRelated");
            $this->BuildArray();  // For old galleries, or updates
            $this->m_yolLocked = $cStorage->GetData("Locked");

            return true;

        }

        function SaveYouTubeGallery()
        {
            $newRow = "\r\n";
            $fileName = $this->m_yolFolder . $this->m_yolId  . ".yol";
            // echo $fileName;

            if ($this->m_yolId == "0" || $this->m_yolFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("youtubevideogallery");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_yolId);
            $cStorage->AddData("Date", $this->m_yolDate);
            $cStorage->AddData("YVideoLinks", implode('-|-', $this->m_yolYouTubeLinks));
            $cStorage->AddData("Descriptions", implode('-|-', $this->m_yolDescriptions));
            $cStorage->AddData("MaxYVideos", $this->m_yolMaxYVideos);
            $cStorage->AddData("SortOrder", $this->m_yolSortOrder);
            $cStorage->AddData("ShowDescriptions", $this->m_yolShowDescriptions);
            $cStorage->AddData("ShowRelated", $this->m_yolShowRelated);
            $cStorage->AddData("Locked", $this->m_yolLocked, false);

            return $cStorage->SaveStorageToFile($fileName);
        }

        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
    }
}