Support for inactive record control added, optimizations.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 19 Sep 2009 20:20:20 +0000 (20:20 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 19 Sep 2009 20:20:20 +0000 (20:20 +0000)
includes/db_pager.inc
includes/ui/db_pager_view.inc

index 819b4757f5c7e3381b749f9bc3d7073b8ea187ec..e09659a0210697dc9171dc17e335d4fe3922ca3e 100644 (file)
@@ -52,11 +52,14 @@ class db_pager {
        
        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%";
@@ -157,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 (!(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;
@@ -217,7 +224,7 @@ class db_pager {
                                        $h = '';
                                        $c = $coldef;
                                } else {
-                                       $h = $coldef;
+                                       $h = $coldef;
                                        $c = 'text';
                                }
                        }
@@ -228,11 +235,13 @@ class db_pager {
                          $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;  
                }
@@ -247,7 +256,7 @@ 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)) {
@@ -372,6 +381,44 @@ class db_pager {
                $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.
@@ -399,20 +446,22 @@ class db_pager {
 //  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.
index ce3ae3ddf6cc2ad184829e339e752a60df747db6..4bc49948dc312bae82ff99a40624a22f793bd908 100644 (file)
@@ -33,6 +33,8 @@ function navi_button($name, $value, $enabled=true, $icon = false) {
 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.
@@ -46,8 +48,9 @@ function display_db_pager(&$pager) {
     $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' : 
@@ -59,7 +62,7 @@ function display_db_pager(&$pager) {
        }
     /* 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}'");
@@ -98,7 +101,6 @@ function display_db_pager(&$pager) {
                        } else
                                $cell = '';
                   }
-
                   switch($coltype) { // format column
                    case 'time':
                          label_cell($cell, "width=40"); break;
@@ -121,8 +123,14 @@ function display_db_pager(&$pager) {
                                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']))
@@ -154,12 +162,16 @@ function display_db_pager(&$pager) {
 
        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');
@@ -173,10 +185,12 @@ function display_db_pager(&$pager) {
                  $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();