Code

Applied in_array strict patches from trunk
[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("OK_CANCEL_DIALOG"       , 10005);
28 define("FATAL_ERROR_DIALOG"     , 10006);
30 class msg_dialog
31 {
32     private $s_Title    = "Undefined";
33     private $s_Message  = "Undefined";
34     private $i_Type     = INFO_DIALOG ;
35     private $i_ID               = 0;
36     private $a_Trace    = array();
38     public function __construct($s_title,$s_message,$i_type)
39     {
40         global $config;
42         if(empty($s_message)) return;
44         if(!in_array_strict($i_type,array(INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG,OK_CANCEL_DIALOG))){
45             trigger_error("Invalid msg_dialog type.");
46             $i_type = INFO_DIALOG;
47         }
49         if((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))){
50             session::set('errorsAlreadyPosted',array());
51         }
53         $errorsAlreadyPosted = session::get('errorsAlreadyPosted');
54         if(!isset($errorsAlreadyPosted[$s_title.$s_message])){
55             $errorsAlreadyPosted[$s_title.$s_message] = 0;
56         }
57         $errorsAlreadyPosted[$s_title.$s_message]++;
59         if($errorsAlreadyPosted[$s_title.$s_message] <=1  ){
61             $this->i_ID          = preg_replace("/[^0-9]*/","",microtime()); 
62             $this->s_Title       = $s_title;
63             $this->s_Message = $s_message;
64             $this->i_Type        = $i_type;
66             /* Append trace information, only if error messages are enabled */
67             if( isset($config) && 
68                     is_object($config) && 
69                     $config->get_cfg_value("core","displayErrors") == "true" ){
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::global_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     }
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     }
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::global_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 ?>