You are here

nicemessages.module in Nice messages 7

Same filename and directory in other branches
  1. 8.2 nicemessages.module
  2. 7.2 nicemessages.module

Nicemessages module

Displays drupal messages with jGrowl jQuery plugin.

File

nicemessages.module
View 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');

  // Atach module files
  drupal_add_css(drupal_get_path('module', 'nicemessages') . '/css/nicemessages.css');
  drupal_add_js(drupal_get_path('module', 'nicemessages') . '/js/nicemessages.js');
  if (variable_get('nicemessages_background_color') == 1) {
    drupal_add_css(drupal_get_path('module', 'nicemessages') . '/css/nicemessages_background_color.css');
  }
  else {
    if (variable_get('nicemessages_background_color') == 2) {
      drupal_add_css(drupal_get_path('module', 'nicemessages') . '/css/nicemessages_background_color_bordered.css');
    }
  }
}

/**
 *  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);
    }
    else {
      if ($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_pages');
  $visibility = variable_get('nicemessages_visibility');
  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
    // the block is displayed 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;
    }
  }
  return FALSE;
}

/**
 * Check if nicemessages are enabled for current user.
 */
function nicemessages_are_enabled() {
  if (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 variable_get('nicemessages_default_state', 1) == 1 ? true : false;
  }
}

/**
 *  Save messages to js settings. 
 * 
 * @TODO moving 'position' setting from line 118 up to foreach line 108 here 
 * and from form/global in line 147 down to foreach form/type in line 158
 * for having individual position for each message type
 * @TODO corresponding changes in js file needed
 */
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', '3') * 1000,
        'glue' => variable_get('nicemessages_' . $type . '_glue', 'after'),
        'speed' => variable_get('nicemessages_' . $type . '_speed', 'normal'),
      );
      $items[] = $item;
    }
  }
  $settings = array(
    'position' => variable_get('nicemessages_position', 'top-right'),
    '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' => 'select',
    '#title' => t('Default state (On/Off)'),
    '#required' => true,
    '#default_value' => variable_get('nicemessages_default_state', 1),
    '#options' => array(
      1 => 'enabled',
      0 => 'disabled',
    ),
    '#description' => t('System wide On/Off. Note: Users with "toggle nicemessages" permission will be able to override this setting for their account.'),
  );
  $form['global']['nicemessages_position'] = array(
    '#type' => 'select',
    '#title' => t('Message screen positionscreen position'),
    '#default_value' => variable_get('nicemessages_position', 'top-right'),
    '#options' => array(
      'top-left' => t('top left corner'),
      'top-right' => t('top right corner'),
      'bottom-left' => t('bottom left corner'),
      'bottom-right' => t('bottom right corner'),
      'center' => t('center of the screen'),
    ),
    '#description' => t('Where on the screen messages should be displayed?'),
  );
  $form['global']['nicemessages_background_color'] = array(
    '#type' => 'select',
    '#title' => t('Message styles (colors, borders, shadow)'),
    '#default_value' => variable_get('nicemessages_background_color', 1),
    '#options' => array(
      0 => t('jGrowl default (black) for all message types'),
      1 => t('Drupal default 3 system message colors'),
      2 => t('Drupal default with borders/shadows'),
    ),
    '#description' => t('In the moment Nicemessages support 3 styles. jGrowl default (all black), Drupal default (3 status colors), and Drupal default with borders and shadows.'),
  );
  $form['global']['nicemessages_visibility'] = array(
    '#type' => 'fieldset',
    '#title' => t('Pages'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['global']['nicemessages_visibility']['nicemessages_visibility'] = array(
    '#type' => 'radios',
    '#title' => t('Show nicemessages on specific pages'),
    '#default_value' => variable_get('nicemessages_visibility', 'all'),
    '#options' => array(
      0 => t('All pages except those listed'),
      1 => t('Only the listed pages'),
    ),
  );
  $form['global']['nicemessages_visibility']['nicemessages_pages'] = array(
    '#type' => 'textarea',
    '#default_value' => variable_get('nicemessages_pages', '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', '3'),
      '#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', 'normal'),
      '#options' => array(
        'slow' => 'slow',
        'normal' => 'normal',
        'fast' => 'fast',
      ),
    );
  }
  return system_settings_form($form);
}

Functions

Namesort descending 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

Namesort descending Description
JGROWL_PATH
NICEMESSAGES_FIELDNAME @file Nicemessages module