tbrehm
2011-04-13 11201c5e3b31d35e23b03f40e3d242014f15ce49
commit | author | age
d4d985 1 <?php
T 2
3 /*
436ed8 4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
d4d985 5 All rights reserved.
T 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 class tform_actions {
32
443d7e 33         public $id;
T 34         public $activeTab;
35         public $dataRecord;
36         public $plugins = array();
37         public $oldDataRecord; // This array is only filled during updates and when db_history is enabled.
d4d985 38
T 39         function onLoad() {
40                 global $app, $conf, $tform_def_file;
41
42                 // Loading template classes and initialize template
43                 if(!is_object($app->tpl)) $app->uses('tpl');
44                 if(!is_object($app->tform)) $app->uses('tform');
45
46                 $app->tpl->newTemplate("tabbed_form.tpl.htm");
47
48                 // Load table definition from file
49                 $app->tform->loadFormDef($tform_def_file);
50                 
51                 // Importing ID
52                 $this->id = (isset($_REQUEST["id"]))?intval($_REQUEST["id"]):0;
53                 
54                 // show print version of the form
55                 if(isset($_GET["print_form"]) && $_GET["print_form"] == 1) {
56                     die('Function disabled.');
57                     $this->onPrintForm();
58                 }
59                 
60                 // send this form by email
61                 if(isset($_GET["send_form_by_mail"]) && $_GET["send_form_by_mail"] == 1) {
62                     die('Function disabled.');
63                     $this->onMailSendForm();
64                 }
65
66                 if(count($_POST) > 1) {
67                         $this->dataRecord = $_POST;
68                         $this->onSubmit();
69                 } else {
70                         $this->onShow();
71                 }
72         }
73
74         /**
75         * Function called on page submit
76         */
77
78         function onSubmit() {
79                 global $app, $conf;
80
81                 // Calling the action functions
82                 if($this->id > 0) {
8eafce 83                     $app->tform->action == 'EDIT';
T 84                     $this->onUpdate();
d4d985 85                 } else {
8eafce 86                     $app->tform->action == 'NEW';
T 87                     $this->onInsert();
d4d985 88                 }
T 89         }
90
91         /**
92         * Function called on data update
93         */
94
95         function onUpdate() {
96                 global $app, $conf;
97                 
98                 $this->onBeforeUpdate();
99                 
100                 $ext_where = '';
101                 $sql = $app->tform->getSQL($this->dataRecord,$app->tform->getCurrentTab(),'UPDATE',$this->id,$ext_where);
102                 if($app->tform->errorMessage == '') {
103                         
104                         if($app->tform->formDef['db_history'] == 'yes') {
105                             $this->oldDataRecord = $app->tform->getDataRecord($this->id);
106                         }
107                         
108                         // Save record in database
109                         $this->onUpdateSave($sql);
110                         
111                         // loading plugins
112                         $next_tab = $app->tform->getCurrentTab();
113                         $this->loadPlugins($next_tab);
114
115                         // Call plugin
116                         foreach($this->plugins as $plugin) {
89a57f 117                                 $plugin->onUpdate();
d4d985 118                         }
f3a56a 119                         
T 120                         $this->onAfterUpdate();
da1da4 121                         $app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$app->tform->formDef['name'].':'.'on_after_update',$this);
d4d985 122                         
T 123                         // Write data history (sys_datalog)
124                         if($app->tform->formDef['db_history'] == 'yes') {
125                             $new_data_record = $app->tform->getDataRecord($this->id);
126                             $app->tform->datalogSave('UPDATE',$this->id,$this->oldDataRecord,$new_data_record);
127                             unset($new_data_record);
128                             unset($old_data_record);
129                         }
130
131                         if($_REQUEST["next_tab"] == '') {
132                            $list_name = $_SESSION["s"]["form"]["return_to"];
133                            // When a list is embedded inside of a form
134                            
135                            //if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
136                            if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
137                                 $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"];
138                                 $_SESSION["s"]["form"]["return_to"] = '';
139                                 session_write_close();
140                                 header($redirect);
141                             // When a returnto variable is set
142                             } elseif ($_SESSION["s"]["form"]["return_to_url"] != '') {
143                                 $redirect = $_SESSION["s"]["form"]["return_to_url"];
144                                 $_SESSION["s"]["form"]["return_to_url"] = '';
145                                 session_write_close();
146                                 header("Location: ".$redirect);
147                                 exit;
148                                 // Use the default list of the form
149                             } else {
150                                 header("Location: ".$app->tform->formDef['list_default']);
151                             }
152                             exit;
153                         } else {
154                                 $this->onShow();
155                         }
156                 } else {
157                         $this->onError();
158                 }
159         }
160         
161         /*
162          Save record in database
163         */
164         
165         function onUpdateSave($sql) {
166             global $app;
ce6dcf 167             if(!empty($sql) && !$app->tform->isReadonlyTab($app->tform->getCurrentTab(),$this->id)) {
d4d985 168                 $app->db->query($sql);
T 169                 if($app->db->errorMessage != '') die($app->db->errorMessage);
170             }
171         }
172         
173
174         /**
175         * Function called on data insert
176         */
177
178         function onInsert() {
179                 global $app, $conf;
180                 
181                 $this->onBeforeInsert();
182
183                 $ext_where = '';
184                 $sql = $app->tform->getSQL($this->dataRecord,$app->tform->getCurrentTab(),'INSERT',$this->id,$ext_where);
185                 if($app->tform->errorMessage == '') {
186                         
187                         $this->id = $this->onInsertSave($sql);
188                         
189                         // loading plugins
190                         $next_tab = $app->tform->getCurrentTab();
191                         $this->loadPlugins($next_tab);
192                         
193                         // Call plugin
194                         foreach($this->plugins as $plugin) {
195                                 $plugin->onInsert();
196                         }
197
198                         $this->onAfterInsert();
da1da4 199                         $app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$app->tform->formDef['name'].':'.'on_after_insert',$this);
52bc49 200
d4d985 201                         // Write data history (sys_datalog)
T 202                         if($app->tform->formDef['db_history'] == 'yes') {
203                             $new_data_record = $app->tform->getDataRecord($this->id);
204                             $app->tform->datalogSave('INSERT',$this->id,array(),$new_data_record);
205                             unset($new_data_record);
206                         }
207                         
208
209                      if($_REQUEST["next_tab"] == '') {
210                          $list_name = $_SESSION["s"]["form"]["return_to"];
211                          // if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
212                          if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
213                             $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"];
214                             $_SESSION["s"]["form"]["return_to"] = '';
215                             session_write_close();
216                             header($redirect);
217                             exit;
218                         } elseif ($_SESSION["s"]["form"]["return_to_url"] != '') {
219                             $_SESSION["s"]["form"]["return_to_url"] = '';
220                             session_write_close();
221                             header("Location: ".$_SESSION["s"]["form"]["return_to_url"]);
222                             exit;
223                         } else {
224                                 header("Location: ".$app->tform->formDef['list_default']);
225                         }
226                         exit;
227                     } else {
228                             $this->onShow();
229                         }
230                 } else {
231                         $this->onError();
232                 }
233         }
234         
235         /*
236          Save record in database
237         */
238         
239         function onInsertSave($sql) {
240             global $app, $conf;
241             $app->db->query($sql);
242             if($app->db->errorMessage != '') die($app->db->errorMessage);
243             return $app->db->insertID();
244         }
245
246         function onBeforeUpdate() {
247             global $app, $conf;
248         }
249
250         function onBeforeInsert() {
251             global $app, $conf;
252         }
253         
254         function onAfterUpdate() {
255             global $app, $conf;
256         }
257
258         function onAfterInsert() {
259             global $app, $conf;
260         }
261
262
263         /**
264         * Function called on data insert or update error
265         */
266
267         function onError() {
268                 global $app, $conf;
269
649d63 270                 $app->tpl->setVar("error","<li>".$app->tform->errorMessage."</li>");
d4d985 271                 $app->tpl->setVar($this->dataRecord);
T 272                 $this->onShow();
273         }
274
275         /**
276         * Function called on data delete
277         */
278
279         function onDelete() {
280                 global $app, $conf,$list_def_file,$tform_def_file;
281
282                 include_once($list_def_file);
283
284                 // Loading tform framework
285                 if(!is_object($app->tform)) $app->uses('tform');
286
287                 // Load table definition from file
288                 $app->tform->loadFormDef($tform_def_file);
289
290                 // importing ID
291                 $this->id = intval($_REQUEST["id"]);
292
293                 if($this->id > 0) {
294
295                         // checking permissions
296                         if($app->tform->formDef['auth'] == 'yes' && $_SESSION["s"]["user"]["typ"] != 'admin') {
297                                 if($app->tform->checkPerm($this->id,'d') == false) $app->error($app->lng('error_no_delete_permission'));
298                         }
299
300                         //$this->dataRecord = $app->db->queryOneRecord("SELECT * FROM ".$liste["table"]." WHERE ".$liste["table_idx"]." = ".$this->id);
301                         $this->dataRecord = $app->tform->getDataRecord($this->id);
302                         
303                         $this->onBeforeDelete();
304
305                         // Saving record to datalog when db_history enabled
306                         if($app->tform->formDef["db_history"] == 'yes') {
307                             //$old_data_record = $app->tform->getDataRecord($this->id);
308                             $app->tform->datalogSave('DELETE',$this->id,$this->dataRecord,array());
309                         }
310
311                         $app->db->query("DELETE FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id." LIMIT 1");
312                         
313                         
314                         // loading plugins
315                         $next_tab = $app->tform->getCurrentTab();
316                         $this->loadPlugins($next_tab);
317                         
318                         
319                         // Call plugin
320                         foreach($this->plugins as $plugin) {
321                                 $plugin->onDelete();
322                         }
323                         
324                         $this->onAfterDelete();
11201c 325                         $app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$app->tform->formDef['name'].':'.'on_after_delete',$this);
d4d985 326                 }
T 327
328                         //header("Location: ".$liste["file"]."?PHPSESSID=".$_SESSION["s"]["id"]);
329                  $list_name = $_SESSION["s"]["form"]["return_to"];
330                  if($list_name != '' && $_SESSION["s"]["list"][$list_name]["parent_id"] != $this->id && $_SESSION["s"]["list"][$list_name]["parent_name"] != $app->tform->formDef["name"]) {
331                         $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"];
332                         $_SESSION["s"]["form"]["return_to"] = '';
333                         session_write_close();
334                         header($redirect);
335                 } else {
336                     header("Location: ".$liste["file"]);
337                 }
338                 exit;
339
340         }
341         
342         function onBeforeDelete() {
343             global $app, $conf;
344         }
345         
346         function onAfterDelete() {
347             global $app, $conf;
348         }
349         
350         /**
351         * Function to print the form content
352         */
353         
354         function onPrintForm() {
355             global $app, $conf;
356             
357             if($app->tform->formDef['template_print'] == '') die('No print template available.');
358             
359             $app->tpl->newTemplate("print.tpl.htm");
360             $app->tpl->setInclude("content_tpl",$app->tform->formDef['template_print']);
361
362             if($app->tform->formDef['auth'] == 'no') {
363                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id;
364             } else {
16230f 365                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id." AND ".$app->tform->getAuthSQL('r');
d4d985 366             }
T 367             if(!$record = $app->db->queryOneRecord($sql)) $app->error($app->lng('error_no_view_permission'));
368             
369             $record["datum"] = date("d.m.Y");
370             
371             $app->tpl->setVar($app->tform->wordbook);
372
373             $app->tpl->setVar($record);
374             $app->tpl_defaults();
375             $app->tpl->pparse();
376             exit;
377             
378         }
379         
380         /**
381         * Function to print the form content
382         */
383         
384         function onMailSendForm() {
385             global $app, $conf;
386             
387             if($app->tform->formDef['template_mailsend'] == '') die('No print template available.');
388             
389             if($_POST["email"] == '' && $_POST["sender"] == '') {
390                 // Zeige Formular zum versenden an.
391                 $app->tpl->newTemplate("form.tpl.htm");
392                 $app->tpl->setInclude("content_tpl",$app->tform->formDef['template_mailsend']);
393                 $app->tpl->setVar('show_form',1);
394                 $app->tpl->setVar("form_action",$app->tform->formDef['action'].'?send_form_by_mail=1');
395                 $app->tpl->setVar("id",$this->id);
396                 $app->tpl_defaults();
397                 $app->tpl->pparse();
398                 exit;
399             } else {
400                 $app->tpl->newTemplate("mail.tpl.htm");
401                 $app->tpl->setInclude("content_tpl",$app->tform->formDef['template_mailsend']);
402                 $app->tpl->setVar('show_mail',1);
403                 if($app->tform->formDef['auth'] == 'no') {
404                     $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id;
405                 } else {
16230f 406                     $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id." AND ".$app->tform->getAuthSQL('r');
d4d985 407                 }
T 408                 if(!$record = $app->db->queryOneRecord($sql)) $app->error($app->lng('error_no_view_permission'));
409             
410                 $record["datum"] = date("d.m.Y");
411                 $record["mailmessage"] = $_POST["message"];
412             
413                 $app->tpl->setVar($app->tform->wordbook);
414
415                 $app->tpl->setVar($record);
416                 $app->tpl_defaults();
417                 
418                 $email_message = $app->tpl->grab();
419                 $email = $_POST["email"];
420                 $sender = $_POST["sender"];
421                 
422                 $headers  = "MIME-Version: 1.0\n";
423                 $headers .= "Content-type: text/html; charset=iso-8859-1\n";
424                 $headers .= "From: $sender\n";
425                 
1bb82f 426                 if (!preg_match('/^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+' . '@' . '([-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.)+' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$/', $sender)) {
d4d985 427                     $sender = 'noreply@iprguard.de';
T 428                   }
429                 
1bb82f 430                 if (preg_match('/^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+' . '@' . '([-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.)+' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$/', $email)) {
d4d985 431                     mail($email, 'Domainrecherche Statement '.$record["domain"], $email_message, $headers);
T 432                   }
433                 echo "<p>&nbsp;</p><p>Email wurde versand.</p>";
434                 exit;
435             }
436             
437             
438
439             if($app->tform->formDef['auth'] == 'no') {
440                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id;
441             } else {
16230f 442                 $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id." AND ".$app->tform->getAuthSQL('r');
d4d985 443             }
T 444             if(!$record = $app->db->queryOneRecord($sql)) $app->error($app->lng('error_no_view_permission'));
445             
446             $record["datum"] = date("d.m.Y");
447             
448             $app->tpl->setVar($app->tform->wordbook);
449
450             $app->tpl->setVar($record);
451             $app->tpl_defaults();
452             $app->tpl->pparse();
453             exit;
454             
455         }
456
457         /**
458         * Function called on page show
459         */
460
461         function onShow() {
462                 global $app, $conf;
463
464                 // Which tab do we render
465                 $this->active_tab = $app->tform->getNextTab();
466
467                 if($this->id > 0) {
468                         $this->onShowEdit();
469                 } else {
470                         $this->onShowNew();
471                 }
472
473                 // make Form and Tabs
474                 $app->tform->showForm();
475
476                 // Setting default values
477                 $app->tpl_defaults();
478                 
479                 // Show the navigation bar of the form
480                 if(isset($app->tform->formDef['navibar']) && $app->tform->formDef['navibar'] == 'yes') {
481                     $navibar = '';
482                     if($app->tform->formDef['template_print'] != '') {
483                         $navibar .= '<a href="'.$app->tform->formDef['action'].'?id='.$this->id.'&print_form=1" target="_blank"><img src="../themes/iprg/icons/printer.png" border="0" alt="Drucken" /></a> &nbsp;';
484                     }
485                     if($app->tform->formDef['template_mailsend'] != '') {
486                         $navibar .= "<a href=\"#\" onClick=\"window.open('".$app->tform->formDef['action'].'?id='.$this->id."&send_form_by_mail=1','send','width=370,height=240')\"><img src=\"../themes/iprg/icons/mail.png\" border=\"0\" alt=\"Als E-Mail versenden\" /></a>";
487                     }
488                     $app->tpl->setVar('form_navibar',$navibar);
489                 }
490                 
491                 
492                 // loading plugins
493                 $this->loadPlugins($this->active_tab);
494
495                 // Calling the Plugin onShow Events and set the data in the
496                 // plugins placeholder in the template
497                 foreach($this->plugins as $plugin_name => $plugin) {
498                         $app->tpl->setVar($plugin_name,$plugin->onShow());
499                 }
500
501                 // Parse the templates and send output to the browser
502                 $this->onShowEnd();
503
504         }
505
506         /**
507         * Function called on new record
508         */
509
510         function onShowNew() {
511                 global $app, $conf;
512
513                 if($app->tform->errorMessage == '') {
514                         $record = array();
515                         $record = $app->tform->getHTML($record, $app->tform->formDef['tab_default'],'NEW');
516                 } else {
517                         $record = $app->tform->getHTML($app->tform->encode($_POST,$this->active_tab),$this->active_tab,'EDIT');
518                 }
519
520                 $app->tpl->setVar($record);
521         }
522
523         /**
524         * Function called on edit record
525         */
526
527         function onShowEdit() {
528                 global $app, $conf;
529
530                 // bestehenden Datensatz anzeigen
531                 if($app->tform->errorMessage == '') {
532                         if($app->tform->formDef['auth'] == 'yes' && $_SESSION["s"]["user"]["typ"] != 'admin') {
16230f 533                             $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id." AND ".$app->tform->getAuthSQL('r');
d4d985 534                         } else {
T 535                             $sql = "SELECT * FROM ".$app->tform->formDef['db_table']." WHERE ".$app->tform->formDef['db_table_idx']." = ".$this->id;
536                         }
537                         if(!$record = $app->db->queryOneRecord($sql)) $app->error($app->lng('error_no_view_permission'));
538                 } else {
539                         // $record = $app->tform->encode($_POST,$this->active_tab);
540                         $record = $app->tform->encode($this->dataRecord,$this->active_tab);
541                 }
542
543                 $this->dataRecord = $record;
544
545                 // Userdaten umwandeln
546                 $record = $app->tform->getHTML($record, $this->active_tab,'EDIT');
547                 $record['id'] = $this->id;
548
549                 $app->tpl->setVar($record);
550         }
551
552         function onShowEnd() {
553                 global $app, $conf;
554
555                 // Template parsen
556                 $app->tpl->pparse();
557         }
558         
559         function loadPlugins($next_tab) {
560             global $app;
561             if(@is_array($app->tform->formDef["tabs"][$next_tab]["plugins"])) {
562                  $app->load('plugin_base');
563                  foreach($app->tform->formDef["tabs"][$next_tab]["plugins"] as $plugin_name => $plugin_settings) {
564                       $plugin_class = $plugin_settings["class"];
565                       $app->load($plugin_class);
566                       $this->plugins[$plugin_name] = new $plugin_class;
567                       $this->plugins[$plugin_name]->setOptions($plugin_name,$plugin_settings['options']);
568                       // Make the data of the form easily accessible for the plugib
569                       $this->plugins[$plugin_name]->form = $this;
570                       $this->plugins[$plugin_name]->onLoad();
571                   }
572              }
573         }
574
575
576 }
577
b5a2f8 578 ?>