Code

Moved braces
[gosa.git] / gosa-core / include / class_msg_dialog.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 define("INFO_DIALOG"            , 10001);
24 define("WARNING_DIALOG"         , 10002);
25 define("ERROR_DIALOG"           , 10003);
26 define("CONFIRM_DIALOG"         , 10004);
27 define("FATAL_ERROR_DIALOG"     , 10005);
29 class msg_dialog
30 {
31         private $s_Title        = "Undefined";
32         private $s_Message      = "Undefined";
33         private $i_Type         = INFO_DIALOG ;
34         private $i_ID           = 0;
35         private $a_Trace        = array();
37         public function __construct($s_title,$s_message,$i_type)
38         {
39                 global $config;
40         
41                 if(!in_array($i_type,array(INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG))){
42                         trigger_error("Invalid msg_dialog type.");
43                         $i_type = INFO_DIALOG;
44                 }
46                 if((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))){
47                         session::set('errorsAlreadyPosted',array());
48                 }
50                 $errorsAlreadyPosted = session::get('errorsAlreadyPosted');
51                 if(!isset($errorsAlreadyPosted[$s_title.$s_message])){
52                         $errorsAlreadyPosted[$s_title.$s_message] = 0;
53                 }
54                 $errorsAlreadyPosted[$s_title.$s_message]++;
56                 if($errorsAlreadyPosted[$s_title.$s_message] <=1  ){
58                         $this->i_ID      = preg_replace("/[^0-9]*/","",microtime()); 
59                         $this->s_Title   = $s_title;
60                         $this->s_Message = $s_message;
61                         $this->i_Type    = $i_type;
63                         /* Append trace information, only if error messages are enabled */
64                         if(     isset($config) && 
65                                 is_object($config) && 
66                                 isset($config->data['MAIN']['DISPLAYERRORS']) &&
67                                 preg_match("/^true$/i",$config->data['MAIN']['DISPLAYERRORS'])){
68                                 $this->a_Trace   = debug_backtrace();
69                         }
70                         if(!session::is_set('msg_dialogs')){
71                                 session::set('msg_dialogs',array());
72                         }
73                         if($this->i_Type == FATAL_ERROR_DIALOG){
74                                 restore_error_handler();
75                                 error_reporting(E_ALL);
76                                 echo $this->execute();
77                         }else{
78                                 $msg_dialogs   = session::get('msg_dialogs');
79                                 $msg_dialogs[] = $this;
80                                 session::set('msg_dialogs',$msg_dialogs);
81                         }
82                 }
83                 session::set('errorsAlreadyPosted',$errorsAlreadyPosted);
84         }
86         public static function display($s_title,$s_message,$i_type = INFO_DIALOG)
87         {
88                 new msg_dialog($s_title,$s_message,$i_type);    
89         }
91         public function get_ID()
92         {
93                 return($this->i_ID);
94         }
96         public function execute()
97         {
98                 if($this->i_Type == FATAL_ERROR_DIALOG) {
99                         $display = 
100                                 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
101                                 \"http://www.w3.org/TR/html4/transitional.dtd\">
102                                 <html><head>
103                                 <title>GOsa startup failed</title>
104                                 </head><body>
105                                 <table style='width:100%; border:2px solid red;' summary=''>
106                                 <tr><td style='vertical-align:top;padding:10px'>
107                                 <img src='images/error.png' alt='{t}Error{/t}'>
108                                 </td><td style='width:100%'>
109                                 <b>".$this->s_Title."</b><br>
110                                 ".$this->s_Message."<br><br>
111                                 "._("Please fix the above error and reload the page.")."
112                                 </td></tr>
113                                 </table></body></html>";
114                         return($display);;
115                 }else{
117                         $smarty = get_smarty();
118                         $smarty->assign("s_Trace"       ,print_a($this->a_Trace,TRUE));
119                         $smarty->assign("i_TraceCnt",count($this->a_Trace));
120                         $smarty->assign("i_Type",$this->i_Type);
121                         $smarty->assign("s_Message",$this->s_Message);
122                         $smarty->assign("s_Title",$this->s_Title);
123                         $smarty->assign("i_ID",$this->i_ID);
124                         $smarty->assign("frame",false);
125                         $smarty->assign("JS",session::get('js'));
126                         $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
127                         return($smarty->fetch(get_template_path('msg_dialog.tpl')));
128                 }
129         }
131         
132         public function is_confirmed()
133         {
134                 if(isset($_POST['MSG_OK'.$this->i_ID])){
135                         return(TRUE);
136                 }else{
137                         return(FALSE);
138                 }
139         }
140         
142         public static function get_dialogs()
143         {
144                 $return  ="";
145                 $dialog_ids= "";
147                 $seen = "";
148                 if(isset($_POST['closed_msg_dialogs'])){
149 #                       $seen = $_POST['closed_msg_dialogs'];
150                 }
152                 if(session::is_set('msg_dialogs') && is_array(session::get('msg_dialogs')) && count(session::get('msg_dialogs'))){
154                         /* Get frame one */
155                         $smarty = get_smarty();
156                         $smarty->assign("frame", true);
157                         $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
158                         $return = $smarty->fetch(get_template_path('msg_dialog.tpl'));
160                         if(!session::get('js')){
161                                 $dialog = array_pop(session::get('msg_dialogs'));
162                                 $return.= $dialog->execute();
163                         }else{
164                                 $msg_dialogs = session::get('msg_dialogs');
165                                 foreach($msg_dialogs as $key => $dialog){
167                                         if(preg_match("/".$dialog->get_ID()."/",$seen)){
168                                                 unset($msg_dialogs[$key]);
169                                         }else{
170                                                 $return.= $dialog->execute();
171                                                 $dialog_ids= $dialog->get_ID().",".$dialog_ids;
172                                         }
173                                         unset($msg_dialogs[$key]);
174                                 }
175                                 session::set('msg_dialogs',$msg_dialogs);
176                         }
177                         $dialog_ids = preg_replace("/,$/","",$dialog_ids);
179                         $return .= "</div>";
180                         $return .="<input type='hidden' style='width:400px;' name='pending_msg_dialogs' id='pending_msg_dialogs' value='".$dialog_ids."'>";
181                         $return .="<input type='hidden' style='width:400px;' name='closed_msg_dialogs' id='closed_msg_dialogs' value=''>";
182                         $return .="<input type='hidden' style='width:400px;' name='current_msg_dialogs' id='current_msg_dialogs' value=''>";
183                         $return .="<input type='hidden' style='width:700px;' name='js_debug' id='js_debug'>";
184                 }
185                 return($return);
186         }
189 ?>