X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=includes%2Fui%2Fsimple_crud_class.inc;h=46b9fbfc3e040d5ac152dcee8c1f88013cf6a0c0;hb=66a62190f99d83f958bb98195b5756b8b307e378;hp=1720516b71df58f327eb678a4bb616a3a4693c26;hpb=d2a8ca44be9bac1e147ae2a9de0db693ce0fcb08;p=fa-stable.git diff --git a/includes/ui/simple_crud_class.inc b/includes/ui/simple_crud_class.inc index 1720516b..46b9fbfc 100644 --- a/includes/ui/simple_crud_class.inc +++ b/includes/ui/simple_crud_class.inc @@ -15,101 +15,158 @@ class simple_crud { var $name; var $Mode; - var $selected_id = ''; - var $handlers; - var $selector; + var $selected_id; + 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 = array(); + var $fields; + var $tool_buttons; + var $options; + var $dec; // // - function simple_crud($name, $options = array()) + function simple_crud($name, $fields = null) { $this->name = $name; - $this->handlers = array( + $this->pre_handlers = array( 'Edit' => '_edit', 'Delete' => '_delete', + 'NEW' => '_edit', 'ADD' => '_add', 'UPDATE' => '_update', 'RESET' => '_cancel', 'CLONE' => '_cloning' ); - // selector type: 'button' (default), or 'list' - $this->selector = @$options['selector']; // TODO - + $this->views = array( + '' => 'list_view', // default view + 'Edit' => 'editor_view', + 'Delete' => 'list_view', + 'NEW' => 'editor_view', + 'ADD' => 'editor_view', + 'UPDATE' => 'editor_view', + 'RESET' => 'list_view', + 'CLONE' => 'editor_view' + ); + $this->tool_buttons['Edit'] = + array(_('Edit'), _('Edit document line'), ICON_EDIT, ''); + $this->tool_buttons['Delete'] = + array(_('Delete'), _('Remove line from document'), ICON_DELETE, ''); + $this->tool_buttons['UPDATE'] = + array(_('Update'), _('Confirm changes'), ICON_UPDATE, ''); + $this->tool_buttons['RESET'] = + array(_('Cancel'), _('Cancel changes'), ICON_CANCEL, ''); + + $this->fields = $fields; + $this->selected_id = $this->_none; + } + /* + Input/output formatters - convert values between php/user domains. + */ + function _format_input($value, $fmt) + { + switch($fmt) { + case 'stock': + $this->dec = get_qty_dec($value); + return $value; + case 'price': + case 'qty': + case 'number': + return user_numeric($value); + case 'percent': + return user_numeric($value)/100; + case 'text': + case 'date': + default: + return $value; + } } - function _check_mode($numeric_id = true) + function _format_output($value, $fmt) + { + switch($fmt) { + case 'price': + return price_format($value); + case 'qty': + return number_format2($value, $this->dec); + case 'number': + return number_format2($value); + case 'percent': + return percent_format($value*100); + case 'stock': + $this->dec = get_qty_dec($value); // retrieve dec for use in later qty fields + case 'text': + case 'date': + default: + return $value; + } + } + + function _check_mode() { 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) { + // 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'); -// $Ajax->activate('_page_body'); -// if ($m == 'RESET' || $m == 'CLONE') -// $this->selected_id = $default; - unset($_POST['_focus']); - $this->Mode = $m; - return; + $val = @key($_POST[$this->name.$m]); + $this->selected_id = $val!==null ? @quoted_printable_decode($val) : $this->_none; + return $m; } } + $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 ''; } - + // // Set record for edition // - function _edit() + function _edit($mode) { - - if ($this->selected_id !== '0') { - $this->data = $this->db_read(); -/* foreach($this->data as $name => $value) { - if (!is_numeric($name)) { - $_POST[$name] = $value; - } + if ($this->Mode != $mode) { + if ($this->selected_id != $this->_none) { + $this->data = $this->db_read(); } -*/ } + $this->set_posts($this->data); + } + $this->Mode = $mode; } // // Update record after edition // - function _update() + function _update($mode) { - if ($this->insert_check()) { - if ($this->db_update()) - $this->selected_id = ''; + $this->get_posts(); + if ($this->update_check()) { + 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->selected_id = ''; + $this->_cancel(); + return; } + $this->Mode = $mode; } // // Delete selected record @@ -118,28 +175,32 @@ class simple_crud { { if ($this->delete_check()) $this->db_delete(); - $this->selected_id = ''; + $this->_cancel(); } // // Return to listing view // function _cancel() { - $this->selected_id = ''; + $this->selected_id = $this->_none; + $this->db_cancel(); + $this->Mode = ''; } // // Clone record for new edition // function _cloning() { - $this->selected_id = '0'; + $this->Mode = ''; + $this->_edit('Edit'); + $this->selected_id = $this->_none; } /* Generate form controls */ function _bottom_controls() { - $clone = $this->selected_id == '0'; + $clone = $this->selected_id != $this->_none; $title=false; $async='both'; @@ -156,15 +217,16 @@ class simple_crud { $async = true; echo "