'Switchtheme', 'description' => "Configure settings for the Switchtheme block, as well as change how a visitor's user agent affects which theme is used.", 'access arguments' => array('administer switch'), 'page callback' => 'drupal_get_form', 'page arguments' => array('switchtheme_admin_settings'), 'file' => 'switchtheme.admin.inc', ); $items['admin/settings/switchtheme/themes'] = array( 'title' => 'Themes', 'description' => 'Theme display settings for the Switchtheme block.', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); if (module_exists('browscap')) { $items['admin/settings/switchtheme/browser'] = array( 'title' => 'Browser', 'description' => "Configuration of switching the theme based on the visitor's browser.", 'page arguments' => array('switchtheme_admin_browser_settings'), 'access arguments' => array('administer switch'), 'type' => MENU_LOCAL_TASK, 'weight' => 2, ); } return $items; } /** * Implementation of hook_init(). */ function switchtheme_init() { global $custom_theme, $user; drupal_add_css(drupal_get_path('module', 'switchtheme') .'/switchtheme.css'); // Other modules may already have set $custom_theme, so we actually switch // the theme only if $custom_theme has not been set yet, or if we are on // administrative pages and admin_theme has been set to "System default" // ('0'). Also note that the chosen theme is stored in the global user // object for authenticated users; that value is automatically used across // sessions by Drupal core if the "select different theme" permission has // been granted. $new = false; if (isset($_REQUEST['theme'])) { $form = array('values' => array('theme' => $_REQUEST['theme'])); switchtheme_switch_form_submit('', $form); } else { if (!empty($_SESSION['custom_theme']) && empty($user->uid)) { $new = $_SESSION['custom_theme']; } } if (module_exists('browscap') && variable_get('switchtheme_browser_enabled', FALSE)) { $browser = browscap_get_browser(); if (isset($browser['parent'])) { $parent = trim($browser['parent']); $browser_theme = variable_get('switchtheme_browser_'. md5($parent), 'default'); if ($browser_theme != 'default') { $new = $browser_theme; } } } if ($new && empty($custom_theme)) { $_SESSION['custom_theme'] = $new; $custom_theme = $new; # We shouldn't need cache_clear_all() but we include it in case some more aggressive # caching module that may get added makes it necessary. It is however required in # switchtheme_switch_form_submit() so if tempted to remove it, leave in it in the # other function. if (empty($user->uid)) { cache_clear_all($base_root . request_uri(), 'cache_page'); } } } /** * Implementation of hook_theme(). */ function switchtheme_theme() { return array( 'switchtheme_block_form' => array('form' => NULL), ); } /** * Implementation of hook_block(). */ function switchtheme_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { $blocks[0]['info'] = t('Switch theme form'); $blocks[1]['info'] = t('Random theme'); return $blocks; } elseif ($delta == 0 && $op == 'view' && user_access('switch theme')) { $block['subject'] = t('Theme'); $block['content'] = drupal_get_form('switchtheme_switch_form'); return $block; } elseif ($delta == 1 && $op == 'view' && user_access('switch theme')) { $block['subject'] = t('Random theme'); $block['content'] = switchtheme_display_random_block(); return $block; } } /** * Render a random theme with screenshot to switch to. */ function switchtheme_display_random_block() { $themes = list_themes(); shuffle($themes); foreach ($themes as $key => $theme) { $theme->screenshot = dirname($theme->filename) .'/screenshot.png'; if (file_exists($theme->screenshot)) { // Return the first theme with a screenshot. $output = l("screenshot\" alt=\"preview of $theme->name\"/>", $_GET['q'], array('query' => 'theme='. $theme->name, 'html' => TRUE)); return $output; } } } function switchtheme_switch_form() { global $user, $custom_theme; $form = array(); $form['theme'] = array( '#type' => 'select', '#default_value' => !empty($custom_theme) ? $custom_theme : $user->theme, '#attributes' => array('title' => t('Change the way this site looks.')), '#options' => switchtheme_select() ); $form['submit'] = array('#id'=>'switchtheme-submit', '#type' => 'submit', '#value' => t('Switch')); return $form; } /** * Theme the block search form. */ function theme_switchtheme_block_form($form) { return '
'. form_render($form) .'
'; } /** * Process a block switchtheme form submission. * * We do not validate the input here, because that is done in init_theme() * already. */ function switchtheme_switch_form_submit($form, &$form_state) { global $custom_theme, $user, $base_root; $new=false; if ($user->uid > 0) { // Save the setting for authenticated users, if the "select different theme" // permission has been granted. if (user_access('select different theme')) { $user = user_save($user, array('theme' => $form_state['values']['theme'])); } // Otherwise save the setting in the session, just like for anonymous users. else { $new = $form_state['values']['theme']; } } elseif (user_access('switch theme')) { // Save the setting in the session for anonymous users. $new = $form_state['values']['theme']; } if ($new != $_SESSION['custom_theme']) { $_SESSION['custom_theme'] = $new; if (empty($custom_theme)) { $custom_theme = $new; if (empty($user->uid)) { cache_clear_all($base_root . request_uri(), 'cache_page'); } } } } /** * Create an array of enabled themes to select from. * * @todo Probably should come back here and cache the theme list. */ function switchtheme_options() { $options = array(); $themes = list_themes(); foreach ($themes as $name => $attr) { if ($attr->status) { $options[] = $attr->name; } } return $options; } /** * Create a select list of themes and labels. */ function switchtheme_select() { $select = array(); $options = switchtheme_options(); foreach ($options as $option) { $select[$option] = variable_get('switchtheme_'. $option, drupal_ucfirst($option)); } asort($select); return $select; }