View source
<?php
function better_messages_init() {
drupal_add_css(drupal_get_path('module', 'better_messages') . '/better_messages_admin.css');
}
function better_messages_menu() {
$items = array();
$items['admin/settings/better-messages'] = array(
'title' => t('Better Messages settings'),
'description' => t('Better handling of Drupal messages UI'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'better_messages_admin',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer better messages',
),
'file' => 'better_messages.inc',
);
return $items;
}
function better_messages_perm() {
return array(
'access better messages',
'administer better messages',
);
}
function better_messages_theme() {
return array(
'better_messages_content' => array(
'arguments' => array(
'display' => null,
),
),
'better_messages' => array(
'template' => 'better_messages',
'arguments' => array(
'content' => null,
),
),
);
}
function better_messages_theme_registry_alter(&$theme_registry) {
$theme_registry['status_messages']['function'] = "theme_better_messages";
}
function theme_better_messages($display = NULL) {
$output = '';
$better_messages = better_messages_process_visibility();
$access = user_access('access better messages');
$message = drupal_get_messages($display, false);
if ($better_messages && $access && !empty($message)) {
$disable_autoclose = array_key_exists('error', $message);
better_messages_add_files($disable_autoclose);
$message = theme('better_messages_content', $display);
$_SESSION['original_messages'] = $message;
$message_skin = theme('better_messages', $message);
$output = "<div id=\"better-messages-wrapper\" style=\"display:none;overflow:visible;position:absolute;z-index:9999;\">\n" . $message_skin . "\n</div>\n";
}
else {
$output = theme('better_messages_content', $display);
}
return $output;
}
function theme_better_messages_content($display = NULL) {
$output = '';
$first = TRUE;
foreach (drupal_get_messages($display) as $type => $messages) {
$class = $first ? 'first' : '';
$first = FALSE;
$output .= "<h2 class=\"messages-label {$type}\">" . t(drupal_ucfirst($type)) . "</h2>\n";
$output .= "<div class=\"messages {$type}\">\n";
if (count($messages) > 1) {
$output .= " <ul>\n";
foreach ($messages as $k => $message) {
if ($k == 0) {
$output .= "<li class='message-item first'>{$message}</li>";
}
else {
if ($k == count($messages) - 1) {
$output .= "<li class='message-item last'>{$message}</li>";
}
else {
$output .= "<li class='message-item'>{$message}</li>";
}
}
}
$output .= " </ul>\n";
}
else {
$output .= $messages[0];
}
$output .= "</div>\n";
}
return $output;
}
function better_messages_preprocess_page(&$vars) {
if ($vars['messages']) {
$settings = better_messages_get_settings();
$better_messages = better_messages_process_visibility();
$access = user_access('access better messages');
if ($better_messages && $access) {
$message = $vars['messages'];
$vars['messages'] = "<noscript>\n" . $_SESSION['original_messages'] . "\n</noscript>\n";
unset($_SESSION['original_messages']);
$vars['closure'] .= $message;
}
}
}
function better_messages_get_settings() {
$settings = array(
'position' => 'center',
'vertical' => 0,
'horizontal' => 10,
'fixed' => 1,
'width' => '400px',
'autoclose' => 0,
'opendelay' => 0.3,
'disable_autoclose' => 0,
'show_countdown' => 1,
'hover_autoclose' => 1,
'popin' => array(
'effect' => 'fadeIn',
'duration' => 'slow',
),
'popout' => array(
'effect' => 'fadeOut',
'duration' => 'slow',
),
'jquery_ui' => array(
'draggable' => 0,
'resizable' => 0,
),
'extra' => array(
'pages' => '',
'visibility' => 0,
),
);
return variable_get('better_messages', $settings);
}
function better_messages_process_visibility() {
$settings = better_messages_get_settings();
if ($settings['extra']['pages']) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $settings['extra']['pages']);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $settings['extra']['pages']);
}
$page_match = !($settings['extra']['visibility'] xor $page_match);
}
else {
$page_match = TRUE;
}
return $page_match;
}
function better_messages_add_files($disable_autoclose) {
$settings = better_messages_get_settings();
drupal_add_css(drupal_get_path('module', 'better_messages') . '/skins/default/better_messages.css');
drupal_add_js(drupal_get_path('module', 'better_messages') . '/better_messages.js');
if (!module_exists('jquery_ui')) {
unset($settings['jquery_ui']);
}
else {
foreach ($settings['jquery_ui'] as $jquery_ui_setting => $value) {
if ($value == 1) {
jquery_ui_add(array(
'ui.' . $jquery_ui_setting,
));
}
}
}
unset($settings['extra']);
if ($disable_autoclose) {
$settings['autoclose'] = 0;
}
drupal_add_js(array(
'betterMessages' => $settings,
), 'setting');
}