Commit e1e52839 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

Improved msg_dialog code

Showing with 48 additions and 41 deletions
+48 -41
...@@ -23,12 +23,12 @@ ...@@ -23,12 +23,12 @@
* Source code for class message dialog * Source code for class message dialog
*/ */
define("INFO_DIALOG", 10001); define('INFO_DIALOG', 10001);
define("WARNING_DIALOG", 10002); define('WARNING_DIALOG', 10002);
define("ERROR_DIALOG", 10003); define('ERROR_DIALOG', 10003);
define("LDAP_ERROR", 10003); // LDAP_ERROR is the same as ERROR_DIALOG define('LDAP_ERROR', 10003); // LDAP_ERROR is the same as ERROR_DIALOG
define("CONFIRM_DIALOG", 10004); define('CONFIRM_DIALOG', 10004);
define("FATAL_ERROR_DIALOG", 10006); define('FATAL_ERROR_DIALOG', 10006);
/*! /*!
* \brief This class contains all the function needed to make messages * \brief This class contains all the function needed to make messages
...@@ -36,13 +36,12 @@ define("FATAL_ERROR_DIALOG", 10006); ...@@ -36,13 +36,12 @@ define("FATAL_ERROR_DIALOG", 10006);
*/ */
class msg_dialog class msg_dialog
{ {
private $s_Title = "Undefined"; private $s_Title = 'Undefined';
private $s_Message = "Undefined"; private $s_Message = 'Undefined';
private $i_Type = INFO_DIALOG; private $i_Type = INFO_DIALOG;
private $i_ID = 0; private $i_ID = 0;
private $a_Trace = array(); private $a_Trace = array();
/*! /*!
* \brief Message dialog constructor * \brief Message dialog constructor
* *
...@@ -53,54 +52,61 @@ class msg_dialog ...@@ -53,54 +52,61 @@ class msg_dialog
* \param string $i_type The type of the message dialog, by default = INFO_DIALOG * \param string $i_type The type of the message dialog, by default = INFO_DIALOG
*/ */
public function __construct($s_title, $s_message, $i_type) public function __construct($s_title, $s_message, $i_type)
{
if (!in_array($i_type, array(INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG))) {
trigger_error('Invalid msg_dialog type.');
$i_type = INFO_DIALOG;
}
$this->i_ID = preg_replace('/[^0-9]*/', '', microtime());
$this->s_Title = $s_title;
$this->s_Message = $s_message;
$this->i_Type = $i_type;
}
protected function show()
{ {
global $config; global $config;
if (empty($s_message)) { if (empty($this->s_Message)) {
return; return;
} }
if (!in_array($i_type, array(INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG))) {
trigger_error("Invalid msg_dialog type.");
$i_type = INFO_DIALOG;
}
if ((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))) { if ((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))) {
session::set('errorsAlreadyPosted', array()); session::set('errorsAlreadyPosted', array());
} }
$errorsAlreadyPosted = session::get('errorsAlreadyPosted'); $errorsAlreadyPosted = session::get('errorsAlreadyPosted');
if (!isset($errorsAlreadyPosted[$s_title.$s_message])) { if (!isset($errorsAlreadyPosted[$this->s_Title.$this->s_Message])) {
$errorsAlreadyPosted[$s_title.$s_message] = 0; $errorsAlreadyPosted[$this->s_Title.$this->s_Message] = 0;
} }
$errorsAlreadyPosted[$s_title.$s_message]++; $errorsAlreadyPosted[$this->s_Title.$this->s_Message]++;
if ($errorsAlreadyPosted[$s_title.$s_message] <= 1) { session::set('errorsAlreadyPosted', $errorsAlreadyPosted);
$this->i_ID = preg_replace("/[^0-9]*/", "", microtime()); if ($errorsAlreadyPosted[$this->s_Title.$this->s_Message] > 1) {
$this->s_Title = $s_title; /* Skip if the same message was already reported once */
$this->s_Message = $s_message; return;
$this->i_Type = $i_type; }
/* Append trace information, only if error messages are enabled */ /* Append trace information, only if error messages are enabled */
if (isset($config) && is_object($config) && if (isset($config) && is_object($config) &&
$config->get_cfg_value("displayerrors") == "TRUE") { $config->get_cfg_value('displayerrors') == 'TRUE') {
$this->a_Trace = debug_backtrace(); $this->a_Trace = debug_backtrace();
} }
if (!session::is_set('msg_dialogs')) { if ($this->i_Type == FATAL_ERROR_DIALOG) {
session::set('msg_dialogs', array()); restore_error_handler();
} error_reporting(E_ALL);
if ($this->i_Type == FATAL_ERROR_DIALOG) { echo $this->execute();
restore_error_handler(); } else {
error_reporting(E_ALL); if (session::is_set('msg_dialogs')) {
echo $this->execute(); $msg_dialogs = session::get('msg_dialogs');
} else { } else {
$msg_dialogs = session::get('msg_dialogs'); $msg_dialogs = array();
$msg_dialogs[] = $this;
session::set('msg_dialogs', $msg_dialogs);
} }
$msg_dialogs[] = $this;
session::set('msg_dialogs', $msg_dialogs);
} }
session::set('errorsAlreadyPosted', $errorsAlreadyPosted);
} }
/*! /*!
...@@ -114,7 +120,8 @@ class msg_dialog ...@@ -114,7 +120,8 @@ class msg_dialog
*/ */
public static function display($s_title, $s_message, $i_type = INFO_DIALOG) public static function display($s_title, $s_message, $i_type = INFO_DIALOG)
{ {
new msg_dialog($s_title, $s_message, $i_type); $dialog = new msg_dialog($s_title, $s_message, $i_type);
$dialog->show();
} }
/* /*
...@@ -125,7 +132,7 @@ class msg_dialog ...@@ -125,7 +132,7 @@ class msg_dialog
public static function displayChecks($messages) public static function displayChecks($messages)
{ {
foreach ($messages as $error) { foreach ($messages as $error) {
msg_dialog::display(_('Error'), $error, ERROR_DIALOG); static::display(_('Error'), $error, ERROR_DIALOG);
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment