<?php
/**
 * TrackBack Dao
 *
 * @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 Dao class based on the Zend_Db_Table
 *
 * @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 Blog_Model_Dao_TrackBack extends HomeWebApp_Model_Dao
{
    /** table name */
    const TABLE_NAME = "trackback";

    /** table name
     * @var string
     */
    protected $_name = self::TABLE_NAME;

    /** primary key column
     * @var string
     */
    protected $_primary = "id";

    /** reference map for the relationship
     * @var array
     */
    protected $_referenceMap = array(
        'Article' => array(
            'columns' => 'article_id',
            'refTableClass' => 'Dao_Article',
            'refColumns'    => 'id'
        )
    );

    /**
     * Paginator if pagination is used
     * @var Zend_Paginator
     */
    private $_paginator;

	/**
     * Get all trackbacks, with some extended information like article name etc
     * Has the pagination support.
     *
     * @param int $pageNo current page number, first page has number 1
     * @param int $perPageItems number of items per page
     * @param array $extendInfo what extend information we want, supported keys 
     * are
     *  - article : for the article information
     * @return array|Zend_Paginator the sequential array of rows with article properties
     * is returned if no pagination is used, Zend_Paginator otherwise
     */
    public function fetchAllItems($pageNo=null,$perPageItems=null,
        $extendInfo=null)
    {
        $articleT = Blog_Model_Dao_Article::TABLE_NAME;

        $select=$this->getAdapter()->select()
            ->from($this->_name)
            ->order("$this->_name.posted desc");

        if (is_array($extendInfo)) {
            $select->group("$this->_name.id");
            if (in_array("article",$extendInfo)) {
                $select->joinLeft($articleT,
                    "$articleT.article_id = {$this->_name}.id",
                    array("commentCount"=>"count($articleT.id)")
                );
            }
        }

        if (!($pageNo && $perPageItems)) {
            return $select->query()->fetchAll();
        }

        $this->_paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $this->_paginator->setCurrentPageNumber($pageNo);
        $this->_paginator->setItemCountPerPage($perPageItems);

        return $this->_paginator;
	}

    /**
     * Gets paginator. Paginator is initialized if the pagination is used,
     * see {@link Blog_Model_Dao_TrackBack::getAll()}
     * @return Zend_Paginator
     */
    public function getPaginator()
    {
        return $this->_paginator;
    }

    public function getCountByArticle($articleId)
    {
        $sql="SELECT count(*) "
            . "FROM $this->_name "
            . "WHERE article_id=$articleId "
            . "AND isSpam=0";

        return $this->getAdapter()->fetchOne($sql);
    }

    public function getByArticleId($articleId)
    {
        $sql="SELECT * "
            . "FROM $this->_name "
            . "WHERE article_id=$articleId "
            . "AND isSpam=0";

        return $this->getAdapter()->fetchAll($sql);
    }

    /**
     * Checks if the trackback entry in the database table already exist.
     * @return bool
     */
    public function entryExist($articleId,$url)
    {
        $sql="SELECT count(*) "
            . "FROM $this->_name "
            . "WHERE article_id=$articleId "
            . "AND url='$url'";

        return $this->getAdapter()->fetchOne($sql);
    }
}
