<?

DEFINE("_PAGER_AREA", 3);

class pager extends component {


    function makeData() {
        $this->page = empty($_REQUEST[$this->param]) ? 1 : $_REQUEST[$this->param];

        list($this->object, $this->source) = explode(".", $this->source);

        $this->data_object = func_new($this->object);

        $result =& call_user_func_array(array(&$this->data_object, "get".$this->source), ($this->page-1)*_ITEMS_PER_PAGE);

        $this->items_count = $result["count"];

        $this->data =& $result["data"];

        if($this->items_count <= _ITEMS_PER_PAGE) {
            $this->show_pager = false;
            return;
        }

        $this->show_pager = true;

        $params = array();
        foreach($this->dialog->params as $p) {
            if((strpos($this->exclude, $p) === false) && 
               ($p != $this->param) && 
               ($this->dialog->$p != "")) {
                $params[] = $p."=".$this->dialog->$p;
            }
        }
        $this->params_str = implode("&", $params);
  
        $last = ceil($this->items_count / _ITEMS_PER_PAGE);

        $left = $this->page - _PAGER_AREA;
        $right = $this->page + _PAGER_AREA;

        $lofs = ($right > $last) ? ($right - $last) : 0;
        $rofs = ($left < 1) ? (1 - $left) : 0;

        $left-=$lofs;
        $right+=$rofs;

        if($left <= 1) {
            $left = 1;
        } else {
            $this->first_page = 1;
        }

        if($right >= $last) {
            $right = $last;
        } else {
            $this->last_page = $last;
        }

        $this->pages = range($left, $right);
    }



    function getTemplate() {
        return "components/pager.tpl";
    }



}
?>