You are here

eu_cookie_compliance.module in EU Cookie Compliance (GDPR Compliance) 5

This module intends to deal with the EU Directive on Privacy and Electronic Communications that comes into effect in the UK on 26th May 2012.

Author: Marcin Pajdzik

File

eu_cookie_compliance.module
View source
<?php

/**
 * @file
 * This module intends to deal with the EU Directive on Privacy and Electronic
 * Communications that comes into effect in the UK on 26th May 2012.
 *
 * Author: Marcin Pajdzik
 */

/**
 * Implements hook_menu().
 */
function eu_cookie_compliance_menu() {
  $items[] = array(
    'path' => 'admin/settings/eu-cookie-compliance',
    'title' => 'EU Cookie Compliance',
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'eu_cookie_compliance_admin_form',
    ),
    'access' => user_access('administer EU Cookie Compliance popup'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Provides form for cookie control popup.
 */
function eu_cookie_compliance_admin_form() {
  global $locale;
  $ln = $locale;
  $popup_settings = eu_cookie_compliance_get_settings();
  $form['eu_cookie_compliance_' . $ln] = array(
    '#type' => 'item',
    '#tree' => TRUE,
  );
  if (module_exists('locale')) {
    $form['eu_cookie_compliance_' . $ln]['#title'] = t('You are editing settings for the %language language.', array(
      '%language' => $ln,
    ));
  }
  $form['eu_cookie_compliance_' . $ln]['popup_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable popup'),
    '#default_value' => $popup_settings['popup_enabled'] ? $popup_settings['popup_enabled'] : 0,
  );
  $form['eu_cookie_compliance_' . $ln]['popup_clicking_confirmation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Consent by clicking'),
    '#default_value' => isset($popup_settings['popup_clicking_confirmation']) ? $popup_settings['popup_clicking_confirmation'] : 1,
    '#description' => t('By default by clicking any link on the website the visitor accepts the cookie policy. Uncheck this box if you do not require this functionality. You may want to edit the pop-up message below accordingly.'),
  );
  $form['eu_cookie_compliance_' . $ln]['popup_position'] = array(
    '#type' => 'checkbox',
    '#title' => t('Place the pop-up at the top of the website'),
    '#default_value' => $popup_settings['popup_position'] ? $popup_settings['popup_position'] : 0,
    '#description' => t('By default the pop-up appears at the bottom of the website. Tick this box if you want it to appear at the top'),
  );
  $form['eu_cookie_compliance_' . $ln]['popup_info'] = array(
    '#type' => 'item',
    '#tree' => TRUE,
  );
  $form['eu_cookie_compliance_' . $ln]['popup_info']['value'] = array(
    '#type' => 'textarea',
    '#title' => t('Popup message - requests consent'),
    '#default_value' => $popup_settings['popup_info']['value'] ? $popup_settings['popup_info']['value'] : '',
    '#required' => TRUE,
  );
  $form['eu_cookie_compliance_' . $ln]['popup_info']['format'] = filter_form($popup_settings['popup_info']['format'], NULL, array(
    'eu_cookie_compliance_' . $ln,
    'popup_info',
    'format',
  ));
  $form['eu_cookie_compliance_' . $ln]['popup_agreed'] = array(
    '#type' => 'item',
    '#tree' => TRUE,
  );
  $form['eu_cookie_compliance_' . $ln]['popup_agreed']['value'] = array(
    '#type' => 'textarea',
    '#title' => t('Popup message - thanks for giving consent'),
    '#default_value' => $popup_settings['popup_agreed']['value'] ? $popup_settings['popup_agreed']['value'] : '',
    '#required' => TRUE,
  );
  $form['eu_cookie_compliance_' . $ln]['popup_agreed']['format'] = filter_form($popup_settings['popup_agreed']['format'], NULL, array(
    'eu_cookie_compliance_' . $ln,
    'popup_agreed',
    'format',
  ));
  $form['eu_cookie_compliance_' . $ln]['popup_link'] = array(
    '#type' => 'textfield',
    '#title' => t('Privacy policy link'),
    '#default_value' => $popup_settings['popup_link'] ? $popup_settings['popup_link'] : '',
    '#size' => 60,
    '#maxlength' => 220,
    '#required' => TRUE,
    '#description' => t('Enter link to your privacy policy or other page that will explain cookies to your users. For external links prepend http://'),
  );
  $form['eu_cookie_compliance_' . $ln]['popup_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Popup height in pixels'),
    '#default_value' => $popup_settings['popup_height'] ? $popup_settings['popup_height'] : '',
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => FALSE,
    '#description' => t('Enter an integer value for a desired height in pixels or leave empty for automatically adjusted height'),
  );
  $form['eu_cookie_compliance_' . $ln]['popup_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Popup width in pixels or a percentage value'),
    '#default_value' => $popup_settings['popup_width'] ? $popup_settings['popup_width'] : '100%',
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#description' => t('Set the width of the popup. This can be either an integer value or percentage of the screen width. For example: 200 or 50%'),
  );
  $form['eu_cookie_compliance_' . $ln]['popup_delay'] = array(
    '#type' => 'textfield',
    '#title' => t('Popup time delay in seconds'),
    '#default_value' => $popup_settings['popup_delay'] ? $popup_settings['popup_delay'] : 1,
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
  );
  return system_settings_form($form);
}

