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
19 var $_none = ''; // selector value when no item is selected
20 var $pre_handlers; // control buttons and related methods called before view display
29 function __construct($name, $fields = null)
32 $this->pre_handlers = array(
34 'Delete' => '_delete',
37 'UPDATE' => '_update',
42 '' => 'list_view', // default view
43 'Edit' => 'editor_view',
44 'Delete' => 'list_view',
45 'NEW' => 'editor_view',
46 'ADD' => 'editor_view',
47 'UPDATE' => 'editor_view',
48 'RESET' => 'list_view',
49 'CLONE' => 'editor_view'
51 $this->tool_buttons['Edit'] =
52 array(_('Edit'), _('Edit document line'), ICON_EDIT, '');
53 $this->tool_buttons['Delete'] =
54 array(_('Delete'), _('Remove line from document'), ICON_DELETE, '');
55 $this->tool_buttons['UPDATE'] =
56 array(_('Update'), _('Confirm changes'), ICON_UPDATE, '');
57 $this->tool_buttons['RESET'] =
58 array(_('Cancel'), _('Cancel changes'), ICON_CANCEL, '');
60 $this->fields = $fields;
61 $this->selected_id = $this->_none;
64 Input/output formatters - convert values between php/user domains.
66 function _format_input($value, $fmt)
70 $this->dec = get_qty_dec($value);
75 return user_numeric($value);
77 return user_numeric($value)/100;
85 function _format_output($value, $fmt)
89 return price_format($value);
91 return number_format2($value, $this->dec);
93 return number_format2($value);
95 return percent_format($value*100);
97 $this->dec = get_qty_dec($value); // retrieve dec for use in later qty fields
105 function _check_mode()
109 // list controls lookup
110 foreach (array_keys($this->pre_handlers) as $m) {
111 if (isset($_POST[$this->name.$m])) {
112 unset($_POST['_focus']); // focus on first form entry
113 $Ajax->activate($this->name.'_div');
114 $val = @key($_POST[$this->name.$m]);
115 $this->selected_id = $val!==null ? @quoted_printable_decode($val) : $this->_none;
119 $mod = get_post($this->name.'Mode', '');
122 $this->selected_id = $val!==null ? @quoted_printable_decode($val) : $this->_none;
129 // Set record for edition
131 function _edit($mode)
133 if ($this->Mode != $mode) {
134 if ($this->selected_id != $this->_none) {
135 $this->data = $this->db_read();
137 $this->set_posts($this->data);
142 // Update record after edition
144 function _update($mode)
147 if ($this->update_check()) {
148 if ($this->db_update()) {
149 $this->selected_id = $this->_none;
162 if ($this->insert_check()) {
170 // Delete selected record
174 if ($this->delete_check())
179 // Return to listing view
183 $this->selected_id = $this->_none;
188 // Clone record for new edition
193 $this->_edit('Edit');
194 $this->selected_id = $this->_none;
197 Generate form controls
199 function _bottom_controls()
201 $clone = $this->selected_id != $this->_none;
209 if ($async === 'both') {
210 $async = 'default'; $cancel = 'cancel';
212 elseif ($async === 'default')
214 elseif ($async === 'cancel')
218 if ($this->Mode == '' || $this->Mode == 'RESET')
219 submit("{$base}NEW", _("Add new"), true, $title, $async);
221 if ($this->Mode == 'NEW' || $this->selected_id==$this->_none)
223 submit("{$base}ADD", _("Add"), true, $title, $async);
225 submit("{$base}UPDATE[{$this->selected_id}]", _("Update"), true, _('Submit changes'), $async);
227 submit("{$base}CLONE[{$this->selected_id}]", _("Clone"), true, _('Edit new record with current data'), $async);
229 submit("{$base}RESET", _("Cancel"), true, _('Cancel edition'), $cancel);
233 //===========================================================================
237 function tool_button($name, $selected_id=null, $params='')
239 $b = $this->tool_buttons[$name];
241 return "<td align='center' $params>"
242 .button( "{$this->name}$name"
243 .($selected_id === null || $selected_id === $this->_none ? '': "[$selected_id]"),
244 $b[0], $b[1], $b[2], $b[3])."</td>";
249 foreach($this->fields as $name => $fmt) {
254 $post = isset($fmt['post']) ? $fmt['post'] : $name;
255 $fld = isset($fmt['fld']) ? $fmt['fld'] : $name;
257 $value = $this->selected_id == $this->_none ? @$fmt['dflt'] :
258 (is_array($this->data) ? $this->data[$fld]: $this->data->$fld);
260 $_POST[$post] = $this->_format_output($value, @$fmt['fmt']);
263 //--------------------------
265 // Get editor POST variables.
267 function get_posts() {
268 foreach ($this->fields as $name => $fmt) {
273 $post = isset($fmt['post']) ? $fmt['post'] : $name;
274 $fld = isset($fmt['fld']) ? $fmt['fld'] : $name;
276 $value = $this->_format_input(@$_POST[$post], @$fmt['fmt']);
277 if (is_array($this->data))
278 $this->data[$fld] = $value;
280 $this->data->$fld = $value;
283 // Main function - display current CRUD editor content
287 if (!isset($_POST[$this->name.'Mode']))
290 $Mode = $this->_check_mode(true);
291 div_start($this->name.'_div');
293 if (array_key_exists($Mode, $this->pre_handlers)) {
294 $fun = $this->pre_handlers[$Mode];
298 if (isset($this->views[$this->Mode]))
299 $this->{$this->views[$this->Mode]}();
301 $this->{$this->views['']}(); // default view
303 $this->_bottom_controls();
304 // this is needed only when we use temporary crud object together with ajax screen updates
305 hidden($this->name.'Mode'.'['.$this->selected_id.']', $this->Mode);
309 //===========================================================================
310 // Database functions placeholders
313 // Read record from db for edition
316 display_notification(__FUNCTION__. ' is not defined...');
320 // Update record in db after edition
329 function db_delete() {
330 display_notification(__FUNCTION__. ' is not defined...');
337 display_notification(__FUNCTION__. ' is not defined...');
341 // Optional things like focus set.
348 function delete_check() {
349 display_notification(__FUNCTION__. ' is not defined...');
353 function insert_check() {
357 function update_check()
359 return $this->insert_check();
363 // Show database content in pager/table
365 function list_view() {
366 display_notification(__FUNCTION__. ' is not defined...');
370 // Show record editor screen content
372 function editor_view() {
373 display_notification(__FUNCTION__. ' is not defined...');