You are here

popup_message.module in Popup message 6

Same filename and directory in other branches
  1. 8 popup_message.module
  2. 7 popup_message.module

Main module file.

File

popup_message.module
View source
<?php

/**
 * @file
 * Main module file.
 */
define('POPUP_MESSAGE_JQUERYCOOKIE_PATH', 'sites/all/libraries/jquery.cookie');
define('POPUP_MESSAGE_CSS_NAME', 'popup.css');
define('POPUP_MESSAGE_THEME_STYLE_DIR', 'popup_message_styles');

/**
 * Implements hook_help().
 */
function popup_message_help($path, $arg) {
  switch ($path) {
    case 'admin/help#popup_message':
      $output .= '<p>' . t('Display popup message for users once per browser session.') . '</p>';
      $output .= '<p>' . t('Configuration') . ':</p>';
      $output .= '<ul>';
      $output .= '<li>' . t('Go to admin/settings/popup_message and set message title and body.') . '</li>';
      $output .= '<li>' . t('Go to admin/user/permissions and set permissions.') . '</li>';
      $output .= '</ul>';
      return $output;
  }
}

/**
 * Implements hook_perm().
 */
function popup_message_perm() {
  return array(
    'popup message administration',
    'display popup message',
  );
}

/**
 * Implements hook_menu().
 */
function popup_message_menu() {
  $items = array();

  // Settings form.
  $items['admin/settings/popup_message'] = array(
    'title' => 'Popup message',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'popup_message_settings',
    ),
    'file' => 'popup_message.admin.inc',
    'access arguments' => array(
      'popup message administration',
    ),
  );
  return $items;
}

/**
 * Implements hook_init().
 */
function popup_message_init() {

  // Check permissions to display message.
  $permission = user_access('display popup message');

  // Get status: enabled/disabled.
  $status = variable_get('popup_message_enable', 1);

  // Allow other modules to modiffy permissions.
  drupal_alter('popup_message_permission', $permission);
  if ($permission && $status) {
    $message_title = filter_xss(variable_get('popup_message_title', ''));
    $message_body = check_markup(variable_get('popup_message_body_text', ''), variable_get('popup_message_body_format', variable_get('filter_default_format', 1)), FALSE);
    $popup_message_parameters = array(
      'title' => $message_title,
      'body' => $message_body,
      'check_cookie' => variable_get('popup_message_check_cookie', 1),
      'width' => variable_get('popup_message_width', 300),
      'height' => variable_get('popup_message_height', 300),
    );

    // Allow other modules to modiffy message parameters.
    drupal_alter('popup_message_perameters', $popup_message_parameters);
    if ($message_title && $message_body) {
      popup_message_show_message($popup_message_parameters);
    }
  }
}

/**
 * Show popup message.
 *
 * @param type $popup_message_parameters
 *   Array of parameters:
 *   - title: message title
 *   - body: message body
 *   - check_cookie: set FALSE if you dont want to check cookie.
 *   - width: window width
 *   - height: window height
 */
function popup_message_show_message($popup_message_parameters) {
  drupal_add_js(array(
    'popup_message' => $popup_message_parameters,
  ), 'setting');
  drupal_add_js(popup_message_get_jquerycookie_path() . '/jquery.cookie.js');
  drupal_add_js(drupal_get_path('module', 'popup_message') . '/js/popup.js');
  $default_style = drupal_get_path('module', 'popup_message') . '/styles/default/' . POPUP_MESSAGE_CSS_NAME;
  $style = variable_get('popup_message_style', $default_style);
  if (!file_exists($style)) {
    $style = $default_style;
  }
  drupal_add_css($style);
}

/**
 * Return the path to the jquery.cookie.js plugin.
 */
function popup_message_get_jquerycookie_path() {
  static $library_path = NULL;

  // Try to locate the library path in any possible setup.
  if ($library_path == NULL) {

    // First check the default location.
    $path = variable_get('popup_message_jquerycookie_path', POPUP_MESSAGE_JQUERYCOOKIE_PATH);
    if (is_dir($path . '/jquery.cookie')) {
      $library_path = $path;
    }
    elseif ($library_path == NULL && module_exists('libraries')) {
      if ($path = libraries_get_path('jquery.cookie')) {
        $library_path = $path;
        variable_set('popup_message_jquerycookie_path', $library_path);
      }
    }
    elseif ($library_path == NULL) {
      $library_path = POPUP_MESSAGE_JQUERYCOOKIE_PATH;
    }
  }
  return $library_path;
}

Functions

Namesort descending Description
popup_message_get_jquerycookie_path Return the path to the jquery.cookie.js plugin.
popup_message_help Implements hook_help().
popup_message_init Implements hook_init().
popup_message_menu Implements hook_menu().
popup_message_perm Implements hook_perm().
popup_message_show_message Show popup message.

Constants