You are here

cookie_content_blocker.admin.inc in Cookie Content Blocker 7

Admin functionality for Cookie content blocker.

File

cookie_content_blocker.admin.inc
View source
<?php

/**
 * @file
 * Admin functionality for Cookie content blocker.
 */

/**
 * Form callback for the admin settings form.
 *
 * @param array $form
 *   The structure of the form.
 * @param array $form_state
 *   The current state of the form.
 *
 * @return array
 *   The configuration form.
 *
 * @see system_settings_form_submit()
 */
function cookie_content_blocker_settings_form(array $form, array &$form_state) {
  $form = _cookie_content_blocker_variable_form($form, 'cookie_content_blocker');
  $form = _cookie_content_blocker_cookie_aware_form($form);
  $form['#after_build'] = array(
    'cookie_content_blocker_settings_form_after_build',
  );
  return system_settings_form($form);
}

/**
 * Form after build callback for the settings form.
 *
 * @param array $form_original
 *   The structure of the form.
 * @param array $form_state
 *   The current state of the form.
 *
 * @return array
 *   The configuration form.
 */
function cookie_content_blocker_settings_form_after_build(array $form_original, array &$form_state) {
  $form_shared['cookie_content_blocker_button_text'] = array(
    '#states' => array(
      'visible' => array(
        ':input[name="cookie_content_blocker_show_button"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form_shared['cookie_content_blocker_enable_click_consent_change'] = array(
    '#states' => array(
      'visible' => array(
        ':input[name="cookie_content_blocker_show_button"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );
  return array_merge_recursive($form_original, $form_shared);
}

/**
 * Add cookie awareness settings to the form.
 *
 * @param array $form
 *   The structure of the form.
 *
 * @return array
 *   The configuration form.
 */
function _cookie_content_blocker_cookie_aware_form(array $form) {
  $consent_settings = variable_get('cookie_content_blocker_consent_awareness', _cookie_consent_blocker_consent_awareness_defaults());
  $consent_settings = array_replace_recursive(_cookie_consent_blocker_consent_awareness_defaults(), $consent_settings);
  $form['cookie_content_blocker_consent_awareness'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('Consent awareness'),
    '#description' => t('Manage how Cookie content blocker knows about cookie consent and how it can change the consent. Note: only one cookie category is supported.'),
    '#collapsible' => TRUE,
    'accepted' => array(
      '#type' => 'fieldset',
      '#title' => t('Consent given'),
      '#description' => t('Define the event that is triggered when a visitor actively gives consent and the cookies to determine if consent already has been given earlier.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ),
    'declined' => array(
      '#type' => 'fieldset',
      '#title' => t('Consent refused'),
      '#description' => t('Define the event that is triggered when a visitor actively declines consent and the cookies to determine if consent already has been declined earlier.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ),
    'change' => array(
      '#type' => 'fieldset',
      '#title' => t('Change consent'),
      '#description' => t('Define the event that has to be triggered when the button or blocked content placeholder is clicked.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ),
  );
  foreach ($consent_settings as $type => $type_settings) {
    $form['cookie_content_blocker_consent_awareness'][$type]['event'] = _cookie_content_blocker_event_form($type_settings['event']);
    if ($type === 'change') {
      continue;
    }
    $form['cookie_content_blocker_consent_awareness'][$type]['cookie']['operator'] = array(
      '#type' => 'select',
      '#title' => t('Cookie comparison operator'),
      '#default_value' => $type_settings['cookie']['operator'],
      '#empty_option' => t('- Select -'),
      '#options' => _cookie_content_blocker_cookie_value_operator_options(),
      '#element_validate' => array(
        '_cookie_content_blocker_element_validate_cleanup_value',
      ),
    );
    $form['cookie_content_blocker_consent_awareness'][$type]['cookie']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Cookie name'),
      '#default_value' => $type_settings['cookie']['name'],
      '#element_validate' => array(
        '_cookie_content_blocker_element_validate_cleanup_value',
      ),
    );
    $form['cookie_content_blocker_consent_awareness'][$type]['cookie']['value'] = array(
      '#type' => 'textfield',
      '#title' => t('Cookie value'),
      '#default_value' => $type_settings['cookie']['value'],
      '#element_validate' => array(
        '_cookie_content_blocker_element_validate_cleanup_value',
      ),
    );
  }
  return $form;
}

/**
 * Create form elements for JavaScript events.
 *
 * @param array $defaults
 *   The defaults for the elements.
 *
 * @return mixed
 *   The form elements structure.
 */
function _cookie_content_blocker_event_form(array $defaults) {
  $elements['name'] = array(
    '#type' => 'textfield',
    '#title' => t('JavaScript event name'),
    '#description' => t('The event for the element selected below.'),
    '#default_value' => $defaults['name'],
    '#element_validate' => array(
      '_cookie_content_blocker_element_validate_cleanup_value',
    ),
  );
  $elements['selector'] = array(
    '#type' => 'textfield',
    '#title' => t('JavaScript event DOM element selector'),
    '#description' => t("The jQuery selector of the DOM element the above event is associated with, omit the jQuery('') or \$('') part. E.g. 'window' or '.some-class > .other-child-class' (without quotes)."),
    '#default_value' => $defaults['selector'],
    '#element_validate' => array(
      '_cookie_content_blocker_element_validate_cleanup_value',
    ),
  );
  return $elements;
}

/**
 * Returns a list of allowed cookie value operator options.
 *
 * @return array
 *   The cookie value operators.
 */
function _cookie_content_blocker_cookie_value_operator_options() {
  return array(
    '===' => t('Equals'),
    '>' => t('Greater than'),
    '<' => t('Less than'),
    'c' => t('Contains'),
    '!c' => t('Not contains'),
    'e' => t('Exists'),
    '!e' => t('Not exists'),
  );
}

/**
 * Cleanup form element values before submissiont.
 *
 * @param array $element
 *   The structure of the form element.
 * @param array $form_state
 *   The current state of the form.
 */
function _cookie_content_blocker_element_validate_cleanup_value(array $element, array &$form_state) {
  form_set_value($element, trim($element['#value']), $form_state);
}

Functions

Namesort descending Description
cookie_content_blocker_settings_form Form callback for the admin settings form.
cookie_content_blocker_settings_form_after_build Form after build callback for the settings form.
_cookie_content_blocker_cookie_aware_form Add cookie awareness settings to the form.
_cookie_content_blocker_cookie_value_operator_options Returns a list of allowed cookie value operator options.
_cookie_content_blocker_element_validate_cleanup_value Cleanup form element values before submissiont.
_cookie_content_blocker_event_form Create form elements for JavaScript events.