<?php

class GuestbookEntry {
    private $mdb;
    protected $mname;
    protected $mentryid;
    protected $mcomments;
    protected $mtime;
    protected $mlat;
    protected $mlng;
    protected $mlocation;
    protected $mdontmap;
    protected $mdeleted;

    public function __construct(){
        $this->mdb = GetDatabaseLink();
        $this->mid = null;
    }

    public function setProperties($props){
        if (is_array($props)){
            isset($props['name']) && $this->mname = $props['name'];
            isset($props['entryid']) && $this->mentryid = $props['entryid'];
            isset($props['comments']) && $this->mcomments = $props['comments'];
            isset($props['time']) && $this->mtime = $props['time'];
            isset($props['lat']) && $this->mlat = $props['lat'];
            isset($props['lng']) && $this->mlng = $props['lng'];
            isset($props['location']) && $this->mlocation = $props['location'];
            isset($props['dontMap']) && $this->mdontmap = $props['dontMap'];
            isset($props['deleted']) && $this->mdeleted = $props['deleted'];
        }
        else if (is_object($props)){
            isset($props->name) && $this->mname = $props->name;
            isset($props->entryid) && $this->mentryid = $props->entryid;
            isset($props->comments) && $this->mcomments = $props->comments;
            isset($props->time) && $this->mtime = $props->time;
            isset($props->lat) && $this->mlat = $props->lat;
            isset($props->lng) && $this->mlng = $props->lng;
            isset($props->location) && $this->mlocation = $props->location;
            isset($props->dontMap) && $this->mdontmap = $props->dontMap;
            isset($props->deleted) && $this->mdeleted = $props->deleted;
        }
    }

    public function __get($nm){
        $nm = strtolower($nm);
        $prop = 'm'.$nm;
        if (isset($this->$prop)){
            return $this->$prop;
        }
        return null;
    }

    public function isPendingEntry(){
        if ($this->mentryid){
            $sql = 'SELECT entryid FROM guestbook WHERE entryid='.$this->mentryid.' AND isPending = 1';
            $res = $this->db->query($sql);
            if (DB::isError($res)){
                throw new DatabaseException('Unable to determine if entry is pending or not.', $res);
            }
            else {
                return $res->numRows() == 1;
            }
        }
        else {
            return false;
        }
    }

    public function validEntry(){
        return !empty($this->mname) && !empty($this->mcomments);
    }
}


?>