Simple abstract CRUD class
[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 $handlers;
20         var $selector;
21         //
22         //
23         function simple_crud($name, $options = array())
24         {
25                 $this->name = $name;
26                 $this->handlers = array(
27                         'Edit' => '_edit',
28                         'Delete' => '_delete',
29                         'ADD' => '_add',
30                         'UPDATE' => '_update',
31                         'RESET' => '_cancel',
32                         'CLONE' => '_cloning'
33                         );
34                 // selector type: 'button' (default),  or 'list'
35                 $this->selector = @$options['selector']; // TODO
36                 
37         }
38         
39         function _check_mode($numeric_id = true)
40         {
41                 global $Ajax;
42
43                 $sel_name = $this->name.'_id';
44
45                 $this->Mode = 'Edit';
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'); 
53                                         $this->Mode = $m;
54                                         return;
55                                 }
56                         }
57                 }
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)) {
63                         $this->Mode = '';
64                         return;
65                 }
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']);
73                                 $this->Mode = $m;
74                                 return;
75                         }
76                 }
77         }
78         
79         //
80         //      Set record for edition
81         //
82         function _edit()
83         {
84
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;
90                                 }
91                         }
92 */              }
93         }
94         //
95         //      Update record after edition
96         //
97         function _update()
98         {
99                 if ($this->insert_check()) {
100                         if ($this->db_update())
101                                 $this->selected_id = '';
102                 }
103         }
104         //
105         //      Add new record
106         //
107         function _add()
108         {
109                 if ($this->insert_check()) {
110                         $this->db_insert();
111                         $this->selected_id = '';
112                 }
113         }
114         //
115         //      Delete selected  record
116         //
117         function _delete()
118         {
119                 if ($this->delete_check())
120                         $this->db_delete();
121                 $this->selected_id = '';
122         }
123         //
124         //      Return to listing view
125         //
126         function _cancel()
127         {
128                 $this->selected_id = '';
129         }
130         //
131         // Clone record for new edition
132         //
133         function _cloning()
134         {
135                 $this->selected_id = '0';
136         }
137         /*
138                 Generate form controls
139         */
140         function _bottom_controls()
141         {
142                 $clone = $this->selected_id == '0';
143
144                 $title=false;
145                 $async='both';
146                 $base=$this->name;
147
148                 $cancel = $async;
149
150                 if ($async === 'both') {
151                         $async = 'default'; $cancel = 'cancel';
152                 } 
153                 else if ($async === 'default')
154                         $cancel = true;
155                 else if ($async === 'cancel')
156                         $async = true;
157                 echo "<center>";
158
159                 if ($this->selected_id == '')
160                         submit("{$base}Edit0", _("Add new"), true, $title, $async);
161                 else {
162                         if ($this->selected_id == '0')
163                                 submit("{$base}ADD", _("Add"), true, $title, $async);
164                         else {
165                                 submit("{$base}UPDATE", _("Update"), true, _('Submit changes'), $async);
166                                 if ($clone) 
167                                         submit("${base}CLONE", _("Clone"), true, _('Edit new record with current data'), $async);
168                         }
169                         submit("{$base}RESET", _("Cancel"), true, _('Cancel edition'), $cancel);
170                 }
171                 echo "</center>";
172         }
173         //===========================================================================
174         // Public functions
175         //
176         
177         //      Main function - display current CRUD editor content
178         //
179         function show()
180         {
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];
185                         $this->$fun();
186                 }
187                 if ($this->selected_id != '')
188                         $this->editor();
189                 else
190                         $this->listing();
191                 $this->_bottom_controls();
192                 hidden($this->name.'_id', $this->selected_id);
193                 div_end();
194         }
195         
196         //===========================================================================
197         //      child class provided sql functions
198         
199         //
200         //      Read record from db for edition
201         //
202         function db_read() {
203                 display_notification('read sql...');
204         }
205         //
206         //      Update record in db after edition
207         //
208         function db_update() {
209                 display_notification('update sql...');
210         }
211         //
212         //      Delete record
213         //
214         function db_delete() {
215                 display_notification('delete sql...');
216         }
217
218         function delete_check() {
219                 display_notification('check delete ...');
220                 return true; 
221         }
222         //
223         //      Insert record
224         //
225         function db_insert() {}
226
227         function insert_check() { return true; }
228         //
229         //      Show database content in pager/table
230         //
231         function listing() {
232                 display_notification('listing ...');
233         }
234
235         //
236         //      Show record editor screen content
237         //
238         function editor() {
239                 display_notification('editor ...');
240         }
241 };
242
243 ?>