/**
 * Validates form for cookie controll popup.
 */
function eu_cookie_compliance_admin_form_validate($form, &$form_state) {
  global $locale;
  $ln = $locale;
  if (!preg_match('/^[1-9][0-9]{0,4}$/', $form_state['eu_cookie_compliance_' . $ln]['popup_height']) && !empty($form_state['eu_cookie_compliance_' . $ln]['popup_height'])) {
    form_set_error('eu_cookie_compliance_popup_height', t('Height must be an integer value .'));
  }
  if (!preg_match('/^[1-9][0-9]{0,4}$/', $form_state['eu_cookie_compliance_' . $ln]['popup_delay'])) {
    form_set_error('eu_cookie_compliance_popup_delay', t('Delay must be an integer value.'));
  }
  if (!preg_match('/^[1-9][0-9]{0,4}\\%?$/', $form_state['eu_cookie_compliance_' . $ln]['popup_width'])) {
    form_set_error('eu_cookie_compliance_popup_width', t('Width must be an integer or a percentage value.'));
  }
}

/**
 * Implements hook_init().
 */
function eu_cookie_compliance_init() {
  $enabled = eu_cookie_compliance_get_settings('popup_enabled');
  if ($enabled && user_access('see EU Cookie Compliance popup')) {
    $path = drupal_get_path('module', 'eu_cookie_compliance');
    drupal_add_css($path . '/css/eu_cookie_compliance.css');
    drupal_add_js($path . '/js/eu_cookie_compliance.js', 'module', 'footer');
  }
}

/**
 * Implements hook_footer().
 */
function eu_cookie_compliance_footer() {
  $popup_settings = eu_cookie_compliance_get_settings();
  if ($popup_settings['popup_enabled'] && user_access('see EU Cookie Compliance popup')) {
    global $locale;
    $ln = $locale;
    $popup_text_info = str_replace(array(
      "\r",
      "\n",
    ), '', $popup_settings['popup_info']['value']);
    $popup_text_agreed = str_replace(array(
      "\r",
      "\n",
    ), '', $popup_settings['popup_agreed']['value']);
    $html_info = theme('eu_cookie_compliance_popup_info', check_markup($popup_text_info, $popup_settings['popup_info']['format'], FALSE));
    $html_agreed = theme('eu_cookie_compliance_popup_agreed', check_markup($popup_text_agreed, $popup_settings['popup_agreed']['format'], FALSE));
    $clicking_confirmation = isset($popup_settings['popup_clicking_confirmation']) ? $popup_settings['popup_clicking_confirmation'] : TRUE;
    $variables = array(
      'popup_enabled' => $popup_settings['popup_enabled'],
      'popup_clicking_confirmation' => $clicking_confirmation,
      'popup_html_info' => $html_info,
      'popup_html_agreed' => $html_agreed,
      'popup_height' => $popup_settings['popup_height'] ? (int) $popup_settings['popup_height'] : 'auto',
      'popup_width' => drupal_substr($popup_settings['popup_width'], -1) == '%' ? $popup_settings['popup_width'] : (int) $popup_settings['popup_width'],
      'popup_delay' => (int) ($popup_settings['popup_delay'] * 1000),
      'popup_link' => $popup_settings['popup_link'],
      'popup_position' => $popup_settings['popup_position'],
      'popup_language' => $ln,
    );
    drupal_add_js(array(
      'eu_cookie_compliance' => $variables,
    ), "setting", 'footer');
  }
}

