You are here

textsize.module in Text Size 5

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

Display a text size changer on the page for a better web accessibility.

File

textsize.module
View source
<?php

/**
 * @file
 * Display a text size changer on the page for a better web accessibility.
 */

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

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

/**
 * Implement hook_menu().
 */
function textsize_menu() {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/textsize',
    'title' => t('Text Size configuration'),
    'description' => t('Settings for the display and values.'),
    'access' => user_access('administer textsize'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'textsize_admin_settings',
    ),
  );
  $items[] = array(
    'path' => 'textsize/increase',
    'title' => t('Text Size') . ' ' . t('Increase'),
    'callback' => 'textsize_increase',
    'access' => user_access('access textsize content'),
    'type' => MENU_CALLBACK,
  );
  $items[] = array(
    'path' => 'textsize/decrease',
    'title' => t('Text Size') . ' ' . t('Decrease'),
    'callback' => 'textsize_decrease',
    'access' => user_access('access textsize content'),
    'type' => MENU_CALLBACK,
  );
  $items[] = array(
    'path' => 'textsize/normal',
    'title' => t('Text Size') . ' ' . t('Normal'),
    'callback' => 'textsize_normal',
    'access' => user_access('access textsize content'),
    'type' => MENU_CALLBACK,
  );
  $items[] = array(
    'path' => 'textsize/set',
    'title' => t('Text Size'),
    'callback' => 'textsize_set',
    'access' => user_access('access textsize content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implement hook_block().
 */
function textsize_block($op = 'list', $delta = 0) {
  $textsize_cookie_expires = textsize_cookie_expires($mode = 'php');
  $textsize_cookie_domain = variable_get('textsize_cookie_domain', base_path());
  $block_title = textsize_block_title($title = 'title');
  $textsize_normal = variable_get('textsize_normal', 100);
  $textsize_increment = variable_get('textsize_increment', 10);
  $textsize_block_title = variable_get('textsize_block_title', 'text_size');
  if (!isset($_SESSION['textsize'])) {
    $_SESSION['textsize'] = $textsize_normal;
  }
  if (!isset($_COOKIE['textsize'])) {
    setcookie("textsize", $textsize_normal, time() + $textsize_cookie_expires, $textsize_cookie_domain, "");
  }
  if ($op == "list") {
    $block[0]["info"] = $block_title;
    return $block;
  }
  elseif ($op == 'view') {
    if (user_access('access textsize content')) {
      $textsize_block_type = variable_get('textsize_block_type', 'text');
      $block_content = theme('textsize_text');
      switch ($textsize_block_type) {
        case 'text':
          $block_content = theme('textsize_text');
          break;
        case 'image':
          $block_content = theme('textsize_image');
          break;
        case 'select':
          $block_content = drupal_get_form('textsize_form');
          break;
        default:
          $block_content = theme('textsize_text');
      }
      $block['subject'] = $block_title;
      $block['content'] = $block_content;
      return $block;
    }
  }
}

/**
 * Check the cookie "textsize" value of changing (bye JavaScript) for anonymus user.
 *
 * @see textsize_set()
 */
function textsize_check() {
  $cache = variable_get('cache', 0);
  $textsize_post = array();
  $textsize_cookie = array();
  $textsize_post['textsize'] = 0;

  // allowed values
  $textsize_allowed = textsize_allowed_values();

  // check the type and the content
  if (isset($_POST['textsize_select'])) {
    if (in_array($_POST['textsize_select'], $textsize_allowed)) {
      $textsize_post['textsize'] = 1;
    }
  }
  if (isset($_COOKIE['textsize'])) {
    if (in_array($_COOKIE['textsize'], $textsize_allowed)) {
      if (function_exists('filter_xss')) {
        $textsize_cookie['textsize'] = (int) filter_xss($_COOKIE['textsize']);
      }
      else {
        $textsize_cookie['textsize'] = $_COOKIE['textsize'];
      }
    }
  }

  // set session
  if ($cache == 1 && isset($_COOKIE['textsize']) && isset($_SESSION['textsize']) && $textsize_post['textsize'] == 0) {
    if ($textsize_cookie['textsize'] != $_SESSION['textsize']) {
      $_SESSION['textsize'] = $textsize_cookie['textsize'];
      if (function_exists('drupal_goto')) {
        $dest['0'] = drupal_get_destination();
        drupal_goto('textsize/set', $dest['0']);
      }
      else {
        textsize_clear_cache();
      }
    }
  }
}

/**
 * Return the current text size.
 *
 * @return
 *   The numeric value of the current text size.
 */
function textsize_get_current($value = 'int') {
  $textsize_normal = variable_get('textsize_normal', 75);
  $textsize_increment = variable_get('textsize_increment', 5);
  $textsize_minimum = variable_get('textsize_minimum', 50);
  $textsize_maximum = variable_get('textsize_maximum', 150);
  $textsize_current = '100';
  $textsize_post = array();
  $textsize_cookie = array();

  // allowed values
  $textsize_allowed = textsize_allowed_values();

  // define the value for return
  if (!isset($_COOKIE['textsize'])) {
    $textsize_current = $textsize_normal;
  }
  elseif (isset($_POST['textsize_select']) && isset($_COOKIE['textsize'])) {

    // check the type and the content
    if (in_array($_POST['textsize_select'], $textsize_allowed)) {
      $textsize_post['textsize'] = (int) filter_xss($_POST['textsize_select']);
    }
    if ($textsize_post['textsize'] <= $textsize_maximum && $textsize_post['textsize'] >= $textsize_minimum && isset($_COOKIE['textsize'])) {
      $textsize_current = $textsize_post['textsize'];
    }
  }
  elseif (isset($_COOKIE['textsize'])) {
    $textsize_cookie = array();
    $textsize_cookie['textsize'] = '100';

    // check the type and the content
    if (in_array($_COOKIE['textsize'], $textsize_allowed)) {
      $textsize_cookie['textsize'] = (int) filter_xss($_COOKIE['textsize']);
    }
    if ($textsize_cookie['textsize'] <= $textsize_maximum && $textsize_cookie['textsize'] >= $textsize_minimum) {
      $textsize_current = $textsize_cookie['textsize'];
    }
  }
  else {
    $textsize_current = $textsize_normal;
  }
  $textsize_current_display = textsize_display($textsize_current, 0);
  if ($value == 'int' or $value == 'display' && variable_get('textsize_display', 1) == 0) {
    return $textsize_current;
  }
  elseif ($value == 'display' && variable_get('textsize_display', 1) == 1) {
    return $textsize_current_display;
  }
}

/**
 * Print HTMl code in the head.
 *
 * @return
 *   CSS HTML code in the head.
 */
function textsize_print_html() {
  $textsize = textsize_get_current($value = 'int');
  $textsize_link_type = variable_get('textsize_link_type', 'variable');
  $textsize_element = variable_get('textsize_element', 'body');
  $textsize_element_class = filter_xss(variable_get('textsize_element_class', ''));
  $textsize_important = " !important";
  if (variable_get('textsize_important', 1) == 0) {
    $textsize_important = NULL;
  }
  $ts_in = variable_get('textsize_increment', 5);
  $ts_no = variable_get('textsize_normal', 75);
  $ts_mi = variable_get('textsize_minimum', 50);
  $ts_ma = variable_get('textsize_maximum', 150);
  $textsize_allowed_values = array();
  if (is_numeric($ts_in) && is_numeric($ts_no) && is_numeric($ts_mi) && is_numeric($ts_ma)) {
    if ($ts_in >= 1 && $ts_no >= 1 && $ts_mi >= 0 && $ts_ma >= 2) {
      $ts_st = $ts_no - floor(($ts_no - $ts_mi) / $ts_in) * $ts_in;
      $ts_en = $ts_no + floor(($ts_ma - $ts_no) / $ts_in) * $ts_in;
    }
  }
  $textsize_head = '<style type="text/css" media="screen, projection, tty, tv, print">
<!--
/* <![CDATA[ */
' . $textsize_element . $textsize_element_class . ' {
  font-size: ' . $textsize . '%' . $textsize_important . ';
}
/* ]]>*/
-->
</style>';
  if ($textsize_important == " !important") {
    $textsize_head = '<style type="text/css" media="screen, projection, tty, tv, print">
<!--
/* <![CDATA[ */
' . $textsize_element . $textsize_element_class . ' {
  font-size: ' . $textsize . '% ' . $textsize_important . ';
}' . "\n";
    if ($textsize_link_type == 'fix') {
      for ($value = $ts_no - $ts_in; $value <= $ts_no + $ts_in; $value = $value + $ts_in) {
        $textsize_head .= $textsize_element . '.textsize-' . $value . ' { font-size: ' . $value . '%' . $textsize_important . '; }' . "\n";
      }
    }
    elseif ($textsize_link_type == 'variable') {
      for ($value = $ts_st; $value <= $ts_en; $value = $value + $ts_in) {
        $textsize_head .= $textsize_element . '.textsize-' . $value . ' { font-size: ' . $value . '%' . $textsize_important . '; }' . "\n";
      }
    }
    $textsize_head .= '/* ]]>*/
-->
</style>';
  }
  drupal_set_html_head($textsize_head);
}

/**
 * Cacluclate the text size for the display and link title
 *
 * @return
 *   The text size for the display.
 */
function textsize_display($value, $tsround) {
  $textsize_normal = variable_get('textsize_normal', 75);
  if (is_numeric($textsize_normal) && $textsize_normal >= 2) {
    $textsize_display = round(100 * $value / $textsize_normal, $tsround);
  }
  else {
    $textsize_normal = "75";
  }
  if (variable_get('textsize_display', 1) == 1) {
    return $textsize_display;
  }
  else {
    return $value;
  }
}

/**
 * Print Javascript and CSS in the head.
 *
 * Note: Required, because the values for the JavaScript variables are defined in the textsize settings page.
 *
 * @return
 *   JavaScript HTML code in the head.
 *
 * @see textsize_admin_settings()
 */
function textsize_print_js_css() {
  $textsize_cookie_expires = textsize_cookie_expires($mode = 'js');
  $textsize_cookie_domain = variable_get('textsize_cookie_domain', base_path());
  $textsize_element = variable_get('textsize_element', 'body');
  $textsize_element_class = filter_xss(variable_get('textsize_element_class', ''));
  $ts_in = variable_get('textsize_increment', 5);
  $ts_no = variable_get('textsize_normal', 75);
  $ts_mi = variable_get('textsize_minimum', 50);
  $ts_ma = variable_get('textsize_maximum', 150);
  $textsize_minimum = $ts_no - floor(($ts_no - $ts_mi) / $ts_in) * $ts_in;
  $textsize_maximum = $ts_no + floor(($ts_ma - $ts_no) / $ts_in) * $ts_in;

  // display
  $textsize_increment_display = textsize_display($ts_in, 0);
  $textsize_normal_display = textsize_display($ts_no, 0);
  $textsize_minimum_display = textsize_display($textsize_minimum, 0);
  $textsize_maximum_display = textsize_display($textsize_maximum, 0);
  $textsize_display = variable_get('textsize_display', 1);
  $textsize_current_textsize_text = textsize_block_title($title = 'current');
  $textsize_javascript = variable_get('textsize_javascript', 1);
  if ($textsize_javascript == 1) {
    drupal_add_js('  var textsizeCookieExpires = ' . $textsize_cookie_expires . ';
  var textsizeCookieDomain = "' . $textsize_cookie_domain . '";
  var textsizeElement = "' . $textsize_element . '";
  var textsizeElementClass = "' . $textsize_element_class . '";
  var textsizeIncrement = ' . $ts_in . ';
  var textsizeNormal = ' . $ts_no . ';
  var textsizeMinimum = ' . $textsize_minimum . ';
  var textsizeMaximum = ' . $textsize_maximum . ';
  var textsizeIncrementDisplay = ' . $textsize_increment_display . ';
  var textsizeNormalDisplay = ' . $textsize_normal_display . ';
  var textsizeDisplay = ' . $textsize_display . ';
  var textsizeMinT = "<abbr title=\\"' . t("Minimum") . '\\" class=\\"textsize\\">' . t("Min.") . '</abbr> ";
  var textsizeMaxT = "<abbr title=\\"' . t("Maximum") . '\\" class=\\"textsize\\">' . t("Max.") . '</abbr> ";
  var textsizeCurrentText = "' . $textsize_current_textsize_text . '";
  var textsizeReset = ' . variable_get('textsize_reset', 0) . ';', 'inline');
    drupal_add_js(drupal_get_path('module', 'textsize') . '/jquery.textsize.js');
  }
  if (function_exists('drupal_add_css')) {
    if (file_exists(path_to_theme() . '/textsize.css')) {
      drupal_add_css(path_to_theme() . '/textsize.css');
    }
    elseif (file_exists(drupal_get_path('module', 'textsize') . '/textsize.css')) {
      drupal_add_css(drupal_get_path('module', 'textsize') . '/textsize.css');
    }
  }
}

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

  // Execute the defined functions.
  if (function_exists('drupal_set_html_head')) {
    textsize_print_html();
  }
  if (function_exists('drupal_add_js')) {
    textsize_print_js_css();
    textsize_jq_cookie();
  }
  textsize_check();
}

/**
 * Note: By default the "jQuery Cookie Plugin" is required. Download the plugin from
 * http://plugins.jquery.com/project/Cookie in the modul textsize.
 */
function textsize_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')) {
    $textsize_jquery_cookie = drupal_get_path('module', 'textsize') . '/jquery.cookie.js';
    $jquery_cookie_plugin_link = l(t('jQuery Cookie plugin'), 'http://plugins.jquery.com/project/Cookie');
    if (!file_exists($textsize_jquery_cookie)) {
      drupal_set_message(t('The Text Size 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 "%textsize_directory".', array(
        '%jquery_cookie_js' => 'jquery.cookie.js',
        '%textsize_directory' => drupal_get_path('module', 'textsize'),
      )), 'error');
    }
    elseif (file_exists($textsize_jquery_cookie)) {
      drupal_add_js(drupal_get_path('module', 'textsize') . '/jquery.cookie.js');
    }
  }
}

Functions

Namesort descending Description
textsize_block Implement hook_block().
textsize_check Check the cookie "textsize" value of changing (bye JavaScript) for anonymus user.
textsize_display Cacluclate the text size for the display and link title
textsize_get_current Return the current text size.
textsize_init Implement hook_init().
textsize_jq_cookie Note: By default the "jQuery Cookie Plugin" is required. Download the plugin from http://plugins.jquery.com/project/Cookie in the modul textsize.
textsize_menu Implement hook_menu().
textsize_perm Implement hook_perm().
textsize_print_html Print HTMl code in the head.
textsize_print_js_css Print Javascript and CSS in the head.