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
42 var $ready = false; // this var is false after change in sql before first
43 // and before first query.
45 // db_pager constructor
46 // accepts $sql like 'SELECT ...[FROM ...][WHERE ...][GROUP ...][ORDER ...]'
47 // $name is base name for pager controls
48 function db_pager($sql, $name, $page_len=0)
50 if ($page_len == 0) $page_len = user_query_size();
52 $this->page_len = $page_len;
56 // Parse base sql select query.
58 function set_sql($sql)
60 if ($sql != $this->sql) {
63 $parts = preg_split('/\sORDER\s*BY\s/si', $sql, 2);
64 if(count($parts) == 2) {
66 $this->order = $parts[1];
68 $parts = preg_split('/\sGROUP\s*BY\s/si', $sql, 2);
69 if(count($parts) == 2) {
71 $this->group = $parts[1];
73 $parts = preg_split('/\sWHERE\s/si', $sql, 2);
74 if(count($parts) == 2) {
76 $this->where = $parts[1];
78 $parts = preg_split('/\sFROM\s/si', $sql, 2);
79 if(count($parts) == 2) {
81 $this->from = $parts[1];
87 // Set additional constraint on record set
89 function set_where($where = null)
92 if (!is_array($where))
93 $where = array($where);
95 if (count($where) == count($this->extra_where) &&
96 !count(array_diff($this->extra_where, $where)))
99 $this->extra_where = $where;
100 $this->ready = false;
103 // Set query result page
105 function change_page($page=null)
107 $this->set_page($page);
112 // Change sort column direction
113 // in order asc->desc->none->asc
115 function sort_table($col)
117 $ord = $this->columns[$col]['ord'];
118 $ord = ($ord == '') ? 'asc' : (($ord == 'asc') ? 'desc' : '');
119 $this->columns[$col]['ord'] = $ord;
131 $Ajax->activate("_{$this->name}_span");
132 $this->data = array();
136 if ($this->rec_count == 0) return true;
138 $sql = $this->_sql_gen(false);
140 $result = db_query($sql, 'Error browsing database: '.$sql );
143 // setting field names for subsequent queries
145 // add result field names to column defs for
146 // col value retrieve and sort purposes
147 for ($c = $i = 0; $c < count($this->columns); $c++) {
148 if (!(isset($this->columns[$c]['insert']) && $this->columns[$c]['insert']))
149 $this->columns[$c]['name']= mysql_field_name($result, $i++);
152 while ($row = db_fetch_assoc($result))
153 $this->data[] = $row;
160 // Calculates page numbers for html controls.
162 function set_page($to)
166 $page = $this->curr_page+1; break;
168 $page = $this->curr_page-1; break;
170 $page = $this->last_page; break;
172 if (is_numeric($to)) {
180 $max = $this->max_page;
183 $this->curr_page = $page;
184 $this->next_page = ($page < $max) ? $page+1 : null;
185 $this->prev_page = ($page > 1) ? ($page-1) : null;
186 $this->last_page = ($page < $max) ? $max : null;
187 $this->first_page = ($page != 1) ? 1: null;
190 // Set column definitions
191 // $flds: array( fldname1, fldname2=>type,...)
192 function set_columns($flds)
194 $this->columns = array();
195 if (!is_array($flds)) {
196 $flds = array($flds);
198 foreach ($flds as $colnum=>$coldef) {
199 if (is_string($colnum)) { // 'colname'=>params
202 } else { // n=>params
203 if (is_array($coldef)) {
211 if (is_string($c)) // params is simple column type
212 $c = array('type'=>$c);
214 if (!isset($c['type']))
220 $c['head'] = $h; break;
221 case 'skip': // skip the column (no header)
222 unset($c['head']); // paranoid code
224 $this->columns[] = $c;
228 // Generate db query from base sql
229 // $count==false - for current page data retrieval
230 // $count==true - for total records count
232 function _sql_gen($count=false)
234 $select = $this->select;
236 $where = $this->where;
237 $group = $this->group;
238 $order = $this->order;
240 if(count($this->extra_where)) {
241 $where .= ($where=='' ? '' : ' AND ')
242 .implode( $this->extra_where, ' AND ');
244 if ($where) $where = " WHERE ($where)";
247 $group = $group == '' ? "*" : "DISTINCT $group";
249 return "SELECT COUNT($group) FROM $from $where";
252 $sql = "$select FROM $from $where";
253 if($group) $sql.= " GROUP BY $group";
256 foreach( $this->columns as $col) {
257 if (isset($col['ord'])) {
258 if ( $col['ord'] != '' && isset($col['name'])) {
259 $ord[] = $col['name'] .' '. $col['ord'];
265 $sql .= " ORDER BY " . implode($ord, ',');
268 $sql .= " ORDER BY $order"; // original base query order
271 $page_len = $this->page_len;
272 $offset = ($this->curr_page - 1) * $page_len;
274 $sql .= " LIMIT $offset, $page_len";
280 // Initialization after changing record set
284 if ($this->ready == false ) {
285 $sql = $this->_sql_gen(true);
286 $result = db_query($sql, 'Error reading record set');
287 if ($result == false)
289 $row = db_fetch_row($result);
290 $this->rec_count = $row[0];
291 $this->max_page = ceil($this->rec_count/$this->page_len);
298 // Set current page in response to user control.
300 function select_records()
304 $page = find_submit($this->name.'_page_', false);
305 $sort = find_submit($this->name.'_sort_', true);
307 $this->change_page($page);
308 if ($page == 'next' && !$this->next_page ||
309 $page == 'last' && !$this->last_page)
310 set_focus($this->name.'_page_prev');
311 if ($page == 'prev' && !$this->prev_page ||
312 $page == 'first' && !$this->first_page)
313 set_focus($this->name.'_page_next');
314 } elseif ($sort != -1) {
315 $this->sort_table($sort);
320 // Set check function to mark some rows.
322 function set_marker($func, $notice='', $markercl='overduebg', $msgclass='overduefg' )
324 $this->marker = $func;
325 $this->marker_txt = $notice;
326 $this->marker_class = $markercl;
327 $this->notice_class = $msgclass;
330 // Set handler to display additional row between titles and pager body.
331 // Return array of column contents.
333 function set_header($func, $headercl='inquirybg')
335 $this->header_fun = $func;
336 $this->header_class = $headercl;
339 // Set handler to display additional row between pager body and navibar.
340 // Return array of column contents.
342 function set_footer($func, $footercl='inquirybg')
344 $this->footer_fun = $func;
345 $this->footer_class = $footercl;
348 //-----------------------------------------------------------------------------
349 // Creates new db_pager $_SESSION object on first page call.
350 // Retrieves from $_SESSION var on subsequent $_POST calls
352 // $name - base name for pager controls and $_SESSION object name
353 // $sql - base sql for data inquiry. Order of fields implies
354 // pager columns order.
355 // $coldef - array of column definitions. Example definitions
356 // Column with title 'User name' and default text format:
358 // Skipped field from sql query. Data for the field is not displayed:
360 // Column without title, data retrieved form row data with function func():
361 // array('fun'=>'func')
362 // Inserted column with title 'Some', formated with function rowfun().
364 // 'Some' => array('type'=>'date, 'insert'=>true, 'fun'=>'rowfun')
365 // Column with name 'Another', formatted as date,
366 // sortable with ascending start order (available orders: asc,desc, '').
367 // 'Another' => array('type'=>'date', 'ord'=>'asc')
369 // All available column format types you will find in db_pager_view.inc file.
370 // If query result has more fields than count($coldef), rest of data is ignored
371 // during display, but can be used in format handlers for 'spec' and 'insert'
374 function &new_db_pager($name, $sql, $coldef, $page_len = 0) {
376 if ($_SERVER['REQUEST_METHOD'] == 'GET')
377 unset($_SESSION[$name]); // kill old pager if any exists on first page call
379 if (!isset($_SESSION[$name])) {
380 $_SESSION[$name] =& new db_pager($sql, $name, $page_len);
381 $_SESSION[$name]->set_sql($sql);
382 $_SESSION[$name]->set_columns($coldef);
385 $ret = &$_SESSION[$name];
390 // Force pager initialization.
392 function refresh_pager($name)
394 if (isset($_SESSION[$name]))
395 $_SESSION[$name]->ready = false;