<?php
/**
 * the model for the trackback
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Trackback model class
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class TrackBackEntry extends Model
{
    /**
     * database id for an trackback item
     * @var int
     */
    private $_id;

    /**
     * id for an article, for which a trackback is
     * @var int
     */
    private $_articleId;

    /**
     * title of the trackback
     * @var string
     */
    private $_title;

	/**
     * trackbarck excerpt
     * @var string
     */
    private $_excerpt;

	/**
     * url from which the trackback was send
     * @var string
     */
    private $_url;

    /**
     * name of blog/cms from which the trackback was send
     * @var string
     */
    private $_blogName;

    /**
     * datetime when the trackback was posted/received
     * (I thing both time are same indeed)
     * @var string
     */
    private $_posted;

    /**
     * is trackback spam or not?
     * @var bool
     */
    private $_isSpam;

    /**
     * source ip address of trackback
     * @var string
     */
    private $_ipAddr;

    protected function _setDefaultDao()
    {
        $this->setDao(new Dao_TrackBack());
    }

    /**
     * getter for the @link{TrackBackEntry::$_id}
     * @return int
     */
    public function getId()
    {
        return $this->_id;
    }

    /**
     * setter for the @link{TrackBackEntry::$_id}
     * @return int
     */
    public function setId($id)
    {
        $this->_id = $id;
    }

    /**
     * Gets all trackback entries
     *
     * @param int $pageNo
     * @param int $perPageItems
     * @param array $extendedInfo
     * @return unknown
     */
    public function getAll($pageNo=null,$perPageItems=null,$extendedInfo=null)
    {
       $rows = $this->getDao()->fetchAllItems($pageNo,$perPageItems,$extendedInfo);

       return $this->_getSet($rows);
    }

    /**
     * Return paginator from {@link Model::$dao}
     * @return Zend_Paginator
     */
    public function getPaginator()
    {
        return $this->getDao()->getPaginator();
    }

    /**
     * Gets the id of the article, to which the trackback belongs
     * @return int
     */
    public function getArticleId()
    {
        return $this->_articleId;
    }

    public function setArticleId($id)
    {
        $this->_articleId=$id;
    }

    /**
     * gets the article, to which the trackback belongs
     * @return Article
     */
    public function getArticle()
    {
        $article = new Article();
        $article->findById($this->getArticleId());
        return $article;
    }

    public function getTitle()
    {
        return $this->_title;
    }

    public function setTitle($title)
    {
        $this->_title=$title;
    }

    public function getUrl()
    {
        return $this->_url;
    }

    public function setUrl($url)
    {
        $this->_url=$url;
    }

    public function getExcerpt()
    {
        return $this->_excerpt;
    }

    public function setExcerpt($excerpt)
    {
        $this->_excerpt=$excerpt;
    }

    public function getBlogName()
    {
        return $this->_blogName;
    }

    public function setBlogName($blogName)
    {
        $this->_blogName=$blogName;
    }

    public function getPosted()
    {
        return $this->_posted;
    }

    public function setPosted($posted)
    {
        $this->_posted=$posted;
    }

    public function setSpam($status=true)
    {
        $this->_isSpam=(bool)$status;
    }

    public function getIpAddr()
    {
        return $this->_ipAddr;
    }

    public function setIpAddr($ipAddr)
    {
        $this->_ipAddr=$ipAddr;
    }

    /**
     * Returns trackbacks for the particular article.
     * @param Article $article
     * @return array array of the TrackBackEntry objects
     * @todo use findDependentRowset with a new select functinality in the ZF 1.5
     */
    public function getByArticle(Article $article)
    {
        return $this->_getSet($this->getDao()->getByArticleId($article->getId()));
    }

    public function getCountByArticle(Article $article)
    {
        return $this->getDao()->getCountByArticle($article->getId());
    }

    public function findById($id)
    {
        $row = $this->getDao()->find($id)->current();
        $this->_setFromDaoRow($row->toArray());
    }

    public function insert()
    {
        $this->getDao()->insert(
            array(
                "article_id"=>$this->_articleId,
                "title"=> (empty($this->_title)? NULL : $this->_title),
                "excerpt"=> (empty($this->_excerpt)? NULL : $this->_excerpt),
                "url"=> $this->_url,
                "blogName"=> (empty($this->_blogName)? NULL : $this->_blogName),
                "posted"=> new Zend_Db_Expr('NOW()'),
                "isSpam"=> $this->_isSpam ? 1 : 0,
                "ipAddr"=> $this->_ipAddr)
            );
    }

    /**
     * updates the trackback information in the database
     * @throws Zend_Db_Exception if the database update fails
     */
    public function update()
    {
        $this->getDao()->update(
            array(
                "title"=>$this->_title,
                "excerpt"=>$this->_excerpt,
                "url"=>$this->_url,
                "blogName"=> $this->_blogName,
                "posted"=>$this->_posted,
                "isSpam"=> $this->_isSpam ? 1 : 0,
                "ipAddr"=>$this->_ipAddr 
            ),"id=$this->_id"
        );
    }

    /** delete the trackback entry from the database
     */
    public function delete()
    {
        $this->getDao()->delete("id=$this->_id");
    }

    /**
     * Gets database row object which corresponds our current properties.
     * It is used mostly for the operation with the relationship. Hence we need
     * to set only keys
     * @param bool $keyOnly true - sets only key properties (primary and foreign) it is
     * enough in most situations, false - sets all properties
     * @return Zend_Db_Table_Row_Abstract
     */
    public function getDaoRow($keyOnly=true)
    {
        $data = array(
            "id"=>$this->_id,
            "article_id"=>$this->_articleId
        );

        if (!$keyOnly) {
            $data["title"]=$this->_title;
            $data["excerpt"]=$this->_excerpt;
            $data["url"]=$this->_url;
            $data["blogName"]=$this->_blogName;
            $data["posted"]=$this->_posted;
            $data["isSpam"]=$this->_isSpam;
            $data["ipAddr"]=$this->_ipAddr;
        }

        $row=$this->getDao()->createRow($data);
        return $row;
    } 

    protected function _setPropFromArray(&$arr)
    {
        foreach($arr as $key=>$val) {
            $this->{"_".$key}=$val;
        }
    }

    public function isExist()
    {
        return $this->getDao()->entryExist($this->_articleId,$this->_url);
    }

    protected function _inflectDaoRowIndexes(&$arr)
    {
        $notAllowedIndexes=array("dao");

        $keys=array_keys($arr);
        foreach($keys as $key) {
            //unset not allowed indexes
            if (in_array($key,$notAllowedIndexes)) {
                throw new Exception("you cannot use $key index as the column index");
            }

            switch ($key) {
                case "article_id":
                Utils::renameKey($arr,$key,"articleId");
                break;
                default:
                break;
            }
        }
    }

}
