From d2a8ca44be9bac1e147ae2a9de0db693ce0fcb08 Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Tue, 11 May 2010 11:45:35 +0000 Subject: [PATCH] Simple abstract CRUD class --- includes/ui/simple_crud_class.inc | 243 ++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 includes/ui/simple_crud_class.inc diff --git a/includes/ui/simple_crud_class.inc b/includes/ui/simple_crud_class.inc new file mode 100644 index 00000000..1720516b --- /dev/null +++ b/includes/ui/simple_crud_class.inc @@ -0,0 +1,243 @@ +. +***********************************************************************/ +// +// Template for simple table editors +// +class simple_crud { + var $name; + var $Mode; + var $selected_id = ''; + var $handlers; + var $selector; + // + // + function simple_crud($name, $options = array()) + { + $this->name = $name; + $this->handlers = array( + 'Edit' => '_edit', + 'Delete' => '_delete', + 'ADD' => '_add', + 'UPDATE' => '_update', + 'RESET' => '_cancel', + 'CLONE' => '_cloning' + ); + // selector type: 'button' (default), or 'list' + $this->selector = @$options['selector']; // TODO + + } + + function _check_mode($numeric_id = true) + { + global $Ajax; + + $sel_name = $this->name.'_id'; + + $this->Mode = 'Edit'; + foreach (array('Edit', 'Delete') as $m) { + foreach ($_POST as $p => $pvar) { + if (strpos($p, $this->name.$m) === 0) { + unset($_POST['_focus']); // focus on first form entry + $this->selected_id = quoted_printable_decode(substr($p, strlen($this->name.$m))); + $Ajax->activate($this->name.'_div'); +// $Ajax->activate('_page_body'); + $this->Mode = $m; + return; + } + } + } + $default = ''; //$numeric_id ? -1 : ''; + $this->selected_id = get_post($sel_name, $default); + if ($this->selected_id === $default) { +// $this->selected_id = @$_POST[$sel_name]; +// if (!isset($this->selected_id)) { + $this->Mode = ''; + return; + } + foreach (array('ADD', 'UPDATE', 'RESET', 'CLONE') as $m) { + if (isset($_POST[$this->name.$m])) { + $Ajax->activate($this->name.'_div'); +// $Ajax->activate('_page_body'); +// if ($m == 'RESET' || $m == 'CLONE') +// $this->selected_id = $default; + unset($_POST['_focus']); + $this->Mode = $m; + return; + } + } + } + + // + // Set record for edition + // + function _edit() + { + + if ($this->selected_id !== '0') { + $this->data = $this->db_read(); +/* foreach($this->data as $name => $value) { + if (!is_numeric($name)) { + $_POST[$name] = $value; + } + } +*/ } + } + // + // Update record after edition + // + function _update() + { + if ($this->insert_check()) { + if ($this->db_update()) + $this->selected_id = ''; + } + } + // + // Add new record + // + function _add() + { + if ($this->insert_check()) { + $this->db_insert(); + $this->selected_id = ''; + } + } + // + // Delete selected record + // + function _delete() + { + if ($this->delete_check()) + $this->db_delete(); + $this->selected_id = ''; + } + // + // Return to listing view + // + function _cancel() + { + $this->selected_id = ''; + } + // + // Clone record for new edition + // + function _cloning() + { + $this->selected_id = '0'; + } + /* + Generate form controls + */ + function _bottom_controls() + { + $clone = $this->selected_id == '0'; + + $title=false; + $async='both'; + $base=$this->name; + + $cancel = $async; + + if ($async === 'both') { + $async = 'default'; $cancel = 'cancel'; + } + else if ($async === 'default') + $cancel = true; + else if ($async === 'cancel') + $async = true; + echo "
"; + + if ($this->selected_id == '') + submit("{$base}Edit0", _("Add new"), true, $title, $async); + else { + if ($this->selected_id == '0') + submit("{$base}ADD", _("Add"), true, $title, $async); + else { + submit("{$base}UPDATE", _("Update"), true, _('Submit changes'), $async); + if ($clone) + submit("${base}CLONE", _("Clone"), true, _('Edit new record with current data'), $async); + } + submit("{$base}RESET", _("Cancel"), true, _('Cancel edition'), $cancel); + } + echo "
"; + } + //=========================================================================== + // Public functions + // + + // Main function - display current CRUD editor content + // + function show() + { + $this->_check_mode(true); + div_start($this->name.'_div'); + if (array_key_exists($this->Mode, $this->handlers)) { + $fun = $this->handlers[$this->Mode]; + $this->$fun(); + } + if ($this->selected_id != '') + $this->editor(); + else + $this->listing(); + $this->_bottom_controls(); + hidden($this->name.'_id', $this->selected_id); + div_end(); + } + + //=========================================================================== + // child class provided sql functions + + // + // Read record from db for edition + // + function db_read() { + display_notification('read sql...'); + } + // + // Update record in db after edition + // + function db_update() { + display_notification('update sql...'); + } + // + // Delete record + // + function db_delete() { + display_notification('delete sql...'); + } + + function delete_check() { + display_notification('check delete ...'); + return true; + } + // + // Insert record + // + function db_insert() {} + + function insert_check() { return true; } + // + // Show database content in pager/table + // + function listing() { + display_notification('listing ...'); + } + + // + // Show record editor screen content + // + function editor() { + display_notification('editor ...'); + } +}; + +?> \ No newline at end of file -- 2.30.2