You are here

purr_messages.module in Purr Messages 6

Purr Messages A jQuery based override of Drupal's core message system

File

purr_messages.module
View source
<?php

/**
 * @file
 * Purr Messages
 * A jQuery based override of Drupal's core message system
 */

/**
 * Implementation of hook_perm().
 */
function purr_messages_perm() {
  return array(
    'view purr messages',
    'administer purr messages',
  );
}

/**
 * Implementation of hook_menu().
 */
function purr_messages_menu() {
  $items = array();
  $items['admin/settings/purr'] = array(
    'title' => t('Purr Messages'),
    'description' => t('Settings to control the purr messages output.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'purr_messages_settings',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer purr messages',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Returns the default javscript options
 */
function purr_messages_defaults() {
  return "\$(notice).purr({\n  fadeInSpeed:1200,\n  fadeOutSpeed:2000,\n  removeTimer:5000,\n  usingTransparentPNG:true\n});";
}

/**
 * Implementation of hook_help().
 * Display some help to ensure the jquery files are downloaded into the right place
 */
function purr_messages_help($path, $arg) {
  if ($path == 'admin/settings/purr') {
    if (module_exists('libraries')) {
      if (!file_exists(libraries_get_path('jquery_purr/jquery.purr.js'))) {
        return t('<strong>jQuery file status:</strong>
          You need to download the Purr jquery file from
          <a href="http://jquery-purr.googlecode.com/files/jquery-purr0-1-0.zip">
          http://jquery-purr.googlecode.com/files/jquery-purr0-1-0.zip</a>.<br/>
          Decompress it and put just the jquery.purr.js file inside a directory
          called jquery_purr in your !libraries path. For example the final path
          could be: /sites/all/libraries/jquery_purr/jquery.purr.js', array(
          '!libraries' => l('Libraries', 'http://drupal.org/project/libraries'),
        ));
      }
      else {
        return t('<strong>jQuery file status:</strong> The jquery file is in place.');
      }
    }
    else {
      return t('You must install the <a href="http://drupal.org/project/libraries">Libraries API</a>');
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function purr_messages_theme($existing, $type, $theme, $path) {
  return array(
    'original_status_messages' => array(
      'arguments' => array(
        'display' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_settings().
 * Configuration settings page.
 */
function purr_messages_settings() {
  $module_path = drupal_get_path('module', 'purr_messages');
  $form['purr_messages_code_status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Purr Messages settings'),
    '#weight' => -10,
    '#description' => purr_messages_status() !== FALSE ? t('You have a custom css file in: <br/>/%path/purrcss/') : t('You are using the default purr style. To use custom css and images simply
      copy the <em>\'purrcss\'</em> folder from: <br/>%modulepath<br/> and place
      it in your theme folder: <br/>%path<br/>Then make the alterations you want
      to the copied version in your theme folder. This module will automatically
      pick up the changes and this message will change if you\'ve done it
      correctly.<br/>', array(
      '%path' => path_to_theme(),
      '%modulepath' => $module_path,
    )),
  );
  $form['purr_messages_code_status']['purr_messages_code'] = array(
    '#type' => 'textarea',
    '#title' => t('The settings code'),
    '#rows' => 10,
    '#weight' => -10,
    '#default_value' => variable_get('purr_messages_code', purr_messages_defaults()),
    '#description' => t('Add the purr messages code here. Example code is already
      provided, simply adjust the values to suit. More information can be found
      here: !netperspective', array(
      '!netperspective' => l('http://kitchen.net-perspective.com/open-source/purr', 'http://kitchen.net-perspective.com/open-source/purr'),
    )),
  );
  $form['purr_messages_admin_path'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable purr messages on admin paths'),
    '#description' => t('Checking this box will disable purr messages on admin paths,
      as determined by the path_is_admin() function.'),
    '#default_value' => variable_get('purr_messages_admin_path', FALSE),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_theme_registry_alter().
 * Allows the module to override the core status_messages function with purr_messages_status_messages.
 */
function purr_messages_theme_registry_alter(&$theme_registry) {
  $theme_registry['status_messages']['function'] = 'purr_messages_status_messages';
}

/**
 * Build the message status html inside a jquery object.
 *
 * Adds jQuery and purr code around the object and then
 * returns it back to the theme layer ($messages).
 */
function purr_messages_status_messages($display = NULL) {
  if (user_access('view purr messages') && variable_get('purr_enabled', TRUE) == TRUE) {
    $dgm = drupal_get_messages($display, FALSE);

    // Try to catch dpm (devel) calls
    if (isset($dgm['status'])) {
      foreach ($dgm['status'] as $msg) {
        if (variable_get('purr_messages_admin_path', FALSE) || strstr($msg, '<pre>') || strstr($msg, '<textarea') || strstr($msg, 'krumo')) {

          // Return the message using the original theme function.
          return theme('original_status_messages');
        }
      }
    }
    $module_path = drupal_get_path('module', 'purr_messages');
    if (!module_exists('libraries')) {

      // Check that Libraries API is installed.
      drupal_set_message(t('Please install the !libraries to use purr messages.', array(
        '!libraries' => l('Libraries API', 'http://drupal.org/project/libraries'),
      )), 'error');
      return theme('original_status_messages');

      // Return a warning.
    }
    if (file_exists(libraries_get_path('jquery_purr/jquery.purr.js'))) {
      drupal_add_js(libraries_get_path('jquery_purr/jquery.purr.js'));
      $custom_css = purr_messages_status($themelayer = TRUE);
      $custom_css ? drupal_add_css($custom_css) : drupal_add_css($module_path . '/purrcss/purr.css');
      $output = '';
      foreach (drupal_get_messages($display) as $type => $messages) {

        // This javascript will be printed instead of the standard message content.
        $output .= "<script type=\"text/javascript\">";
        $output .= "Drupal.behaviors.purr_messages = function(context) {\n var notice = ";
        $output .= "'<div class=\"notice {$type}\">'\n + '<div class=\"notice-body\">'";
        if (count($messages) > 1) {
          $output .= "+ '<ul>'\n";
          foreach ($messages as $message) {
            $output .= "+  '<li>" . addslashes($message) . "</li>'\n";
          }
          $output .= "+ '</ul>'\n";
        }
        else {
          $output .= "\n+ '" . $messages[0] . "'\n";
        }
        $output .= "+ '</div>'\n + '<div class=\"notice-bottom\">'\n + '</div>' + '</div>'\n";

        // Gets the settings if they've been set, otherwise use some defaults.
        $output .= variable_get('purr_messages_code', purr_messages_defaults());

        // Finish off the script.
        $output .= "};</script>\n";

        // Next we add the old style output if javascript is turned off.
        $output .= "<noscript>\n";
        $output .= "<div class=\"messages {$type}\">\n";
        if (count($messages) > 1) {
          $output .= " <ul>\n";
          foreach ($messages as $message) {
            $output .= '  <li>' . $message . "</li>\n";
          }
          $output .= " </ul>\n";
        }
        else {
          $output .= $messages[0];
        }
        $output .= "</div></noscript>\n";
      }
      return $output;
    }
    else {
      drupal_set_message('You need to install jquery.purr.js in jquery_purr
      in your libraries directory. See README.txt for details.', 'warning');
      return theme('original_status_messages', $variables);
    }
  }
  else {

    // Return the message using the original theme function.
    return theme('original_status_messages');
  }
}

/**
 * Check to see whether the custom files exist.
 *
 * Makes a check to the current theme's folder to see whether the
 * purrcss folder and purr.css file exists. Also returns the correct path
 * depending on where the function has been called from.
 */
function purr_messages_status($themelayer = FALSE) {
  if ($themelayer == TRUE) {

    // Called from theme function so path_to_theme returns incorrect result.
    global $theme;
    $custom_css = drupal_get_path('theme', $theme) . '/purrcss/purr.css';
  }
  else {

    // Called from non theme function (admin).
    $custom_css = path_to_theme() . '/purrcss/purr.css';
  }
  if (!is_file($custom_css)) {
    return FALSE;
  }
  return $custom_css;
}

/**
 * Return a themed set of status and/or error messages. The messages are grouped
 * by type.
 *
 * This is the original output which we use if purr messages is turned off.
 *
 * @param $display
 *   (optional) Set to 'status' or 'error' to display only messages of that type.
 *
 * @return
 *   A string containing the messages.
 */
function theme_original_status_messages($display = NULL) {
  $output = '';
  foreach (drupal_get_messages($display) as $type => $messages) {
    $output .= "<div class=\"messages {$type}\">\n";
    if (count($messages) > 1) {
      $output .= " <ul>\n";
      foreach ($messages as $message) {
        $output .= '  <li>' . $message . "</li>\n";
      }
      $output .= " </ul>\n";
    }
    else {
      $output .= $messages[0];
    }
    $output .= "</div>\n";
  }
  return $output;
}

Functions

Namesort descending Description
purr_messages_defaults Returns the default javscript options
purr_messages_help Implementation of hook_help(). Display some help to ensure the jquery files are downloaded into the right place
purr_messages_menu Implementation of hook_menu().
purr_messages_perm Implementation of hook_perm().
purr_messages_settings Implementation of hook_settings(). Configuration settings page.
purr_messages_status Check to see whether the custom files exist.
purr_messages_status_messages Build the message status html inside a jquery object.
purr_messages_theme Implementation of hook_theme().
purr_messages_theme_registry_alter Implementation of hook_theme_registry_alter(). Allows the module to override the core status_messages function with purr_messages_status_messages.
theme_original_status_messages Return a themed set of status and/or error messages. The messages are grouped by type.