You are here

pagestyle.module in Page Style 7

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

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

File

pagestyle.module
View source
<?php

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

/**
 * Include pagestyle .inc files.
 */
module_load_include('inc', 'pagestyle', 'includes/pagestyle.admin');
module_load_include('inc', 'pagestyle', 'includes/pagestyle.block');

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

/**
 * Implement hook_permission().
 */
function pagestyle_permission() {
  return array(
    'administer pagestyle' => array(
      'title' => t('Administer Page Style'),
    ),
    'access pagestyle content' => array(
      'title' => t('Access Page Style content'),
    ),
  );
}

/**
 * Helper function to set determine if a session value should be set.
 */
function _pagestyle_set_session() {
  return variable_get('pagestyle_set_anonymous_session', 1) || !user_is_anonymous();
}

/**
 * Implement hook_menu().
 */
function pagestyle_menu() {
  $items = array();
  $items['admin/config/user-interface/pagestyle'] = array(
    'title' => 'Page Style',
    'description' => 'Settings for the display and values.',
    'access arguments' => array(
      'administer pagestyle',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pagestyle_admin_settings',
    ),
  );
  $items['pagestyle/black_white'] = array(
    'title' => 'Page Style Black/White',
    'page callback' => 'pagestyle_black_white',
    'description' => 'Black/White page style.',
    'access arguments' => array(
      'access pagestyle content',
    ),
    'menu_name' => 'pagestyle',
  );
  $items['pagestyle/white_black'] = array(
    'title' => 'Page Style White/Black',
    'page callback' => 'pagestyle_white_black',
    'description' => 'White/Black page style.',
    'access arguments' => array(
      'access pagestyle content',
    ),
    'menu_name' => 'pagestyle',
  );
  $items['pagestyle/yellow_blue'] = array(
    'title' => 'Page Style Yellow/Blue',
    'page callback' => 'pagestyle_yellow_blue',
    'description' => 'Yellow/Blue page style.',
    'access arguments' => array(
      'access pagestyle content',
    ),
    'menu_name' => 'pagestyle',
  );
  $items['pagestyle/standard'] = array(
    'title' => 'Page Style Standard',
    'page callback' => 'pagestyle_standard',
    'description' => 'Standard page style.',
    'access arguments' => array(
      'access pagestyle content',
    ),
    'menu_name' => 'pagestyle',
  );
  $items['pagestyle/set'] = array(
    'title' => 'Page Style',
    'page callback' => 'pagestyle_set',
    'description' => 'Set the page style.',
    'access arguments' => array(
      'access pagestyle content',
    ),
    'menu_name' => 'pagestyle',
  );
  return $items;
}

/**
 * Implement hook_block_info().
 */
function pagestyle_block_info() {
  $blocks['form']['info'] = pagestyle_block_title($title = 'title');
  $blocks['form']['cache'] = DRUPAL_NO_CACHE;
  return $blocks;
}

/**
 * Implement hook_block_view().
 */
function pagestyle_block_view($delta = '') {
  $pagestyle_cookie_expires = pagestyle_cookie_expires($mode = 'php');
  $pagestyle_cookie_domain = variable_get('pagestyle_cookie_domain', base_path());
  if (user_access('access pagestyle content')) {
    $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_block_type = variable_get('pagestyle_block_type', 'text');
    $pagestyle_javascript = variable_get('pagestyle_javascript', 1);
    $block_content = theme('pagestyle_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');
    }
    if (!isset($_COOKIE['pagestyle']) && _pagestyle_set_session()) {
      setcookie("pagestyle", $pagestyle_normal, time() + $pagestyle_cookie_expires, $pagestyle_cookie_domain, "");
    }
    $block['content'] = $block_content;
    if ($pagestyle_javascript == 0 || $pagestyle_javascript == 1) {
      if (!isset($_SESSION['pagestyle']) && _pagestyle_set_session()) {
        $_SESSION['pagestyle'] = $pagestyle_normal;
      }
    }
    $block['subject'] = $block_title;
    $block['content'] = $block_content;
    return $block;
  }
}

