You are here

high_contrast.module in High contrast 6

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

File

high_contrast.module
View source
<?php

// $Id$

/*
TO-DO:
 - Provide hooks: hook_high_contrast_enable, hook_high_contrast_disable
 - Make it work if cache is enabled
 - Add widgets: select list, radio buttons, etc.
 - Improve CSS stylesheet
*/

/**
 * Implementation of hook_init()
 */
function high_contrast_init() {
  drupal_add_css(drupal_get_path('module', 'high_contrast') . '/high_contrast.css');
}

/**
 * Implementation of hook_menu()
 */
function high_contrast_menu() {
  $items['admin/settings/high-contrast'] = array(
    'title' => 'High Contrast',
    'description' => 'Settings related to High Contrast',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'high_contrast_settings_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  $items['high_contrast_enable'] = array(
    'title' => 'Activate high contrast styles',
    'description' => '',
    'page callback' => 'high_contrast_enable_callback',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['high_contrast_disable'] = array(
    'title' => 'Disable high contrast styles',
    'description' => '',
    'page callback' => 'high_contrast_disable_callback',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_block()
 */
function high_contrast_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0] = array(
      'info' => t('High contrast switcher'),
    );
    return $blocks;
  }
  elseif ($op == 'view') {
    switch ($delta) {
      case 0:
        $switcher = theme("high_contrast_switcher");
        $block = array(
          'subject' => ' ',
          'content' => $switcher,
        );
        break;
    }
    return $block;
  }
}

/**
 * Settings form
 */
function high_contrast_settings_form() {
  $form = array();
  $widgets = array(
    "links" => "Links",
  );
  $form['high_contrast_switcher_widget'] = array(
    '#type' => 'select',
    '#title' => t('Switcher widget'),
    '#default_value' => _high_contrast_variable_get("high_contrast_switcher_widget"),
    '#options' => $widgets,
    '#required' => TRUE,
  );
  $form['high_contrast_switcher_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Switcher label'),
    '#default_value' => _high_contrast_variable_get("high_contrast_switcher_label"),
    '#required' => TRUE,
  );
  $form['high_contrast_high_label'] = array(
    '#type' => 'textfield',
    '#title' => t('High contrast label'),
    '#default_value' => _high_contrast_variable_get("high_contrast_high_label"),
    '#required' => TRUE,
  );
  $form['high_contrast_separator'] = array(
    '#type' => 'textfield',
    '#title' => t('Separator'),
    '#default_value' => _high_contrast_variable_get("high_contrast_separator"),
    '#required' => TRUE,
  );
  $form['high_contrast_normal_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Normal contrast label'),
    '#default_value' => _high_contrast_variable_get("high_contrast_normal_label"),
    '#required' => TRUE,
  );
  $form['high_contrast_css_styles'] = array(
    '#type' => 'textarea',
    '#title' => t('Used CSS styles'),
    '#default_value' => _high_contrast_get_current_css_styles(),
    '#required' => TRUE,
    '#attributes' => array(
      'style' => 'width:50em; height:30em; line-height:1.2em; font-family:"Courier New", "Courier";',
    ),
    '#suffix' => '<p>' . t('Default CSS styles used to override the active theme') . ':</p><div><pre>' . _high_contrast_get_default_css_styles() . '</pre></div>',
  );
  return system_settings_form($form);
}

/**
 * Menu callback to enable the high contrast styles
 */
function high_contrast_enable_callback() {
  $destination = $_GET["destination"];
  high_contrast_switch(TRUE, $destination);
}

/**
 * Menu callback to disable the high contrast styles
 */
function high_contrast_disable_callback() {
  $destination = $_GET["destination"];
  high_contrast_switch(FALSE, $destination);
}

/**
 * Enables/Disables high contrast styles and redirects the user to a destination url
 */
function high_contrast_switch($enable, $destination = "") {
  $_SESSION["high_contrast_activated"] = $enable ? 1 : 0;
  if (empty($destination)) {
    $destination = _high_contrast_get_current_url();
  }
  drupal_goto($destination);
}

/***** theme and preprocess functions *****/
function high_contrast_preprocess_page(&$vars) {
  if ($_SESSION["high_contrast_activated"] == 1) {
    $vars["styles"] .= '<style type="text/css">' . _high_contrast_get_current_css_styles() . '</style>';
  }
}

/**
 * Implementation of hook_theme()
 */
