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;
var $dec;
//
//
- function simple_crud($name, $fields = null)
+ function __construct($name, $fields = null)
{
$this->name = $name;
$this->pre_handlers = array(
{
global $Ajax;
- $sel_name = $this->name.'_id';
-
// list controls lookup
foreach (array_keys($this->pre_handlers) as $m) {
if (isset($_POST[$this->name.$m])) {
unset($_POST['_focus']); // focus on first form entry
$Ajax->activate($this->name.'_div');
- $val = @key($_POST[$this->name.$m]);
+ $val = is_array($_POST[$this->name.$m]) ? key($_POST[$this->name.$m]) : null;
$this->selected_id = $val!==null ? @quoted_printable_decode($val) : $this->_none;
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 '';
}
//
//
// 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
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>";
$name = $fmt;
$fmt = array();
}
+ if (is_string($fmt)) {
+ $fmt = array('fmt' => $fmt);
+ }
$post = isset($fmt['post']) ? $fmt['post'] : $name;
$fld = isset($fmt['fld']) ? $fmt['fld'] : $name;
- $value = $this->selected_id == $this->_none ? @$fmt['dflt'] :
+ $value = $this->selected_id == $this->_none ? (isset($fmt['dflt']) ? $fmt['dflt'] : null) :
(is_array($this->data) ? $this->data[$fld]: $this->data->$fld);
- $_POST[$post] = $this->_format_output($value, @$fmt['fmt']);
+ $_POST[$post] = $this->_format_output($value, isset($fmt['fmt']) ? $fmt['fmt'] : null);
}
}
//--------------------------
$name = $fmt;
$fmt = array();
}
+ if (is_string($fmt)) {
+ $fmt = array('fmt' => $fmt);
+ }
$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
$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();
}
}
};
-?>
\ No newline at end of file