X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=includes%2Fdb_pager.inc;h=e09659a0210697dc9171dc17e335d4fe3922ca3e;hb=510d6e1925c4d1621ae3efd85e117cc9bb4320f0;hp=ce739535acad64f41e7af8c0f4aa5db418429929;hpb=ffb0ae97be5c30eeee98ad9aac45cf5077b515c7;p=fa-stable.git diff --git a/includes/db_pager.inc b/includes/db_pager.inc index ce739535..e09659a0 100644 --- a/includes/db_pager.inc +++ b/includes/db_pager.inc @@ -1,4 +1,14 @@ . +***********************************************************************/ // // Controler part of database table pager with column sort. // To display actual html object call display_db_pager($name) inside @@ -16,7 +26,11 @@ class db_pager { var $marker_txt; var $marker_class; var $notice_class; - + var $width; // table width (default '95%') + var $header_fun; // additional row between title and body + var $header_class; + var $footer_fun; + var $footer_class; var $data = array(); var $curr_page, @@ -32,16 +46,23 @@ class db_pager { var $select, $where, $from, - $group; + $group, + $order; var $extra_where; - var $ready = false; - + var $ready = false; // this var is false after change in sql before first + // and before first query. + var $inactive_ctrl = false; + var $main_tbl; // table and key field name for inactive ctrl and edit/delete links + var $key; // key field name + // db_pager constructor - // accepts $sql like 'SELECT .... FROM .... [WHERE ...] [GROUP ...]' + // accepts $sql like 'SELECT ...[FROM ...][WHERE ...][GROUP ...][ORDER ...]' // $name is base name for pager controls - function db_pager($sql, $name, $page_len=0) + function db_pager($sql, $name, $table = null, $page_len=0) { + global $table_style; + $this->width = "95%"; if ($page_len == 0) $page_len = user_query_size(); $this->name = $name; $this->page_len = $page_len; @@ -55,49 +76,44 @@ class db_pager { if ($sql != $this->sql) { $this->sql = $sql; $this->ready = false; - $select = $sql; - $from = $where = $group = ''; + $parts = preg_split('/\sORDER\s*BY\s/si', $sql, 2); + if(count($parts) == 2) { + $sql = $parts[0]; + $this->order = $parts[1]; + } + $parts = preg_split('/\sGROUP\s*BY\s/si', $sql, 2); + if(count($parts) == 2) { + $sql = $parts[0]; + $this->group = $parts[1]; + } + $parts = preg_split('/\sWHERE\s/si', $sql, 2); + if(count($parts) == 2) { + $sql = $parts[0]; + $this->where = $parts[1]; + } $parts = preg_split('/\sFROM\s/si', $sql, 2); if(count($parts) == 2) { - $select = $parts[0]; - $from = $parts[1]; - $parts = preg_split('/\sWHERE\s/si', $from, 2); - if(count($parts) == 2) { - $from = $parts[0]; - $where = $parts[1]; - $parts = preg_split('/\sGROUP\s*BY\s/si', $where, 2); - if(count($parts) == 2) { - $where = $parts[0]; - $group = $parts[1]; - } - } + $sql = $parts[0]; + $this->from = $parts[1]; } - $this->select = $select; - $this->from = $from; - $this->where = $where; - $this->group = $group; -/* - display_error("sql: $sql"); - display_error($select); - display_error("FROM $from"); - display_error("WHERE $where"); - display_error("GROUP BY $group"); -*/ + $this->select = $sql; } } // // Set additional constraint on record set // - function set_where($where) + function set_where($where = null) { - if (!is_array($where)) - $where = array($where); + if ($where) { + if (!is_array($where)) + $where = array($where); - if (count($where) != count($this->extra_where) || - count(array_diff($this->extra_where, $where))) { - $this->extra_where = $where; - $this->ready = false; - } + if (count($where) == count($this->extra_where) && + !count(array_diff($this->extra_where, $where))) + return; + } + $this->extra_where = $where; + $this->ready = false; } // // Set query result page @@ -112,7 +128,7 @@ class db_pager { // Change sort column direction // in order asc->desc->none->asc // - function sort_table($col=null) + function sort_table($col) { $ord = $this->columns[$col]['ord']; $ord = ($ord == '') ? 'asc' : (($ord == 'asc') ? 'desc' : ''); @@ -144,14 +160,18 @@ class db_pager { $c = 0; // add result field names to column defs for // col value retrieve and sort purposes - for ($c = $i = 0; $c < count($this->columns); $c++) { - if ($this->columns[$c]['type'] != 'insert') - $this->columns[$c]['name']= mysql_field_name($result, $i++); + $cnt = min(mysql_num_fields($result), count($this->columns)); + for ($c = $i = 0; $c < $cnt; $c++) { + if (!(isset($this->columns[$c]['insert']) && $this->columns[$c]['insert'])) { +// if (!@($this->columns[$c]['type']=='skip')) + $this->columns[$c]['name']= mysql_field_name($result, $i); + if (!@($this->columns[$c]['type']=='insert')) + $i++; + } } - - while ($row = db_fetch_assoc($result)) + while ($row = db_fetch_assoc($result)) { $this->data[] = $row; - + } } else return false; return true; @@ -196,7 +216,7 @@ class db_pager { $flds = array($flds); } foreach ($flds as $colnum=>$coldef) { - if (!is_numeric($colnum)) { // 'colname'=>params + if (is_string($colnum)) { // 'colname'=>params $h = $colnum; $c = $coldef; } else { // n=>params @@ -204,23 +224,24 @@ class db_pager { $h = ''; $c = $coldef; } else { - $h = $coldef; + $h = $coldef; $c = 'text'; } - $c = ''; } - if (!is_array($c)) // params is simple column type + if (is_string($c)) // params is simple column type $c = array('type'=>$c); if (!isset($c['type'])) $c['type'] = 'text'; switch($c['type']) { - case 'insert': - default: - $c['head'] = $h; break; - case 'skip': // skip the column (no header) - unset($c['head']); // paranoid code + case 'inactive': + $this->inactive_ctrl = true; + case 'insert': + default: + $c['head'] = $h; break; + case 'skip': // skip the column (no header) + unset($c['head']); break; } $this->columns[] = $c; } @@ -235,13 +256,14 @@ class db_pager { $select = $this->select; $from = $this->from; $where = $this->where; - $group = $this->group; + $group = $this->group; + $order = $this->order; if(count($this->extra_where)) { - $wherw .= ($where=='' ? '' : ' AND ') + $where .= ($where=='' ? '' : ' AND ') .implode( $this->extra_where, ' AND '); } - if ($where) $where = " WHERE($where)"; + if ($where) $where = " WHERE ($where)"; if ($count) { $group = $group == '' ? "*" : "DISTINCT $group"; @@ -249,10 +271,11 @@ class db_pager { return "SELECT COUNT($group) FROM $from $where"; } - $sql = "$select FROM $from $where GROUP BY $group"; + $sql = "$select FROM $from $where"; + if ($group) $sql.= " GROUP BY $group"; $ord = array(); - foreach( $this->columns as $col) { + foreach ($this->columns as $col) { if (isset($col['ord'])) { if ( $col['ord'] != '' && isset($col['name'])) { $ord[] = $col['name'] .' '. $col['ord']; @@ -260,8 +283,12 @@ class db_pager { } } - if (count($ord)) + if (count($ord)) { $sql .= " ORDER BY " . implode($ord, ','); + } else { + if($order) + $sql .= " ORDER BY $order"; // original base query order + } $page_len = $this->page_len; $offset = ($this->curr_page - 1) * $page_len; @@ -276,6 +303,8 @@ class db_pager { // function _init() { + global $go_debug; + if ($this->ready == false ) { $sql = $this->_sql_gen(true); $result = db_query($sql, 'Error reading record set'); @@ -283,7 +312,20 @@ class db_pager { return false; $row = db_fetch_row($result); $this->rec_count = $row[0]; - $this->max_page = ceil($this->rec_count/$this->page_len); + $this->max_page = $this->page_len ? + ceil($this->rec_count/$this->page_len) : 0; + + if ($go_debug) { // FIX - need column name parsing, but for now: + // check if field names are set explicite in col def + // for all initially ordered columns + foreach ($this->columns as $col) { + if (isset($col['ord']) && $col['ord'] != '' + && !isset($col['name'])) { + display_warning("Result field names must be set + for all intially ordered db_pager columns."); + } + } + } $this->set_page(1); $this->ready = true; } @@ -297,10 +339,16 @@ class db_pager { global $Ajax; $page = find_submit($this->name.'_page_', false); - $sort = find_submit($this->name.'_sort_', false); + $sort = find_submit($this->name.'_sort_', true); if ($page) { $this->change_page($page); - } elseif ($sort) { + if ($page == 'next' && !$this->next_page || + $page == 'last' && !$this->last_page) + set_focus($this->name.'_page_prev'); + if ($page == 'prev' && !$this->prev_page || + $page == 'first' && !$this->first_page) + set_focus($this->name.'_page_next'); + } elseif ($sort != -1) { $this->sort_table($sort); } else $this->query(); @@ -315,6 +363,62 @@ class db_pager { $this->marker_class = $markercl; $this->notice_class = $msgclass; } + // + // Set handler to display additional row between titles and pager body. + // Return array of column contents. + // + function set_header($func, $headercl='inquirybg') + { + $this->header_fun = $func; + $this->header_class = $headercl; + } + // + // Set handler to display additional row between pager body and navibar. + // Return array of column contents. + // + function set_footer($func, $footercl='inquirybg') + { + $this->footer_fun = $func; + $this->footer_class = $footercl; + } + // + // Setter for table editors with inactive cell control. + // + function set_inactive_ctrl($table, $key) { + $this->inactive_ctrl = array('table'=>$table, 'key'=>$key); + } + // + // Helper for display inactive control cells + // + function inactive_control_cell(&$row) + { + if ($this->inactive_ctrl) { +// return inactive_control_cell($row[$this->inactive_ctrl['key']], +// $row['inactive'], $this->inactive_ctrl['table'], +// $this->inactive_ctrl['key']); + + global $Ajax; + + $key = $this->key ? + $this->key : $this->columns[0]['name']; // TODO - support for complex keys + $id = $row[$key]; + $table = $this->main_tbl; + $name = "Inactive". $id; + $value = $row['inactive'] ? 1:0; + + if (check_value('show_inactive')) { + if (isset($_POST['LInact'][$id]) && (get_post('_Inactive'.$id.'_update') || + get_post('Update')) && (check_value('Inactive'.$id) != $value)) { + update_record_status($id, !$value, $table, $key); + $value = !$value; + } + echo '