<?php
/**
 * CategoryArticle dao class for the M:N relation between categories and articles
 *
 * @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
 */

/**
 * CategoryArticle dao class for the M:N relation between categories and articles
 * 
 * @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_CategoryArticle extends HomeWebApp_Model_Dao
{
    const TABLE_NAME = "categoryarticle";
    protected $_name = self::TABLE_NAME;
    protected $_primary = array('category_id', 'article_id');

    protected $_referenceMap    = array(
        'Category' => array(
            'columns'           => array('category_id'),
            'refTableClass'     => 'Blog_Model_Dao_Category',
            'refColumns'        => array('id')
        ),
        'Article' => array(
            'columns'           => array('article_id'),
            'refTableClass'     => 'Blog_Model_Dao_Article',
            'refColumns'        => array('id')
        )
    );

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

    /**
     * Gets count of articles for a specific category
     * @param int $categoryId the category identifier, which determines category
     * for which we want article count
     * @return int
     */
    public function getArticleCount($categoryId)
    {
        $sql = "select count(*) from $this->_name where category_id=$categoryId";
        return $this->getAdapter()->fetchOne($sql);
    }

    /**
     * Gets articles for the particular category
     * @param int $categoryId the category identifier, which determines category
     * @param int $pageNo current page number, first page has number 1
     * @param int $perPageItems number of items per page
     * @return array|Zend_Paginator Zend_Paginator is returned when pagination is
     * used, array otherwise
     */
    public function getArticlesByCategory($categoryId,$pageNo=null,$perPageItems=null)
    {
        $commentT = Blog_Model_Dao_Comment::TABLE_NAME;
        $articleT = Blog_Model_Dao_Article::TABLE_NAME;
        $authorT = Blog_Model_Dao_Author::TABLE_NAME;

        $select = $this->getAdapter()->select()
            ->from($this->_name)
            ->from($articleT)
            ->joinLeft($commentT,"$commentT.article_id = $articleT.id",
                array("commentCount"=>"count($commentT.id)"))
            ->joinLeft($authorT,"$authorT.id = $articleT.author_id",
                array("authorName"=>"username"))
            ->where("$this->_name.article_id=$articleT.id")
            ->where("$this->_name.category_id=$categoryId")
            ->group("$articleT.id")
            ->order("$articleT.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;
    }

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