You are here

pagestyle.module in Page Style 5

Same filename and directory in other branches
  1. 6 pagestyle.module
  2. 7 pagestyle.module

Display a style changer on the page and in the browser menue for a better web accessibility.

File

pagestyle.module
View source
<?php

/**
 * @file
 * Display a style changer on the page and in the browser menue for a better web accessibility.
 */

/**
 * Include pagestyle .inc files.
 */
include_once "includes/pagestyle.admin.inc";
include_once "includes/pagestyle.block.inc";

/**
 * Implement hook_perm().
 */
function pagestyle_perm() {
  return array(
    'administer pagestyle',
    'access pagestyle content',
  );
}

/**
 * Implement hook_menu().
 */
function pagestyle_menu() {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/pagestyle',
    'title' => t('Page Style configuration'),
    'description' => 'Settings for the display and values.',
    'access' => user_access('administer pagestyle'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'pagestyle_admin_settings',
    ),
  );
  $items[] = array(
    'path' => 'pagestyle/black_white',
    'title' => t('Page Style') . ' ' . t('Black') . '/' . t('White'),
    'callback' => 'pagestyle_black_white',
    'access' => user_access('access pagestyle content'),
    'type' => MENU_CALLBACK,
  );
  $items[] = array(
    'path' => 'pagestyle/white_black',
    'title' => t('Page Style') . ' ' . t('White') . '/' . t('Black'),
    'callback' => 'pagestyle_white_black',
    'access' => user_access('access pagestyle content'),
    'type' => MENU_CALLBACK,
  );
  $items[] = array(
    'path' => 'pagestyle/yellow_blue',
    'title' => t('Page Style') . ' ' . t('Yellow') . '/' . t('Blue'),
    'callback' => 'pagestyle_yellow_blue',
    'access' => user_access('access pagestyle content'),
    'type' => MENU_CALLBACK,
  );
  $items[] = array(
    'path' => 'pagestyle/standard',
    'title' => t('Page Style') . ' ' . t('Standard'),
    'callback' => 'pagestyle_standard',
    'access' => user_access('access pagestyle content'),
    'type' => MENU_CALLBACK,
  );
  $items[] = array(
    'path' => 'pagestyle/set',
    'title' => t('Page Style'),
    'callback' => 'pagestyle_set',
    'access' => user_access('access pagestyle content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implement hook_block().
 */
function pagestyle_block($op = 'list', $delta = 0) {
  $pagestyle_cookie_expires = pagestyle_cookie_expires($mode = 'php');
  $pagestyle_cookie_domain = variable_get('pagestyle_cookie_domain', base_path());
  $block_title = pagestyle_block_title($title = 'title');
  $pagestyle_current_text = variable_get('pagestyle_display_current_pagestyle', 'display');
  $pagestyle_normal = variable_get('pagestyle_normal', 'standard');
  $pagestyle_increment = variable_get('pagestyle_increment', 10);
  if (!isset($_SESSION['pagestyle'])) {
    $_SESSION['pagestyle'] = $pagestyle_normal;
  }
  if (!isset($_COOKIE['pagestyle'])) {
    setcookie("pagestyle", $pagestyle_normal, time() + $pagestyle_cookie_expires, $pagestyle_cookie_domain, "");
  }
  if ($op == "list") {
    $block[0]["info"] = $block_title;
    return $block;
  }
  elseif ($op == 'view') {
    if (user_access('access pagestyle content')) {
      $pagestyle_block_type = variable_get('pagestyle_block_type', 'text');
      switch ($pagestyle_block_type) {
        case 'text':
          $block_content = theme('pagestyle_text');
          break;
        case 'image':
          $block_content = theme('pagestyle_image');
          break;
        case 'select':
          $block_content = drupal_get_form('pagestyle_form');
          break;
        default:
          $block_content = theme('pagestyle_text');
      }
      $block['subject'] = $block_title;
      $block['content'] = $block_content;
      return $block;
    }
  }
}

/**
 * Check the cookie "pagestyle" value of changing (bye JavaScript) for anonymus user.
 *
 * @see pagestyle_set()
 */
function pagestyle_check() {
  $cache = variable_get('cache', 0);
  $pagestyle_post = array();
  $pagestyle_cookie = array();
  $pagestyle_post['pagestyle'] = 0;

  // allowed values
  $pagestyle_allowed = pagestyle_allowed_values();

  // check the type and the content
  if (isset($_POST['pagestyle_select'])) {
    if (in_array($_POST['pagestyle_select'], $pagestyle_allowed)) {
      $pagestyle_post['pagestyle'] = 1;
    }
  }
  if (isset($_COOKIE['pagestyle'])) {
    if (in_array($_COOKIE['pagestyle'], $pagestyle_allowed)) {
      if (function_exists('filter_xss')) {
        $pagestyle_cookie['pagestyle'] = (string) filter_xss($_COOKIE['pagestyle']);
      }
      else {
        $pagestyle_cookie['pagestyle'] = $_COOKIE['pagestyle'];
      }
    }
  }

  // set session
  if ($cache != 0 && isset($_COOKIE['pagestyle']) && isset($_SESSION['pagestyle']) && $pagestyle_post['pagestyle'] != 1) {
    if ($pagestyle_cookie['pagestyle'] != $_SESSION['pagestyle']) {
      $_SESSION['pagestyle'] = $pagestyle_cookie['pagestyle'];
      if (function_exists('drupal_goto')) {
        $dest['0'] = drupal_get_destination();
        drupal_goto('pagestyle/set', $dest['0']);
      }
      else {
        pagestyle_clear_cache();
      }
    }
  }
}

/**
 * Return the current page style.
 *
 * @return
 *   The text value of the current page style.
 */
function pagestyle_get_current($value = 'int') {
  $pagestyle_normal = variable_get('pagestyle_normal', 'standard');
  $pagestyle_current = 'standard';
  $pagestyle_current_name = t('Standard');
  $pagestyle = array(
    'black_white' => t('Black') . '/' . t('White'),
    'white_black' => t('White') . '/' . t('Black'),
    'yellow_blue' => t('Yellow') . '/' . t('Blue'),
    'standard' => t('Standard'),
  );
  $pagestyle_post = array();
  $pagestyle_cookie = array();

  // allowed values
  $pagestyle_allowed = pagestyle_allowed_values();

  // define the value for return
  if (!isset($_COOKIE['pagestyle'])) {
    $pagestyle_current = $pagestyle_normal;
    $pagestyle_current_name = $pagestyle[$pagestyle_normal];
  }
  elseif (isset($_POST['pagestyle_select']) && isset($_COOKIE['pagestyle'])) {

    // check the type and the content
    if (in_array($_POST['pagestyle_select'], $pagestyle_allowed)) {
      $pagestyle_post['pagestyle'] = (string) filter_xss($_POST['pagestyle_select']);
    }
    $pagestyle_current = $pagestyle_post['pagestyle'];
    $pagestyle_current_name = $pagestyle[$pagestyle_post['pagestyle']];
  }
  elseif (isset($_COOKIE['pagestyle'])) {

    // check the type and the content
    if (in_array($_COOKIE['pagestyle'], $pagestyle_allowed)) {
      $pagestyle_cookie['pagestyle'] = (string) filter_xss($_COOKIE['pagestyle']);
    }
    $pagestyle_current = $pagestyle_cookie['pagestyle'];
    $pagestyle_current_name = $pagestyle[$pagestyle_cookie['pagestyle']];
  }
  else {
    $pagestyle_current = 'standard';
    $pagestyle_current_name = $pagestyle[$pagestyle_normal];
  }
  if ($value == 'int') {
    return $pagestyle_current;
  }
  elseif ($value == 'name') {
    return $pagestyle_current_name;
  }
}

/**
 * Print HTMl code in the head
 */
function pagestyle_print_html() {
  $uri = base_path() . drupal_get_path('module', 'pagestyle');
  $pagestyle_current = pagestyle_get_current($value = 'int');
  $pagestyle_black_white = variable_get('pagestyle_black_white', 1);
  $pagestyle_white_black = variable_get('pagestyle_white_black', 1);
  $pagestyle_yellow_blue = variable_get('pagestyle_yellow_blue', 1);
  $pagestyle_standard = variable_get('pagestyle_standard', 1);
  $pagestyle_fontweight_black_white = variable_get('pagestyle_fontweight_black_white', 'normal');
  $pagestyle_fontweight_white_black = variable_get('pagestyle_fontweight_white_black', 'bold');
  $pagestyle_fontweight_yellow_blue = variable_get('pagestyle_fontweight_yellow_blue', 'bold');
  $pagestyle_fontweight_standard = variable_get('pagestyle_fontweight_standard', 'normal');
  $pagestyle_fontweight = array(
    'black_white' => $pagestyle_fontweight_black_white,
    'white_black' => $pagestyle_fontweight_white_black,
    'yellow_blue' => $pagestyle_fontweight_yellow_blue,
    'standard' => $pagestyle_fontweight_standard,
  );
  $pagestyle_head = "";
  $pagestyle = array(
    'black_white' => t('Black') . '/' . t('White'),
    'white_black' => t('White') . '/' . t('Black'),
    'yellow_blue' => t('Yellow') . '/' . t('Blue'),
    'standard' => t('Standard'),
  );
  switch ($pagestyle_current) {
    case "black_white":
      $pagestyle_rel = array(
        'standard' => 'alternate ',
        'black_white' => '',
        'white_black' => 'alternate ',
        'yellow_blue' => 'alternate ',
      );
      break;
    case "white_black":
      $pagestyle_rel = array(
        'standard' => 'alternate ',
        'black_white' => 'alternate ',
        'white_black' => '',
        'yellow_blue' => 'alternate ',
      );
      break;
    case "yellow_blue":
      $pagestyle_rel = array(
        'standard' => 'alternate ',
        'black_white' => 'alternate ',
        'white_black' => 'alternate ',
        'yellow_blue' => '',
      );
      break;
    case "standard":
      $pagestyle_rel = array(
        'standard' => '',
        'black_white' => 'alternate ',
        'white_black' => 'alternate ',
        'yellow_blue' => 'alternate ',
      );
      break;
    default:
      $pagestyle_rel = array(
        'standard' => '',
        'black_white' => 'alternate ',
        'white_black' => 'alternate ',
        'yellow_blue' => 'alternate ',
      );
  }
  if ($pagestyle_black_white == 1) {
    $pagestyle_head = '<link rel="' . $pagestyle_rel['black_white'] . 'stylesheet" type="text/css" media="screen, projection, tty, tv" class="ps_black_white" title="' . $pagestyle['black_white'] . '" href="' . $uri . '/css/style_black_white.css" />' . "\n";
  }
  if ($pagestyle_white_black == 1) {
    $pagestyle_head .= '<link rel="' . $pagestyle_rel['white_black'] . 'stylesheet" type="text/css" media="screen, projection, tty, tv" class="ps_white_black" title="' . $pagestyle['white_black'] . '" href="' . $uri . '/css/style_white_black.css" />' . "\n";
  }
  if ($pagestyle_yellow_blue == 1) {
    $pagestyle_head .= '<link rel="' . $pagestyle_rel['yellow_blue'] . 'stylesheet" type="text/css" media="screen, projection, tty, tv" class="ps_yellow_blue" title="' . $pagestyle['yellow_blue'] . '" href="' . $uri . '/css/style_yellow_blue.css" />' . "\n";
  }
  if ($pagestyle_standard == 1) {
    $pagestyle_head .= '<link rel="' . $pagestyle_rel['standard'] . 'stylesheet" type="text/css" media="screen, projection, tty, tv" class="ps_standard" title="' . $pagestyle['standard'] . '" href="' . $uri . '/css/style_standard.css" />' . "\n";
  }

  // font-weight
  $pagestyle_head .= '<style type="text/css" media="screen, projection, tty, tv, print">
<!--
/* <![CDATA[ */' . "\n";

  // general font-weight
  if ($pagestyle_fontweight_black_white != 'normal' && $pagestyle_current == "black_white" || $pagestyle_fontweight_white_black != 'normal' && $pagestyle_current == "white_black" || $pagestyle_fontweight_yellow_blue != 'normal' && $pagestyle_current == "yellow_blue" || $pagestyle_fontweight_standard != 'normal' && $pagestyle_current == "standard") {
    $pagestyle_head .= 'body, * { font-weight: ' . $pagestyle_fontweight[$pagestyle_current] . ' !important; }' . "\n";
  }

  // font-weight class style for jQuery
  $pagestyle_head .= 'body.pagestyle_black_white, body.pagestyle_black_white * { font-weight: ' . $pagestyle_fontweight_black_white . ' !important; }' . "\n";
  $pagestyle_head .= 'body.pagestyle_white_black, body.pagestyle_white_black * { font-weight: ' . $pagestyle_fontweight_white_black . ' !important; }' . "\n";
  $pagestyle_head .= 'body.pagestyle_yellow_blue, body.pagestyle_yellow_blue * { font-weight: ' . $pagestyle_fontweight_yellow_blue . ' !important; }' . "\n";
  $pagestyle_head .= 'body.pagestyle_standard, body.pagestyle_standard * { font-weight: ' . $pagestyle_fontweight_standard . ' !important; }' . "\n";

  // admin
  if (user_access('administer pagestyle') && $_GET['q'] == 'admin/settings/pagestyle') {
    $pagestyle_head .= 'div#edit-pagestyle-black-white-wrapper label, div#edit-pagestyle-fontweight-black-white-wrapper label { font-weight: ' . $pagestyle_fontweight_black_white . '; }' . "\n";
    $pagestyle_head .= 'div#edit-pagestyle-white-black-wrapper label, div#edit-pagestyle-fontweight-white-black-wrapper label { font-weight: ' . $pagestyle_fontweight_white_black . '; }' . "\n";
    $pagestyle_head .= 'div#edit-pagestyle-yellow-blue-wrapper label, div#edit-pagestyle-fontweight-yellow-blue-wrapper label { font-weight: ' . $pagestyle_fontweight_yellow_blue . '; }' . "\n";
    $pagestyle_head .= 'div#edit-pagestyle-standard-wrapper label, div#edit-pagestyle-fontweight-standard-wrapper label { font-weight: ' . $pagestyle_fontweight_standard . '; }' . "\n";
  }
  $pagestyle_head .= '/* ]]>*/
-->
</style>' . "\n";
  drupal_set_html_head($pagestyle_head);
}

/**
 * Print Javascript and CSS in the head.
 *
 * @return
 *   Link to the jquery.pagestyle.js and pagestyle.css.
 */
function pagestyle_print_js_css() {
  $pagestyle_cookie_expires = pagestyle_cookie_expires($mode = 'js');
  $pagestyle_cookie_domain = variable_get('pagestyle_cookie_domain', base_path());
  $pagestyle_path = drupal_get_path('module', 'pagestyle');
  $pagestyle_current = pagestyle_get_current($value = 'int');
  $pagestyle_javascript = variable_get('pagestyle_javascript', 1);
  $pagestyle = array(
    'black_white' => t('Black') . '/' . t('White'),
    'white_black' => t('White') . '/' . t('Black'),
    'yellow_blue' => t('Yellow') . '/' . t('Blue'),
    'standard' => t('Standard'),
  );
  if ($pagestyle_javascript == 1) {
    drupal_add_js('  var pagestyleCookieExpires = ' . $pagestyle_cookie_expires . ';
  var pagestyleCookieDomain = "' . $pagestyle_cookie_domain . '";
  var pagestylePath = "' . $pagestyle_path . '";
  var pagestyleCurrent = "' . $pagestyle_current . '";
  var pagestyle = new Array();
  pagestyle["black_white"] = "' . $pagestyle['black_white'] . '";
  pagestyle["white_black"] = "' . $pagestyle['white_black'] . '";
  pagestyle["yellow_blue"] = "' . $pagestyle['yellow_blue'] . '";
  pagestyle["standard"] = "' . $pagestyle['standard'] . '";
  var pagestyleCurrentText = "' . t("Current Style") . '";', 'inline');
    drupal_add_js(drupal_get_path('module', 'pagestyle') . '/jquery.pagestyle.js');
  }
  if (function_exists('drupal_add_css')) {
    if (file_exists(path_to_theme() . '/pagestyle.css')) {
      drupal_add_css(path_to_theme() . '/pagestyle.css');
    }
    elseif (file_exists(drupal_get_path('module', 'pagestyle') . '/pagestyle.css')) {
      drupal_add_css(drupal_get_path('module', 'pagestyle') . '/pagestyle.css');
    }
  }
}

/**
 * Implement hook_init().
 */
function pagestyle_init() {

  // Execute the defined functions.
  if (function_exists('drupal_set_html_head')) {
    pagestyle_print_html();
  }
  if (function_exists('drupal_add_js')) {
    pagestyle_print_js_css();
    pagestyle_jq_cookie();
  }
  pagestyle_check();
}

/**
 * Note: By default the "jQuery Cookie Plugin" is required. Download the plugin from
 * http://plugins.jquery.com/project/Cookie in the modul pagestyle.
 */
function pagestyle_jq_cookie() {
  if (!function_exists("drupal_get_path")) {
    function drupal_get_path($type, $name) {
      return dirname(drupal_get_filename($type, $name));
    }
  }
  if (function_exists('drupal_add_js')) {
    $pagestyle_jquery_cookie = drupal_get_path('module', 'pagestyle') . '/jquery.cookie.js';
    $jquery_cookie_plugin_link = l(t('jQuery Cookie plugin'), 'http://plugins.jquery.com/project/Cookie');
    if (!file_exists($pagestyle_jquery_cookie)) {
      drupal_set_message(t('The Page Style module require the !jquery_cookie_plugin_link.', array(
        '!jquery_cookie_plugin_link' => $jquery_cookie_plugin_link,
      )) . ' ' . t('Place the plugin (%jquery_cookie_js) in the module directory "%pagestyle_directory".', array(
        '%jquery_cookie_js' => 'jquery.cookie.js',
        '%pagestyle_directory' => drupal_get_path('module', 'pagestyle'),
      )), 'error');
    }
    elseif (file_exists($pagestyle_jquery_cookie)) {
      drupal_add_js(drupal_get_path('module', 'pagestyle') . '/jquery.cookie.js');
    }
  }
}

Functions

Namesort descending Description
pagestyle_block Implement hook_block().
pagestyle_check Check the cookie "pagestyle" value of changing (bye JavaScript) for anonymus user.
pagestyle_get_current Return the current page style.
pagestyle_init Implement hook_init().
pagestyle_jq_cookie Note: By default the "jQuery Cookie Plugin" is required. Download the plugin from http://plugins.jquery.com/project/Cookie in the modul pagestyle.
pagestyle_menu Implement hook_menu().
pagestyle_perm Implement hook_perm().
pagestyle_print_html Print HTMl code in the head
pagestyle_print_js_css Print Javascript and CSS in the head.