pagestyle.module in Page Style 6
Same filename and directory in other branches
Display a style changer on the page and in the browser menu for a better web accessibility.
File
pagestyle.moduleView 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_perm().
*/
function pagestyle_perm() {
return array(
'administer pagestyle',
'access pagestyle content',
);
}
/**
* Implement hook_menu().
*/
function pagestyle_menu() {
$items = array();
$items['admin/settings/pagestyle'] = array(
'title' => 'Page Style configuration',
'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().
*/
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);
$pagestyle_javascript = variable_get('pagestyle_javascript', 1);
if ($pagestyle_javascript == 0 || $pagestyle_javascript == 1) {
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;
$block[0]["cache"] = BLOCK_NO_CACHE;
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'] = check_plain($block_title);
$block['content'] = $block_content;
return $block;
}
}
}
/**
* Add body classes into the page template.
*
* @see template_preprocess_page()
*/
function pagestyle_preprocess_page(&$variables) {
$pagestyle_browser_style_switcher = variable_get('pagestyle_browser_style_switcher', 0);
if (function_exists('pagestyle_get_current') && $pagestyle_browser_style_switcher == 0) {
$variables['body_classes'] .= ' 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']) {
$_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(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 ',
);
}
if ($pagestyle_browser_style_switcher == 1) {
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";
// 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/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 .= '/* ]]>*/
-->
</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);
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');
}
drupal_add_css(drupal_get_path('module', 'pagestyle') . '/pagestyle.css');
}
/**
* Implement hook_init().
*/
function pagestyle_init() {
// Execute the defined functions.
pagestyle_print_html();
pagestyle_print_js_css();
pagestyle_jq_plugin();
if (function_exists('variable_get')) {
$pagestyle_javascript = variable_get('pagestyle_javascript', 1);
if ($pagestyle_javascript == 0 || $pagestyle_javascript == 1) {
pagestyle_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 pagestyle, uncomment the code below and delete the code:
*
* Delete the line:
* jquery_plugin_add('cookie');
*
* Uncomment example:
* drupal_add_js(drupal_get_path('module', 'pagestyle') .'/jquery.cookie.js');
*/
function pagestyle_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 Page Style functionality.', array(
'!link_jquery_plugin' => $link_jquery_plugin,
)), 'error');
}
}
Functions
Name![]() |
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_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 pagestyle, uncomment the code below and delete the code: |
pagestyle_menu | Implement hook_menu(). |
pagestyle_perm | Implement hook_perm(). |
pagestyle_preprocess_page | Add body classes into the page template. |
pagestyle_print_html | Print HTMl code in the head |
pagestyle_print_js_css | Print Javascript and CSS in the head. |