FusionDirectory
class_msg_dialog.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2020 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
26 define('INFO_DIALOG', 10001);
27 define('WARNING_DIALOG', 10002);
28 define('ERROR_DIALOG', 10003);
29 // LDAP_ERROR is the same as ERROR_DIALOG
30 define('LDAP_ERROR', 10003);
31 define('CONFIRM_DIALOG', 10004);
32 define('FATAL_ERROR_DIALOG', 10006);
33 
39 {
40  private $s_Title;
41  private $s_Message;
42  private $i_Type;
43  private $i_ID;
44  private $a_Trace;
45 
57  public function __construct (string $title, string $message, int $type = INFO_DIALOG, array $trace = [])
58  {
59  if (!in_array($type, [INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG])) {
60  trigger_error('Invalid msg_dialog type.');
61  $type = INFO_DIALOG;
62  }
63 
64  $this->i_ID = (int)preg_replace('/[^0-9]*/', '', microtime());
65  $this->s_Title = $title;
66  $this->s_Message = $message;
67  $this->i_Type = $type;
68  $this->a_Trace = $trace;
69  }
70 
71  protected function show ()
72  {
73  global $config;
74 
75  if (empty($this->s_Message)) {
76  return;
77  }
78 
79  if ((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))) {
80  session::set('errorsAlreadyPosted', []);
81  }
82 
83  $errorsAlreadyPosted = session::get('errorsAlreadyPosted');
84  if (!isset($errorsAlreadyPosted[$this->s_Title.$this->s_Message])) {
85  $errorsAlreadyPosted[$this->s_Title.$this->s_Message] = 0;
86  }
87  $errorsAlreadyPosted[$this->s_Title.$this->s_Message]++;
88 
89  session::set('errorsAlreadyPosted', $errorsAlreadyPosted);
90 
91  if ($errorsAlreadyPosted[$this->s_Title.$this->s_Message] > 1) {
92  /* Skip if the same message was already reported once */
93  return;
94  }
95 
96  /* Append trace information, only if error messages are enabled */
97  if (isset($config) && is_object($config) &&
98  $config->get_cfg_value('displayerrors') == 'TRUE') {
99  if (empty($this->a_Trace)) {
100  $this->a_Trace = debug_backtrace();
101  }
102  } else {
103  $this->a_Trace = [];
104  }
105  if (session::is_set('msg_dialogs')) {
106  $msg_dialogs = session::get('msg_dialogs');
107  } else {
108  $msg_dialogs = [];
109  }
110  $msg_dialogs[] = $this;
111  session::set('msg_dialogs', $msg_dialogs);
112  }
113 
125  public static function display ($title, string $message, int $type = INFO_DIALOG, array $trace = [])
126  {
127  if ($type === FATAL_ERROR_DIALOG) {
128  /* Deprecated */
129  throw new FatalError($message);
130  }
131  if ($title instanceof FusionDirectoryError) {
132  static::display(...$title->computeMsgDialogParameters());
133  return;
134  }
135  $dialog = new msg_dialog($title, $message, $type, $trace);
136  $dialog->show();
137  }
138 
139  /*
140  * \brief Display checks
141  *
142  * \param array $messages Contains messages
143  */
144  public static function displayChecks ($messages)
145  {
146  foreach ($messages as $error) {
147  if ($error instanceof FusionDirectoryError) {
148  static::display(...$error->computeMsgDialogParameters());
149  } else {
150  static::display(_('Error'), $error, ERROR_DIALOG);
151  }
152  }
153  }
154 
155  /*
156  * \brief Accessor of message dialog's identifier
157  *
158  * \return The identifier of the message dialog
159  */
160  public function getId (): int
161  {
162  return $this->i_ID;
163  }
164 
170  public function is_confirmed (): bool
171  {
172  return isset($_POST['MSG_OK'.$this->i_ID]);
173  }
174 
178  protected function getDialogInfos (): array
179  {
180  return [
181  'id' => $this->i_ID,
182  'type' => $this->i_Type,
183  'title' => $this->s_Title,
184  'message' => $this->s_Message,
185  'trace' => ((count($this->a_Trace) > 0) ? print_a($this->a_Trace, TRUE) : ''),
186  ];
187  }
188 
192  public static function get_dialogs (): string
193  {
194  if (session::is_set('msg_dialogs') &&
195  is_array(session::get('msg_dialogs')) &&
196  count(session::get('msg_dialogs'))) {
197  $smarty = get_smarty();
198 
199  $msg_dialogs = session::get('msg_dialogs');
200  $dialogInfos = [];
201  $dialogIds = [];
202  foreach ($msg_dialogs as $dialog) {
203  $dialogInfos[] = $dialog->getDialogInfos();
204  $dialogIds[] = $dialog->getId();
205  }
206  session::set('msg_dialogs', []);
207 
208  $smarty->assign('dialogInfos', $dialogInfos);
209  $smarty->assign('dialogIds', $dialogIds);
210 
211  return $smarty->fetch(get_template_path('msg_dialog.tpl'));
212  } else {
213  return '';
214  }
215  }
216 }
get_template_path($filename='', $plugin=FALSE, $path='')
Return themed path for specified base file.
Definition: functions.inc:174
static get($name)
Accessor of a session var.
static get_dialogs()
Accessor of the message dialog rendered HTML.
is_confirmed()
Check if the message is confirmed by user.
__construct(string $title, string $message, int $type=INFO_DIALOG, array $trace=[])
Message dialog constructor.
static display($title, string $message, int $type=INFO_DIALOG, array $trace=[])
Display a message dialog.
static set($name, $value)
Set a value in a session.
getDialogInfos()
Return an array with infos for the template.
& get_smarty()
Get global smarty object.
Definition: functions.inc:324
This class contains all the function needed to make messages dialogs.
Fatal error class. Does not extend FusionDirectoryError.
Parent class for all errors in FusionDirectory.
static is_set($name)
Check if the name of the session is set.