Code

Updated msgDialog
[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;
41                 if(empty($s_message)) return;
42         
43                 if(!in_array($i_type,array(INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG))){
44                         trigger_error("Invalid msg_dialog type.");
45                         $i_type = INFO_DIALOG;
46                 }
48                 if((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))){
49                         session::set('errorsAlreadyPosted',array());
50                 }
52                 $errorsAlreadyPosted = session::get('errorsAlreadyPosted');
53                 if(!isset($errorsAlreadyPosted[$s_title.$s_message])){
54                         $errorsAlreadyPosted[$s_title.$s_message] = 0;
55                 }
56                 $errorsAlreadyPosted[$s_title.$s_message]++;
58                 if($errorsAlreadyPosted[$s_title.$s_message] <=1  ){
60                         $this->i_ID      = preg_replace("/[^0-9]*/","",microtime()); 
61                         $this->s_Title   = $s_title;
62                         $this->s_Message = $s_message;
63                         $this->i_Type    = $i_type;
65                         /* Append trace information, only if error messages are enabled */
66                         if(     isset($config) && 
67                                 is_object($config) && 
68                                 isset($config->data['MAIN']['DISPLAYERRORS']) &&
69                                 preg_match("/^true$/i",$config->data['MAIN']['DISPLAYERRORS'])){
70                                 $this->a_Trace   = debug_backtrace();
71                         }
72                         if(!session::is_set('msg_dialogs')){
73                                 session::set('msg_dialogs',array());
74                         }
75                         if($this->i_Type == FATAL_ERROR_DIALOG){
76                                 restore_error_handler();
77                                 error_reporting(E_ALL);
78                                 echo $this->execute();
79                         }else{
80                                 $msg_dialogs   = session::get('msg_dialogs');
81                                 $msg_dialogs[] = $this;
82                                 session::set('msg_dialogs',$msg_dialogs);
83                         }
84                 }
85                 session::set('errorsAlreadyPosted',$errorsAlreadyPosted);
86         }
89         public static function display($s_title,$s_message,$i_type = INFO_DIALOG)
90         {
91                 new msg_dialog($s_title,$s_message,$i_type);    
92         }
95         public static function displayChecks($messages)
96         {
97                 /* Assemble the message array to a plain string */
98                 foreach ($messages as $error){
99                         msg_dialog::display(_("Error"), $error, ERROR_DIALOG);
100                 }
101         }
104         public function get_ID()
105         {
106                 return($this->i_ID);
107         }
109         public function execute()
110         {
111                 if($this->i_Type == FATAL_ERROR_DIALOG) {
112                         $display = 
113                                 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
114                                 \"http://www.w3.org/TR/html4/transitional.dtd\">
115                                 <html><head>
116                                 <title>GOsa startup failed</title>
117                                 </head><body>
118                                 <table style='width:100%; border:2px solid red;' summary=''>
119                                 <tr><td style='vertical-align:top;padding:10px'>
120                                 <img src='images/error.png' alt='{t}Error{/t}'>
121                                 </td><td style='width:100%'>
122                                 <b>".$this->s_Title."</b><br>
123                                 ".$this->s_Message."<br><br>
124                                 "._("Please fix the above error and reload the page.")."
125                                 </td></tr>
126                                 </table></body></html>";
127                         return($display);;
128                 }else{
130                         $smarty = get_smarty();
131                         $smarty->assign("s_Trace"       ,print_a($this->a_Trace,TRUE));
132                         $smarty->assign("i_TraceCnt",count($this->a_Trace));
133                         $smarty->assign("i_Type",$this->i_Type);
134                         $smarty->assign("s_Message",$this->s_Message);
135                         $smarty->assign("s_Title",$this->s_Title);
136                         $smarty->assign("i_ID",$this->i_ID);
137                         $smarty->assign("frame",false);
138                         $smarty->assign("JS",session::get('js'));
139                         $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
140                         return($smarty->fetch(get_template_path('msg_dialog.tpl')));
141                 }
142         }
144         
145         public function is_confirmed()
146         {
147                 if(isset($_POST['MSG_OK'.$this->i_ID])){
148                         return(TRUE);
149                 }else{
150                         return(FALSE);
151                 }
152         }
153         
155         public static function get_dialogs()
156         {
157                 $return  ="";
158                 $dialog_ids= "";
160                 $seen = "";
161                 if(isset($_POST['closed_msg_dialogs'])){
162 #                       $seen = $_POST['closed_msg_dialogs'];
163                 }
165                 if(session::is_set('msg_dialogs') && is_array(session::get('msg_dialogs')) && count(session::get('msg_dialogs'))){
167                         /* Get frame one */
168                         $smarty = get_smarty();
169                         $smarty->assign("frame", true);
170                         $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
171                         $return = $smarty->fetch(get_template_path('msg_dialog.tpl'));
173                         if(!session::get('js')){
174                                 $dialog = array_pop(session::get('msg_dialogs'));
175                                 $return.= $dialog->execute();
176                         }else{
177                                 $msg_dialogs = session::get('msg_dialogs');
178                                 foreach($msg_dialogs as $key => $dialog){
180                                         if(preg_match("/".$dialog->get_ID()."/",$seen)){
181                                                 unset($msg_dialogs[$key]);
182                                         }else{
183                                                 $return.= $dialog->execute();
184                                                 $dialog_ids= $dialog->get_ID().",".$dialog_ids;
185                                         }
186                                         unset($msg_dialogs[$key]);
187                                 }
188                                 session::set('msg_dialogs',$msg_dialogs);
189                         }
190                         $dialog_ids = preg_replace("/,$/","",$dialog_ids);
192                         $return .= "</div>";
193                         $return .="<input type='hidden' style='width:400px;' name='pending_msg_dialogs' id='pending_msg_dialogs' value='".$dialog_ids."'>";
194                         $return .="<input type='hidden' style='width:400px;' name='closed_msg_dialogs' id='closed_msg_dialogs' value=''>";
195                         $return .="<input type='hidden' style='width:400px;' name='current_msg_dialogs' id='current_msg_dialogs' value=''>";
196                         $return .="<input type='hidden' style='width:700px;' name='js_debug' id='js_debug'>";
197                 }
198                 return($return);
199         }
202 ?>