<?php

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

    filename: class_gallery.php
*/


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

    define ("GALLERY_PREVIEW_TYPE_IMAGE", "0");
    define ("GALLERY_PREVIEW_TYPE_NEWWINDOW", "1");
    define ("GALLERY_PREVIEW_TYPE_SAMEWINDOW", "2");
    define ("GALLERY_PREVIEW_TYPE_SAMEWINDOW2", "3");
    define ("GALLERY_PREVIEW_TYPE_SAMEWINDOW3", "4");

    class cGallery
    {
        var $m_galId;
        var $m_galDate;
        var $m_galThumbs;
        var $m_galImages;
        var $m_galDescriptions;
        var $m_galLinks;
        var $m_galMaxImages;
        var $m_galSortOrder;
        var $m_galShowDescriptions;
        var $m_galEnablePreview;
        var $m_galPreviewType;
        var $m_galOpenInNewWindow;
        var $m_galLocked;
        var $m_galFolder;

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

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

        function Init()
        {
            $this->m_galId = "0";
            $this->m_galDate = "";
            unset($this->m_galImages);
            $this->m_galImages = array();
            unset($this->m_galThumbs);
            $this->m_galThumbs = array();
            unset($this->m_galDescriptions);
            $this->m_galDescriptions = array();
            unset($this->m_galLinks);
            $this->m_galLinks = array();
            $this->m_galMaxImages = 1;
            $this->m_galSortOrder = 1;
            $this->m_galShowDescriptions = 1;
            $this->m_galEnablePreview = 1;
            $this->m_galPreviewType = GALLERY_PREVIEW_TYPE_NEWWINDOW;
            $this->m_galOpenInNewWindow = 0;
            $this->BuildArray();
            $this->m_galLocked = "0";

            $this->m_galFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_galId;
        }
        
        function GetDate()
        {
            return $this->m_galDate;
        }
        
        function DeleteImage($index)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            if (file_exists($this->m_galImages[$index]) == true)
            {
                unlink($this->m_galImages[$index]);
                $this->m_galImages[$index] = "";
            }
            if (file_exists($this->m_galThumbs[$index]) == true)
            {
                unlink($this->m_galThumbs[$index]);
                $this->m_galThumbs[$index] = "";
            }
            $this->m_galDescriptions[$index] = "";
            $this->m_galLinks[$index] = "";
        }
        
        function SetImage($index, $url)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            $this->m_galImages[$index] = $url;
        }
        function GetImage($index)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            return $this->m_galImages[$index];
        }
        
        function SetThumb($index, $url)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            $this->m_galThumbs[$index] = $url;
        }
        function GetThumb($index)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            return $this->m_galThumbs[$index];
        }

        function SetDescription($index, $description)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            $this->m_galDescriptions[$index] = $description;
        }
        function GetDescription($index)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            return $this->m_galDescriptions[$index];
        }

        function SetLink($index, $link)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            $this->m_galLinks[$index] = $link;
        }
        function GetLink($index)
        {
            if ($index >= $this->m_galMaxImages)
                return;
            return $this->m_galLinks[$index];
        }

        function SetImages($images)
        {
            $this->m_galImages = $images;
        }
        function GetImages()
        {
            return $this->m_galImages;
        }
        
        function SetThumbs($thumbs)
        {
            $this->m_galThumbs = $thumbs;
        }
        function GetThumbs()
        {
            return $this->m_galThumbs;
        }

        function SetDescriptions($descriptions)
        {
            $this->m_galDescriptions = $descriptions;
        }
        function GetDescriptions()
        {
            return $this->m_galDescriptions;
        }

        function SetLinks($links)
        {
            $this->m_galLinks = $links;
        }
        function GetLinks()
        {
            return $this->m_galLinks;
        }

        function SetMaxImages($count)
        {
            if ($count <= 0)
                $count = 1;
            unset($this->m_galImages);
            unset($this->m_galThumbs);
            unset($this->m_galDescriptions);
            unset($this->m_galLinks);
            $this->m_galMaxImages = $count;
            $this->BuildArray();
        }
        function ResizeMaxImages($count)
        {
            if ($count <= 0)
                $count = 1;
            if ($count == $this->m_galMaxImages)
                return;
            if ($count > $this->m_galMaxImages)  // Enlarge
            {
                $this->m_galMaxImages = $count;
                $this->BuildArray();
            }
            else  // Truncate
            {
                $newImages = array();
                $newThumbs = array();
                $newDescriptions = array();
                $newLinks = array();
                for ($i = 0; $i < $count ; $i ++)
                {
                    $newImages[] = $this->m_galImages[$i];
                    $newThumbs[] = $this->m_galThumbs[$i];
                    $newDescriptions[] = $this->m_galDescriptions[$i];
                    $newLinks[] = $this->m_galLinks[$i];
                }
                // Delete images
                for ($i = $count; $i < $this->m_galMaxImages; $i ++)
                {
                    if (file_exists($this->m_galImages[$i]) == true)
                        unlink($this->m_galImages[$i]);
                    if (file_exists($this->m_galThumbs[$i]) == true)
                        unlink($this->m_galThumbs[$i]);
                }
                $this->m_galImages = $newImages;
                $this->m_galThumbs = $newThumbs;
                $this->m_galDescriptions = $newDescriptions;
                $this->m_galLinks = $newLinks;
                $this->m_galMaxImages = $count;
            }

        }
        function BuildArray()
        {
            for ($i = 0; $i < $this->m_galMaxImages; $i ++)
            {
                if (isset($this->m_galImages[$i]) == false)
                    $this->m_galImages[$i] = "";
                if (isset($this->m_galThumbs[$i]) == false)
                    $this->m_galThumbs[$i] = "";
                if (isset($this->m_galDescriptions[$i]) == false)
                    $this->m_galDescriptions[$i] = "";
                if (isset($this->m_galLinks[$i]) == false)
                    $this->m_galLinks[$i] = "";
            }
        }
        function GetMaxImages()
        {
            return $this->m_galMaxImages;
        }

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

        function GetSortOrder()
        {
            return $this->m_galSortOrder;
        }
        
        function SortForFrontend()
        {
            $newImages = array();
            $newThumbs = array();
            $newDescriptions = array();
            $newLinks = array();
            if ($this->m_galSortOrder == -1)  // reverse order
            {
                for ($i = ($this->m_galMaxImages - 1); $i >= 0; $i --)
                {
                    $newImages[] = $this->m_galImages[$i];
                    $newThumbs[] = $this->m_galThumbs[$i];
                    $newDescriptions[] = $this->m_galDescriptions[$i];
                    $newLinks[] = $this->m_galLinks[$i];
                }
                $this->m_galImages = $newImages;
                $this->m_galThumbs = $newThumbs;
                $this->m_galDescriptions = $newDescriptions;
                $this->m_galLinks = $newLinks;
            }
            else if ($this->m_galSortOrder == -2 || $this->m_galSortOrder == 2)  // sort by description
            {
                $preArray = array();
                for ($i = 0; $i < $this->m_galMaxImages; $i++)
                    $preArray[] = $this->m_galDescriptions[$i] . '-|-' . $this->m_galImages[$i] . '-|-' . $this->m_galThumbs[$i] . '-|-' . $this->m_galLinks[$i];
                if ($this->m_galSortOrder == 2)  // ascending
                    sort($preArray);
                else
                    rsort($preArray);
                foreach ($preArray as $value)
                {
                    $tmpArray = explode('-|-', $value);
                    $newDescriptions[] = $tmpArray[0];
                    $newImages[] = $tmpArray[1];
                    $newThumbs[] = $tmpArray[2];
                    $newLinks[] = $tmpArray[3];
                }
                $this->m_galDescriptions = $newDescriptions;
                $this->m_galImages = $newImages;
                $this->m_galThumbs = $newThumbs;
                $this->m_galLinks = $newLinks;
            }
        }
        
        function ShowDescriptions()
        {
            return ($this->m_galShowDescriptions == "1");
        }
        
        function SetShowDescriptions($bShowDescriptions)
        {
            $this->m_galShowDescriptions = "1";
            if ($bShowDescriptions == false)
                $this->m_galShowDescriptions = "0";
        }

        function EnablePreview()
        {
            return ($this->m_galEnablePreview == "1");
        }

        function SetEnablePreview($bEnablePreview)
        {
            $this->m_galEnablePreview = "1";
            if ($bEnablePreview == false)
                $this->m_galEnablePreview = "0";
        }

        function GetPreviewType()
        {
            if ($this->m_galPreviewType == "")
                return GALLERY_PREVIEW_TYPE_NEWWINDOW;
            return $this->m_galPreviewType;
        }
        function SetPreviewType($previewType)
        {
            $this->m_galPreviewType = $previewType;
        }

        function OpenInNewWindow()
        {
            return ($this->m_galOpenInNewWindow == "1");
        }

        function SetOpenInNewWindow($bOpenInNewWindow)
        {
            $this->m_galOpenInNewWindow = "1";
            if ($bOpenInNewWindow == false)
                $this->m_galOpenInNewWindow = "0";
        }

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

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

        function SetFolder($folder)
        {
            $this->m_galFolder = $folder;
        }
        
        // Main methods
        function CreateGallery()
        {
            $this->Init();
            $this->m_galId = uniqid("gal", FALSE);
            $this->m_galDate = date("Y-m-d H:i:s");
        }
        function CreateGalleryById($galId)
        {
            $this->Init();
            $this->m_galId = $galId;
            $this->m_galDate = date("Y-m-d H:i:s");
        }

        function DeleteGalleryFinal()
        {
            // Delete images
            for ($i = 0; $i < count($this->m_galImages); $i ++)
            {
                if (file_exists($this->m_galImages[$i]) == true)
                    unlink ($this->m_galImages[$i]);
                if (file_exists($this->m_galThumbs[$i]) == true)
                    unlink ($this->m_galThumbs[$i]);
            }
            return unlink($this->m_galFolder . $this->m_galId . ".gal");
        }

        function DeleteGalleryFinalFromId($galId)
        {
            $this->m_galId = $galId;
            return DeleteGalleryFinal();
        }


        function LoadGalleryFromId($galId)
        {
            $this->m_galId = $galId;
            return $this->LoadGallery();
        }

        function LoadGallery()
        {
            $fileName = $this->m_galFolder . $this->m_galId  . ".gal";
            // echo  $fileName . '<br>';

            if ($this->m_galId == "0" || $this->m_galFolder == "")
                return false;


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

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

            $this->m_galId = $cStorage->GetData("Id");
            $this->m_galDate = $cStorage->GetData("Date");
            $this->m_galImages = explode("-|-", $cStorage->GetData("Images"));
            $this->m_galThumbs = explode("-|-", $cStorage->GetData("Thumbs"));
            $this->m_galDescriptions = explode("-|-", $cStorage->GetData("Descriptions"));
            $this->m_galLinks = explode("-|-", $cStorage->GetData("Links"));
            $this->m_galMaxImages = $cStorage->GetData("MaxImages");
            $this->m_galSortOrder = $cStorage->GetData("SortOrder");
            $this->m_galShowDescriptions = $cStorage->GetData("ShowDescriptions");
            $this->m_galEnablePreview = $cStorage->GetData("EnablePreview");
            $this->m_galPreviewType = $cStorage->GetData("PreviewType");
            $this->m_galOpenInNewWindow = $cStorage->GetData("OpenInNewWindow");
            $this->BuildArray();  // For old galleries, or updates
            $this->m_galLocked = $cStorage->GetData("Locked");

            return true;

        }

        function SaveGallery()
        {
            $newRow = "\r\n";
            $fileName = $this->m_galFolder . $this->m_galId  . ".gal";
            // echo $fileName;

            if ($this->m_galId == "0" || $this->m_galFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("gallery");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_galId);
            $cStorage->AddData("Date", $this->m_galDate);
            $cStorage->AddData("Images", implode('-|-', $this->m_galImages));
            $cStorage->AddData("Thumbs", implode('-|-', $this->m_galThumbs));
            $cStorage->AddData("Descriptions", implode('-|-', $this->m_galDescriptions));
            $cStorage->AddData("Links", implode('-|-', $this->m_galLinks));
            $cStorage->AddData("MaxImages", $this->m_galMaxImages);
            $cStorage->AddData("SortOrder", $this->m_galSortOrder);
            $cStorage->AddData("ShowDescriptions", $this->m_galShowDescriptions);
            $cStorage->AddData("EnablePreview", $this->m_galEnablePreview);
            $cStorage->AddData("PreviewType", $this->m_galPreviewType);
            $cStorage->AddData("OpenInNewWindow", $this->m_galOpenInNewWindow);
            $cStorage->AddData("Locked", $this->m_galLocked, false);

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

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