<?php if($_SERVER['SCRIPT_FILENAME'] == __file__) exit; /*
FrameworkDrawCoders : 3.*
Last modification : 29-4-2011
Class version : 1.2
*/
class paginator{
/* Results */
var $total_items;
var $total_pages;
var $actual_page;
var $actual_url;
var $matrix_content;
var $matrix_paginator;
/* Configurations */
var $items_by_page = 15;
var $pages_by_screen = 10;
var $page_var = '_p';
var $custom_css = false;
var $base_url; /* Blank. Change in function. */
/* Libs */
var $sql;
/* System Vars */
var $last_error;
var $libs_need;
/* Default libs */
var $conf;
var $str;
var $path;
var $headers;
function __construct(){
$this->libs_need = array('sql'); /* Need */ }
function matrix_sql($query_sql){
/* Reset value */
$this->total_items = null;
$this->total_pages = null;
$this->actual_page = null;
$this->actual_url = null;
$this->matrix_content = null;
$this->matrix_paginator = null;
$this->last_error = null;
$this->base_url = null;
/* Count results */
$this->total_items = $this->sql->fast_select('select count(*) from '.substr($query_sql, strripos($query_sql, ' from ') + 6, strlen($query_sql) - (strripos($query_sql, ' from ') + 6)), true); $this->total_pages = ceil($this->total_items / (int
)$this->items_by_page);
/* Clean get var */
if((int)$_GET[$this->page_var] == 0)
$this->actual_page = 1; /* Default page */
elseif($_GET[$this->page_var] > (int)$this->total_pages)
$this->actual_page = (int)$this->total_pages;
else
$this->actual_page = (int)$_GET[$this->page_var];
/* Make content */
$this->matrix_content = $this->sql->fast_select($query_sql.' limit '.(((int)$this->actual_page - 1) * (int)$this->items_by_page).', '.(int)$this->items_by_page);
/* Configuration of paginator */
if((int)$this->actual_page > ($this->pages_by_screen / 2)){
$nstart_paginator = floor(((int
)$this->actual_page + 1 - $this->pages_by_screen / 2)); if($nstart_paginator < 1)
$nstart_paginator = 1;
$nlast_paginator = ($this->pages_by_screen + $nstart_paginator - 1);
}else{
$nstart_paginator = 1;
$nlast_paginator = (int)$this->pages_by_screen;
}
/* Make URL base */
$this->base_url = $this->conf->script_url .'?';
if($_GET){
foreach($_GET as $var => $val){
if($var != $this->page_var){
foreach($val as $a => $b){
}
}else{
}
}
}
$this->base_url = substr($this->base_url, 0, strlen($this->base_url) - 1); }
/* Make paginator */
for($c = $nstart_paginator; $c <= $nlast_paginator; $c++){
if($c > (int)$this->total_pages)
break;
$this->matrix_paginator[] = array( 'id' => $c,
'url' => $this->path->make_permalink($this->base_url.'&'.$this->page_var .'='.(int)$c),
);
}
$this->actual_url = $this->base_url.'&'.$this->page_var .'='.(int)$this->actual_page;
'matrix_paginator' => $this->matrix_paginator,
'matrix_content' => $this->matrix_content
);
}
function make_content_html($headers, $custom_css = false){
if(!$this->matrix_content){
$this->last_error = array('id' => 1, 'text' => 'No have content'); return false;
}
$return = '';
/* Include default css */
if((!$custom_css) and (!$this->conf->css_paginator_content_inserted)){
$return .= '<link href="'.$this->path->libs().'res_paginator/default_content.css" type="text/css" media="screen" rel="stylesheet" />';
$this->conf->css_paginator_content_inserted = true;
}
$return .= '
<table class="__cpag_a" id="__pag_a" name="__pag_a">
<thead class="__cpag_b">
<tr class="__cpag_c">
';
/* Calculate columns */
$th = 0;
foreach($this->matrix_content[0] as $var => $val){
$th++;
$return .= '
<th class="__cpag_d __cpag_th_'.(int)$th.'">'.$this->str->toHtml($headers[$var]).'</th>
';
}
$return .= '
</tr>
</thead>
<tbody class="__cpag_e">
';
foreach($this->matrix_content as $trid => $content){
$return .= '
<tr class="__cpag_f __cpag_tr_'.$trid.'" id="__cpag_id_'.$trid.'" name="__cpag_id_'.$trid.'">
';
$tdid = 0;
foreach($content as $col => $val){
$tdid++;
$return .= '
<td class="__cpag_g __cpag_td_'.(int)$tdid.'" id="__cpag_id_'.$trid.'_'.$tdid.'" name="__cpag_id_'.$trid.'_'.$tdid.'">
'.$this->str->toHtml($val).'
</td>
';
}
$return .= '
</tr>
';
}
$return .= '
</tbody>
</table>
';
return $return;
}
function make_paginator_html($custom_css = false){
if(!$this->matrix_content){
$this->last_error = array('id' => 1, 'text' => 'No have content'); return false;
}
$return = '';
/* Include default css */
if((!$custom_css) and (!$this->conf->css_paginator_inserted)){
$return .= '<link href="'.$this->path->libs().'res_paginator/default_paginator.css" type="text/css" media="screen" rel="stylesheet" />';
$this->conf->css_paginator_inserted = true;
}
$return .= '
<table class="__pag_a" id="__pag_a" name="__pag_a">
<tbody class="__pag_b">
<tr class="_pag_c">
<td class="__pag_d __pag_td_start">
<a class="__pag_e __pag_href_start" href="'.$this->path->make_permalink($this->base_url .'&'.$this->page_var .'=1').'"><div class="__pag_f __pag_div_start"><<</div></a>
</td>
';
foreach($this->matrix_paginator as $id => $pag){
$id = ($id + 1);
if($this->actual_page == $pag['id']){
$return .= '
<td class="__pag_d __pag_td_'.$id.' __pag_td_actual">
<a class="__pag_e __pag_href_'.$id.' __pag_href_actual" href="'.$pag['url'].'"><div class="__pag_f __pag_div_'.$id.' __pag_div_actual">'.$pag['id'].'</div></a>
</td>
';
}else{
$return .= '
<td class="__pag_d __pag_td_'.$id.'">
<a class="__pag_e __pag_href_'.$id.'" href="'.$pag['url'].'"><div class="__pag_f __pag_div_'.$id.'">'.$pag['id'].'</div></a>
</td>
';
}
}
$return .= '
<td class="__pag_d __pag_td_end">
<a class="__pag_e __pag_href_end" href="'.$this->path->make_permalink($this->base_url .'&'.$this->page_var .'='.$this->total_pages).'"><div class="__pag_f __pag_div_end">>></div></a>
</td>
</tr>
</tbody>
</table>
';
return $return;
}
function make_all_html($headers, $custom_css = false){
return ($paginator = $this->make_paginator_html($custom_css)).$this->make_content_html($headers, $custom_css).$paginator;
}
}