X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=includes%2Fdb_pager.inc;h=e3f3b167ad777ccdf87d77dd12d4092fdeb5a95f;hb=48589f9ce6c51bb25e2fa8ed83c57d9dfa485e44;hp=da4ee963c928ab380bd0ada46bfc6b5405dff816;hpb=91bede4226ba8b45151f0501ac88eb946ed5f2a1;p=fa-stable.git diff --git a/includes/db_pager.inc b/includes/db_pager.inc index da4ee963..e3f3b167 100644 --- a/includes/db_pager.inc +++ b/includes/db_pager.inc @@ -29,6 +29,7 @@ class db_pager { var $width; // table width (default '95%') var $header_fun; // additional row between title and body var $header_class; + var $row_fun; // Function for row preprocessing var $footer_fun; var $footer_class; var $data = array(); @@ -48,7 +49,7 @@ class db_pager { $from, $group, $order; - var $extra_where; + var $extra_where = array(); var $ready = false; // this var is false after change in sql before first // and before first query. @@ -64,7 +65,7 @@ class db_pager { // array('select' => 'SUM(quantity)', 'from' => TB_PREF."stock_moves", 'group' => 'location') // // $name is base name for pager controls - function db_pager($sql, $name, $table = null, $page_len=0) + function __construct($sql, $name, $table = null, $page_len=0) { $this->width = "95%"; if ($page_len == 0) $page_len = user_query_size(); @@ -77,6 +78,8 @@ class db_pager { // function set_sql($sql) { + global $SysPrefs; + if ($sql != $this->sql) { $this->sql = $sql; $this->ready = false; @@ -94,7 +97,7 @@ class db_pager { $this->select = $parts[0]; $sql = $parts[1]; } else { - if ($go_debug) + if ($SysPrefs->go_debug) display_error("Invalid sql input for db_pager"); } @@ -117,11 +120,6 @@ class db_pager { } } } - //_vd($this->select); - //_vd($this->from); - //_vd($this->where); - //_vd($this->group); - //_vd($this->order); } // // Set additional constraint on record set @@ -191,22 +189,22 @@ class db_pager { if ($result) { // setting field names for subsequent queries $c = 0; - // add result field names to column defs for - // col value retrieve and sort purposes - $cnt = min(db_num_fields($result), count($this->columns)); + // add result field names to column defs for + // col value retrieve and sort purposes + $cnt = min(db_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']= db_field_name($result, $i); if (!@($this->columns[$c]['type']=='insert')) - $i++; - } + $i++; + } } while ($row = db_fetch_assoc($result)) { $this->data[] = $row; } } else - return false; + return false; return true; } // @@ -215,29 +213,29 @@ class db_pager { function set_page($to) { switch($to) { - case 'next': - $page = $this->curr_page+1; break; - case 'prev': - $page = $this->curr_page-1; break; - case 'last': - $page = $this->last_page; break; - default: - if (is_numeric($to)) { - $page = $to; break; - } - case 'first': - $page = 1; break; + case 'next': + $page = $this->curr_page+1; break; + case 'prev': + $page = $this->curr_page-1; break; + case 'last': + $page = $this->last_page; break; + default: + if (is_numeric($to)) { + $page = $to; break; + } + case 'first': + $page = 1; break; } - if ($page < 1) - $page = 1; - $max = $this->max_page; - if ($page > $max) - $page = $max; - $this->curr_page = $page; - $this->next_page = ($page < $max) ? $page+1 : null; - $this->prev_page = ($page > 1) ? ($page-1) : null; - $this->last_page = ($page < $max) ? $max : null; - $this->first_page = ($page != 1) ? 1: null; + if ($page < 1) + $page = 1; + $max = $this->max_page; + if ($page > $max) + $page = $max; + $this->curr_page = $page; + $this->next_page = ($page < $max) ? $page+1 : null; + $this->prev_page = ($page > 1) ? ($page-1) : null; + $this->last_page = ($page < $max) ? $max : null; + $this->first_page = ($page != 1) ? 1: null; } // // Set column definitions @@ -250,8 +248,8 @@ class db_pager { } foreach ($flds as $colnum=>$coldef) { if (is_string($colnum)) { // 'colname'=>params - $h = $colnum; - $c = $coldef; + $h = $colnum; + $c = $coldef; } else { // n=>params if (is_array($coldef)) { $h = ''; @@ -262,10 +260,10 @@ class db_pager { } } if (is_string($c)) // params is simple column type - $c = array('type'=>$c); + $c = array('type'=>$c); if (!isset($c['type'])) - $c['type'] = 'text'; + $c['type'] = 'text'; switch($c['type']) { case 'inactive': @@ -298,24 +296,21 @@ class db_pager { } if ($where) $where = " WHERE ($where)"; - if ($count) { - $group = $group == '' ? "*" : "DISTINCT $group"; - - return "SELECT COUNT($group) FROM $from $where"; - } + if ($count) + return "SELECT COUNT(*) FROM ($this->sql) tmp_count"; $sql = "$select FROM $from $where"; if ($group) $sql.= " GROUP BY $group"; $ord = array(); - // sort order column by priority instead of table order. - $columns = array(); + // sort order column by priority instead of table order. + $columns = array(); foreach ($this->columns as $col) { - if(isset($col['ord_priority'])) { - $columns[$col['ord_priority']] = $col; - } + if(isset($col['ord_priority'])) { + $columns[$col['ord_priority']] = $col; } - krsort($columns); + } + krsort($columns); foreach ($columns as $col) { if (isset($col['ord'])) { @@ -324,10 +319,9 @@ class db_pager { } } } - + if (count($ord)) { - $ord = array_map(function_exists('mysql_real_escape_string') ? - 'mysql_real_escape_string': 'mysql_escape_string', $ord); + $ord = array_map('db_escape_function', $ord); $sql .= " ORDER BY " . implode(',', $ord); } else { if($order) @@ -347,7 +341,7 @@ class db_pager { // function _init() { - global $go_debug; + global $SysPrefs; if ($this->ready == false ) { $sql = $this->_sql_gen(true); @@ -355,11 +349,12 @@ class db_pager { if ($result == false) return false; $row = db_fetch_row($result); - $this->rec_count = $row[0]; + $this->rec_count = is_array($row) ? $row[0] : 0; +; $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: + if ($SysPrefs->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) { @@ -437,9 +432,6 @@ class db_pager { 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; @@ -515,4 +507,3 @@ function refresh_pager($name) if (isset($_SESSION[$name])) $_SESSION[$name]->ready = false; } -?>