You are here

disclaimer.module in Disclaimer 7

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

Create and show disclaimer for your site.

File

disclaimer.module
View source
<?php

/**
 * @file
 * Create and show disclaimer for your site.
 */

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

  // Autocomplete search node.
  $items['node/autocomplete'] = array(
    'title' => 'Autocomplete for nodes',
    'page callback' => '_disclaimer_node_autocomplete',
    'access arguments' => array(
      'administer disclaimer settings',
    ),
    'type' => MENU_CALLBACK,
  );

  // Disclaimer module settings.
  $items['admin/config/system/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;
}

/**
 * Implements hook_page_build().
 */
function disclaimer_page_build(&$page) {
  if (!disclaimer_show()) {
    return;
  }
  if ($key = variable_get('disclaimer_region', NULL)) {

    // Add disclaimer content on page.
    $page[$key]['disclaimer'] = array(
      '#markup' => _disclaimer_build_content(),
    );
  }
}

/**
 * Add needed js settings.
 */
function _disclaimer_js_settings() {
  $settings['disclaimer'] = array(
    'cookie_name' => variable_get('disclaimer_cookie_name', 'disclaimerShow'),
    'cookie_path' => variable_get('disclaimer_cookie_path', '/'),
    'cookie_domain' => variable_get('disclaimer_cookie_domain', $_SERVER['SERVER_NAME']),
    // Colorbox settings.
    'width' => variable_get('disclaimer_width', '80%'),
    'height' => variable_get('disclaimer_height', '80%'),
    'initialwidth' => variable_get('disclaimer_initialwidth', '300'),
    'initialheight' => variable_get('disclaimer_initialheight', '250'),
    'ageform' => variable_get('disclaimer_age_form', 0),
    'limit' => variable_get('disclaimer_age_limit', 18),
    'exiturl' => variable_get('disclaimer_exit_url', 'http://www.google.com'),
  );
  drupal_add_js($settings, 'setting');
}

/**
 * Implements hook_permission().
 */
function disclaimer_permission() {
  return array(
    'administer disclaimer settings' => array(
      'title' => t('Administer disclaimer settings'),
      'description' => t('Full disclaimer administration access.'),
    ),
    'bypass disclaimer' => array(
      'title' => t('Bypass disclaimer'),
      'description' => t('Hide disclaimer depending by roles.'),
    ),
  );
}

/**
 * Function to control disclaimer display depending user and accesses.
 *
 * @return bool
 *   True if must be shown.
 */
