<?php

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

    filename: class_linklist.php
*/


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

    class cLinkList
    {
        var $m_lilId;
        var $m_lilDate;
        var $m_lilLink;
        var $m_lilDescriptions;
        var $m_lilMaxLinks;
        var $m_lilShowDescriptions;
        var $m_lilSortOrder;
        var $m_lilOpenInNewWindow;
        var $m_lilLocked;
        var $m_lilFolder;

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

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

        function Init()
        {
            $this->m_lilId = "0";
            $this->m_lilDate = "";
            unset($this->m_lilLinks);
            $this->m_lilLinks = array();
            unset($this->m_lilDescriptions);
            $this->m_lilDescriptions = array();
            $this->m_lilMaxLinks = 1;
            $this->m_lilSortOrder = 1;
            $this->m_lilShowDescriptions = 1;
            $this->m_lilOpenInNewWindow = 0;
            $this->BuildArray();
            $this->m_lilLocked = "0";

            $this->m_lilFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_lilId;
        }
        
        function GetDate()
        {
            return $this->m_lilDate;
        }
        

        function SetLink($index, $url)
        {
            if ($index >= $this->m_lilMaxLinks)
                return;
            $this->m_lilLinks[$index] = $url;
        }
        function GetLink($index)
        {
            if ($index >= $this->m_lilMaxLinks)
                return;
            return $this->m_lilLinks[$index];
        }
        
        function SetDescription($index, $description)
        {
            if ($index >= $this->m_lilMaxLinks)
                return;
            $this->m_lilDescriptions[$index] = $description;
        }
        function GetDescription($index)
        {
            if ($index >= $this->m_lilMaxLinks)
                return;
            return $this->m_lilDescriptions[$index];
        }

        function SetLinks($links)
        {
            $this->m_lilLinks = $links;
        }
        function GetLinks()
        {
            return $this->m_lilLinks;
        }
        
        function SetDescriptions($descriptions)
        {
            $this->m_lilDescriptions = $descriptions;
        }
        function GetDescriptions()
        {
            return $this->m_lilDescriptions;
        }

        function SetMaxLinks($count)
        {
            if ($count <= 0)
                $count = 1;
            unset($this->m_lilLinks);
            unset($this->m_lilDescriptions);
            $this->m_lilMaxLinks = $count;
            $this->BuildArray();
        }
        function ResizeMaxLinks($count)
        {
            if ($count <= 0)
                $count = 1;
            if ($count == $this->m_lilMaxLinks)
                return;
            if ($count > $this->m_lilMaxLinks)  // Enlarge
            {
                $this->m_lilMaxLinks = $count;
                $this->BuildArray();
            }
            else  // Truncate
            {
                $newLinks = array();
                $newDescriptions = array();
                for ($i = 0; $i < $count ; $i ++)
                {
                    $newLinks[] = $this->m_lilLinks[$i];
                    $newDescriptions[] = $this->m_lilDescriptions[$i];
                }
                $this->m_lilLinks = $newLinks;
                $this->m_lilDescriptions = $newDescriptions;
                $this->m_lilMaxLinks = $count;
            }

        }
        function BuildArray()
        {
            for ($i = 0; $i < $this->m_lilMaxLinks; $i ++)
            {
                if (isset($this->m_lilLinks[$i]) == false)
                    $this->m_lilLinks[$i] = "";
                if (isset($this->m_lilDescriptions[$i]) == false)
                    $this->m_lilDescriptions[$i] = "";
            }
        }
        function GetMaxLinks()
        {
            return $this->m_lilMaxLinks;
        }

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

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

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

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

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

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

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

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

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

        function SetFolder($folder)
        {
            $this->m_lilFolder = $folder;
        }
        
        // Main methods
        function CreateLinkList()
        {
            $this->Init();
            $this->m_lilId = uniqid("lil", FALSE);
            $this->m_lilDate = date("Y-m-d H:i:s");
        }
        function CreateLinkListById($galId)
        {
            $this->Init();
            $this->m_lilId = $galId;
            $this->m_lilDate = date("Y-m-d H:i:s");
        }

        function DeleteLinkListFinal()
        {
            return unlink($this->m_lilFolder . $this->m_lilId . ".lil");
        }

        function DeleteLinkListFinalFromId($galId)
        {
            $this->m_lilId = $galId;
            return DeleteLinkListFinalFinal();
        }


        function LoadLinkListFromId($lilId)
        {
            $this->m_lilId = $lilId;
            return $this->LoadLinkList();
        }

        function LoadLinkList()
        {
            $fileName = $this->m_lilFolder . $this->m_lilId  . ".lil";
            // echo  $fileName . '<br>';

            if ($this->m_lilId == "0" || $this->m_lilFolder == "")
                return false;


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

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

            $this->m_lilId = $cStorage->GetData("Id");
            $this->m_lilDate = $cStorage->GetData("Date");
            $this->m_lilLinks = explode("-|-", $cStorage->GetData("Links"));
            $this->m_lilDescriptions = explode("-|-", $cStorage->GetData("Descriptions"));
            $this->m_lilMaxLinks = $cStorage->GetData("MaxLinks");
            $this->m_lilSortOrder = $cStorage->GetData("SortOrder");
            $this->m_lilShowDescriptions = $cStorage->GetData("ShowDescriptions");
            $this->m_lilOpenInNewWindow = $cStorage->GetData("OpenInNewWindow");
            $this->BuildArray();  // For old galleries, or updates
            $this->m_lilLocked = $cStorage->GetData("Locked");

            return true;

        }

        function SaveLinkList()
        {
            $newRow = "\r\n";
            $fileName = $this->m_lilFolder . $this->m_lilId  . ".lil";
            // echo $fileName;

            if ($this->m_lilId == "0" || $this->m_lilFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("linklist");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_lilId);
            $cStorage->AddData("Date", $this->m_lilDate);
            $cStorage->AddData("Links", implode('-|-', $this->m_lilLinks));
            $cStorage->AddData("Descriptions", implode('-|-', $this->m_lilDescriptions));
            $cStorage->AddData("MaxLinks", $this->m_lilMaxLinks);
            $cStorage->AddData("SortOrder", $this->m_lilSortOrder);
            $cStorage->AddData("ShowDescriptions", $this->m_lilShowDescriptions);
            $cStorage->AddData("OpenInNewWindow", $this->m_lilOpenInNewWindow);
            $cStorage->AddData("Locked", $this->m_lilLocked, false);

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

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