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 ...][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%";
$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 (!(isset($this->columns[$c]['insert']) && $this->columns[$c]['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;
$h = '';
$c = $coldef;
} else {
- $h = $coldef;
+ $h = $coldef;
$c = 'text';
}
}
$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;
}
$select = $this->select;
$from = $this->from;
$where = $this->where;
- $group = $this->group;
+ $group = $this->group;
$order = $this->order;
if(count($this->extra_where)) {
$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 '<td align="center">'. checkbox(null, $name, $value, true, '', "align='center'")
+ . hidden("LInact[$id]", $value, false) . '</td>';
+ }
+ } else
+ return '';
+ }
+
};
//-----------------------------------------------------------------------------
// Creates new db_pager $_SESSION object on first page call.
// during display, but can be used in format handlers for 'spec' and 'insert'
// type columns.
-function &new_db_pager($name, $sql, $coldef, $page_len = 0) {
-
- if ($_SERVER['REQUEST_METHOD'] == 'GET')
- unset($_SESSION[$name]); // kill old pager if any exists on first page call
+function &new_db_pager($name, $sql, $coldef, $table = null, $key = null, $page_len = 0) {
+ if (isset($_SESSION[$name]) &&
+ ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SESSION[$name]->sql != $sql)) {
+ unset($_SESSION[$name]); // kill pager if sql has changed
+ }
if (!isset($_SESSION[$name])) {
- $_SESSION[$name] =& new db_pager($sql, $name, $page_len);
+ $_SESSION[$name] =& new db_pager($sql, $name, $table, $page_len);
+ $_SESSION[$name]->main_tbl = $table;
+ $_SESSION[$name]->key = $key;
$_SESSION[$name]->set_sql($sql);
$_SESSION[$name]->set_columns($coldef);
}
- $ret = &$_SESSION[$name];
+ return $_SESSION[$name];
- return $ret;
}
//
// Force pager initialization.
function navi_button_cell($name, $value, $enabled=true, $align='left') {
label_cell(navi_button($name, $value, $enabled), "align='$align'");
}
+
+
//-----------------------------------------------------------------------------
//
// Sql paged table view. Call this function inside form.
$headers = array();
foreach($pager->columns as $num_col=>$col) {
- if (isset($col['head'])) {
- if (!isset($col['ord']))
+ // record status control column is displayed only when control checkbox is on
+ if (isset($col['head']) && ($col['type']!='inactive' || get_post('show_inactive'))) {
+ if (!isset($col['ord']))
$headers[] = $col['head'];
else {
$icon = (($col['ord'] == 'desc') ? 'sort_desc.gif' :
}
/* show a table of records returned by the sql */
start_table("$table_style width=$pager->width");
- table_header($headers, 'nowrap');
+ table_header($headers);
if($pager->header_fun) { // if set header handler
start_row("class='{$pager->header_class}'");
} else
$cell = '';
}
-
switch($coltype) { // format column
case 'time':
label_cell($cell, "width=40"); break;
label_cell('');
else
qty_cell($cell, false, isset($col['dec']) ? $col['dec'] : null); break;
+ case 'email':
+ email_cell($cell,isset( $col['align']) ? "align='" . $col['align'] . "'" : null);
+ break;
case 'rate':
label_cell(number_format2($cell, user_exrate_dec()), "align=center"); break;
+ case 'inactive':
+ if(get_post('show_inactive'))
+ $pager->inactive_control_cell($row); break;
default:
// case 'text':
if (isset( $col['align']))
start_row("class='navibar'");
$colspan = count($pager->columns);
+ $inact = $pager->inactive_ctrl==true
+ ? ' '.checkbox(null, 'show_inactive', null, true). _("Show also Inactive") : '';
if($pager->rec_count) {
echo "<td colspan=$colspan class='navibar' style='border:none;padding:3px;'>";
echo "<div style='float:right;'>";
$but_pref = $pager->name.'_page_';
start_table();
start_row();
+ if ($pager->inactive_ctrl)
+ submit('Update', _('Update'), true, '', null); // inactive update
echo navi_button_cell($but_pref.'first', _('First'), $pager->first_page, 'right');
echo navi_button_cell($but_pref.'prev', _('Prev'), $pager->prev_page,'right');
echo navi_button_cell($but_pref.'next', _('Next'), $pager->next_page,'right');
$to = $pager->rec_count;
$all = $pager->rec_count;
echo sprintf( _('Records %d-%d of %d'), $from, $to, $all);
+ echo $inact;
echo "</td>";
} else {
- label_cell( _('No records'), "colspan=$colspan class='navibar'");
+ label_cell( _('No records') . $inact, "colspan=$colspan class='navibar'");
}
+
end_row();
end_table();