<?php
/**
 * Comment 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
 */

/**
 * Comment 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 Dao_Comment extends Dao
{
    /** table name */
    const TABLE_NAME = "comment";

    /** 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 paginatio is used
     * @var Zend_Paginator
     */
    private $_paginator;

    /**
     * Get all comment.
     * Has the pagination support.
     *
     * @param int $pageNo current page number, first page has number 1
     * @param int $perPageItems number of items per page
     * @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)
    {
        $select=$this->getAdapter()->select()
            ->from($this->_name)
            ->order("posted desc");

        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 Dao_Comment::fetchAllItems()}
     * @return Zend_Paginator
     */
    public function getPaginator()
    {
        return $this->_paginator;
    }

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

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

    /**
     * Returns the comment order for an article. It is used only for article
     * comments, which are sorted in ascending order.
     *
     * @param int $commentId id for the comment
     * @param inr $articleId int for the comment article
     * @return int comment order
     * @throws new Exception if $commentId for the article with $articleId doesn't
     * exist
     */
    public function getOrderforArticle($commentId,$articleId)
    {
        $sql="SELECT id "
            . "FROM $this->_name "
            . "WHERE article_id=$articleId "
            . "ORDER BY posted ASC";

        $rows=$this->getAdapter()->fetchAll($sql);

        $rowOrder=0;
        foreach($rows as $row) {
            $rowOrder++;
            if ($row["id"]==$commentId) return $rowOrder;
        }

        throw new Exception("comment id $commentId for the article with id
            $articleId doesn't exist");
    }

}
