<?php

class Guestbook {
    public $db;

    public function __construct(){
        $this->db = GetDatabaseLink();
    }

    public function getEntryCount($includeDeleted = false){
        $sql = "SELECT COUNT(*) FROM guestbook WHERE isPending = 0 ";
        if (!$includeDeleted){
            $sql .= ' AND deletedEntry=0';
        }
    
        $e = $this->db->getOne($sql);
        if (DB::isError($e)){
            throw new DatabaseException('Unable to pull count of guestbook entries.', $e);
        }
        else {
            return $e;
        }
    }

    public function deleteEntriesById($ids){
        if (is_array($ids)){
            $ids = implode(', ', $ids);
        }

        $sql = 'UPDATE guestbook SET deletedEntry = 1 WHERE entryId IN ('.$ids.')';

        LogError('DEBUG: Delete from guestbook sql: '.$sql, __FILE__, __LINE__);

        $res = $this->db->query($sql);
        if (DB::isError($res)){
            throw new DatabaseException('Unable to delete selected entries.', $res);
        }
        else {
            return true;
        }
    }

    public function getEntries($start=0, $numToGet=null, $includeDeleted=false){
        $sql  = "SELECT entryid, name, lat, lng, location, comments, time, dontMap, deletedEntry ";
        $sql .= "FROM guestbook WHERE isPending = 0 ";

        if (!$includeDeleted){
            $sql .= ' AND deletedEntry = 0';
        }

        $sql .= ' ORDER BY time DESC';

        if (!is_null($numToGet)){
            $res = $this->db->limitQuery($sql, $start, $numToGet);
        }
        else {
            $res = $this->db->query($sql);
        }

        if (DB::isError($res)){
            throw new DatabaseException('Unable to pull guestbook entries.', $res);
        }

        $entries = array();
        while ($row = $res->fetchRow()){
            $entry = new GuestbookEntry();
            $entry->setProperties($row);
            $entries[] = $entry;
        }
        return $entries;
    }

    public function addPendingEntry($entry){
        $map = ($entry->DontMap=='1')?1:0;

        $sql = 'INSERT INTO guestbook (name, lat, lng, location, comments, time, dontMap, deletedEntry, isPending) ';
        $sql .= 'VALUES ('.$this->db->quoteSmart($entry->Name).', '.$this->db->quoteSmart($entry->Lat).', '.$this->db->quoteSmart($entry->Lng);
        $sql .= ', '.$this->db->quoteSmart($entry->Location).', '.$this->db->quoteSmart($entry->Comments).', CURRENT_TIMESTAMP';
        $sql .= ', '.$this->db->quoteSmart($map).', 0, 1)';

        $res = $this->db->query($sql);
        if (DB::isError($res)){
            throw new DatabaseException('Unable to add pending entry to guestbook.', $res);
        }
        else {
            $res = $this->db->getOne('SELECT LAST_INSERT_ID()');
            if (DB::isError($res)){
                throw new DatabaseException('Unable to add pending entry to guestbook.  Could not fetch new ID', $res);
            }
            else {
                return $res;
            }
        }
    }

    public function restorePendingEntry($id){
        $sql = 'SELECT entryid, name, lat, lng, location, comments, time, dontMap FROM guestbook ';
        $sql .= 'WHERE isPending=1 AND entryid = '.intval($id);

        $res = $this->db->query($sql);
        if (DB::isError($res)){
            throw new DatabaseException('Unable to pull pending entry information.', $res);
        }
        else if ($res->numRows() == 0){
            throw new Exception('Pending entry not found.', 0);
        }
        else {
            $row=$res->fetchRow();
            $entry = new GuestbookEntry();
            $entry->setProperties($row);
            return $entry;
        }
    }

    public function removePendingEntry($id){
        $res = $this->db->query('DELETE FROM guestbook WHERE entryid = '.intval($id).' AND isPending=1');
        if (DB::isError($res)){
            throw new DatabaseException('Unable to delete pending entry.');
        }
        else {
            return true;
        }
    }

    public function add($entry){
        if (!$entry->isPendingEntry()){
            $id = $this->addPendingEntry($entry);
            $entry->setProperties(array('entryid'=>$id));
        }
        $sql = 'UPDATE guestbook SET isPending = 0, deletedEntry = 0 WHERE entryid = '.$entry->EntryID;
        $res = $this->db->query($sql);
        if (DB::isError($res)){
            throw new DatabaseException('Unable to add entry to guestbook.', $res);
        }
        else {
            return true;
        }
    }
}

?>