/**
 * Implements hook_perm().
 */
function eu_cookie_compliance_perm() {
  return array(
    'administer EU Cookie Compliance popup',
    'see EU Cookie Compliance popup',
  );
}

/**
 * This is a template function for a pop-up prompting user to give their consent for
 * the website to set cookies.
 *
 * When overriding this template it is important to note that jQuery will use
 * the following classes to assign actions to buttons:
 *
 * agree-button      - agree to setting cookies
 * find-more-button  - link to an information page
 *
 * Variables available:
 * - $message:  Contains the text that will be display whithin the pop-up
 * - $link:     Contains a link to an information page (not used in the original
 *              template as JQuery redirects to the information page on clinking
 *              the find-more button. $link is available though in case you want
 *              to overrride the template and display the link another way.
 */
function theme_eu_cookie_compliance_popup_info($message = NULL, $link = NULL) {
  $agree = t("Yes, I agree");
  $find_more = t("No, I want to find out more");
  return <<<POPUP
\t\t<div>
\t\t\t<div class ="popup-content info">
\t\t\t\t<div id="popup-text">
\t\t\t\t\t{<span class="php-variable">$message</span>}
\t\t\t\t</div>
\t\t\t\t<div id="popup-buttons">
\t\t\t\t\t<button type="button" class="agree-button">{<span class="php-variable">$agree</span>}</button>
\t\t\t\t\t<button type="button" class="find-more-button">{<span class="php-variable">$find_more</span>}</button>
\t\t\t\t</div>
\t\t\t</div>
\t\t</div>
POPUP;
}

/**
 * This is a template fucntion for a pop-up informing a user that he has already
 * agreed to cookies.
 *
 * When overriding this template it is important to note that jQuery will use
 * the following classes to assign actions to buttons:
 *
 * hide-popup-button - destroy the pop-up
 * find-more-button  - link to an information page
 *
 * Variables available:
 * - $message:  Contains the text that will be display whithin the pop-up
 * - $link:     Contains a link to an information page (not used in the original
 *              template as JQuery redirects to the information page on clinking
 *              the find-more button. $link is available though in case you want
 *              to overrride the template and display the link another way.
 */
function theme_eu_cookie_compliance_popup_agreed($message = NULL, $link = NULL) {
  $hide_msg = t("Hide this message");
  $find_more = t("More information on cookies");
  return <<<POPUP
\t\t<div>
\t\t\t<div class ="popup-content agreed">
\t\t\t\t<div id="popup-text">
\t\t\t\t\t{<span class="php-variable">$message</span>}
\t\t\t\t</div>
\t\t\t\t<div id="popup-buttons">
\t\t\t\t\t<button type="button" class="hide-popup-button">{<span class="php-variable">$hide_msg</span>}</button>
\t\t\t\t\t<button type="button" class="find-more-button" >{<span class="php-variable">$find_more</span>}</button>
\t\t\t\t</div>
\t\t\t</div>
\t\t</div>
POPUP;
}

/**
 *
 * Retrieves settings from the database for a current language.
 *
 * @global type $language
 * @param type $setting
 * @return type
 */
function eu_cookie_compliance_get_settings($setting = 'all') {
  global $locale;
  $ln = $locale;
  $popup_settings = variable_get('eu_cookie_compliance_' . $ln, array());
  if ($setting == 'all') {
    return $popup_settings;
  }
  return $popup_settings[$setting];
}

Functions

Namesort descending Description
eu_cookie_compliance_admin_form Provides form for cookie control popup.
eu_cookie_compliance_admin_form_validate Validates form for cookie controll popup.
eu_cookie_compliance_footer Implements hook_footer().
eu_cookie_compliance_get_settings Retrieves settings from the database for a current language.
eu_cookie_compliance_init Implements hook_init().
eu_cookie_compliance_menu Implements hook_menu().
eu_cookie_compliance_perm Implements hook_perm().
theme_eu_cookie_compliance_popup_agreed This is a template fucntion for a pop-up informing a user that he has already agreed to cookies.
theme_eu_cookie_compliance_popup_info This is a template function for a pop-up prompting user to give their consent for the website to set cookies.