tbrehm
2006-09-16 dc8167ef127f52087b463e4720886a8e163899d3
commit | author | age
fa804b 1 <?php
F 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
40         var $id;
41         var $activeTab;
42         var $dataRecord;
43         var $plugins = array();
44
45         function onLoad() {
46                 global $app, $conf, $tform_def_file;
47
48                 // Loading template classes and initialize template
49                 if(!is_object($app->tpl)) $app->uses('tpl');
50                 if(!is_object($app->tform)) $app->uses('tform');
51
52                 $app->tpl->newTemplate("tabbed_form.tpl.htm");
53
54                 // Load table definition from file
55                 $app->tform->loadFormDef($tform_def_file);
dc8167 56                 
T 57                 // Importing ID
fa804b 58                 $this->id = intval($_REQUEST["id"]);
F 59
60                 if(count($_POST) > 1) {
61                         $this->dataRecord = $_POST;
62                         $this->onSubmit();
63                 } else {
64                         $this->onShow();
65                 }
66         }
67
68         /**
69         * Function called on page submit
70         */
71
72         function onSubmit() {
73                 global $app, $conf;
74
75                 // Calling the action functions
76                 if($this->id > 0) {
77                         $this->onUpdate();
78                 } else {
79                         $this->onInsert();
80                 }
81         }
82
83         /**
84         * Function called on data update
85         */
86
87         function onUpdate() {
88                 global $app, $conf;
89
90                 $ext_where = '';
91                 $sql = $app->tform->getSQL($this->dataRecord,$app->tform->getCurrentTab(),'UPDATE',$this->id,$ext_where);
92                 if($app->tform->errorMessage == '') {
93
94                         if(!empty($sql)) {
95                                 $app->db->query($sql);
96                             if($app->db->errorMessage != '') die($app->db->errorMessage);
97                         }
dc8167 98                         
T 99                         // loading plugins
100                         $next_tab = $app->tform->getCurrentTab();
101                         $this->loadPlugins($next_tab);
fa804b 102
F 103                         // Call plugin
104                         foreach($this->plugins as $plugin) {
105                                 $plugin->onInsert();
106                         }
107
108                         $this->onAfterUpdate();
109
110                         if($_REQUEST["next_tab"] == '') {
111                                 $list_name = $_SESSION["s"]["form"]["return_to"];
112                                 if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
113                                 $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"];
114                                 $_SESSION["s"]["form"]["return_to"] = '';
115                                 session_write_close();
116                                 header($redirect);
117                         } else {
118                                                    header("Location: ".$app->tform->formDef['list_default']);
119                         }
120                         exit;
121                     } else {
122                                 $this->onShow();
123                         }
124                 } else {
125                         $this->onError();
126                 }
127         }
128
129         /**
130         * Function called on data insert
131         */
132
133         function onInsert() {
134                 global $app, $conf;
135
136                 $ext_where = '';
137                 $sql = $app->tform->getSQL($this->dataRecord,$app->tform->getCurrentTab(),'INSERT',$this->id,$ext_where);
138                 if($app->tform->errorMessage == '') {
139                         $app->db->query($sql);
140                         if($app->db->errorMessage != '') die($app->db->errorMessage);
141                         $this->id = $app->db->insertID();
dc8167 142                         
T 143                         // loading plugins
144                         $next_tab = $app->tform->getCurrentTab();
145                         $this->loadPlugins($next_tab);
146                         
fa804b 147                         // Call plugin
F 148                         foreach($this->plugins as $plugin) {
149                                 $plugin->onInsert();
150                         }
151
152                         $this->onAfterInsert();
153
154                      if($_REQUEST["next_tab"] == '') {
155                          $list_name = $_SESSION["s"]["form"]["return_to"];
156                          if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
157                                                         $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"];
158                             $_SESSION["s"]["form"]["return_to"] = '';
159                             session_write_close();
160                             header($redirect);
161                         } else {
162                                 header("Location: ".$app->tform->formDef['list_default']);
163                         }
164                         exit;
165                     } else {
166                             $this->onShow();
167                         }
168                 } else {
169                         $this->onError();
170                 }
171         }
172
173                 function onAfterUpdate() {
174                         global $app, $conf;
175                 }
176
177                 function onAfterInsert() {
178                         global $app, $conf;
179                 }
180
181
182         /**
183         * Function called on data insert or update error
184         */
185
186         function onError() {
187                 global $app, $conf;
188
189                 $app->tpl->setVar("error","<b>".$app->lng('Error').":</b><br>".$app->tform->errorMessage);
190                 $app->tpl->setVar($this->dataRecord);
191                 $this->onShow();
192         }
193
194         /**
195         * Function called on data delete
196         */
197
198         function onDelete() {
199                 global $app, $conf,$list_def_file,$tform_def_file;
200
201                 include_once($list_def_file);
202
203                 // Loading tform framework
204                 if(!is_object($app->tform)) $app->uses('tform');
205
206                 // Load table definition from file
207                 $app->tform->loadFormDef($tform_def_file);
208
209                 // importing ID
210                 $this->id = intval($_REQUEST["id"]);
211
212                 if($this->id > 0) {
213
214                         // checking permissions
215                         if($app->tform->formDef['auth'] == 'yes') {
216                                 if($app->tform->checkPerm($this->id,'d') == false) $app->error($app->lng('error_no_delete_permission'));
217                         }
218
219                         $record_old = $app->db->queryOneRecord("SELECT * FROM ".$liste["table"]." WHERE ".$liste["table_idx"]." = ".$this->id);
220
221                         // Saving record to datalog when db_history enabled
222                         if($form["db_history"] == 'yes') {
223                                 $diffrec = array();
224
225                                 foreach($record_old as $key => $val) {
226                                         // Record has changed
227                                         $diffrec[$key] = array('old' => $val,
228                                                                                            'new' => '');
229                                 }
230
231                                 $diffstr = $app->db->quote(serialize($diffrec));
232                                 $username = $app->db->quote($_SESSION["s"]["user"]["username"]);
233                                 $dbidx = $app->tform->formDef['db_table_idx'].":".$this->id;
234                                 $sql = "INSERT INTO sys_datalog (dbtable,dbidx,action,tstamp,user,data) VALUES ('".$app->tform->formDef['db_table']."','$dbidx','d','".time()."','$username','$diffstr')";
235                                 $app->db->query($sql);
236                         }
237
238                         $app->db->query("DELETE FROM ".$liste["table"]." WHERE ".$liste["table_idx"]." = ".$this->id);
dc8167 239                         
T 240                         // loading plugins
241                         $next_tab = $app->tform->getCurrentTab();
242                         $this->loadPlugins($next_tab);
243                         
244                         
fa804b 245                         // Call plugin
F 246                         foreach($this->plugins as $plugin) {
247                                 $plugin->onInsert();
248                         }
249                 }
250
251                 //header("Location: ".$liste["file"]."?PHPSESSID=".$_SESSION["s"]["id"]);
252                 $list_name = $_SESSION["s"]["form"]["return_to"];
253                                 if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
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"];
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();
dc8167 286                 
T 287                 // loading plugins
288                 //$next_tab = $app->tform->getNextTab();
289                 $this->loadPlugins($this->active_tab);
fa804b 290
F 291                 // Calling the Plugin onShow Events and set the data in the
292                 // plugins placeholder in the template
293                 foreach($this->plugins as $plugin_name => $plugin) {
294                         $app->tpl->setVar($plugin_name,$plugin->onShow());
295                 }
296
297                 // Parse the templates and send output to the browser
298                 $this->onShowEnd();
299
300         }
301
302         /**
303         * Function called on new record
304         */
305
306         function onShowNew() {
307                 global $app, $conf;
308
309                 if($app->tform->errorMessage == '') {
310                         $record = array();
311                         $record = $app->tform->getHTML($record, $app->tform->formDef['tab_default'],'NEW');
312                 } else {
313                         $record = $app->tform->getHTML($app->tform->encode($_POST,$this->active_tab),$this->active_tab,'EDIT');
314                 }
315
316                 $app->tpl->setVar($record);
317         }
318
319         /**
320         * Function called on edit record
321         */
322
323         function onShowEdit() {
324                 global $app, $conf;
325
326                 // bestehenden Datensatz anzeigen
327                 if($app->tform->errorMessage == '') {
328                         if($app->tform->formDef['auth'] == 'no') {
329                                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id;
330                         } else {
331                                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id." AND ".$app->tform->getAuthSQL('u');
332                         }
333                         if(!$record = $app->db->queryOneRecord($sql)) $app->error($app->lng('error_no_view_permission'));
334                 } else {
335                         $record = $app->tform->encode($_POST,$this->active_tab);
336                 }
337
338                 $this->dataRecord = $record;
339
340             // Userdaten umwandeln
341                 $record = $app->tform->getHTML($record, $this->active_tab,'EDIT');
342                 $record['id'] = $this->id;
343
344                 $app->tpl->setVar($record);
345         }
346
347         function onShowEnd() {
348                 global $app, $conf;
349
350                 // Template parsen
351                 $app->tpl->pparse();
352         }
dc8167 353         
T 354         function loadPlugins($next_tab) {
355             global $app;
356             if(is_array($app->tform->formDef["tabs"][$next_tab]["plugins"])) {
357                  $app->load('plugin_base');
358                  foreach($app->tform->formDef["tabs"][$next_tab]["plugins"] as $plugin_name => $plugin_settings) {
359                       $plugin_class = $plugin_settings["class"];
360                       $app->load($plugin_class);
361                       $this->plugins[$plugin_name] = new $plugin_class;
362                       $this->plugins[$plugin_name]->setOptions($plugin_name,$plugin_settings['options']);
363                       $this->plugins[$plugin_name]->onLoad();
364                   }
365              }
366         }
fa804b 367
F 368
369 }
370
b5a2f8 371 ?>