ftimme
2005-12-21 1d3319e2b412c7f301146878700b9733cbc1bab0
commit | author | age
b5a2f8 1 <?php
T 2
3 /*
4 Copyright (c) 2005, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * Action framework for the tform library.
33 *
34 * @author Till Brehm <t.brehm@scrigo.org>
35 * @copyright Copyright &copy; 2005, Till Brehm
36 */
37
38 class tform_actions {
39
1645bb 40         var $id;
F 41         var $activeTab;
42         var $dataRecord;
43         var $plugins = array();
b5a2f8 44
1645bb 45         function onLoad() {
F 46                 global $app, $conf, $tform_def_file;
b5a2f8 47
1645bb 48                 // Loading template classes and initialize template
F 49                 if(!is_object($app->tpl)) $app->uses('tpl');
50                 if(!is_object($app->tform)) $app->uses('tform');
b5a2f8 51
1645bb 52                 $app->tpl->newTemplate("tabbed_form.tpl.htm");
b5a2f8 53
1645bb 54                 // Load table definition from file
F 55                 $app->tform->loadFormDef($tform_def_file);
b5a2f8 56
1645bb 57                 // loading plugins
F 58                 $next_tab = $app->tform->getNextTab();
59                 if(is_array($app->tform->formDef["tabs"][$next_tab]["plugins"])) {
60                         $app->load('plugin_base');
61                         foreach($app->tform->formDef["tabs"][$next_tab]["plugins"] as $plugin_name => $plugin_settings) {
62                                 $plugin_class = $plugin_settings["class"];
63                                 $app->load($plugin_class);
64                                 $this->plugins[$plugin_name] = new $plugin_class;
65                                 $this->plugins[$plugin_name]->setOptions($plugin_name,$plugin_settings['options']);
66                                 $this->plugins[$plugin_name]->onLoad();
67                         }
68                 }
b5a2f8 69
1645bb 70                 // Importing ID
F 71                 $this->id = intval($_REQUEST["id"]);
b5a2f8 72
1645bb 73                 if(count($_POST) > 1) {
F 74                         $this->dataRecord = $_POST;
75                         $this->onSubmit();
76                 } else {
77                         $this->onShow();
78                 }
79         }
b5a2f8 80
1645bb 81         /**
F 82         * Function called on page submit
83         */
b5a2f8 84
1645bb 85         function onSubmit() {
F 86                 global $app, $conf;
87
88                 // Calling the action functions
89                 if($this->id > 0) {
90                         $this->onUpdate();
91                 } else {
92                         $this->onInsert();
93                 }
94         }
95
96         /**
97         * Function called on data update
98         */
99
100         function onUpdate() {
101                 global $app, $conf;
102
103                 $ext_where = '';
104                 $sql = $app->tform->getSQL($this->dataRecord,$app->tform->getCurrentTab(),'UPDATE',$this->id,$ext_where);
105                 if($app->tform->errorMessage == '') {
106
107                         if(!empty($sql)) {
108                                 $app->db->query($sql);
109                                 if($app->db->errorMessage != '') die($app->db->errorMessage);
110                         }
111
112                         // Call plugin
113                         foreach($this->plugins as $plugin) {
114                                 $plugin->onInsert();
115                         }
1d3319 116
F 117                                                 $this->onAfterUpdate();
1645bb 118
F 119                         if($_REQUEST["next_tab"] == '') {
775e53 120                                 $list_name = $_SESSION["s"]["form"]["return_to"];
1d3319 121                                                                 if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
1645bb 122                                         $redirect = "Location: ".$_SESSION["s"]["list"][$list_name]["parent_script"]."?id=".$_SESSION["s"]["list"][$list_name]["parent_id"]."&next_tab=".$_SESSION["s"]["list"][$list_name]["parent_tab"];
F 123                                         $_SESSION["s"]["form"]["return_to"] = '';
124                                         session_write_close();
125                                         header($redirect);
126                                 } else {
127                                     header("Location: ".$app->tform->formDef['list_default']);
128                                 }
129                         exit;
130                     } else {
131                                 $this->onShow();
132                         }
133                 } else {
134                         $this->onError();
135                 }
136         }
137
138         /**
139         * Function called on data insert
140         */
141
142         function onInsert() {
143                 global $app, $conf;
144
145                 $ext_where = '';
146                 $sql = $app->tform->getSQL($this->dataRecord,$app->tform->getCurrentTab(),'INSERT',$this->id,$ext_where);
147                 if($app->tform->errorMessage == '') {
148                         $app->db->query($sql);
149                         if($app->db->errorMessage != '') die($app->db->errorMessage);
150                         $this->id = $app->db->insertID();
151
152                         // Call plugin
153                         foreach($this->plugins as $plugin) {
154                                 $plugin->onInsert();
155                         }
1d3319 156
F 157                                                 $this->onAfterInsert();
1645bb 158
F 159                         if($_REQUEST["next_tab"] == '') {
775e53 160                             $list_name = $_SESSION["s"]["form"]["return_to"];
1d3319 161                                                                 if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
1645bb 162                                         $redirect = "Location: ".$_SESSION["s"]["list"][$list_name]["parent_script"]."?id=".$_SESSION["s"]["list"][$list_name]["parent_id"]."&next_tab=".$_SESSION["s"]["list"][$list_name]["parent_tab"];
F 163                                         $_SESSION["s"]["form"]["return_to"] = '';
164                                         session_write_close();
165                                         header($redirect);
166                                 } else {
167                                     header("Location: ".$app->tform->formDef['list_default']);
168                                 }
169                         exit;
170                     } else {
171                                 $this->onShow();
172                         }
173                 } else {
174                         $this->onError();
175                 }
176         }
1d3319 177
F 178                 function onAfterUpdate() {
179                         global $app, $conf;
180                 }
181
182                 function onAfterInsert() {
183                         global $app, $conf;
184                 }
185
1645bb 186
F 187         /**
188         * Function called on data insert or update error
189         */
190
191         function onError() {
192                 global $app, $conf;
193
194                 $app->tpl->setVar("error","<b>".$app->lng('Error').":</b><br>".$app->tform->errorMessage);
195                 $app->tpl->setVar($this->dataRecord);
196                 $this->onShow();
197         }
198
199         /**
200         * Function called on data delete
201         */
202
203         function onDelete() {
204                 global $app, $conf,$list_def_file,$tform_def_file;
205
206                 include_once($list_def_file);
207
208                 // Loading tform framework
209                 if(!is_object($app->tform)) $app->uses('tform');
210
211                 // Load table definition from file
212                 $app->tform->loadFormDef($tform_def_file);
213
214                 // importing ID
215                 $this->id = intval($_REQUEST["id"]);
216
217                 if($this->id > 0) {
218
219                         // checking permissions
220                         if($app->tform->formDef['auth'] == 'yes') {
1d3319 221                                 if($app->tform->checkPerm($this->id,'d') == false) $app->error($app->lng('error_no_delete_permission'));
1645bb 222                         }
F 223
224                         $record_old = $app->db->queryOneRecord("SELECT * FROM ".$liste["table"]." WHERE ".$liste["table_idx"]." = ".$this->id);
225
226                         // Saving record to datalog when db_history enabled
227                         if($form["db_history"] == 'yes') {
228                                 $diffrec = array();
229
230                                 foreach($record_old as $key => $val) {
231                                         // Record has changed
232                                         $diffrec[$key] = array('old' => $val,
233                                                                                            'new' => '');
234                                 }
235
236                                 $diffstr = $app->db->quote(serialize($diffrec));
237                                 $username = $app->db->quote($_SESSION["s"]["user"]["username"]);
238                                 $dbidx = $app->tform->formDef['db_table_idx'].":".$this->id;
239                                 $sql = "INSERT INTO sys_datalog (dbtable,dbidx,action,tstamp,user,data) VALUES ('".$app->tform->formDef['db_table']."','$dbidx','d','".time()."','$username','$diffstr')";
240                                 $app->db->query($sql);
241                         }
242
243                         $app->db->query("DELETE FROM ".$liste["table"]." WHERE ".$liste["table_idx"]." = ".$this->id);
244
245                         // Call plugin
246                         foreach($this->plugins as $plugin) {
247                                 $plugin->onInsert();
248                         }
249                 }
250
251                 //header("Location: ".$liste["file"]."?PHPSESSID=".$_SESSION["s"]["id"]);
775e53 252                 $list_name = $_SESSION["s"]["form"]["return_to"];
1d3319 253                                 if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
1645bb 254                         $redirect = "Location: ".$_SESSION["s"]["list"][$list_name]["parent_script"]."?id=".$_SESSION["s"]["list"][$list_name]["parent_id"]."&next_tab=".$_SESSION["s"]["list"][$list_name]["parent_tab"];
F 255                         $_SESSION["s"]["form"]["return_to"] = '';
256                         session_write_close();
257                         header($redirect);
258                 } else {
259                     header("Location: ".$liste["file"]);
260                 }
261                 exit;
262
263         }
264
265         /**
266         * Function called on page show
267         */
268
269         function onShow() {
270                 global $app, $conf;
271
272                 // Which tab do we render
273                 $this->active_tab = $app->tform->getNextTab();
274
275                 if($this->id > 0) {
276                         $this->onShowEdit();
277                 } else {
278                         $this->onShowNew();
279                 }
280
281                 // make Form and Tabs
282                 $app->tform->showForm();
283
284                 // Setting default values
285                 $app->tpl_defaults();
286
287                 // Calling the Plugin onShow Events and set the data in the
288                 // plugins placeholder in the template
289                 foreach($this->plugins as $plugin_name => $plugin) {
290                         $app->tpl->setVar($plugin_name,$plugin->onShow());
291                 }
292
293                 // Parse the templates and send output to the browser
294                 $this->onShowEnd();
295
296         }
297
298         /**
299         * Function called on new record
300         */
301
302         function onShowNew() {
303                 global $app, $conf;
304
305                 if($app->tform->errorMessage == '') {
306                         $record = array();
307                         $record = $app->tform->getHTML($record, $app->tform->formDef['tab_default'],'NEW');
308                 } else {
309                         $record = $app->tform->getHTML($app->tform->encode($_POST,$this->active_tab),$this->active_tab,'EDIT');
310                 }
311
312                 $app->tpl->setVar($record);
313         }
314
315         /**
316         * Function called on edit record
317         */
318
319         function onShowEdit() {
320                 global $app, $conf;
321
322                 // bestehenden Datensatz anzeigen
323                 if($app->tform->errorMessage == '') {
324                         if($app->tform->formDef['auth'] == 'no') {
325                                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id;
326                         } else {
327                                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id." AND ".$app->tform->getAuthSQL('u');
328                         }
1d3319 329                         if(!$record = $app->db->queryOneRecord($sql)) $app->error($app->lng('error_no_view_permission'));
1645bb 330                 } else {
F 331                         $record = $app->tform->encode($_POST,$this->active_tab);
332                 }
333
334                 $this->dataRecord = $record;
335
336             // Userdaten umwandeln
337                 $record = $app->tform->getHTML($record, $this->active_tab,'EDIT');
338                 $record['id'] = $this->id;
339
340                 $app->tpl->setVar($record);
341         }
342
343         function onShowEnd() {
344                 global $app, $conf;
345
346                 // Template parsen
347                 $app->tpl->pparse();
348         }
349
350
b5a2f8 351 }
T 352
353 ?>