purr_messages.module in Purr Messages 7.2
Same filename and directory in other branches
Purr Messages A jQuery based override of Drupal's core message system
File
purr_messages.moduleView source
<?php
/**
* @file
* Purr Messages
* A jQuery based override of Drupal's core message system
*/
/**
* Constants
*/
// Define a default fade in speed.
define('PURR_FADE_IN', 1200);
// Define a default fade out speed.
define('PURR_FADE_OUT', 2000);
// Define a default timeout speed.
define('PURR_TIMER', 5000);
// Show this notice on every page except the listed pages.
define('PURR_VISIBILITY_NOTLISTED', 0);
// Show this notice on only the listed pages.
define('PURR_VISIBILITY_LISTED', 1);
// Show this notice if the associated PHP code returns TRUE.
define('PURR_VISIBILITY_PHP', 2);
// Which DOM element to attach to.
define('PURR_ATTACH_TO', 'body');
// Whether notices should be sticky or not.
define('PURR_STICKY', FALSE);
/**
* Implements hook_permission().
*
* @return array
*/
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().
*
* @return array
*/
function purr_messages_menu() {
$items = array();
$items['admin/config/user-interface/purr'] = array(
'title' => 'Configure 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',
),
'file' => 'purr_messages.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_help().
* Display some help to ensure the jquery files are downloaded into the right place
*
* @param $path
* @param $arg
*
* @return array|null|string
*/
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().
*
* @param $existing
* @param $type
* @param $theme
* @param $path
*
* @return array
*/
function purr_messages_theme($existing, $type, $theme, $path) {
return array(
'original_status_messages' => array(
'variables' => array(
'type' => NULL,
'messages' => NULL,
),
),
);
}
/**
* Implements hook_theme_registry_alter().
*
* Allows the module to override the core status_messages function.
*
* @param $theme_registry
*/
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 string A string containing a formatted message, either purr or original style.
*/
function purr_messages_status_messages($variables) {
$display = $variables['display'];
$output = '';
$purr = NULL;
// Get the messages.
$messages = drupal_get_messages($display);
// Filter messages if disable_message module is enabled and filtering is
// also enabled.
if (module_exists('disable_messages') && variable_get('disable_messages_enable', '1')) {
$messages = disable_messages_apply_filters($messages);
}
foreach ($messages as $type => $messages) {
if (purr_messages_type($type, $messages) !== FALSE) {
$purr[] = _purr_messages_purr($type, array_unique($messages));
}
else {
$output .= theme('original_status_messages', array(
'type' => $type,
'messages' => $messages,
));
}
}
$module_path = drupal_get_path('module', 'purr_messages');
$custom_css = purr_messages_status(TRUE);
$custom_css ? drupal_add_css($custom_css) : drupal_add_css($module_path . '/purrcss/purr.css');
if ($purr) {
// Add the purr js
drupal_add_js($module_path . '/js/jquery.timer.js');
drupal_add_js($module_path . '/js/jquery.purr.js');
// Add the settings
$settings = array(
'fadeInSpeed' => variable_get('purr_messages_fade_in', PURR_FADE_IN),
'fadeOutSpeed' => variable_get('purr_messages_fade_out', PURR_FADE_OUT),
'removeTimer' => variable_get('purr_messages_timer', PURR_TIMER),
'pauseOnHover' => variable_get('purr_messages_hover', TRUE) ? TRUE : FALSE,
'usingTransparentPNG' => variable_get('purr_messages_transparent', TRUE) ? TRUE : FALSE,
'attachTo' => variable_get('purr_messages_attachto', PURR_ATTACH_TO),
'sticky' => variable_get('purr_messages_sticky', PURR_STICKY),
);
drupal_add_js(array(
'purr_messages' => $settings,
), 'setting');
$output .= "<script type=\"text/javascript\">";
$output .= "(function(\$) {\nDrupal.behaviors.purr_messages = {\n\n attach: function(context) {\n if (\$('#purr-container').length == 0) { \nvar notice = ";
foreach ($purr as $purr_message) {
$script[] = $purr_message['script'];
}
$script = array_unique($script);
$output .= implode(' + ', $script);
$output .= "\$(notice).purr();";
// Finish off the script.
$output .= "\n}\n}\n}\n})(jQuery);</script>\n";
$output .= "<noscript>\n";
foreach ($purr as $purr_message) {
$output .= $purr_message['noscript'];
}
$output .= "</noscript>\n";
}
return $output;
}
/**
* Contains the logic to determine whether a message should be shown
* using purr messages style or the standard Drupal method.
*
* @param $type
* A string which is usually status but could be anything.
*
* @param $messages
*
* @return bool Boolean, true for purr messages, false for standard messages.
*/
function purr_messages_type($type, $messages) {
// Set defaults for and page.
$page_match = TRUE;
// First check if the user has permission to view purr messages
if (user_access('view purr messages') && variable_get('purr_enabled', TRUE) == TRUE) {
$visibility = variable_get('purr_messages_visibility', 0);
$pages = variable_get('purr_messages_pages', '');
// Check to see if admin path is selected and this is an admin path.
if (variable_get('purr_messages_admin_path', FALSE) && path_is_admin(substr(request_uri(), 1))) {
return FALSE;
}
// Check to see if this path is allowed
if ($pages !== '') {
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = drupal_strtolower($pages);
if ($visibility < PURR_VISIBILITY_PHP) {
// Convert the Drupal path to lowercase
$path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
// When $visibility has a value of 0 (PURR_VISIBILITY_NOTLISTED),
// the message is displayed on all pages except those listed in $pages.
// When set to 1 (PURR_VISIBILITY_LISTED), it is displayed only on those
// pages listed in $pages.
$page_match = !($visibility xor $page_match);
}
elseif (module_exists('php')) {
$page_match = php_eval($pages);
}
}
if ($page_match == TRUE) {
// Check to see if purr messages has been explicitly called.
if (variable_get('purr_messages_explicit', FALSE)) {
if (strpos($type, 'purr') !== FALSE) {
return (bool) _purr_messages_devel_check($messages);
}
else {
return FALSE;
}
}
// Try to catch dpm (devel) calls
return (bool) _purr_messages_devel_check($messages);
}
return FALSE;
}
// Shouldn't usually get to here so return default FALSE.
return FALSE;
}
/**
* Checks for the existence of devel code and returns boolean
*
* @param $messages
* An array, each containing a message.
*
* @return bool 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 FALSE;
}
}
// No devel messages.
return TRUE;
}
/**
* 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 array A string containing the formatted messages.
*/
function _purr_messages_purr($type, $messages) {
$script = '';
$pattern = array(
"\r\n",
"\r",
"\n",
"\t",
);
$script .= "'<div class=\"notice {$type}\">'\n + '<div class=\"notice-body\">'";
if (count($messages) > 1) {
$script .= "+ '<ul>'\n";
foreach ($messages as $message) {
$script .= "+ '<li>" . str_replace($pattern, ' ', addslashes($message)) . "</li>'\n";
}
$script .= "+ '</ul>'\n";
}
else {
$script .= "\n+ '" . str_replace($pattern, ' ', addslashes($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 bool $themelayer
* (optional) Boolean determines where function has been called from.
*
* @return bool|string 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 $vars
*
* @internal param $type String containing a message type. Used to set the class on the message div.
*
* @internal param $messages An array, each containing a message.
*
* @return string 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;
}
Functions
Name![]() |
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_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(). |
purr_messages_type | Contains the logic to determine whether a message should be shown using purr messages style or the standard Drupal method. |
theme_original_status_messages | Return a themed set of status and/or error messages. The messages are grouped by type. |
_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 |