You are here

simple_cookie_compliance.module in Simple Cookie Compliance 6

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

All the necessary functions for configuring and output.

File

simple_cookie_compliance.module
View source
<?php

/**
 * @file
 * All the necessary functions for configuring and output.
 */

/**
 * Implements hook_menu().
 */
function simple_cookie_compliance_menu() {
  $items = array();
  $items['admin/settings/simple-cookie-compliance'] = array(
    'title' => 'Cookie Compliance Settings',
    'description' => 'Settings for the cookie compliance window that is used.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      '_simple_cookie_compliance_settings_form',
    ),
    'access arguments' => array(
      'administer cookie compliance',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['simple_cookie_compliance/js'] = array(
    'title' => 'Cookie Compliance Callback',
    'page callback' => 'simple_cookie_compliance_js',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['simple_cookie_compliance/has-cookie'] = array(
    'title' => 'Cookie Compliance Callback',
    'page callback' => 'simple_cookie_compliance_has_cookie',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function simple_cookie_compliance_perm() {
  return array(
    'administer cookie compliance',
  );
}

/**
 * Implements hook_theme().
 */
function simple_cookie_compliance_theme($existing, $type, $theme, $path) {
  return array(
    'cookie_compliance' => array(
      'arguments' => array(
        'message' => NULL,
        'form' => NULL,
      ),
      'template' => 'templates/cookie-compliance',
    ),
  );
}

/**
 * Implements hook_init().
 */
function simple_cookie_compliance_init() {
  $path = drupal_get_path('module', 'simple_cookie_compliance');
  drupal_add_js($path . '/js/simple_cookie_compliance.js', 'module', 'footer');
}

/**
 * Implements hook_preprocess_page().
 */
function simple_cookie_compliance_preprocess_page(&$vars) {
  global $user;
  global $language;

  // Get the text.
  $message = filter_xss(variable_get('simple_cookie_compliance_content_' . $language->language, t('This website uses cookies to help us give you the best experience when you visit our website. By continuing to use this website, you consent to our use of these cookies.')));

  // Add base style, update styles is needed
  drupal_add_css(drupal_get_path('module', 'simple_cookie_compliance') . '/css/simple_cookie_compliance.css');
  $vars['styles'] = drupal_get_css();

  // Render form and update scripts since 'drupal_get_form' is adding js
  $form = drupal_get_form('simple_cookie_compliance_dismiss_form');
  $vars['scripts'] = drupal_get_js();

  // Add the text and form.
  $vars['simple_cookie_compliance'] = theme('cookie_compliance', $message, $form);
}

/**
 * Dismiss form.
 */
function simple_cookie_compliance_dismiss_form(&$form_state) {
  global $language;

  // The form and submit button.
  $button_text = check_plain(variable_get('simple_cookie_compliance_button_agree_' . $language->language, t('OK')));
  $form = array();
  $form['simple_cookie_compliance_dismiss'] = array(
    '#type' => 'submit',
    '#value' => $button_text,
    '#attributes' => array(
      'class' => 'cookie-compliance__button',
    ),
    '#ahah' => array(
      'path' => 'simple_cookie_compliance/js',
      'wrapper' => 'cookie-compliance',
      'method' => 'replace',
    ),
    '#submit' => array(
      'simple_cookie_compliance_dismiss_form_dismiss',
    ),
  );
  return $form;
}

/**
 * Default cookie expiration time
 */
function _simple_cookie_compliance_default_expiration_time() {
  $three_months = 60 * 60 * 24 * 30 * 3;
  return $three_months;
}

/**
 * Submit handler for dismiss form.
 */
function simple_cookie_compliance_dismiss_form_dismiss($form, $form_state) {

  // Set the cookie with the expiration time defined in module settings.
  $default_expiration_time = _simple_cookie_compliance_default_expiration_time();
  $expiration_time = $_SERVER['REQUEST_TIME'] + variable_get('simple_cookie_compliance_expiration_time', $default_expiration_time);
  setcookie('simple_cookie_compliance_dismissed', 1, $expiration_time, '/');
}

/**
 * Menu callback for AHAH additions.
 */
function simple_cookie_compliance_js() {

  // Set the cookie with the expiration time defined in module settings.
  $default_expiration_time = _simple_cookie_compliance_default_expiration_time();
  $expiration_time = $_SERVER['REQUEST_TIME'] + variable_get('simple_cookie_compliance_expiration_time', $default_expiration_time);
  setcookie('simple_cookie_compliance_dismissed', 1, $expiration_time, '/');
  drupal_json(array(
    'status' => TRUE,
    'data' => '',
  ));
  exit;
}

/**
 * Menu callback for AHAH additions.
 */
function simple_cookie_compliance_has_cookie() {
  $GLOBALS['conf']['cache'] = 0;
  $data = FALSE;

  // Check the cookie.
  if (isset($_COOKIE['simple_cookie_compliance_dismissed'])) {
    $data = $_COOKIE['simple_cookie_compliance_dismissed'] ? TRUE : FALSE;
  }

  // $data = 'test';
  drupal_json(array(
    'status' => TRUE,
    'cookie' => $data,
  ));
  exit;
}

/**
 * Custom form function.
 */
function _simple_cookie_compliance_settings_form() {
  $form = array();
  $languages = language_list();

  // Cookie expiration.
  $default_expiration_time = _simple_cookie_compliance_default_expiration_time();
  $form['simple_cookie_compliance_expiration_time'] = array(
    '#type' => 'textfield',
    '#title' => t('Cookie Compliance Expiration Time'),
    '#description' => t('The time it will take to expire the cookie in seconds. Default value is equal to three months.'),
    '#default_value' => variable_get('simple_cookie_compliance_expiration_time', $default_expiration_time),
    '#required' => TRUE,
  );
  foreach ($languages as $language) {
    if ($language->enabled) {

      // Field group per language.
      $form['language_' . $language->language] = array(
        '#type' => 'fieldset',
        '#title' => check_plain($language->name),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );

      // Text setting for message.
      $form['language_' . $language->language]['simple_cookie_compliance_content_' . $language->language] = array(
        '#type' => 'textarea',
        '#title' => t('Message'),
        '#default_value' => variable_get('simple_cookie_compliance_content_' . $language->language, t('This website uses cookies to help us give you the best experience when you visit our website. By continuing to use this website, you consent to our use of these cookies.')),
        '#required' => TRUE,
      );

      // Text setting for agree button.
      $form['language_' . $language->language]['simple_cookie_compliance_button_agree_' . $language->language] = array(
        '#type' => 'textfield',
        '#title' => t('Agree button'),
        '#default_value' => variable_get('simple_cookie_compliance_button_agree_' . $language->language, t('OK')),
        '#required' => TRUE,
      );
    }
  }
  return system_settings_form($form);
}

Functions

Namesort descending Description
simple_cookie_compliance_dismiss_form Dismiss form.
simple_cookie_compliance_dismiss_form_dismiss Submit handler for dismiss form.
simple_cookie_compliance_has_cookie Menu callback for AHAH additions.
simple_cookie_compliance_init Implements hook_init().
simple_cookie_compliance_js Menu callback for AHAH additions.
simple_cookie_compliance_menu Implements hook_menu().
simple_cookie_compliance_perm Implements hook_permission().
simple_cookie_compliance_preprocess_page Implements hook_preprocess_page().
simple_cookie_compliance_theme Implements hook_theme().
_simple_cookie_compliance_default_expiration_time Default cookie expiration time
_simple_cookie_compliance_settings_form Custom form function.