Merged changes from stable branch up to 2.3.23.
[fa-stable.git] / includes / ui / simple_crud_class.inc
1 <?php
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 ***********************************************************************/
12 //
13 //      Template for simple table editors
14 //
15 class simple_crud {
16         var $name;
17         var $Mode;
18         var $selected_id;
19         var $_none = ''; // selector value when no item is selected
20         var $pre_handlers; // control buttons and related methods called before view display
21         var $views;
22         var $data = array();
23         var $fields;
24         var $tool_buttons;
25         var $options;
26         var $dec;
27         //
28         //
29         function simple_crud($name, $fields = null)
30         {
31                 $this->name = $name;
32                 $this->pre_handlers = array(
33                         'Edit' => '_edit',
34                         'Delete' => '_delete',
35                         'NEW' => '_edit',
36                         'ADD' => '_add',
37                         'UPDATE' => '_update',
38                         'RESET' => '_cancel',
39                         'CLONE' => '_cloning'
40                         );
41                 $this->views = array(
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'
50                         );
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, '');
59
60                 $this->fields = $fields;
61                 $this->selected_id = $this->_none;
62         }
63         /*
64                 Input/output formatters - convert values between php/user domains.
65         */
66         function _format_input($value, $fmt)
67         {
68                 switch($fmt) {
69                         case 'stock':
70                                 $this->dec = get_qty_dec($value);
71                                 return $value;
72                         case 'price':
73                         case 'qty':
74                         case 'number':
75                                     return user_numeric($value);
76                         case 'percent':
77                                         return user_numeric($value)/100;
78                         case 'text':
79                         case 'date':
80                         default:
81                                 return $value;
82                 }
83         }
84         
85         function _format_output($value, $fmt)
86         {
87                 switch($fmt) {
88                         case 'price':
89                                 return price_format($value);
90                         case 'qty':
91                                 return number_format2($value, $this->dec);
92                         case 'number':
93                                 return number_format2($value);
94                         case 'percent':
95                                 return percent_format($value*100);
96                         case 'stock':
97                                 $this->dec = get_qty_dec($value); // retrieve dec for use in later qty fields
98                         case 'text':
99                         case 'date':
100                         default:
101                                 return $value;
102                 }
103         }
104
105         function _check_mode()
106         {
107                 global $Ajax;
108
109                 $sel_name = $this->name.'_id';
110
111                 // list controls lookup
112                 foreach (array_keys($this->pre_handlers) as $m) {
113                         if (isset($_POST[$this->name.$m])) {
114                                 unset($_POST['_focus']); // focus on first form entry
115                                 $Ajax->activate($this->name.'_div');
116                                 $val = @key($_POST[$this->name.$m]);
117                                 $this->selected_id = $val!==null ? @quoted_printable_decode($val) : $this->_none;
118                                 return $m;
119                         }
120                 }
121                 $mod = get_post($this->name.'Mode', '');
122                 if ($mod) {
123                         $val = @key($mod);
124                         $this->selected_id = $val!==null ? @quoted_printable_decode($val) : $this->_none;
125                         return $mod[$val];
126                 }
127                 return '';
128         }
129
130         //
131         //      Set record for edition
132         //
133         function _edit($mode)
134         {
135                 if ($this->Mode != $mode) {
136                         if ($this->selected_id != $this->_none) {
137                                 $this->data = $this->db_read();
138                         }
139                         $this->set_posts($this->data);
140                 }
141                 $this->Mode = $mode;
142         }
143         //
144         //      Update record after edition
145         //
146         function _update($mode)
147         {
148                 $this->get_posts();
149                 if ($this->update_check()) {
150                         if ($this->db_update()) {
151                                 $this->selected_id = $this->_none;
152                                 $this->Mode = '';
153                                 return;
154                         }
155                 }
156                 $this->Mode = $mode;
157         }
158         //
159         //      Add new record
160         //
161         function _add($mode)
162         {
163                 $this->get_posts();
164                 if ($this->insert_check()) {
165                         $this->db_insert();
166                         $this->_cancel();
167                         return;
168                 }
169                 $this->Mode = $mode;
170         }
171         //
172         //      Delete selected  record
173         //
174         function _delete()
175         {
176                 if ($this->delete_check())
177                         $this->db_delete();
178                 $this->_cancel();
179         }
180         //
181         //      Return to listing view
182         //
183         function _cancel()
184         {
185                 $this->selected_id = $this->_none;
186                 $this->db_cancel();
187                 $this->Mode = '';
188         }
189         //
190         // Clone record for new edition
191         //
192         function _cloning()
193         {
194                 $this->Mode = '';
195                 $this->_edit('Edit');
196                 $this->selected_id = $this->_none;
197         }
198         /*
199                 Generate form controls
200         */
201         function _bottom_controls()
202         {
203                 $clone = $this->selected_id != $this->_none;
204
205                 $title=false;
206                 $async='both';
207                 $base=$this->name;
208
209                 $cancel = $async;
210
211                 if ($async === 'both') {
212                         $async = 'default'; $cancel = 'cancel';
213                 } 
214                 else if ($async === 'default')
215                         $cancel = true;
216                 else if ($async === 'cancel')
217                         $async = true;
218                 echo "<center>";
219
220                 if ($this->Mode == '' || $this->Mode == 'RESET')
221                         submit("{$base}NEW", _("Add new"), true, $title, $async);
222                 else {
223                         if ($this->Mode == 'NEW' || $this->selected_id==$this->_none)
224                                 
225                                 submit("{$base}ADD", _("Add"), true, $title, $async);
226                         else {
227                                 submit("{$base}UPDATE[{$this->selected_id}]", _("Update"), true, _('Submit changes'), $async);
228                                 if ($clone) 
229                                         submit("{$base}CLONE[{$this->selected_id}]", _("Clone"), true, _('Edit new record with current data'), $async);
230                         }
231                         submit("{$base}RESET", _("Cancel"), true, _('Cancel edition'), $cancel);
232                 }
233                 echo "</center>";
234         }
235         //===========================================================================
236         // Public functions
237         //
238         
239         function tool_button($name, $selected_id=null, $params='')
240         {
241                 $b = $this->tool_buttons[$name];
242
243                 return "<td align='center' $params>"
244                         .button( "{$this->name}$name"
245                                 .($selected_id === null || $selected_id === $this->_none ? '': "[$selected_id]"),
246                                 $b[0], $b[1], $b[2], $b[3])."</td>";
247         }
248         
249         function set_posts()
250         {
251                 foreach($this->fields as $name => $fmt) {
252                         if (is_int($name)) {
253                                 $name = $fmt;
254                                 $fmt = array();
255                         }
256                         $post = isset($fmt['post']) ? $fmt['post'] : $name;
257                         $fld = isset($fmt['fld']) ? $fmt['fld'] : $name;
258
259                         $value = $this->selected_id == $this->_none ? @$fmt['dflt'] :
260                                 (is_array($this->data) ? $this->data[$fld]: $this->data->$fld);
261
262                         $_POST[$post] = $this->_format_output($value, @$fmt['fmt']);
263                 }
264         }
265         //--------------------------
266         //
267         //      Get editor POST variables. 
268         //
269         function get_posts() {
270                 foreach ($this->fields as $name => $fmt) {
271                         if (is_int($name)) {
272                                 $name = $fmt;
273                                 $fmt = array();
274                         }
275                         $post = isset($fmt['post']) ? $fmt['post'] : $name;
276                         $fld = isset($fmt['fld']) ? $fmt['fld'] : $name;
277
278                         $value = $this->_format_input(@$_POST[$post], @$fmt['fmt']);
279                         if (is_array($this->data))
280                                 $this->data[$fld] = $value;
281                         else
282                                 $this->data->$fld = $value;
283                 }
284         }
285         //      Main function - display current CRUD editor content
286         //
287         function show()
288         {
289                 if (!isset($_POST[$this->name.'Mode']))
290                         $this->set_posts();
291
292                 $Mode = $this->_check_mode(true);
293                 div_start($this->name.'_div');
294
295                 if (array_key_exists($Mode, $this->pre_handlers)) {
296                         $fun = $this->pre_handlers[$Mode];
297                         $this->$fun($Mode);
298                 }
299
300                 if (isset($this->views[$this->Mode]))
301                         $this->{$this->views[$this->Mode]}();
302                 else
303                         $this->{$this->views['']}(); // default view
304
305                 $this->_bottom_controls();
306                 // this is needed only when we use temporary crud object together with ajax screen updates
307                 hidden($this->name.'Mode'.'['.$this->selected_id.']', $this->Mode);
308                 div_end();
309         }
310         
311         //===========================================================================
312         //      Database functions placeholders
313         
314         //
315         //      Read record from db for edition
316         //
317         function db_read() {
318                 display_notification(__FUNCTION__. ' is not defined...');
319                 return array();
320         }
321         //
322         //      Update record in db after edition
323         //
324         function db_update()
325         {
326                 $this->db_insert();
327         }
328         //
329         //      Delete record
330         //
331         function db_delete() {
332                 display_notification(__FUNCTION__. ' is not defined...');
333         }
334         //
335         //      Insert record
336         //
337         function db_insert()
338         {
339                 display_notification(__FUNCTION__. ' is not defined...');
340         }
341         //
342         //      Cancel edition
343         //      Optional things like focus set.
344         //
345         function db_cancel()
346         {
347         }
348
349
350         function delete_check() {
351                 display_notification(__FUNCTION__. ' is not defined...');
352                 return true; 
353         }
354
355         function insert_check() {
356                 return true;
357         }
358
359         function update_check() 
360         {
361                 return $this->insert_check(); 
362         }
363
364         //
365         //      Show database content in pager/table
366         //
367         function list_view() {
368                 display_notification(__FUNCTION__. ' is not defined...');
369         }
370
371         //
372         //      Show record editor screen content
373         //
374         function editor_view() {
375                 display_notification(__FUNCTION__. ' is not defined...');
376         }
377 };
378