<?php
/**
 * PagerAction
 *
 * @copyright NetMonsters <team@netmonsters.ru>
 * @link
 * @package Nakon
 * @subpackage face
 * @since 04.02.2008
 * @version SVN$
 * @filesource $URL$
 */


class Pager extends Action
{
    public $template_dir = '.';

    protected $current_page = 1;
    protected $max_page_num;
    protected $num_rows;
    protected $limit;
    protected $start_offset = 0;
    protected $count = 0;

    public function __construct($count, $current_page = 1, $records_limit = 20)
    {
        parent::__construct();

        $this->count = $count;
        $this->limit = $records_limit;

        $this->max_page_num = ceil($this->count/$this->limit);
        $this->current_page = ((int)$current_page <= $this->max_page_num && (int)$current_page > 0) ? (int)$current_page : 1;

        $this->start_offset = $this->limit * ($this->current_page - 1);
        $this->num_rows = ($this->limit + $this->start_offset) <= $this->count ? ($this->limit + $this->start_offset) : $this->count;
    }

    /**
     * Возвращает текущую страницу
     *
     * @return int
     */
    public function getPage()
    {
        return $this->current_page;
    }

    public function startOffset()
    {
        return $this->start_offset;
    }

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

    public function setNumRows($num_rows)
    {
        $this->num_rows = $num_rows;
    }

    function init()
    {
        $this->template = "Pager";
    }

    function prepare()
    {
        $this->templater->assign('page', $this->current_page);
        $this->templater->assign('page_max', $this->max_page_num);
        $this->templater->assign('limit', $this->limit);
        $this->templater->assign('count', $this->count);
    }
}
?>