function disclaimer_show() {
  global $user;

  // Cookie override.
  if (isset($_COOKIE[variable_get('disclaimer_cookie_name', 'disclaimerShow')])) {
    return FALSE;
  }

  // Visibility by roles override.
  if (user_access('bypass disclaimer')) {
    return FALSE;
  }

  // Configuration and user access.
  if (($user->uid == 0 || variable_get('disclaimer_logged', 1) == 1 && $user->uid >= 0) && _disclaimer_visibility() == 1) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Generate disclaimer content.
 *
 * @param bool $hidden
 *   Default style dsplay hidden.
 *
 * @return string
 *   Html content rendered.
 */
function _disclaimer_build_content($hidden = TRUE) {

  // Load cookie library.
  drupal_add_library('system', 'jquery.cookie');

  // Build renderable array.
  $render_array = array(
    '#theme' => 'disclaimer',
    '#hidden' => $hidden,
  );
  $exit_url = variable_get('disclaimer_exit_url', 'http://www.google.com');
  $enter_url = variable_get('disclaimer_enter_url', '');

  // Build button form depending settings.
  if (variable_get('disclaimer_action_type', 'text') == 'image') {
    $enter = _disclaimer_render_image();
    $exit = _disclaimer_render_image('exit');
  }
  else {
    $enter = variable_get('disclaimer_enter_txt', t('ENTER'));
    $exit = variable_get('disclaimer_exit_txt', t('EXIT'));
  }

  // Check age form.
  if (variable_get('disclaimer_age_form', 0) == 1) {
    $age_form = drupal_get_form('disclaimer_age_form');
    $render_array['#age_form'] = render($age_form);
  }

  // Enter redirect or not.
  if ($enter_url) {
    $enter = l($enter, $enter_url, array(
      'html' => TRUE,
      'attributes' => array(
        'id' => 'disclaimer_enter',
      ),
    ));
  }
  else {
    $enter = '<a href="#" id="disclaimer_enter">' . $enter . '</a>';
  }
  $exit = l($exit, $exit_url, array(
    'html' => TRUE,
  ));

  // Show main content.
  $render_array['#content'] = _disclaimer_render_content();

  // Buttons render.
  $render_array['#enter_link'] = $enter;
  $render_array['#exit_link'] = $exit;

  // Footer content.
  $render_array['#footer'] = _disclaimer_render_content('footer');

  // Add minimal css.
  if (variable_get('disclaimer_css', 1) == 1) {
    drupal_add_css(drupal_get_path('module', 'disclaimer') . '/disclaimer.css');
  }

  // Add settings.
  _disclaimer_js_settings();

  // Add js.
  drupal_add_js(drupal_get_path('module', 'disclaimer') . '/disclaimer.js', array(
    'scope' => 'footer',
  ));
  return render($render_array);
}

/**
 * Build images as link for disclaimer actions.
 *
 * @param string $key
 *   Button concerned, enter or exit.
 *
 * @return string
 *   Html image rendered.
 */
function _disclaimer_render_image($key = 'enter') {
  $image = '';
  if ($key == 'enter') {
    $alt = t('ENTER');
  }
  else {
    $alt = t('EXIT');
  }
  $variables['alt'] = $variables['title'] = variable_get('disclaimer_' . $key . '_img_alt', $alt);

  // Get file image.
  $file = file_load(variable_get('disclaimer_' . $key . '_img'));
  if ($file) {
    $variables['path'] = $file->uri;
    if (variable_get('disclaimer_' . $key . '_img_style') != "") {
      $variables['style_name'] = variable_get('disclaimer_' . $key . '_img_style');

      // Avoid php notice.
      // $variables['attributes'] = '';
      $image = theme('image_style', $variables);
    }
    else {
      $image = theme('image', $variables);
    }
  }
  else {
    $image = variable_get('disclaimer_' . $key . '_txt', $alt);
  }
  return $image;
}

/**
 * Build links for disclaimer actions.
 *
 * @param string $key
 *   Button concerned, enter or exit.
 *
 * @return string
 *   Html button rendered.
 */
function _disclaimer_render_button($key = 'enter') {
  $output = '';
  if ($zone == 'footer') {
    $key = 'disclaimer_footer_';
  }
  else {
    $key = 'disclaimer_main_';
  }
  if (variable_get($key . 'source') == 0) {
    $content = variable_get($key . 'content', array(
      'value' => '',
      'format' => NULL,
    ));
    $output = check_markup($content['value'], $content['format']);
  }
  else {
    $node = node_load(variable_get($key . 'node', 0));
    if (!empty($node)) {
      $node = node_view($node, variable_get($key . 'node_view'));
      if (variable_get($key . 'node_view_links') != 1) {
        $node['links']['#access'] = FALSE;
      }
      $output = drupal_render($node);
    }
  }
  return $output;
}

/**
 * Helper to build top and bottom render.
 *
 * @param string $zone
 *   Dynamically build content depending place, footer or top.
 *
 * @return string
 *   Html content rendered.
 */
function _disclaimer_render_content($zone = '') {
  $output = '';
  if ($zone == 'footer') {
    $key = 'disclaimer_footer_';
  }
  else {
    $key = 'disclaimer_main_';
  }
  if (variable_get($key . 'source') == 0) {
    $content = variable_get($key . 'content', array(
      'value' => '',
      'format' => NULL,
    ));
    $output = check_markup($content['value'], $content['format']);
  }
  else {
    $node = node_load(variable_get($key . 'node', 0));
    if (!empty($node)) {
      $node = node_view($node, variable_get($key . 'node_view'));
      if (variable_get($key . 'node_view_links') != 1) {
        $node['links']['#access'] = FALSE;
      }
      $output = drupal_render($node);
    }
  }
  return $output;
}

/**
 * Calculate visibility of disclaimer if set.
 *
 * This is a copy from block.module (~807), thanks for the original code.
 *
 * @return bool
 *   Page match or not.
 */
function _disclaimer_visibility() {
  $separator = "\n";
  $negator = '!';
  $visibility = variable_get('disclaimer_visibility', 0);
  $pages = variable_get('disclaimer_pages', implode($separator, array(
    'admin',
    'user',
  )));
  $pages = array_map('trim', explode($separator, $pages));
  $not_pages = implode($separator, array_map(static function ($pattern) {
    return substr($pattern, 1);
  }, array_filter($pages, static function ($pattern) use ($negator) {
    return $pattern[0] === $negator;
  })));
  $pages = implode($separator, array_filter($pages, static function ($pattern) use ($negator) {
    return $pattern[0] !== $negator;
  }));

  // Convert path to lowercase. This allows comparison of the same path
  // with different case. Ex: /Page, /page, /PAGE.
  $pages = drupal_strtolower($pages);
  $not_pages = drupal_strtolower($not_pages);
  if ($visibility < 2) {
    $q = $_GET['q'];

    // Convert the Drupal path to lowercase.
    $path = drupal_strtolower(drupal_get_path_alias($q));

    // Compare the lowercase internal and lowercase path alias (if any).
    $page_match = drupal_match_path($path, $pages) && !drupal_match_path($path, $not_pages);
    if ($path !== $q) {
      $page_match = $page_match || drupal_match_path($q, $pages) && !drupal_match_path($q, $not_pages);
    }

    // When $visibility has a value of 0,
    // the disclaimer 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);
  }
  elseif (module_exists('php')) {
    $page_match = php_eval($pages);
  }
  else {
    $page_match = FALSE;
  }
  return $page_match;
}

/**
 * Function to set age form on disclaimer.
 *
 * @return array
 *   An array of forms.
 */
function disclaimer_age_form($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;
}

/**
 * Autocomplete helper for nodes search.
 *
 * @param string $string
 *   String for search.
 */
function _disclaimer_node_autocomplete($string = "") {
  $matches = array();
  if ($string) {
    $result = db_select('node')
      ->fields('node', array(
      'nid',
      'title',
    ))
      ->condition('title', db_like($string) . '%', 'LIKE')
      ->range(0, 10)
      ->execute();
    foreach ($result as $node) {
      $matches[$node->title . ' [' . $node->nid . ']'] = check_plain($node->title);
    }
  }

  // Return the result to the form in json.
  drupal_json_output($matches);
}

/**
 * Implements hook_theme().
 */
function disclaimer_theme() {
  return array(
    'disclaimer' => array(
      'variables' => array(
        'hidden' => NULL,
        'enter_link' => NULL,
        'exit_link' => NULL,
        'content' => NULL,
        'footer' => NULL,
        'age_form' => NULL,
      ),
      'template' => 'disclaimer',
    ),
  );
}

Functions

Namesort descending Description
disclaimer_age_form Function to set age form on disclaimer.
disclaimer_menu Implements hook_menu().
disclaimer_page_build Implements hook_page_build().
disclaimer_permission Implements hook_permission().
disclaimer_show Function to control disclaimer display depending user and accesses.
disclaimer_theme Implements hook_theme().
_disclaimer_build_content Generate disclaimer content.
_disclaimer_js_settings Add needed js settings.
_disclaimer_node_autocomplete Autocomplete helper for nodes search.
_disclaimer_render_button Build links for disclaimer actions.
_disclaimer_render_content Helper to build top and bottom render.
_disclaimer_render_image Build images as link for disclaimer actions.
_disclaimer_visibility Calculate visibility of disclaimer if set.