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

/**
 * Category 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_Category extends Dao
{
    const TABLE_NAME = "category";
    protected $_name = self::TABLE_NAME;
    protected $_primary = "id";
    
    /**
     * Get all categories, with some extended information like informtions about
     * count of articles 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 the count of articles in the category
     *
     * @return array the sequential array of rows with category properties
     */
    public function fetchAllItems($pageNo=null,$perPageItems=null,
        $extendInfo=null)
    {
        $categoryArticleRelT=Dao_CategoryArticle::TABLE_NAME;
        $authorT=Dao_Author::TABLE_NAME;

        $select=$this->getAdapter()->select()->from($this->_name);
        
        if (is_array($extendInfo)) {
            $select->group("$this->_name.id");
            if (in_array("article",$extendInfo)) {
                $select->joinLeft("$categoryArticleRelT",
                    "$categoryArticleRelT.category_id = $this->_name.id",
                    array("articleCount"=>"count($categoryArticleRelT.article_id)")
                );
            }
        }

        return $select->query()->fetchAll();
    }

    /**
     * Finds a category by the title url.
     * @param string $titleUrl
     * @return Zend_Db_Table_Row_Abstract or null
     * @throws new Zend_Db_Exception when some database operation fails
     */
    public function findByTitleUrl($titleUrl)
    {
        $whereSQL=$this->getAdapter()->quoteInto('titleURL = ?',$titleUrl);
        return $this->fetchRow($whereSQL);
    }
}
