You are here

textsize.module in Text Size 6

Same filename and directory in other branches
  1. 5 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.
 */
module_load_include('inc', 'textsize', 'includes/textsize.admin');
module_load_include('inc', 'textsize', 'includes/textsize.block');

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

/**
 * Implement hook_menu().
 */
function textsize_menu() {
  $items = array();
  $items['admin/settings/textsize'] = array(
    'title' => 'Text Size configuration',
    'description' => 'Settings for the display and values.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'textsize_admin_settings',
    ),
    'access arguments' => array(
      'administer textsize',
    ),
  );
  $items['textsize/increase'] = array(
    'title' => 'Text Size Increase',
    'page callback' => 'textsize_increase',
    'description' => 'Decrease text size.',
    'access arguments' => array(
      'access textsize content',
    ),
    'menu_name' => 'textsize',
  );
  $items['textsize/decrease'] = array(
    'title' => 'Text Size Decrease',
    'page callback' => 'textsize_decrease',
    'description' => 'Increase text size.',
    'access arguments' => array(
      'access textsize content',
    ),
    'menu_name' => 'textsize',
  );
  $items['textsize/normal'] = array(
    'title' => 'Text Size Normal',
    'page callback' => 'textsize_normal',
    'description' => 'Set the text size to normal.',
    'access arguments' => array(
      'access textsize content',
    ),
    'menu_name' => 'textsize',
  );
  $items['textsize/set'] = array(
    'title' => 'Text Size',
    'page callback' => 'textsize_set',
    'description' => 'Set the text size.',
    'access arguments' => array(
      'access textsize content',
    ),
    'menu_name' => 'textsize',
  );
  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_increment = variable_get('textsize_increment', 5);
  $textsize_normal = variable_get('textsize_normal', 75);
  $textsize_block_title = variable_get('textsize_block_title', 'text_size');
  $block_content = theme('textsize_text');
  $textsize_javascript = variable_get('textsize_javascript', 1);
  if ($textsize_javascript == 0 || $textsize_javascript == 1) {
    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;
    $block[0]["cache"] = BLOCK_NO_CACHE;
    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'] = check_plain($block_title);
      $block['content'] = $block_content;
      return $block;
    }
  }
}

/**
 * Add body classes into the page template.
 *
 * @see template_preprocess_page()
 */
function textsize_preprocess_page(&$variables) {
  if (function_exists('textsize_get_current')) {
    $variables['body_classes'] .= ' textsize-' . textsize_get_current($value = 'int');
  }
}

/**
 * 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(check_plain($_POST['textsize_select']), $textsize_allowed)) {
      $textsize_post['textsize'] = 1;
    }
  }
  if (isset($_COOKIE['textsize'])) {
    if (in_array($_COOKIE['textsize'], $textsize_allowed)) {
      $textsize_cookie['textsize'] = (double) filter_xss($_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_increment = variable_get('textsize_increment', 5);
  $textsize_normal = variable_get('textsize_normal', 75);
  $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(check_plain($_POST['textsize_select']), $textsize_allowed)) {
      $textsize_post['textsize'] = (double) 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'] = (double) 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);
  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 - ceil(($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-' . str_replace('.', '-', round($value, 2)) . ' { 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-' . str_replace('.', '-', round($value, 2)) . ' { 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);
  $textsize_display = round(100 * $value / $textsize_normal, $tsround);
  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_animate = variable_get('textsize_animate', 0);
  $textsize_animate_duration = variable_get('textsize_animate_duration', 1000);
  $textsize_cookie_expires = textsize_cookie_expires($mode = 'js');
  $textsize_cookie_domain = variable_get('textsize_cookie_domain', base_path());
  $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', ''));
  $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_get_current($value = 'int');
  $textsize_current_textsize_text = textsize_block_title($title = 'current');
  $textsize_javascript = variable_get('textsize_javascript', 1);
  if ($textsize_javascript == 1 || $textsize_javascript == 2) {
    drupal_add_js('  var textsizeAnimate = ' . $textsize_animate . ';
  var textsizeAnimateDuration = ' . $textsize_animate_duration . ';
  var textsizeCookieExpires = ' . $textsize_cookie_expires . ';
  var textsizeCookieDomain = "' . $textsize_cookie_domain . '";
  var textsizeLinkeType = "' . $textsize_link_type . '";
  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 textsizeJavaScript = ' . $textsize_javascript . ';
  var textsizeCurrent = "' . $textsize_current . '";
  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');
  }
  drupal_add_css(drupal_get_path('module', 'textsize') . '/textsize.css');
}

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

  // Execute the defined functions.
  textsize_print_html();
  textsize_print_js_css();
  textsize_jq_plugin();
  if (function_exists('variable_get')) {
    $textsize_javascript = variable_get('textsize_javascript', 1);
    if ($textsize_javascript == 0 || $textsize_javascript == 1) {
      textsize_check();
    }
  }
}

/**
 * Note: By default the "jQuery plugins" module is required. You can also download the plugin from
 * http://plugins.jquery.com/project/Cookie in the modul textsize, uncomment the code below and delete the code:
 *
 * Delete the line:
 *   jquery_plugin_add('cookie');
 *
 * Uncomment example:
 *   drupal_add_js(drupal_get_path('module', 'textsize') .'/jquery.cookie.js');
 */
function textsize_jq_plugin() {
  $link_jquery_plugin = l(t('jQuery plugins'), 'http://drupal.org/project/jquery_plugin');
  if (function_exists('jquery_plugin_add')) {
    jquery_plugin_add('cookie');
  }
  else {
    drupal_set_message(t('Drupal\'s %link_jquery_plugin Module Required for Text Size functionality.', array(
      '%link_jquery_plugin' => $link_jquery_plugin,
    )), 'error');
  }
}

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_plugin Note: By default the "jQuery plugins" module is required. You can also download the plugin from http://plugins.jquery.com/project/Cookie in the modul textsize, uncomment the code below and delete the code:
textsize_menu Implement hook_menu().
textsize_perm Implement hook_perm().
textsize_preprocess_page Add body classes into the page template.
textsize_print_html Print HTMl code in the head.
textsize_print_js_css Print Javascript and CSS in the head.