You are here

better_messages.module in Better Messages 6.2

File

better_messages.module
View source
<?php

/*
Implementation of hook_menu
*/
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 site configuration',
    ),
    'file' => 'better_messages.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function better_messages_perm() {
  return array(
    'access better messages',
  );
}

/*
Implementation of hook_theme
*/
function better_messages_theme() {
  return array(
    'better_messages_content' => array(
      'arguments' => array(
        'display' => null,
      ),
    ),
    'better_messages_skin' => array(
      'template' => 'better_messages',
      'arguments' => array(
        'content' => null,
      ),
    ),
  );
}

/*
Help function to scan for skins
*/
function better_messages_get_skins() {
  $default_theme = variable_get('theme_default', 'none');

  // Call skins in modules skins directory
  $default_skins_directory = drupal_get_path('module', 'better_messages') . '/skins';
  $files = file_scan_directory($default_skins_directory, '\\.css$');

  // Now we call skins in the default theme skins directory
  $theme_skins_directory = drupal_get_path('theme', $default_theme) . '/skins';
  $theme_files = file_scan_directory($theme_skins_directory, '\\.css$');

  // Merge the options and make the names readable.
  $files = array_merge($files, $theme_files);
  $skins = array();
  foreach ($files as $file) {
    $skins[$file->filename] = drupal_ucfirst($file->name);
  }
  ksort($skins);
  return $skins;
}

/*
Implementation of hook_theme_registry_alter to add better_messages theme function
*/
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);

    // We save the intial output to SESSION so we can put it in <noscript> on page_preprocess
    $_SESSION['original_messages'] = $message;
    $message_skin = theme('better_messages_skin', $message);

    // We add a special ID so that JavaScript knows what to handle. Changing the ID will break JavaScript handling code.
    $output = "<div id=\"better-messages-wrapper\" style=\"display:none;overflow:visible;position:absolute;z-index:101;\">\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;
}

/*
Preprocess the default status_messages so we add our template file here
*/
function better_messages_preprocess_page(&$vars) {
  if ($vars['messages']) {
    $settings = better_messages_get_settings();

    // Check if path Better Messages is enabled for this path
    $better_messages = better_messages_process_visibility();
    $access = user_access('access better messages');
    if ($better_messages && $access) {
      $message = $vars['messages'];

      // Here comes the graceful degrade for browsers with JS disabled
      $vars['messages'] = "<noscript>\n" . $_SESSION['original_messages'] . "\n</noscript>\n";
      unset($_SESSION['original_messages']);
      $vars['closure'] .= $message;
    }
  }
}

/*
Helper function to get the settings
*/
function better_messages_get_settings() {

  // Invoke better_messages_skins
  $skins = better_messages_get_skins();

  // Settings
  $settings = array(
    'position' => variable_get('better_messages_pos', 'center'),
    'vertical' => variable_get('better_messages_vertical', 0),
    'horizontal' => variable_get('better_messages_horizontal', 10),
    'fixed' => variable_get('better_messages_fixed', 1),
    'width' => variable_get('better_messages_width', '400px'),
    'autoclose' => variable_get('better_messages_autoclose', 0),
    'opendelay' => variable_get('better_messages_open_delay', 0.3),
    'disable_autoclose' => variable_get('better_messages_disable_autoclose', 0),
    'popin' => array(
      'effect' => variable_get('better_messages_popin_effect', 'fadeIn'),
      'duration' => variable_get('better_messages_popin_duration', 'slow'),
    ),
    'popout' => array(
      'effect' => variable_get('better_messages_popout_effect', 'fadeOut'),
      'duration' => variable_get('better_messages_popout_duration', 'slow'),
    ),
    'extra' => array(
      'skin' => variable_get('better_messages_skin', key($skins)),
      'skins' => $skins,
      'pages' => variable_get('better_messages_pages', ''),
      'visibility' => variable_get('better_messages_visibility', 0),
    ),
  );
  return $settings;
}

/*
Helper function to check where to popup Better Messages relative to path
*/
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;
}

/*
Helper function to add required JS and CSS only when required
*/
function better_messages_add_files($disable_autoclose) {
  $settings = better_messages_get_settings();
  drupal_add_css($settings['extra']['skin']);
  drupal_add_js(drupal_get_path('module', 'better_messages') . '/better_messages.js');

  // Remove extra info from JS
  unset($settings['extra']);
  if ($disable_autoclose) {
    $settings['autoclose'] = 0;
  }
  drupal_add_js(array(
    'betterMessages' => $settings,
  ), 'setting');
}