textsize.module in Text Size 7
Same filename and directory in other branches
Display a text size changer on the page for a better web accessibility.
File
textsize.moduleView 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_permission().
*/
function textsize_permission() {
return array(
'administer textsize' => array(
'title' => t('Administer Text Size'),
),
'access textsize content' => array(
'title' => t('Access Text Size content'),
),
);
}
/**
* Helper function to set determine if a session value should be set.
*/
function _textsize_set_session() {
return variable_get('textsize_set_anonymous_session', 1) || !user_is_anonymous();
}
/**
* Implement hook_menu().
*/
function textsize_menu() {
$items = array();
$items['admin/config/user-interface/textsize'] = array(
'title' => 'Text Size',
'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_info().
*/
function textsize_block_info() {
$blocks['form']['info'] = textsize_block_title($title = 'title');
$blocks['form']['cache'] = DRUPAL_NO_CACHE;
return $blocks;
}
/**
* Implement hook_block_view().
*/
function textsize_block_view($delta = '') {
$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', 6);
$textsize_normal = variable_get('textsize_normal', 76);
$textsize_current_text = variable_get('textsize_display_current_textsize', 'display');
$textsize_block_title = variable_get('textsize_block_title', 'text_size');
$textsize_block_type = variable_get('textsize_block_type', 'text');
$textsize_javascript = variable_get('textsize_javascript', 1);
$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');
}
if (!isset($_COOKIE['textsize']) && _textsize_set_session()) {
setcookie("textsize", $textsize_normal, time() + $textsize_cookie_expires, $textsize_cookie_domain, "");
}
$block['content'] = $block_content;
if ($textsize_javascript == 0 || $textsize_javascript == 1) {
if (!isset($_SESSION['textsize']) && _textsize_set_session()) {
$_SESSION['textsize'] = $textsize_normal;
}
}
if (user_access('access textsize content')) {
$block['subject'] = $block_title;
$block['content'] = $block_content;
return $block;
}
}
/**
* Add body classes into the html template.
*
* @see template_preprocess_html()
*/
function textsize_preprocess_html(&$variables) {
if (function_exists('textsize_get_current')) {
$variables['classes_array'][] = ' 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_cookie['textsize'] = NULL;
$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']) {
if (_textsize_set_session()) {
$_SESSION['textsize'] = $textsize_cookie['textsize'];
}
if (function_exists('drupal_goto') && user_access('access textsize content')) {
$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', 6);
$textsize_normal = variable_get('textsize_normal', 76);
$textsize_minimum = variable_get('textsize_minimum', 50);
$textsize_maximum = variable_get('textsize_maximum', 150);
$textsize_current = '100';
$textsize_post = array();
$textsize_cookie = array();
$textsize_cookie['textsize'] = NULL;
// 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, 2);
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_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', 6);
$ts_no = variable_get('textsize_normal', 76);
$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 >= 0.01 && $ts_no >= 0.02 && $ts_mi >= 0.01 && $ts_ma >= 0.03) {
$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 = $textsize_element . $textsize_element_class . ' {
font-size: ' . $textsize . '%' . $textsize_important . ';
}';
if ($textsize_important == " !important") {
$textsize_head = $textsize_element . $textsize_element_class . ' {
font-size: ' . $textsize . '% ' . $textsize_important . ';
}';
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: ' . round($value, 2) . '%' . $textsize_important . '; } ';
}
}
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: ' . round($value, 2) . '%' . $textsize_important . '; } ';
}
}
}
// 'media' => 'screen, projection, tty, tv, print'
if (!textsize_admin_theme_check()) {
drupal_add_css($textsize_head, $options = array(
'type' => 'inline',
'media' => 'screen',
));
}
}
/**
* 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', 76);
$textsize_display = round(100 * $value / $textsize_normal, $tsround);
if (variable_get('textsize_display', 1) == 1) {
return $textsize_display;
}
else {
return $value;
}
}
/**
* Returns TRUE if we're on the admin theme AND we want it disabled.
*
* @return bool
*/
function textsize_admin_theme_check() {
if (variable_get('textsize_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.
*
* 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', 6);
$ts_no = variable_get('textsize_normal', 76);
$ts_mi = variable_get('textsize_minimum', 50);
$ts_ma = variable_get('textsize_maximum', 150);
$textsize_minimum = $ts_no - ceil(($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, 2);
$textsize_normal_display = textsize_display($ts_no, 2);
$textsize_minimum_display = textsize_display($textsize_minimum, 2);
$textsize_maximum_display = textsize_display($textsize_maximum, 2);
$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', array(
'every_page' => TRUE,
));
}
//todo
if (!textsize_admin_theme_check()) {
drupal_add_css(drupal_get_path('module', 'textsize') . '/textsize.css');
}
}
/**
* Implement hook_init().
*/
function textsize_init() {
if (function_exists('variable_get')) {
$textsize_javascript = variable_get('textsize_javascript', 1);
if ($textsize_javascript == 0 || $textsize_javascript == 1) {
textsize_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 textsize_js_alter(&$javascript) {
if (textsize_admin_theme_check()) {
// Unset the JS added with key
$key = drupal_get_path('module', 'textsize') . '/jquery.textsize.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'], 'textsize')) {
unset($javascript[$key]);
}
}
}
}
/**
* Implement hook_page_build()
*/
function textsize_page_build() {
textsize_print_html();
textsize_print_js_css();
drupal_add_library('system', 'jquery.cookie', TRUE);
}
Functions
Name![]() |
Description |
---|---|
textsize_admin_theme_check | Returns TRUE if we're on the admin theme AND we want it disabled. |
textsize_block_info | Implement hook_block_info(). |
textsize_block_view | Implement hook_block_view(). |
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_js_alter | Implement hook_js_alter(). |
textsize_menu | Implement hook_menu(). |
textsize_page_build | Implement hook_page_build() |
textsize_permission | Implement hook_permission(). |
textsize_preprocess_html | Add body classes into the html template. |
textsize_print_html | Print HTMl code in the head. |
textsize_print_js_css | Print Javascript and CSS in the head. |
_textsize_set_session | Helper function to set determine if a session value should be set. |