3 // Controler part of database table pager with column sort.
4 // To display actual html object call display_db_pager($name) inside
7 // View definition you will find in the following file:
8 include_once($path_to_root."/includes/ui/db_pager_view.inc");
13 var $columns; // column definitions (head, type, order)
15 var $marker; // marker check function
19 var $header_fun; // additional row between title and body
41 var $ready = false; // this var is false after change in sql before first
42 // and before first query.
44 // db_pager constructor
45 // accepts $sql like 'SELECT .... FROM .... [WHERE ...] [GROUP ...]'
46 // $name is base name for pager controls
47 function db_pager($sql, $name, $page_len=0)
49 if ($page_len == 0) $page_len = user_query_size();
51 $this->page_len = $page_len;
55 // Parse base sql select query.
57 function set_sql($sql)
59 if ($sql != $this->sql) {
63 $from = $where = $group = '';
64 $parts = preg_split('/\sFROM\s/si', $sql, 2);
65 if(count($parts) == 2) {
68 $parts = preg_split('/\sWHERE\s/si', $from, 2);
69 if(count($parts) == 2) {
72 $parts = preg_split('/\sGROUP\s*BY\s/si', $where, 2);
73 if(count($parts) == 2) {
79 $this->select = $select;
81 $this->where = $where;
82 $this->group = $group;
84 display_error("sql: $sql");
85 display_error($select);
86 display_error("FROM $from");
87 display_error("WHERE $where");
88 display_error("GROUP BY $group");
93 // Set additional constraint on record set
95 function set_where($where)
97 if (!is_array($where))
98 $where = array($where);
100 if (count($where) != count($this->extra_where) ||
101 count(array_diff($this->extra_where, $where))) {
102 $this->extra_where = $where;
103 $this->ready = false;
107 // Set query result page
109 function change_page($page=null)
111 $this->set_page($page);
116 // Change sort column direction
117 // in order asc->desc->none->asc
119 function sort_table($col)
121 $ord = $this->columns[$col]['ord'];
122 $ord = ($ord == '') ? 'asc' : (($ord == 'asc') ? 'desc' : '');
123 $this->columns[$col]['ord'] = $ord;
135 $Ajax->activate("_{$this->name}_span");
136 $this->data = array();
140 if ($this->rec_count == 0) return true;
142 $sql = $this->_sql_gen(false);
144 $result = db_query($sql, 'Error browsing database: '.$sql );
147 // setting field names for subsequent queries
149 // add result field names to column defs for
150 // col value retrieve and sort purposes
151 for ($c = $i = 0; $c < count($this->columns); $c++) {
152 if (!(isset($this->columns[$c]['insert']) && $this->columns[$c]['insert']))
153 $this->columns[$c]['name']= mysql_field_name($result, $i++);
156 while ($row = db_fetch_assoc($result))
157 $this->data[] = $row;
164 // Calculates page numbers for html controls.
166 function set_page($to)
170 $page = $this->curr_page+1; break;
172 $page = $this->curr_page-1; break;
174 $page = $this->last_page; break;
176 if (is_numeric($to)) {
184 $max = $this->max_page;
187 $this->curr_page = $page;
188 $this->next_page = ($page < $max) ? $page+1 : null;
189 $this->prev_page = ($page > 1) ? ($page-1) : null;
190 $this->last_page = ($page < $max) ? $max : null;
191 $this->first_page = ($page != 1) ? 1: null;
194 // Set column definitions
195 // $flds: array( fldname1, fldname2=>type,...)
196 function set_columns($flds)
198 $this->columns = array();
199 if (!is_array($flds)) {
200 $flds = array($flds);
202 foreach ($flds as $colnum=>$coldef) {
203 if (is_string($colnum)) { // 'colname'=>params
206 } else { // n=>params
207 if (is_array($coldef)) {
215 if (is_string($c)) // params is simple column type
216 $c = array('type'=>$c);
218 if (!isset($c['type']))
224 $c['head'] = $h; break;
225 case 'skip': // skip the column (no header)
226 unset($c['head']); // paranoid code
228 $this->columns[] = $c;
232 // Generate db query from base sql
233 // $count==false - for current page data retrieval
234 // $count==true - for total records count
236 function _sql_gen($count=false)
238 $select = $this->select;
240 $where = $this->where;
241 $group = $this->group;
243 if(count($this->extra_where)) {
244 $where .= ($where=='' ? '' : ' AND ')
245 .implode( $this->extra_where, ' AND ');
247 if ($where) $where = " WHERE($where)";
250 $group = $group == '' ? "*" : "DISTINCT $group";
252 return "SELECT COUNT($group) FROM $from $where";
255 $sql = "$select FROM $from $where";
256 if($group) $sql.= " GROUP BY $group";
259 foreach( $this->columns as $col) {
260 if (isset($col['ord'])) {
261 if ( $col['ord'] != '' && isset($col['name'])) {
262 $ord[] = $col['name'] .' '. $col['ord'];
268 $sql .= " ORDER BY " . implode($ord, ',');
270 $page_len = $this->page_len;
271 $offset = ($this->curr_page - 1) * $page_len;
273 $sql .= " LIMIT $offset, $page_len";
279 // Initialization after changing record set
283 if ($this->ready == false ) {
284 $sql = $this->_sql_gen(true);
285 $result = db_query($sql, 'Error reading record set');
286 if ($result == false)
288 $row = db_fetch_row($result);
289 $this->rec_count = $row[0];
290 $this->max_page = ceil($this->rec_count/$this->page_len);
297 // Set current page in response to user control.
299 function select_records()
303 $page = find_submit($this->name.'_page_', false);
304 $sort = find_submit($this->name.'_sort_', true);
306 $this->change_page($page);
307 if ($page == 'next' && !$this->next_page ||
308 $page == 'last' && !$this->last_page)
309 set_focus($this->name.'_page_prev');
310 if ($page == 'prev' && !$this->prev_page ||
311 $page == 'first' && !$this->first_page)
312 set_focus($this->name.'_page_next');
313 } elseif ($sort != -1) {
314 $this->sort_table($sort);
319 // Set check function to mark some rows.
321 function set_marker($func, $notice='', $markercl='overduebg', $msgclass='overduefg' )
323 $this->marker = $func;
324 $this->marker_txt = $notice;
325 $this->marker_class = $markercl;
326 $this->notice_class = $msgclass;
329 // Set handler to display additional row between titles and pager body.
330 // Return array of column contents.
332 function set_header($func, $headercl='inquirybg')
334 $this->header_fun = $func;
335 $this->header_class = $headercl;
338 // Set handler to display additional row between pager body and navibar.
339 // Return array of column contents.
341 function set_footer($func, $footercl='inquirybg')
343 $this->footer_fun = $func;
344 $this->footer_class = $footercl;
347 //-----------------------------------------------------------------------------
348 // Creates new db_pager $_SESSION object on first page call.
349 // Retrieves from $_SESSION var on subsequent $_POST calls
351 // $name - base name for pager controls and $_SESSION object name
352 // $sql - base sql for data inquiry. Order of fields implies
353 // pager columns order.
354 // $coldef - array of column definitions. Example definitions
355 // Column with title 'User name' and default text format:
357 // Skipped field from sql query. Data for the field is not displayed:
359 // Column without title, data retrieved form row data with function func():
360 // array('fun'=>'func')
361 // Inserted column with title 'Some', formated with function rowfun().
363 // 'Some' => array('type'=>'date, 'insert'=>true, 'fun'=>'rowfun')
364 // Column with name 'Another', formatted as date,
365 // sortable with ascending start order (available orders: asc,desc, '').
366 // 'Another' => array('type'=>'date', 'ord'=>'asc')
368 // All available column format types you will find in db_pager_view.inc file.
369 // If query result has more fields than count($coldef), rest of data is ignored
370 // during display, but can be used in format handlers for 'spec' and 'insert'
373 function &new_db_pager($name, $sql, $coldef, $page_len = 0) {
375 if ($_SERVER['REQUEST_METHOD'] == 'GET')
376 unset($_SESSION[$name]); // kill old pager if any exists on first page call
378 if (!isset($_SESSION[$name]))
379 $_SESSION[$name] =& new db_pager($sql, $name, $page_len);
381 $ret = &$_SESSION[$name];
383 $ret->set_columns($coldef);