Code

Updated class plugin.
[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         }
87         public static function display($s_title,$s_message,$i_type = INFO_DIALOG)
88         {
89                 new msg_dialog($s_title,$s_message,$i_type);    
90         }
93         public static function displayChecks($messages)
94         {
95                 /* Assemble the message array to a plain string */
96                 foreach ($messages as $error){
97                         msg_dialog::display(_("Error"), $error, ERROR_DIALOG);
98                 }
99         }
102         public function get_ID()
103         {
104                 return($this->i_ID);
105         }
107         public function execute()
108         {
109                 if($this->i_Type == FATAL_ERROR_DIALOG) {
110                         $display = 
111                                 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
112                                 \"http://www.w3.org/TR/html4/transitional.dtd\">
113                                 <html><head>
114                                 <title>GOsa startup failed</title>
115                                 </head><body>
116                                 <table style='width:100%; border:2px solid red;' summary=''>
117                                 <tr><td style='vertical-align:top;padding:10px'>
118                                 <img src='images/error.png' alt='{t}Error{/t}'>
119                                 </td><td style='width:100%'>
120                                 <b>".$this->s_Title."</b><br>
121                                 ".$this->s_Message."<br><br>
122                                 "._("Please fix the above error and reload the page.")."
123                                 </td></tr>
124                                 </table></body></html>";
125                         return($display);;
126                 }else{
128                         $smarty = get_smarty();
129                         $smarty->assign("s_Trace"       ,print_a($this->a_Trace,TRUE));
130                         $smarty->assign("i_TraceCnt",count($this->a_Trace));
131                         $smarty->assign("i_Type",$this->i_Type);
132                         $smarty->assign("s_Message",$this->s_Message);
133                         $smarty->assign("s_Title",$this->s_Title);
134                         $smarty->assign("i_ID",$this->i_ID);
135                         $smarty->assign("frame",false);
136                         $smarty->assign("JS",session::get('js'));
137                         $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
138                         return($smarty->fetch(get_template_path('msg_dialog.tpl')));
139                 }
140         }
142         
143         public function is_confirmed()
144         {
145                 if(isset($_POST['MSG_OK'.$this->i_ID])){
146                         return(TRUE);
147                 }else{
148                         return(FALSE);
149                 }
150         }
151         
153         public static function get_dialogs()
154         {
155                 $return  ="";
156                 $dialog_ids= "";
158                 $seen = "";
159                 if(isset($_POST['closed_msg_dialogs'])){
160 #                       $seen = $_POST['closed_msg_dialogs'];
161                 }
163                 if(session::is_set('msg_dialogs') && is_array(session::get('msg_dialogs')) && count(session::get('msg_dialogs'))){
165                         /* Get frame one */
166                         $smarty = get_smarty();
167                         $smarty->assign("frame", true);
168                         $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
169                         $return = $smarty->fetch(get_template_path('msg_dialog.tpl'));
171                         if(!session::get('js')){
172                                 $dialog = array_pop(session::get('msg_dialogs'));
173                                 $return.= $dialog->execute();
174                         }else{
175                                 $msg_dialogs = session::get('msg_dialogs');
176                                 foreach($msg_dialogs as $key => $dialog){
178                                         if(preg_match("/".$dialog->get_ID()."/",$seen)){
179                                                 unset($msg_dialogs[$key]);
180                                         }else{
181                                                 $return.= $dialog->execute();
182                                                 $dialog_ids= $dialog->get_ID().",".$dialog_ids;
183                                         }
184                                         unset($msg_dialogs[$key]);
185                                 }
186                                 session::set('msg_dialogs',$msg_dialogs);
187                         }
188                         $dialog_ids = preg_replace("/,$/","",$dialog_ids);
190                         $return .= "</div>";
191                         $return .="<input type='hidden' style='width:400px;' name='pending_msg_dialogs' id='pending_msg_dialogs' value='".$dialog_ids."'>";
192                         $return .="<input type='hidden' style='width:400px;' name='closed_msg_dialogs' id='closed_msg_dialogs' value=''>";
193                         $return .="<input type='hidden' style='width:400px;' name='current_msg_dialogs' id='current_msg_dialogs' value=''>";
194                         $return .="<input type='hidden' style='width:700px;' name='js_debug' id='js_debug'>";
195                 }
196                 return($return);
197         }
200 ?>