<?php

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

File:         class_guestbookentrylist.php

*/

include "class_guestbookentry.php";

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

    class cGuestbookEntryList
    {

        var $m_guestlFolder;
        var $m_guestlIds;
        var $m_guestlPageId;

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

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

        function Init()
        {
            unset($this->m_guestlIds);
            $this->m_guestlIds = array();
            $this->m_guestlFolder = "";
            $this->m_guestlPageId = "0";
        }
        
        function GetPageId()
        {
            return $this->$m_guestlPageId;
        }
        function SetPageId($pageId)
        {
            $this->m_guestlPageId = $pageId;
        }
        

        function SetFolder($guestlFolder)
        {
            $this->m_guestlFolder = $guestlFolder;
        }

        function SetGuestbookEntryList($guestlArray)
        {
            $this->m_guestlIds = $guestlArray;
        }

        function GetGuestbookEntryList()
        {
            if (isset($this->m_guestlIds) == true)
                return $this->m_guestlIds;
            else
                return array();
        }
        
        function LoadGuestbookEntryList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_guestlFolder);
            $strEntry = "";
            $fileName = "";
            $guestGuest = new cGuestbookEntry;
            $guestGuest->SetFolder($this->m_guestlFolder);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.gbe')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    if ($filter == 0)
                    {
                        if ($this->m_guestlPageId == "0") // All entries
                            $this->m_guestlIds[] = $fileName;
                        else
                        {
                            if ($guestGuest->LoadGuestbookEntryFromId($fileName) == true)
                            {
                                if ($guestGuest->GetPageId() == $this->m_guestlPageId)
                                    $this->m_guestlIds[] = $fileName;
                            }
                        }
                    }
                    else
                    {
                        if ($guestGuest->LoadGuestbookEntryFromId($fileName) == true)
                        {
                            $addMe = false;
                            switch($filter)
                            {
                                case 1:   // unlocked
                                   if ($guestGuest->IsLocked() == false)
                                       $addMe = true;
                                break;
                                case 2:   // locked
                                   if ($guestGuest->IsLocked() == true)
                                       $addMe = true;
                                break;
                            }
                            if ($addMe == true)
                            {
                                if ($this->m_guestlPageId == "0") // All entries
                                    $this->m_guestlIds[] = $fileName;
                                else
                                {
                                    if ($guestGuest->GetPageId() == $this->m_guestlPageId)
                                        $this->m_guestlIds[] = $fileName;

                                }
                            }
                        }
                    }
                }
            }
            if ($sort == true)
                $this->SortByEnterDate(true);
        }
        
        function DeleteAllEntriesForPage($pageId)
        {
            $this->SetPageId($pageId);
            $this->LoadGuestbookEntryList();
            $guestGuest = new cGuestbookEntry;
            $guestGuest->SetFolder($this->m_guestlFolder);
            foreach ($this->m_guestlIds as $value)
            {
                $guestGuest->LoadGuestbookEntryFromId($value);
                $guestGuest->DeleteGuestbookEntryFinal();
            }
        }
        
        function SortByEnterDate($bAscending = true)
        {
            if (isset($this->m_guestlIds) == false)
                return false;
            $guestGuest = new cGuestbookEntry;
            $guestGuest->SetFolder($this->m_guestlFolder);
            $preArray = array();
            foreach ($this->m_guestlIds as $value)
            {
                $guestGuest->LoadGuestbookEntryFromId($value);
                $preArray[] = sprintf("%s", $guestGuest->GetEnterDate()) . '-|-' . $guestGuest->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_guestlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_guestlIds[] = $tmpArray[1];
            }
        }

        function SortByPageId($bAscending = true)
        {
            if (isset($this->m_guestlIds) == false)
                return false;
            $guestGuest = new cGuestbookEntry;
            $guestGuest->SetFolder($this->m_guestlFolder);
            $preArray = array();
            foreach ($this->m_guestlIds as $value)
            {
                $guestGuest->LoadGuestbookEntryFromId($value);
                $preArray[] = sprintf("%s", $guestGuest->GetPageId()) . '-|-' . $guestGuest->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_guestlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_guestlIds[] = $tmpArray[1];
            }
        }

    }
}
?>