/**
 * Add body classes into the html template.
 *
 * @see template_preprocess_html()
 */
function pagestyle_preprocess_html(&$variables) {
  $pagestyle_browser_style_switcher = variable_get('pagestyle_browser_style_switcher', 0);
  if (function_exists('pagestyle_get_current') && $pagestyle_browser_style_switcher == 0) {
    $variables['classes_array'][] = ' pagestyle_' . pagestyle_get_current($value = 'int');
  }
}

/**
 * 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(check_plain($_POST['pagestyle_select']), $pagestyle_allowed)) {
      $pagestyle_post['pagestyle'] = 1;
    }
  }
  if (isset($_COOKIE['pagestyle'])) {
    if (in_array($_COOKIE['pagestyle'], $pagestyle_allowed)) {
      $pagestyle_cookie['pagestyle'] = (string) filter_xss($_COOKIE['pagestyle']);
    }
  }

  // set session
  if ($cache != 0 && isset($_COOKIE['pagestyle']) && isset($_SESSION['pagestyle']) && $pagestyle_post['pagestyle'] != 1) {
    if ($pagestyle_cookie['pagestyle'] != $_SESSION['pagestyle']) {
      if (_pagestyle_set_session()) {
        $_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) {
  $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(check_plain($_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 = drupal_get_path('module', 'pagestyle');
  $pagestyle_current = pagestyle_get_current($value = 'int');
  $pagestyle_browser_style_switcher = variable_get('pagestyle_browser_style_switcher', 0);
  $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_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 ',
      );
  }
  $attributes_black_white = array(
    'class' => array(
      'ps_black_white',
    ),
    'rel' => $pagestyle_rel['black_white'] . 'stylesheet',
    'type' => 'text/css',
    'media' => 'screen',
    'title' => $pagestyle['black_white'],
    'href' => $uri . '/css/style_black_white.css',
    'class' => 'ps_black_white',
  );
  $attributes_white_black = array(
    'class' => array(
      'ps_white_black',
    ),
    'rel' => $pagestyle_rel['white_black'] . 'stylesheet',
    'type' => 'text/css',
    'media' => 'screen',
    'title' => $pagestyle['white_black'],
    'href' => $uri . '/css/style_white_black.css',
    'class' => 'ps_white_black',
  );
  $attributes_yellow_blue = array(
    'class' => array(
      'ps_yellow_blue',
    ),
    'rel' => $pagestyle_rel['yellow_blue'] . 'stylesheet',
    'type' => 'text/css',
    'media' => 'screen',
    'title' => $pagestyle['yellow_blue'],
    'href' => $uri . '/css/style_yellow_blue.css',
    'class' => 'ps_yellow_blue',
  );
  $attributes_standard = array(
    'class' => array(
      'ps_standard',
    ),
    'rel' => $pagestyle_rel['standard'] . 'stylesheet',
    'type' => 'text/css',
    'media' => 'screen',
    'title' => $pagestyle['standard'],
    'href' => $uri . '/css/style_standard.css',
    'class' => 'ps_standard',
  );
  if ($pagestyle_browser_style_switcher == 1) {
    if ($pagestyle_black_white == 1) {
      drupal_add_html_head_link($attributes_black_white, $header = FALSE);
    }
    if ($pagestyle_white_black == 1) {
      drupal_add_html_head_link($attributes_white_black, $header = FALSE);
    }
    if ($pagestyle_yellow_blue == 1) {
      drupal_add_html_head_link($attributes_yellow_blue, $header = FALSE);
    }
    if ($pagestyle_standard == 1) {
      drupal_add_html_head_link($attributes_standard, $header = FALSE);
    }
  }

  // font-weight
  // 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";

  // admin
  if (user_access('administer pagestyle') && $_GET['q'] == 'admin/config/content/pagestyle') {
    $pagestyle_head .= 'div.form-item-pagestyle-black-white label.option, div.form-item-pagestyle-fontweight-black-white label { font-weight: ' . $pagestyle_fontweight_black_white . '; }' . "\n";
    $pagestyle_head .= 'div.form-item-pagestyle-white-black label.option, div.form-item-pagestyle-fontweight-white-black label { font-weight: ' . $pagestyle_fontweight_white_black . '; }' . "\n";
    $pagestyle_head .= 'div.form-item-pagestyle-yellow-blue label.option, div.form-item-pagestyle-fontweight-yellow-blue label { font-weight: ' . $pagestyle_fontweight_yellow_blue . '; }' . "\n";
  }

  // 'media' => 'screen, projection, tty, tv, print'
  if (!pagestyle_admin_theme_check()) {
    drupal_add_css($pagestyle_head, $options = array(
      'type' => 'inline',
      'media' => 'screen',
    ));
  }
}

/**
 * Returns TRUE if we're on the admin theme AND we want it disabled.
 *
 * @return bool
 */
