'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, ); } Lg(__FUNCTION__, 'E', '', ''); return $items; } /** * Implementation of hook_init(). */ function switchtheme_init() { Lg(__FUNCTION__, 'S', '', ''); global $custom_theme, $user; Lg(__FUNCTION__, '1', '', ''); drupal_add_css(drupal_get_path('module', 'switchtheme') .'/switchtheme.css'); Lg(__FUNCTION__, '2', '', ''); // If query parameter 'theme' is set, we always assign a new theme. if (isset($_REQUEST['theme'])) { Lg(__FUNCTION__, '', '', ''); $form_state = array('values' => array('theme' => $_REQUEST['theme'])); switchtheme_switch_form_submit(array(), $form_state); } Lg(__FUNCTION__, '3', 'custom_theme', $custom_theme); Lg(__FUNCTION__, '', '_SESSION[custom_theme]', $_SESSION['custom_theme']); // We only switch the theme, if // - no other module defined the global $custom_theme yet // - the current page is an administrative page and the 'admin_theme' variable // is set to the system default ('0') // @see system_init() // For users with the 'select different theme' permission, the chosen theme is // stored in the {users}.theme; which is automatically used by Drupal core. // @see init_theme() if (isset($_SESSION['custom_theme']) && (empty($custom_theme) || $custom_theme === '0')) { $custom_theme = $_SESSION['custom_theme']; } Lg(__FUNCTION__, '4', 'custom_theme', $custom_theme); if (module_exists('browscap') && variable_get('switchtheme_browser_enabled', FALSE)) { Lg(__FUNCTION__, '5', '', ''); $browser = browscap_get_browser(); Lg(__FUNCTION__, '6', '', ''); if (isset($browser['parent'])) { $parent = trim($browser['parent']); Lg(__FUNCTION__, '7', '', ''); $browser_theme = variable_get('switchtheme_browser_'. md5($parent), 'default'); if ($browser_theme != 'default') { $custom_theme = $browser_theme; } } } Lg(__FUNCTION__, 'E', '', ''); } /** * Implementation of hook_theme(). */ function switchtheme_theme() { Lg(__FUNCTION__, 'S', '', ''); Lg(__FUNCTION__, 'E', '', ''); return array( 'switchtheme_block_form' => array('form' => NULL), ); } /** * Implementation of hook_block(). */ function switchtheme_block($op = 'list', $delta = 0, $edit = array()) { Lg(__FUNCTION__, 'S', '', ''); if ($op == 'list') { Lg(__FUNCTION__, '1', '', ''); $blocks[0]['info'] = t('Switch theme form'); Lg(__FUNCTION__, '2', '', ''); $blocks[1]['info'] = t('Random theme'); Lg(__FUNCTION__, 'E1', '', ''); return $blocks; } elseif ($delta == 0 && $op == 'view' && user_access('switch theme')) { Lg(__FUNCTION__, '3', '', ''); $block['subject'] = t('Theme'); Lg(__FUNCTION__, '4', '', ''); $block['content'] = drupal_get_form('switchtheme_switch_form'); Lg(__FUNCTION__, 'E2', '', ''); return $block; } elseif ($delta == 1 && $op == 'view' && user_access('switch theme')) { Lg(__FUNCTION__, '5', '', ''); $block['subject'] = t('Random theme'); Lg(__FUNCTION__, '6', '', ''); $block['content'] = switchtheme_display_random_block(); Lg(__FUNCTION__, 'E3', '', ''); return $block; } Lg(__FUNCTION__, 'E4', '', ''); } /** * Render a random theme with screenshot to switch to. */ function switchtheme_display_random_block() { Lg(__FUNCTION__, 'S', '', ''); $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)); Lg(__FUNCTION__, 'E1', '', ''); return $output; } } Lg(__FUNCTION__, 'E2', '', ''); } function switchtheme_switch_form() { Lg(__FUNCTION__, 'S', '', ''); 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')); Lg(__FUNCTION__, 'E', '', ''); return $form; } /** * Theme the block search form. */ function theme_switchtheme_block_form($form) { Lg(__FUNCTION__, 'S', '', ''); Lg(__FUNCTION__, 'E', '', ''); 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) { Lg(__FUNCTION__, 'S', '', ''); global $user; 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 { $_SESSION['custom_theme'] = $form_state['values']['theme']; } } elseif (user_access('switch theme')) { // Save the setting in the session for anonymous users. $_SESSION['custom_theme'] = $form_state['values']['theme']; } Lg(__FUNCTION__, 'E', '', ''); } /** * Create an array of enabled themes to select from. */ function switchtheme_options() { Lg(__FUNCTION__, 'S', '', ''); $options = array(); // Ensure the database is ready. // @see init_theme() drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE); foreach (list_themes() as $name => $info) { if ($info->status) { $options[$name] = $info->name; } } Lg(__FUNCTION__, 'E', '', ''); return $options; } /** * Create a select list of themes and labels. */ function switchtheme_select() { Lg(__FUNCTION__, 'S', '', ''); $select = array(); $options = switchtheme_options(); foreach ($options as $option) { $select[$option] = variable_get('switchtheme_'. $option, drupal_ucfirst($option)); } asort($select); Lg(__FUNCTION__, 'E', '', ''); return $select; }