You are here

disclaimer.module in Disclaimer 6

Same filename and directory in other branches
  1. 8 disclaimer.module
  2. 6.2 disclaimer.module
  3. 7 disclaimer.module

Create and show disclaimer for your site. Visotors need js enabled and accept cookie

This module is for Drupal 6.x only.

Module by: Mog Mailto: tech@arthura.fr

File

disclaimer.module
View source
<?php

/**
 * @file
 * Create and show disclaimer for your site.
 * Visotors need js enabled and accept cookie
 *
 * This module is for Drupal 6.x only.
 *
 * Module by: Mog
 * Mailto: tech@arthura.fr
 *
 */

/**
 * Implementation of hook_menu().
 */
function disclaimer_menu() {
  $items = array();

  // disclaimer page
  $items['disclaimer'] = array(
    'title' => 'Disclaimer',
    'page callback' => '_disclaimer_get_content',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_SUGGESTED_ITEM,
  );

  // configuration page of the Disclaimer module
  $items['admin/settings/disclaimer'] = array(
    'title' => 'Disclaimer',
    'description' => 'Administer Disclaimer settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'disclaimer_admin_settings',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer disclaimer settings',
    ),
    'file' => 'disclaimer.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_init().
 */
function disclaimer_init() {
  global $user;
  if ((!user_access('bypass disclaimer') || variable_get('disclaimer_logged', 1) == 1 && $user->uid) && _disclaimer_visibility() == 1) {
    $path = drupal_get_path('module', 'disclaimer');
    $modal = variable_get('disclaimer_modal', 'thickbox');
    switch ($modal) {
      case 'nyroModal':
        $path_css = '/nyroModal/styles/nyroModal.css';
        $path_js = '/nyroModal/js/jquery.nyroModal.js';
        break;
      case 'jqModal':
        $path_css = '/jqModal/jqModal.css';
        $path_js = '/jqModal/jqModal.js';
        break;
      case 'thickbox':
        $path_css = FALSE;
        $path_js = FALSE;
        break;
    }
    if (file_exists($path . $path_css)) {
      drupal_add_css(drupal_get_path('module', 'disclaimer') . $path_css);
    }
    if (file_exists($path . $path_js)) {
      drupal_add_js(drupal_get_path('module', 'disclaimer') . $path_js);
    }
    drupal_add_js(drupal_get_path('module', 'disclaimer') . '/disclaimer.js', 'module', variable_get('disclaimer_location_js', 'header'));
    drupal_add_js(_disclaimer_get_js(), 'inline', variable_get('disclaimer_location_js', 'header'));
  }
}

/**
 * Implementation of hook_perm().
 */
function disclaimer_perm() {
  return array(
    'administer disclaimer settings',
    'bypass disclaimer',
  );
}

/**
 * Declare footer hook
 * @return string A string containing HTML to be inserted for jqModal
 */
function disclaimer_footer() {
  global $user;
  if (!user_access('bypass disclaimer') || variable_get('disclaimer_logged', 1) == 1 && $user->uid) {
    if (variable_get('disclaimer_modal', 'thickbox') == 'jqModal') {
      return '<div id="disclaimer" class="jqmWindow" style="display: none;">&nbsp;</div>';
    }
  }
}

/**
 * Generate disclaimer content
 */
function _disclaimer_get_content() {
  $action_type = variable_get('disclaimer_action_type', 'text');
  $exit_url = variable_get('disclaimer_exit_url', 'http://www.google.com');
  $enter_url = variable_get('disclaimer_enter_url', '');
  $nid = variable_get('disclaimer_node', '');
  $nid_footer = variable_get('disclaimer_node_footer', '');
  $age = variable_get('disclaimer_age_form', 0);
  $age_limit = variable_get('disclaimer_age_limit', 18);
  $modal = variable_get('disclaimer_modal', 'thickbox');
  $js = '';
  switch ($action_type) {
    case "image":
      $enter_alt = variable_get('disclaimer_enter_img_alt', t('ENTER'));
      $exit_alt = variable_get('disclaimer_exit_img_alt', t('EXIT'));
      $enter = theme('image', variable_get('disclaimer_enter_img_path', FALSE), $enter_alt, $enter_alt);
      $exit = theme('image', variable_get('disclaimer_exit_img_path', FALSE), $exit_alt, $exit_alt);
      break;
    default:
      $enter = variable_get('disclaimer_enter_txt', t('ENTER'));
      $exit = variable_get('disclaimer_exit_txt', t('EXIT'));
  }
  if ($age == 1) {
    $age_form = drupal_get_form('disclaimer_age_form');
    $rows[] = array(
      array(
        'data' => $age_form,
        'align' => 'center',
        'colspan' => '2',
      ),
    );
    $js = "CheckAge('" . variable_get('disclaimer_cookie_name', 'disclaimerShow') . "', " . $age_limit . ", '" . $exit_url . "', '" . $modal . "');";
  }
  else {
    $js = 'WriteCookie(\'' . variable_get('disclaimer_cookie_name', 'disclaimerShow') . '\', \'1\');';
    switch ($modal) {
      case 'nyroModal':
        $js .= '$.nyroModalRemove();';
        break;
      case 'jqModal':
        $js .= '$(\'#disclaimer\').jqmHide();';
        break;
      case 'thickbox':
        $js .= 'tb_remove();';
        break;
    }
  }
  if ($enter_url) {
    $enter = l($enter, $enter_url, array(
      'attributes' => array(
        'onclick' => $js,
      ),
      'html' => TRUE,
    ));
  }
  else {
    $enter = '<a href="javascript:void(0);" onclick="' . $js . '">' . $enter . '</a>';
  }
  $exit = l($exit, $exit_url, array(
    'html' => TRUE,
  ));
  $rows[] = array(
    array(
      'data' => $enter,
      'align' => 'center',
    ),
    array(
      'data' => $exit,
      'align' => 'center',
    ),
  );
  if ($nid == 0) {
    $output = check_markup(variable_get('disclaimer_content', ''), variable_get('disclaimer_content_format', 1), FALSE);
  }
  else {
    $output = node_view(node_load($nid), FALSE, TRUE);
  }
  $output .= theme('table', array(), $rows);
  if ($nid_footer == 0) {
    $output .= check_markup(variable_get('disclaimer_content_footer', ''), variable_get('disclaimer_content_footer_format', 1), FALSE);
  }
  else {
    $output .= node_view(node_load($nid_footer), FALSE, TRUE);
  }
  return $output;
}

/**
 * Generate js function
 */
function _disclaimer_get_js() {
  $cookie_path = variable_get('disclaimer_cookie_path', '/');
  $cookie_expires = variable_get('disclaimer_cookie_expires', '');
  $cookie_domain = variable_get('disclaimer_cookie_domain', '');
  $modal = variable_get('disclaimer_modal', 'thickbox');
  if ($modal == 'nyroModal' || $modal == 'thickbox') {
    $debug = variable_get('disclaimer_debug', 'false');
    $width = variable_get('disclaimer_width', 400);
    $height = variable_get('disclaimer_height', 300);
    $padding = variable_get('disclaimer_padding', 20);
    $bgcolor = variable_get('disclaimer_bgcolor', '000000');
  }
  elseif ($modal == 'jqModal') {
    $overlay = variable_get('disclaimer_jqmodal_overlay', 90);
  }
  $js = 'function WriteCookie(nom, valeur){' . "\n";
  $js .= 'var argv=WriteCookie.arguments;' . "\n";
  $js .= 'var argc=WriteCookie.arguments.length;' . "\n";
  $js .= 'var expires=(argc > 2) ? argv[2] : null;' . "\n";
  $js .= 'var secure=(argc > 5) ? argv[5] : false;' . "\n";
  if (empty($cookie_path)) {
    $js .= 'var path=(argc > 3) ? argv[3] : null;' . "\n";
  }
  if (empty($cookie_domain)) {
    $js .= 'var domain=(argc > 4) ? argv[4] : null;' . "\n";
  }
  $js .= 'document.cookie=nom+"="+escape(valeur)+' . "\n";
  $js .= '((expires==null) ? "" : ("; expires="+expires.toGMTString()))+' . "\n";
  if (empty($cookie_path)) {
    $js .= '((path==null) ? "" : ("; path="+path))+' . "\n";
  }
  else {
    $js .= '("; path=' . $cookie_path . '")+' . "\n";
  }
  if (empty($cookie_domain)) {
    $js .= '((domain==null) ? "" : ("; domain="+domain))+' . "\n";
  }
  else {
    $js .= '("; domain=' . $cookie_domain . '")+' . "\n";
  }
  $js .= '((secure==true) ? "; secure" : "");}' . "\n";
  $js .= 'if (ReadCookie("' . variable_get('disclaimer_cookie_name', 'disclaimerShow') . '") == null) {';
  $js .= '$(document).ready(function(){';
  switch ($modal) {
    case 'nyroModal':
      $js .= '$.nyroModalManual({url:\'' . url('disclaimer') . '\',modal:true, minWidth:' . $width . ', minHeight:' . $height . ', padding:' . $padding . ', debug:' . $debug . ', bgcolor:\'#' . $bgcolor . '\'});';
      break;
    case 'jqModal':
      $js .= '$(\'#disclaimer\').jqm({ajax:\'' . url('disclaimer') . '\', overlay:' . $overlay . ', modal:true}).jqmShow();';
      break;
    case 'thickbox':
      $js .= 'tb_show(\'test\', \'' . url('disclaimer', array(
        'query' => array(
          'modal' => 'true',
          'height' => $height,
          'width' => $width,
        ),
      )) . '\')';
      break;
  }
  $js .= '});}';
  return $js;
}

/**
 * Calculate visibility of disclaimer if set
 * function copy from block.module, thanks for the original code.
 * @return bolean
 */
function _disclaimer_visibility() {
  $visibility = variable_get('disclaimer_visibility', 0);
  $pages = variable_get('disclaimer_pages', "admin/*\nuser");
  if ($pages) {
    if ($visibility < 2) {
      $path = drupal_get_path_alias($_GET['q']);
      $page_match = drupal_match_path($path, $pages);
      if ($path != $_GET['q']) {
        $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
      }
      $page_match = !($visibility xor $page_match);
    }
    else {
      $page_match = drupal_eval($pages);
    }
  }
  else {
    $page_match = TRUE;
  }
  return $page_match;
}

/**
 * function to set age form on disclaimer
 * @return array an array of forms
 */
function disclaimer_age_form() {
  $form['disclaimer_age'] = array(
    '#type' => 'date',
    '#title' => t('Date of Birth'),
    '#description' => t('You must be at least @age years old to visit @site.', array(
      '@age' => variable_get('disclaimer_age_limit', 18),
      '@site' => variable_get('site_name', ''),
    )),
  );
  return $form;
}

Functions

Namesort descending Description
disclaimer_age_form function to set age form on disclaimer
disclaimer_footer Declare footer hook
disclaimer_init Implementation of hook_init().
disclaimer_menu Implementation of hook_menu().
disclaimer_perm Implementation of hook_perm().
_disclaimer_get_content Generate disclaimer content
_disclaimer_get_js Generate js function
_disclaimer_visibility Calculate visibility of disclaimer if set function copy from block.module, thanks for the original code.