You are here

purr_messages.module in Purr Messages 7

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
 */

/**
 * Implements hook_permission().
 */
function purr_messages_permission() {
  return array(
    'view purr messages' => array(
      'title' => 'View purr messages',
    ),
    'administer purr messages' => array(
      'title' => 'Administer purr messages',
    ),
  );
}

/**
 * Implements hook_menu().
 */
function purr_messages_menu() {
  $items = array();
  $items['admin/config/user-interface/purr'] = array(
    'title' => 'Purr Messages',
    'description' => '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;
}

/**
 * Implements 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') {
    return t('Set your options and decide whether you want the purr styled messages
      to appear on admin pages or not.');
  }
}

/**
 * Implements hook_theme().
 */
function purr_messages_theme($existing, $type, $theme, $path) {
  return array(
    'original_status_messages' => array(
      'variables' => array(
        'type' => NULL,
        'messages' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_settings().
 * Configuration settings page.
 *
 * @return
 *    Array of the system settings form.
 */
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 options here. Example option code is
      already provided, simply adjust the values to suit. More information about the
      options 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),
  );
  $form['purr_messages_explicit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only show purr message style if explicitly called'),
    '#description' => t('This option will only display a purr style message
      if called explicitly with the \'purr\' parameter as the type in
      <code>drupal_set_message()</code>.<br/> For example:
      <code>drupal_set_message(\'my message\'), \'purr\');</code>'),
    '#default_value' => variable_get('purr_messages_explicit', FALSE),
  );
  return system_settings_form($form);
}

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

/**
 * Checks options and determines which type of message
 * to return to the theme layer.
 *
 * @param $variables
 *   Array containing theme variables.
 *
 * @return
 *    A string containing a formatted message, either purr or original style.
 */
function purr_messages_status_messages($variables) {
  $display = $variables['display'];
  $output = '';
  $purr = NULL;
  foreach (drupal_get_messages($display) as $type => $messages) {
    if (user_access('view purr messages') && variable_get('purr_enabled', TRUE) == TRUE) {
      if (variable_get('purr_messages_admin_path', FALSE) && path_is_admin(substr(request_uri(), 1))) {
        $output .= theme('original_status_messages', array(
          'type' => $type,
          'messages' => $messages,
        ));
      }
      else {
        if (variable_get('purr_messages_explicit', FALSE)) {
          if ($type == 'purr') {
            $purr[] = _purr_messages_purr($type, $messages);
          }
          else {
            $output .= theme('original_status_messages', array(
              'type' => $type,
              'messages' => $messages,
            ));
          }
        }
        else {
          if (_purr_messages_devel_check($messages)) {
            $output .= theme('original_status_messages', array(
              'type' => $type,
              'messages' => $messages,
            ));
          }
          else {
            $purr[] = _purr_messages_purr($type, $messages);
          }
        }
      }
    }
    else {

      // Return the message using the original theme function.
      $output .= theme('original_status_messages', array(
        'type' => $type,
        'messages' => $messages,
      ));
    }
  }
  $module_path = drupal_get_path('module', 'purr_messages');
  $custom_css = purr_messages_status($themelayer = TRUE);
  $custom_css ? drupal_add_css($custom_css) : drupal_add_css($module_path . '/purrcss/purr.css');
  if ($purr) {
    drupal_add_js($module_path . '/js/jquery.purr.js');
    $output .= "<script type=\"text/javascript\">";
    $output .= "(function (\$) {\nDrupal.behaviors.purr_messages = {\n \n      attach: function(context) {\n var notice = ";
    foreach ($purr as $purr_message) {
      $script[] = $purr_message['script'];
    }
    $output .= implode(' + ', $script);

    // 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 .= "}\n};\n})(jQuery);</script>\n";
    $output .= "<noscript>\n";
    foreach ($purr as $purr_message) {
      $output .= $purr_message['noscript'];
    }
    $output .= "</noscript>\n";
  }
  return $output;
}

/**
 * Checks for the existence of devel code and returns boolean
 *
 * @param $messages
 *   An array, each containing a message.
 *
 * @return
 *   Boolean.
 */
function _purr_messages_devel_check($messages) {
  foreach ($messages as $message) {
    if (strstr($message, '<pre>') || strstr($message, '<textarea') || strstr($message, 'krumo')) {

      // Devel message found.
      return TRUE;
    }
  }

  // No devel messages.
  return FALSE;
}

/**
 * Builds and returns the formatted purr message code
 *
 * @param $type
 *   String containing a message type. Used to set the class on the message div.
 *
 * @param $messages
 *   An array, each containing a message.
 *
 * @return
 *   A string containing the formatted messages.
 */
function _purr_messages_purr($type, $messages) {
  $script = '';
  $script .= "'<div class=\"notice {$type}\">'\n + '<div class=\"notice-body\">'";
  if (count($messages) > 1) {
    $script .= "+ '<ul>'\n";
    foreach ($messages as $message) {
      $script .= "+  '<li>" . addslashes($message) . "</li>'\n";
    }
    $script .= "+ '</ul>'\n";
  }
  else {
    $script .= "\n+ '" . $messages[0] . "'\n";
  }
  $script .= "+ '</div>'\n + '<div class=\"notice-bottom\">'\n + \n    '</div>' + '</div>'\n";
  $output['script'] = $script;
  $output['noscript'] = theme('original_status_messages', array(
    'type' => $type,
    'messages' => $messages,
  ));
  return $output;
}

/**
 * Checks 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.
 *
 * @param $themelayer
 *   (optional) Boolean determines where function has been called from.
 *
 * @return
 *    A string containing the path to the custom css.
 */
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 $type
 *   String containing a message type. Used to set the class on the message div.
 *
 * @param $messages
 *   An array, each containing a message.
 *
 * @return
 *   A string containing the formatted messages.
 */
function theme_original_status_messages($vars) {
  $type = $vars['type'];
  $messages = $vars['messages'];
  $output = '';
  $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;
}

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

Functions

Namesort descending Description
purr_messages_help Implements hook_help(). Display some help to ensure the jquery files are downloaded into the right place
purr_messages_menu Implements hook_menu().
purr_messages_permission Implements hook_permission().
purr_messages_settings Implements hook_settings(). Configuration settings page.
purr_messages_status Checks to see whether the custom files exist.
purr_messages_status_messages Checks options and determines which type of message to return to the theme layer.
purr_messages_theme Implements hook_theme().
purr_messages_theme_registry_alter Implements hook_theme_registry_alter().
theme_original_status_messages Return a themed set of status and/or error messages. The messages are grouped by type.
_purr_messages_defaults Returns the default javscript options
_purr_messages_devel_check Checks for the existence of devel code and returns boolean
_purr_messages_purr Builds and returns the formatted purr message code