function high_contrast_theme() {
  return array(
    'high_contrast_switcher' => array(
      'arguments' => array(
        'vars' => NULL,
      ),
    ),
    'high_contrast_switcher_links' => array(
      'arguments' => array(
        'vars' => NULL,
      ),
    ),
  );
}
function theme_high_contrast_switcher($vars) {
  $code = theme("high_contrast_switcher_links");

  /*
  //TO-DO: More widgets
  $widget = _high_contrast_variable_get("high_contrast_widget");

  if ($widget == "select"){
    $code = theme("high_contrast_switcher_select");
  }
  else if ($widget == "radio"){
    $code = theme("high_contrast_switcher_radio");
  }
  else{
    $code = theme("high_contrast_switcher_links");
  }
  */
  return $code;
}
function theme_high_contrast_switcher_links($vars) {
  $label = t(_high_contrast_variable_get("high_contrast_switcher_label"));
  $current_url = _high_contrast_get_current_url();
  if ($_SESSION["high_contrast_activated"] == 1) {
    $link_high = t(_high_contrast_variable_get("high_contrast_high_label"));
    $link_normal = l(t(_high_contrast_variable_get("high_contrast_normal_label")), 'high_contrast_disable', array(
      'query' => 'destination=' . $current_url,
    ));
  }
  else {
    $link_high = l(t(_high_contrast_variable_get("high_contrast_high_label")), 'high_contrast_enable', array(
      'query' => 'destination=' . $current_url,
    ));
    $link_normal = t(_high_contrast_variable_get("high_contrast_normal_label"));
  }
  $separator = t(_high_contrast_variable_get("high_contrast_separator"));

  //output
  $code = '
  <div class="high_contrast_switcher high_contrast_switcher_links">
    <p><span class="high_contrast_switcher_label">' . $label . '</span><span class="high_contrast_switcher_high">' . $link_high . '</span><span class="high_contrast_switcher_separator">' . $separator . '</span><span class="high_contrast_switcher_normal">' . $link_normal . '</span></p>
  </div>
  ';
  return $code;
}

/***** helper functions *****/
function _high_contrast_variable_get($var) {
  return variable_get($var, "");
}
function _high_contrast_get_current_css_styles() {
  $styles = _high_contrast_variable_get("high_contrast_css_styles");
  if (empty($styles)) {
    $styles = _high_contrast_get_default_css_styles();
    variable_set("high_contrast_css_styles", $styles);
  }
  return $styles;
}
function _high_contrast_get_current_url() {
  $page_url = 'http';
  if ($_SERVER["HTTPS"] == "on") {
    $page_url .= "s";
  }
  $page_url .= "://";
  if ($_SERVER["SERVER_PORT"] != "80") {
    $page_url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  }
  else {
    $page_url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  }
  return $page_url;
}
function _high_contrast_get_default_css_styles() {
  $color1 = 'black';
  $color2 = 'yellow';
  $color3 = 'white';
  $css = '
*{
  background-color:' . $color1 . ' !important;
  background-image:none !important;
  color:' . $color3 . ' !important;
  line-height:1.5em !important;
  text-shadow: none !important;
}

a, a *{
  background-color:' . $color1 . ' !important;
  color:' . $color2 . ' !important;
  text-decoration:underline !important;
}
  a:hover, a:hover *{
    background-color:' . $color2 . ' !important;
    color:' . $color1 . ' !important;
    text-decoration:underline !important;
    text-decoration:none !important;
  }

input{
  background-color:' . $color3 . ' !important;
  color:' . $color1 . ' !important;
}

input[type=submit],
input[type=button],
button{
  background-color: ' . $color2 . ' !important;
  color: ' . $color1 . ' !important;
}
  input[type=submit]:hover,
  input[type=button]:hover,
  button:hover{
    text-decoration:underline !important;
  }
';
  return $css;
}

Functions

Namesort descending Description
high_contrast_block Implementation of hook_block()
high_contrast_disable_callback Menu callback to disable the high contrast styles
high_contrast_enable_callback Menu callback to enable the high contrast styles
high_contrast_init Implementation of hook_init()
high_contrast_menu Implementation of hook_menu()
high_contrast_preprocess_page
high_contrast_settings_form Settings form
high_contrast_switch Enables/Disables high contrast styles and redirects the user to a destination url
high_contrast_theme Implementation of hook_theme()
theme_high_contrast_switcher
theme_high_contrast_switcher_links
_high_contrast_get_current_css_styles
_high_contrast_get_current_url
_high_contrast_get_default_css_styles
_high_contrast_variable_get