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
40 // db_pager constructor
41 // accepts $sql like 'SELECT .... FROM .... [WHERE ...] [GROUP ...]'
42 // $name is base name for pager controls
43 function db_pager($sql, $name, $page_len=0)
45 if ($page_len == 0) $page_len = user_query_size();
47 $this->page_len = $page_len;
51 // Parse base sql select query.
53 function set_sql($sql)
55 if ($sql != $this->sql) {
59 $from = $where = $group = '';
60 $parts = preg_split('/\sFROM\s/si', $sql, 2);
61 if(count($parts) == 2) {
64 $parts = preg_split('/\sWHERE\s/si', $from, 2);
65 if(count($parts) == 2) {
68 $parts = preg_split('/\sGROUP\s*BY\s/si', $where, 2);
69 if(count($parts) == 2) {
75 $this->select = $select;
77 $this->where = $where;
78 $this->group = $group;
80 display_error("sql: $sql");
81 display_error($select);
82 display_error("FROM $from");
83 display_error("WHERE $where");
84 display_error("GROUP BY $group");
89 // Set additional constraint on record set
91 function set_where($where)
93 if (!is_array($where))
94 $where = array($where);
96 if (count($where) != count($this->extra_where) ||
97 count(array_diff($this->extra_where, $where))) {
98 $this->extra_where = $where;
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;
239 if(count($this->extra_where)) {
240 $wherw .= ($where=='' ? '' : ' AND ')
241 .implode( $this->extra_where, ' AND ');
243 if ($where) $where = " WHERE($where)";
246 $group = $group == '' ? "*" : "DISTINCT $group";
248 return "SELECT COUNT($group) FROM $from $where";
251 $sql = "$select FROM $from $where";
252 if($group) $sql.= " GROUP BY $group";
255 foreach( $this->columns as $col) {
256 if (isset($col['ord'])) {
257 if ( $col['ord'] != '' && isset($col['name'])) {
258 $ord[] = $col['name'] .' '. $col['ord'];
264 $sql .= " ORDER BY " . implode($ord, ',');
266 $page_len = $this->page_len;
267 $offset = ($this->curr_page - 1) * $page_len;
269 $sql .= " LIMIT $offset, $page_len";
275 // Initialization after changing record set
279 if ($this->ready == false ) {
280 $sql = $this->_sql_gen(true);
281 $result = db_query($sql, 'Error reading record set');
282 if ($result == false)
284 $row = db_fetch_row($result);
285 $this->rec_count = $row[0];
286 $this->max_page = ceil($this->rec_count/$this->page_len);
293 // Set current page in response to user control.
295 function select_records()
299 $page = find_submit($this->name.'_page_', false);
300 $sort = find_submit($this->name.'_sort_', true);
302 $this->change_page($page);
303 if ($page == 'next' && !$this->next_page ||
304 $page == 'last' && !$this->last_page)
305 set_focus($this->name.'_page_prev');
306 if ($page == 'prev' && !$this->prev_page ||
307 $page == 'first' && !$this->first_page)
308 set_focus($this->name.'_page_next');
309 } elseif ($sort != -1) {
310 $this->sort_table($sort);
315 // Set check function to mark some rows.
317 function set_marker($func, $notice='', $markercl='overduebg', $msgclass='overduefg' )
319 $this->marker = $func;
320 $this->marker_txt = $notice;
321 $this->marker_class = $markercl;
322 $this->notice_class = $msgclass;
325 //-----------------------------------------------------------------------------
326 // Creates new db_pager $_SESSION object on first page call.
327 // Retrieves from $_SESSION var on subsequent $_POST calls
329 // $name - base name for pager controls and $_SESSION object name
330 // $sql - base sql for data inquiry. Order of fields implies
331 // pager columns order.
332 // $coldef - array of column definitions. Example definitions
333 // Column with title 'User name' and default text format:
335 // Skipped field from sql query. Data for the field is not displayed:
337 // Column without title, data retrieved form row data with function func():
338 // array('fun'=>'func')
339 // Inserted column with title 'Some', formated with function rowfun().
341 // 'Some' => array('type'=>'date, 'insert'=>true, 'fun'=>'rowfun')
342 // Column with name 'Another', formatted as date,
343 // sortable with ascending start order (available orders: asc,desc, '').
344 // 'Another' => array('type'=>'date', 'ord'=>'asc')
346 // All available column format types you will find in db_pager_view.inc file.
347 // If query result has more fields than count($coldef), rest of data is ignored
348 // during display, but can be used in format handlers for 'spec' and 'insert'
351 function &new_db_pager($name, $sql, $coldef, $page_len = 0) {
353 if (isset($name) && isset($_SESSION[$name])) {
354 // kill old pager if any on first page call
355 if ($_SERVER['REQUEST_METHOD'] == 'GET')
356 unset($_SESSION[$name]);
358 return $_SESSION[$name];
360 $ret =& new db_pager($sql, $name, $page_len);
361 $ret->set_columns($coldef);
363 if (isset($name)) $_SESSION[$name] = &$ret;