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');
58 $this->selected_id = get_post($sel_name, $default);
59 if ($this->selected_id === $default) {
63 foreach (array('ADD', 'UPDATE', 'RESET', 'CLONE') as $m) {
64 if (isset($_POST[$this->name.$m])) {
65 $Ajax->activate($this->name.'_div');
66 unset($_POST['_focus']);
74 // Set record for edition
79 if ($this->selected_id !== '0') {
80 $this->data = $this->db_read();
81 /* foreach($this->data as $name => $value) {
82 if (!is_numeric($name)) {
83 $_POST[$name] = $value;
89 // Update record after edition
93 if ($this->insert_check()) {
94 if ($this->db_update())
95 $this->selected_id = '';
103 if ($this->insert_check()) {
105 $this->selected_id = '';
109 // Delete selected record
113 if ($this->delete_check())
115 $this->selected_id = '';
118 // Return to listing view
122 $this->selected_id = '';
125 // Clone record for new edition
129 $this->selected_id = '0';
132 Generate form controls
134 function _bottom_controls()
136 $clone = $this->selected_id == '0';
144 if ($async === 'both') {
145 $async = 'default'; $cancel = 'cancel';
147 else if ($async === 'default')
149 else if ($async === 'cancel')
153 if ($this->selected_id == '')
154 submit("{$base}Edit0", _("Add new"), true, $title, $async);
156 if ($this->selected_id == '0')
157 submit("{$base}ADD", _("Add"), true, $title, $async);
159 submit("{$base}UPDATE", _("Update"), true, _('Submit changes'), $async);
161 submit("${base}CLONE", _("Clone"), true, _('Edit new record with current data'), $async);
163 submit("{$base}RESET", _("Cancel"), true, _('Cancel edition'), $cancel);
167 //===========================================================================
171 // Main function - display current CRUD editor content
175 $this->_check_mode(true);
176 div_start($this->name.'_div');
177 if (array_key_exists($this->Mode, $this->handlers)) {
178 $fun = $this->handlers[$this->Mode];
181 if ($this->selected_id != '')
185 $this->_bottom_controls();
186 hidden($this->name.'_id', $this->selected_id);
190 //===========================================================================
191 // Database functions placeholders
194 // Read record from db for edition
197 display_notification('read sql...');
200 // Update record in db after edition
202 function db_update() {
203 display_notification('update sql...');
208 function db_delete() {
209 display_notification('delete sql...');
212 function delete_check() {
213 display_notification('check delete ...');
219 function db_insert() {}
221 function insert_check() { return true; }
223 // Show database content in pager/table
226 display_notification('listing ...');
230 // Show record editor screen content
233 display_notification('editor ...');