You are here

popup_message.module in Popup message 7

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

Main module file.

File

popup_message.module
View source
<?php

/**
 * @file
 * Main module file.
 */
define('POPUP_MESSAGE_CSS_NAME', 'popup.css');
define('POPUP_MESSAGE_THEME_STYLE_DIR', 'popup_message_styles');
define('POPUP_MESSAGE_VISIBILITY_NOTLISTED', 0);
define('POPUP_MESSAGE_VISIBILITY_LISTED', 1);
define('POPUP_MESSAGE_VISIBILITY_PHP', 2);

/**
 * 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_permission().
 */
function popup_message_permission() {
  return array(
    'popup message administration' => array(
      'title' => t('Administer Popup message module'),
      'description' => t('Perform administration tasks for Popup message module.'),
    ),
    'display popup message' => array(
      'title' => t('Display popup message'),
      'description' => t('Allow users to see popup message.'),
    ),
  );
}

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

  // Settings form.
  $items['admin/config/user-interface/popup_message'] = array(
    'title' => 'Popup message',
    'description' => 'Popup message configuration',
    '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);

  // Get popup message visibility settings.
  $visibility = variable_get('popup_message_visibility', POPUP_MESSAGE_VISIBILITY_NOTLISTED);

  // Get popup message visibility pages settings.
  $visibility_pages = variable_get('popup_message_visibility_pages', '');

  // Predefine value.
  $page_match = TRUE;

  // Limited visibility popup message must list at least one page.
  if ($visibility == POPUP_MESSAGE_VISIBILITY_LISTED && empty($visibility_pages)) {
    $status = FALSE;
  }

  // Match path if necessary.
  if ($visibility_pages && $status) {

    // Convert path to lowercase. This allows comparison of the same path
    // with different case. Ex: /Page, /page, /PAGE.
    $pages = drupal_strtolower($visibility_pages);
    if ($visibility < POPUP_MESSAGE_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 (POPUP_MESSAGE_VISIBILITY_NOTLISTED),
      // the popup message is displayed on all pages except those listed in
      // $visibility_pages.
      // When set to 1 (POPUP_MESSAGE_VISIBILITY_LISTED), it is displayed only
      // on those
      // pages listed in $visibility_pages.
      $page_match = !($visibility xor $page_match);
    }
    elseif (module_exists('php')) {
      $page_match = php_eval($visibility_pages);
    }
    else {
      $page_match = FALSE;
    }
  }
  if ($permission && $status && $page_match) {
    $message_title = filter_xss(variable_get('popup_message_title', ''));
    $message_body_variable = variable_get('popup_message_body');
    $message_body = check_markup($message_body_variable['value'], $message_body_variable['format'] ? $message_body_variable['format'] : variable_get('filter_default_format', NULL), 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),
      'delay' => variable_get('popup_message_delay', 0),
    );

    // 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 array $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('misc/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);
}