<?php
//include '../../config.inc.php';
//include ABS_PATH.'core/init.inc.php';
//require_once('table_builder.php');
class admin_table{
public $table;
public $items;
private $img_path;
private $cols=array();
// private $first_r=array();
private $table_name;
private $key_col;
private $items_per_page;
private $form=false;
private $order_default;
private $order;
private $page;
private $link;
function __construct($tags=''){
$this->table=new table_builder($tags);
$this->img_path=ABS_PATH.'images/admin/';
}
/*prepare*/
function set_link($link){
$this->link=$link;
}
function set_table_name($table){
$this->table_name=$table;
}
function set_key_col($key_col){
$this->key_col=$key_col;
}
function set_items_per_page($items){
$this->items_per_page=$items;
}
function set_order_default($field){
$this->order_default=$field;
}
function add_col($name,$arr){
$this->cols[$name]=$arr;
}
function use_form($method='post'){
$this->form=new form_builder('',$method);
}
/*process*/
function process(){
if(!$page=get_var('page',$_GET,'i'))
$page=1;
$order=get_var('order',$_GET);
if($order){
//проверим что ордер это один из стобцов по которому мы можем сортировать
$tmp=isset($this->cols[$order]) &&
isset($this->cols[$order]['in_table']) &&
$this->cols[$order]['in_table'] &&
isset($this->cols[$order]['is_ordered']) &&
$this->cols[$order]['is_ordered'];
if(!$tmp) $order=false;
}
if(!$order)
$order=$this->order_default;
$desc=get_var('desc',$_GET,'bi');
//общее число элементов в таблице
$num=DB::Fast_Query("SELECT COUNT(*) FROM `".$this->table_name."`");
$this->max_page=$max_page=ceil($num/$this->items_per_page);
if(!$page) $page=1;
if($page>$max_page) $page=$max_page;
if($max_page>1){
$page_line='<div class=pages>'.make_pages($page,$max_page," [%s] "," <a href='?module=board&action=view_adverts&page=%1\$s'>%1\$s</a> ",2).'</div>';
}
$this->order=$order;
$this->page=$page;
//задаем ширину столбцов
$widths=array_one_dimension($this->cols,'name','width');
call_user_func_array(array(&$this->table, "set_widths"),$widths);
//установка параметров для столбцов
foreach($this->cols as $coldata){
$additions[] = get_var('addition',$coldata);
$footers[] = get_var('footer',$coldata);
}
call_user_func_array(array(&$this->table, "set_addition"),$additions);
//получаем и добавляем данные в таблицу
$this->items=DB::Fast_Arr_Query("SELECT * FROM `{$this->table_name}` ".
"ORDER BY `$order` ".($desc?'DESC ':'').
"LIMIT ".($page-1)*$this->items_per_page.', '.$this->items_per_page);
$this->items=make_id_array($this->items,$this->key_col,0);
//добавление строк в таблицу
foreach($this->items as $id=>$item){
$cols=array();
foreach($this->cols as $colname=>$coldata){
$function_name=$coldata['handler'];
if(is_callable($function_name))
$cols[]=call_user_func($function_name, $id, (isset($item[$colname])?$item[$colname]:''),$item);
else
$cols[]=$item[$colname];
}
call_user_func_array(array(&$this->table, "add_row"),$cols);
}
//добавление заглавной строки в таблицу
$cols=array();
foreach($this->cols as $colname=>$coldata){
$title=$coldata['title'];
if($coldata['is_ordered'])
$title="<a href='{$this->link}&order=$colname&desc=1'>v</a>".$title."<a href='{$this->link}&order=$colname'>^</a>\n";
$cols[]=$title;
}
call_user_func_array(array(&$this->table, "add_head_row"),$cols);
call_user_func_array(array(&$this->table, "add_foot_row"),$footers);
$this->link.="&order=$order&desc=$desc";
}
function print_table(){
if($this->max_page>1){
$page_line='<div class=pages>'.make_pages($this->page,$this->max_page," [%s] "," <a href='{$this->link}&page=%1\$s'>%1\$s</a> ",2).'</div>';
print $page_line;
}
print $this->table->generate();
if ($this->max_page>1)
print $page_line;
}
}
?>
admin_table