admin_table

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?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,"&nbsp;[%s]&nbsp;","&nbsp;<a href='?module=board&action=view_adverts&page=%1\$s'>%1\$s</a>&nbsp;",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,"&nbsp;[%s]&nbsp;","&nbsp;<a href='{$this->link}&page=%1\$s'>%1\$s</a>&nbsp;",2).'</div>';
print $page_line;
}
print $this->table->generate();
if ($this->max_page>1)
print $page_line;
}
}
?>