nicemessages.module in Nice messages 7.2
Same filename and directory in other branches
Nicemessages module
Displays drupal messages with jGrowl jQuery plugin.
File
nicemessages.moduleView source
<?php
/**
* @file
* Nicemessages module
*
* Displays drupal messages with jGrowl jQuery plugin.
*/
define('NICEMESSAGES_FIELDNAME', 'field_user_nicemessages_enabled');
define('JGROWL_PATH', 'sites/all/libraries/jgrowl');
/**
* Implements hook_init().
*/
function nicemessages_init() {
// Attach jGrowl files
drupal_add_css(JGROWL_PATH . '/jquery.jgrowl.min.css');
drupal_add_js(JGROWL_PATH . '/jquery.jgrowl.min.js');
// Attach module files
drupal_add_css(drupal_get_path('module', 'nicemessages') . '/css/nicemessages_positions.css');
drupal_add_css(drupal_get_path('module', 'nicemessages') . '/css/' . variable_get('nicemessages_style'));
if (variable_get('nicemessages_shadows') === 1) {
drupal_add_css(drupal_get_path('module', 'nicemessages') . '/css/shadows.css');
}
drupal_add_js(drupal_get_path('module', 'nicemessages') . '/js/nicemessages.js');
}
/**
* Implements hook_menu().
*/
function nicemessages_menu() {
// Module configuration form
$items['admin/config/system/nicemessages'] = array(
'title' => 'Nice messages',
'description' => 'Nice messages configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'nicemessages_settings_form',
),
'access arguments' => array(
'administer nicemessages',
),
);
return $items;
}
/**
* Implements hook_permission().
*/
function nicemessages_permission() {
return array(
'administer nicemessages' => array(
'title' => t('Administer nicemessages'),
'description' => t('Adjust nicemessages settings.'),
),
'toggle nicemessages' => array(
'title' => t('Toggle nicemessages'),
'description' => t('Users with this permission can choose to turn nicemessages on or off for their account.'),
),
);
}
/**
* Implements hook_field_access().
*/
function nicemessages_field_access($op, $field, $entity_type, $entity, $account) {
if ($field['field_name'] == NICEMESSAGES_FIELDNAME) {
// Users with toggle nicemessages permission can edit their settings.
if ($op == 'edit') {
return user_access('toggle nicemessages', $account);
}
elseif ($op == 'view') {
return user_access('administer users');
}
}
return TRUE;
}
/**
* Implements hook_preprocess_page().
*/
function nicemessages_preprocess_page(&$vars) {
if (nicemessages_are_enabled()) {
if (nicemessages_pages_visibility() == FALSE) {
return;
}
// Do not display messages in standard way,
$vars['show_messages'] = FALSE;
// display them with jGrowl
nicemessages_set_messages();
}
}
/**
* Show nicemessages on specific pages
*/
function nicemessages_pages_visibility() {
$pages = variable_get('nicemessages_paths');
$visibility = variable_get('nicemessages_pages');
if (!empty($pages)) {
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = drupal_strtolower($pages);
// 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);
// When $visibility has a value of 0
// preprocess toggles to display nicemessgaes on all pages except those listed in $pages.
// When set to 1,
// it is displayed only on those pages listed in $pages.
$page_match = !($visibility xor $page_match);
if ($page_match) {
return TRUE;
}
else {
return FALSE;
}
// else if ($pages is empty) and $visibility is not NULL (Only listes pages)
// toggles not to display Nicemessgaes on any pages at all.
}
else {
if ($visibility) {
return FALSE;
// else if ($pages is empty) and $visibility is NULL (All pages except listed)
// toggles to display Nicemessages on all pages.
}
else {
return TRUE;
}
}
}
/**
* Check if nicemessages are enabled for current user.
*/
function nicemessages_are_enabled() {
if (variable_get('nicemessages_default_state') === 'on') {
return TRUE;
}
elseif (variable_get('nicemessages_default_state') === 'off') {
return FALSE;
}
elseif (variable_get('nicemessages_default_state') === 'user' && user_access('toggle nicemessages')) {
global $user;
// Load the full user entity with fields attached.
$accounts = entity_load('user', array(
$user->uid,
));
$account = array_shift($accounts);
return empty($account->{NICEMESSAGES_FIELDNAME}[LANGUAGE_NONE][0]['value']) ? FALSE : TRUE;
}
else {
return TRUE;
}
}
/**
* Save messages to js settings.
*/
function nicemessages_set_messages() {
$items = array();
foreach (drupal_get_messages() as $type => $messages) {
foreach ($messages as $key => $message) {
$item = array(
'type' => $type,
'content' => $message,
'life' => variable_get('nicemessages_' . $type . '_life', '6') * 1000,
'glue' => variable_get('nicemessages_' . $type . '_glue', 'after'),
'speed' => variable_get('nicemessages_' . $type . '_speed', 'slow'),
);
$items[] = $item;
}
}
$settings = array(
'position' => variable_get('nicemessages_position', 'bottom-left'),
'items' => $items,
);
drupal_alter('nicemessages', $settings);
drupal_add_js(array(
'nicemessages' => $settings,
), 'setting');
}
/**
* Module settings form.
*/
function nicemessages_settings_form() {
$form['global'] = array(
'#type' => 'fieldset',
'#title' => t('Global settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['global']['nicemessages_default_state'] = array(
'#type' => 'radios',
'#title' => t('Activation method'),
'#default_value' => variable_get('nicemessages_default_state', 'on'),
'#options' => array(
'on' => t('On'),
'off' => t('Off'),
'user' => t('User'),
),
'#description' => t('' . 'The settings <strong>On</strong> (1) and <strong>Off</strong> (2) ' . 'are both global settings affecting all users including Anonoumus user, ' . 'ignoring any user settings on user account profile forms.<br /> ' . 'The setting <strong>User</strong> (3) lets users ' . '(but only of user roles with "toggle nicemessages" permission!) ' . 'toggle Nicemessages by selecting On/Off on their user account profile forms.'),
);
$form['global']['nicemessages_position'] = array(
'#type' => 'select',
'#title' => t('Message popup screen position'),
'#default_value' => variable_get('nicemessages_position', 'top-right'),
'#options' => array(
'top-left' => t('top left'),
'center' => t('top center'),
'top-right' => t('top right'),
'bottom-left' => t('bottom left'),
'bottom-right' => t('bottom right'),
),
'#description' => t('' . 'Choose where the popup messages should be displayed in the browser port view.<br /> ' . 'The appending/prepending of multiple messages will automatically change regarding to your choosen position.'),
);
$form['global']['nicemessages_style'] = array(
'#type' => 'select',
'#title' => t('Nice messages styles (colors)'),
'#default_value' => variable_get('nicemessages_style', 'nicemessages_jgrowl.css'),
'#options' => array(
'nicemessages_jgrowl.css' => t('jGrowl'),
'nicemessages_drupal.css' => t('Drupal'),
'nicemessages_facebook.css' => t('Facebook'),
),
'#description' => t('' . 'In the moment Nicemessages supports 3 styles:<br />' . '<ol>' . '<li><em><strong>jGrowl</strong> - ' . 'default jGrowl popup style based on the popup script. (black, no icons) ' . 'for all message types.</em></li>' . '<li><em><strong>Drupal</strong> - ' . 'Drupals default 3 system message type icons & colors.</em></li>' . '<li><em><strong>Facebook</strong> - ' . 'imitating the light-grey-blue rounded popup style of Facebook statuses ' . 'for all message types. Errors get red borders.</em></li>' . '</ol>' . 'Additional styles can be added by copying and modifying css files in module ' . '/css folder and changing this description and selection form.'),
);
$form['global']['nicemessages_shadows'] = array(
'#type' => 'checkbox',
'#title' => t('Nice messages box shadows'),
'#default_value' => variable_get('nicemessages_shadows', 1),
'#options' => array(
0 => FALSE,
1 => TRUE,
),
'#description' => t('' . 'Accentuation of the popup boxes by emphasing with light surrounding shadows.'),
);
$form['global']['nicemessages_visibility'] = array(
'#type' => 'fieldset',
'#title' => t('Pages'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['global']['nicemessages_visibility']['nicemessages_pages'] = array(
'#type' => 'radios',
'#title' => t('Show nicemessages on specific pages'),
'#default_value' => variable_get('nicemessages_pages', 0),
'#options' => array(
0 => t('All pages except those listed'),
1 => t('Only the listed pages'),
),
);
$form['global']['nicemessages_visibility']['nicemessages_paths'] = array(
'#type' => 'textarea',
'#default_value' => variable_get('nicemessages_paths', 'admin/*'),
'#description' => t("" . "Specify pages by using their paths. Enter one path per line. " . "The '*' character is a wildcard. Example paths are " . "%blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
)),
);
foreach (array(
'status',
'warning',
'error',
) as $type) {
$type_prefix = 'nicemessages_' . $type . '_';
$form[$type] = array(
'#type' => 'fieldset',
'#title' => t('@type messages', array(
'@type' => $type,
)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form[$type][$type_prefix . 'life'] = array(
'#type' => 'textfield',
'#title' => t('How long should the message be visible?'),
'#default_value' => variable_get($type_prefix . 'life', '6'),
'#size' => 10,
'#maxlength' => 10,
'#required' => FALSE,
'#field_suffix' => 'seconds',
'#description' => t('Enter 0 for sticky messages'),
);
$form[$type][$type_prefix . 'glue'] = array(
'#type' => 'select',
'#title' => t('Should new messages be prepended or appended to the list?'),
'#default_value' => variable_get($type_prefix . 'glue', 'after'),
'#options' => array(
'before' => 'prepended',
'after' => 'appended',
),
);
$form[$type][$type_prefix . 'speed'] = array(
'#type' => 'select',
'#title' => t('Animation speed'),
'#default_value' => variable_get($type_prefix . 'speed', 'slow'),
'#options' => array(
'slow' => 'slow',
'normal' => 'normal',
'fast' => 'fast',
),
);
}
return system_settings_form($form);
}
Functions
Name![]() |
Description |
---|---|
nicemessages_are_enabled | Check if nicemessages are enabled for current user. |
nicemessages_field_access | Implements hook_field_access(). |
nicemessages_init | Implements hook_init(). |
nicemessages_menu | Implements hook_menu(). |
nicemessages_pages_visibility | Show nicemessages on specific pages |
nicemessages_permission | Implements hook_permission(). |
nicemessages_preprocess_page | Implements hook_preprocess_page(). |
nicemessages_settings_form | Module settings form. |
nicemessages_set_messages | Save messages to js settings. |
Constants
Name![]() |
Description |
---|---|
JGROWL_PATH | |
NICEMESSAGES_FIELDNAME | @file Nicemessages module |