2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 // Template for simple table editors
18 var $selected_id = '';
23 function simple_crud($name, $options = array())
26 $this->handlers = array(
28 'Delete' => '_delete',
30 'UPDATE' => '_update',
34 // selector type: 'button' (default), or 'list'
35 $this->selector = @$options['selector']; // TODO
39 function _check_mode($numeric_id = true)
43 $sel_name = $this->name.'_id';
46 foreach (array('Edit', 'Delete') as $m) {
47 foreach ($_POST as $p => $pvar) {
48 if (strpos($p, $this->name.$m) === 0) {
49 unset($_POST['_focus']); // focus on first form entry
50 $this->selected_id = quoted_printable_decode(substr($p, strlen($this->name.$m)));
51 $Ajax->activate($this->name.'_div');
52 // $Ajax->activate('_page_body');
58 $default = ''; //$numeric_id ? -1 : '';
59 $this->selected_id = get_post($sel_name, $default);
60 if ($this->selected_id === $default) {
61 // $this->selected_id = @$_POST[$sel_name];
62 // if (!isset($this->selected_id)) {
66 foreach (array('ADD', 'UPDATE', 'RESET', 'CLONE') as $m) {
67 if (isset($_POST[$this->name.$m])) {
68 $Ajax->activate($this->name.'_div');
69 // $Ajax->activate('_page_body');
70 // if ($m == 'RESET' || $m == 'CLONE')
71 // $this->selected_id = $default;
72 unset($_POST['_focus']);
80 // Set record for edition
85 if ($this->selected_id !== '0') {
86 $this->data = $this->db_read();
87 /* foreach($this->data as $name => $value) {
88 if (!is_numeric($name)) {
89 $_POST[$name] = $value;
95 // Update record after edition
99 if ($this->insert_check()) {
100 if ($this->db_update())
101 $this->selected_id = '';
109 if ($this->insert_check()) {
111 $this->selected_id = '';
115 // Delete selected record
119 if ($this->delete_check())
121 $this->selected_id = '';
124 // Return to listing view
128 $this->selected_id = '';
131 // Clone record for new edition
135 $this->selected_id = '0';
138 Generate form controls
140 function _bottom_controls()
142 $clone = $this->selected_id == '0';
150 if ($async === 'both') {
151 $async = 'default'; $cancel = 'cancel';
153 else if ($async === 'default')
155 else if ($async === 'cancel')
159 if ($this->selected_id == '')
160 submit("{$base}Edit0", _("Add new"), true, $title, $async);
162 if ($this->selected_id == '0')
163 submit("{$base}ADD", _("Add"), true, $title, $async);
165 submit("{$base}UPDATE", _("Update"), true, _('Submit changes'), $async);
167 submit("${base}CLONE", _("Clone"), true, _('Edit new record with current data'), $async);
169 submit("{$base}RESET", _("Cancel"), true, _('Cancel edition'), $cancel);
173 //===========================================================================
177 // Main function - display current CRUD editor content
181 $this->_check_mode(true);
182 div_start($this->name.'_div');
183 if (array_key_exists($this->Mode, $this->handlers)) {
184 $fun = $this->handlers[$this->Mode];
187 if ($this->selected_id != '')
191 $this->_bottom_controls();
192 hidden($this->name.'_id', $this->selected_id);
196 //===========================================================================
197 // child class provided sql functions
200 // Read record from db for edition
203 display_notification('read sql...');
206 // Update record in db after edition
208 function db_update() {
209 display_notification('update sql...');
214 function db_delete() {
215 display_notification('delete sql...');
218 function delete_check() {
219 display_notification('check delete ...');
225 function db_insert() {}
227 function insert_check() { return true; }
229 // Show database content in pager/table
232 display_notification('listing ...');
236 // Show record editor screen content
239 display_notification('editor ...');