function pagestyle_admin_theme_check() {
  if (variable_get('pagestyle_disable_on_admin_theme', 0)) {
    global $theme;
    $admin_theme = variable_get('admin_theme', 0);
    return $theme == $admin_theme;
  }
  return FALSE;
}

/**
 * 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);
  if ($pagestyle_javascript == 1 || $pagestyle_javascript == 2) {
    drupal_add_js('  var pagestyleCookieExpires = ' . $pagestyle_cookie_expires . ';
  var pagestyleCookieDomain = "' . $pagestyle_cookie_domain . '";
  var pagestylePath = "' . $pagestyle_path . '";
  var pagestyleJavaScript = ' . $pagestyle_javascript . ';
  var pagestyleCurrent = "' . $pagestyle_current . '";', 'inline');
    drupal_add_js(drupal_get_path('module', 'pagestyle') . '/jquery.pagestyle.js');
  }

  //todo
  if (!pagestyle_admin_theme_check()) {
    drupal_add_css(drupal_get_path('module', 'pagestyle') . '/pagestyle.css');
  }
}

/**
 * Implement hook_init().
 */
function pagestyle_init() {
  if (function_exists('variable_get')) {
    $pagestyle_javascript = variable_get('pagestyle_javascript', 1);
    if ($pagestyle_javascript == 0 || $pagestyle_javascript == 1) {
      pagestyle_check();
    }
  }
}

/**
 * Implement hook_js_alter().
 *
 * Removes any JavaScript added if we want textsize disabled
 * on the admin theme AND we're currently using the admin theme.
 *
 * @param $javascript
 */
function pagestyle_js_alter(&$javascript) {
  if (pagestyle_admin_theme_check()) {

    // Unset the JS added with key
    $key = drupal_get_path('module', 'pagestyle') . '/jquery.pagestyle.js';
    unset($javascript[$key]);

    // Check any inline JS added with a numeric key
    foreach ($javascript as $key => $js) {
      if (!is_array($javascript[$key]['data']) && strpos($javascript[$key]['data'], 'pagestyle')) {
        unset($javascript[$key]);
      }
    }
  }
}

/**
 * Implement hook_page_build().
 */
function pagestyle_page_build() {
  pagestyle_print_html();
  pagestyle_print_js_css();
  drupal_add_library('system', 'jquery.cookie', TRUE);
}

Functions

Namesort descending Description
pagestyle_admin_theme_check Returns TRUE if we're on the admin theme AND we want it disabled.
pagestyle_block_info Implement hook_block_info().
pagestyle_block_view Implement hook_block_view().
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_js_alter Implement hook_js_alter().
pagestyle_menu Implement hook_menu().
pagestyle_page_build Implement hook_page_build().
pagestyle_permission Implement hook_permission().
pagestyle_premission Implement hook_permission().
pagestyle_preprocess_html Add body classes into the html template.
pagestyle_print_html Print HTMl code in the head
pagestyle_print_js_css Print Javascript and CSS in the head.
_pagestyle_set_session Helper function to set determine if a session value should be set.