ef19ca83c394b8e026d1c2db4d9cce8ea7308e1e
[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                                         $this->Mode = $m;
53                                         return;
54                                 }
55                         }
56                 }
57                 $default = '';
58                 $this->selected_id = get_post($sel_name, $default);
59                 if ($this->selected_id === $default) {
60                         $this->Mode = '';
61                         return;
62                 }
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']);
67                                 $this->Mode = $m;
68                                 return;
69                         }
70                 }
71         }
72         
73         //
74         //      Set record for edition
75         //
76         function _edit()
77         {
78
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;
84                                 }
85                         }
86 */              }
87         }
88         //
89         //      Update record after edition
90         //
91         function _update()
92         {
93                 if ($this->insert_check()) {
94                         if ($this->db_update())
95                                 $this->selected_id = '';
96                 }
97         }
98         //
99         //      Add new record
100         //
101         function _add()
102         {
103                 if ($this->insert_check()) {
104                         $this->db_insert();
105                         $this->selected_id = '';
106                 }
107         }
108         //
109         //      Delete selected  record
110         //
111         function _delete()
112         {
113                 if ($this->delete_check())
114                         $this->db_delete();
115                 $this->selected_id = '';
116         }
117         //
118         //      Return to listing view
119         //
120         function _cancel()
121         {
122                 $this->selected_id = '';
123         }
124         //
125         // Clone record for new edition
126         //
127         function _cloning()
128         {
129                 $this->selected_id = '0';
130         }
131         /*
132                 Generate form controls
133         */
134         function _bottom_controls()
135         {
136                 $clone = $this->selected_id == '0';
137
138                 $title=false;
139                 $async='both';
140                 $base=$this->name;
141
142                 $cancel = $async;
143
144                 if ($async === 'both') {
145                         $async = 'default'; $cancel = 'cancel';
146                 } 
147                 else if ($async === 'default')
148                         $cancel = true;
149                 else if ($async === 'cancel')
150                         $async = true;
151                 echo "<center>";
152
153                 if ($this->selected_id == '')
154                         submit("{$base}Edit0", _("Add new"), true, $title, $async);
155                 else {
156                         if ($this->selected_id == '0')
157                                 submit("{$base}ADD", _("Add"), true, $title, $async);
158                         else {
159                                 submit("{$base}UPDATE", _("Update"), true, _('Submit changes'), $async);
160                                 if ($clone) 
161                                         submit("${base}CLONE", _("Clone"), true, _('Edit new record with current data'), $async);
162                         }
163                         submit("{$base}RESET", _("Cancel"), true, _('Cancel edition'), $cancel);
164                 }
165                 echo "</center>";
166         }
167         //===========================================================================
168         // Public functions
169         //
170         
171         //      Main function - display current CRUD editor content
172         //
173         function show()
174         {
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];
179                         $this->$fun();
180                 }
181                 if ($this->selected_id != '')
182                         $this->editor();
183                 else
184                         $this->listing();
185                 $this->_bottom_controls();
186                 hidden($this->name.'_id', $this->selected_id);
187                 div_end();
188         }
189         
190         //===========================================================================
191         //      Database functions placeholders
192         
193         //
194         //      Read record from db for edition
195         //
196         function db_read() {
197                 display_notification('read sql...');
198         }
199         //
200         //      Update record in db after edition
201         //
202         function db_update() {
203                 display_notification('update sql...');
204         }
205         //
206         //      Delete record
207         //
208         function db_delete() {
209                 display_notification('delete sql...');
210         }
211
212         function delete_check() {
213                 display_notification('check delete ...');
214                 return true; 
215         }
216         //
217         //      Insert record
218         //
219         function db_insert() {}
220
221         function insert_check() { return true; }
222         //
223         //      Show database content in pager/table
224         //
225         function listing() {
226                 display_notification('listing ...');
227         }
228
229         //
230         //      Show record editor screen content
231         //
232         function editor() {
233                 display_notification('editor ...');
234         }
235 };
236
237 ?>