Cleanup comments. Small bugfixes. Second run
[fa-stable.git] / includes / ui / simple_crud_class.inc
index 2f0d76711a63253d2d8e28ba8b7d570fe35e1c30..05d9280135e9e57a67509edc11475da0a0d5b5bc 100644 (file)
@@ -19,7 +19,7 @@ class simple_crud {
        var $_none = ''; // selector value when no item is selected
        var $pre_handlers; // control buttons and related methods called before view display
        var $views;
-       var $data;
+       var $data = array();
        var $fields;
        var $tool_buttons;
        var $options;
@@ -106,8 +106,6 @@ class simple_crud {
        {
                global $Ajax;
 
-               $sel_name = $this->name.'_id';
-
                // list controls lookup
                foreach (array_keys($this->pre_handlers) as $m) {
                        if (isset($_POST[$this->name.$m])) {
@@ -118,7 +116,13 @@ class simple_crud {
                                return $m;
                        }
                }
-               return get_post($this->name.'Mode', '');
+               $mod = get_post($this->name.'Mode', '');
+               if ($mod) {
+                       $val = @key($mod);
+                       $this->selected_id = $val!==null ? @quoted_printable_decode($val) : $this->_none;
+                       return $mod[$val];
+               }
+               return '';
        }
 
        //
@@ -137,25 +141,30 @@ class simple_crud {
        //
        //      Update record after edition
        //
-       function _update()
+       function _update($mode)
        {
                $this->get_posts();
                if ($this->update_check()) {
-                       if ($this->db_update())
+                       if ($this->db_update()) {
                                $this->selected_id = $this->_none;
                                $this->Mode = '';
+                               return;
+                       }
                }
+               $this->Mode = $mode;
        }
        //
        //      Add new record
        //
-       function _add()
+       function _add($mode)
        {
                $this->get_posts();
                if ($this->insert_check()) {
                        $this->db_insert();
                        $this->_cancel();
+                       return;
                }
+               $this->Mode = $mode;
        }
        //
        //      Delete selected  record
@@ -200,9 +209,9 @@ class simple_crud {
                if ($async === 'both') {
                        $async = 'default'; $cancel = 'cancel';
                } 
-               else if ($async === 'default')
+               elseif ($async === 'default')
                        $cancel = true;
-               else if ($async === 'cancel')
+               elseif ($async === 'cancel')
                        $async = true;
                echo "<center>";
 
@@ -264,8 +273,7 @@ class simple_crud {
                        $post = isset($fmt['post']) ? $fmt['post'] : $name;
                        $fld = isset($fmt['fld']) ? $fmt['fld'] : $name;
 
-                       $value = $this->_format_input($_POST[$post], @$fmt['fmt']);
-
+                       $value = $this->_format_input(@$_POST[$post], @$fmt['fmt']);
                        if (is_array($this->data))
                                $this->data[$fld] = $value;
                        else
@@ -286,14 +294,15 @@ class simple_crud {
                        $fun = $this->pre_handlers[$Mode];
                        $this->$fun($Mode);
                }
-               
+
                if (isset($this->views[$this->Mode]))
                        $this->{$this->views[$this->Mode]}();
                else
                        $this->{$this->views['']}(); // default view
 
                $this->_bottom_controls();
-               hidden($this->name.'Mode', $this->Mode.'['.$this->selected_id.']');
+               // this is needed only when we use temporary crud object together with ajax screen updates
+               hidden($this->name.'Mode'.'['.$this->selected_id.']', $this->Mode);
                div_end();
        }
        
@@ -365,4 +374,3 @@ class simple_crud {
        }
 };
 
-?>
\ No newline at end of file