/[drupal]/contributions/modules/switchtheme/switchtheme.module
ViewVC logotype

Contents of /contributions/modules/switchtheme/switchtheme.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.17 - (show annotations) (download) (as text)
Mon Aug 10 23:47:00 2009 UTC (3 months, 2 weeks ago) by sun
Branch: MAIN
CVS Tags: HEAD
Changes since 1.16: +12 -14 lines
File MIME type: text/x-php
#455796 by sun: Fixed query string allows to select disabled themes.
1 <?php
2 // $Id: switchtheme.module,v 1.16 2009/08/02 23:42:11 sun Exp $
3
4 /**
5 * @file
6 * Adds a block with a user theme switcher.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function switchtheme_help($path, $arg) {
13 switch ($path) {
14 case 'admin/settings/modules#description':
15 return t('Creates a switch theme block to allow users to switch themes on the fly. The block can be enabled by role and the admin can define labels to use for each available theme. It also allows the theme to be changed based on the visitor browser (requires browscap).');
16
17 case 'admin/settings/switchtheme':
18 case 'admin/settings/switchtheme/themes':
19 return t('Set a label for each enabled theme. This is what will be displayed to the user in the selection box.');
20
21 case 'admin/settings/switchtheme/browser':
22 return t('Change which theme is used for each browser.');
23 }
24 }
25
26 /**
27 * Implementation of hook_perm().
28 */
29 function switchtheme_perm() {
30 return array('administer switch', 'switch theme');
31 }
32
33 /**
34 * Implementation of hook_menu().
35 */
36 function switchtheme_menu() {
37 $items = array();
38 $items['admin/settings/switchtheme'] = array(
39 'title' => 'Switchtheme',
40 'description' => "Configure settings for the Switchtheme block, as well as change how a visitor's user agent affects which theme is used.",
41 'access arguments' => array('administer switch'),
42 'page callback' => 'drupal_get_form',
43 'page arguments' => array('switchtheme_admin_settings'),
44 'file' => 'switchtheme.admin.inc',
45 );
46 $items['admin/settings/switchtheme/themes'] = array(
47 'title' => 'Themes',
48 'description' => 'Theme display settings for the Switchtheme block.',
49 'type' => MENU_DEFAULT_LOCAL_TASK,
50 'weight' => -10,
51 );
52 if (module_exists('browscap')) {
53 $items['admin/settings/switchtheme/browser'] = array(
54 'title' => 'Browser',
55 'description' => "Configuration of switching the theme based on the visitor's browser.",
56 'page arguments' => array('switchtheme_admin_browser_settings'),
57 'access arguments' => array('administer switch'),
58 'type' => MENU_LOCAL_TASK,
59 'weight' => 2,
60 );
61 }
62 return $items;
63 }
64
65 /**
66 * Implementation of hook_init().
67 */
68 function switchtheme_init() {
69 global $custom_theme, $user;
70 drupal_add_css(drupal_get_path('module', 'switchtheme') .'/switchtheme.css');
71
72 // If query parameter 'theme' is set, we always assign a new theme.
73 if (isset($_REQUEST['theme'])) {
74 $form = array('values' => array('theme' => $_REQUEST['theme']));
75 switchtheme_switch_form_submit('', $form);
76 }
77 // Other modules may already have set $custom_theme, so we actually switch
78 // the theme only if $custom_theme has not been set yet, or if we are on
79 // administrative pages and admin_theme has been set to "System default"
80 // ('0'). Also note that the chosen theme is stored in the global user
81 // object for authenticated users; that value is automatically used across
82 // sessions by Drupal core if the "select different theme" permission has
83 // been granted.
84 if (isset($_SESSION['custom_theme']) && (!isset($custom_theme) || $custom_theme === '0')) {
85 $custom_theme = $_SESSION['custom_theme'];
86 }
87
88 if (module_exists('browscap') && variable_get('switchtheme_browser_enabled', FALSE)) {
89 $browser = browscap_get_browser();
90 if (isset($browser['parent'])) {
91 $parent = trim($browser['parent']);
92 $browser_theme = variable_get('switchtheme_browser_'. md5($parent), 'default');
93 if ($browser_theme != 'default') {
94 $custom_theme = $browser_theme;
95 }
96 }
97 }
98 }
99
100 /**
101 * Implementation of hook_theme().
102 */
103 function switchtheme_theme() {
104 return array(
105 'switchtheme_block_form' => array('form' => NULL),
106 );
107 }
108
109 /**
110 * Implementation of hook_block().
111 */
112 function switchtheme_block($op = 'list', $delta = 0, $edit = array()) {
113 if ($op == 'list') {
114 $blocks[0]['info'] = t('Switch theme form');
115 $blocks[1]['info'] = t('Random theme');
116 return $blocks;
117 }
118 elseif ($delta == 0 && $op == 'view' && user_access('switch theme')) {
119 $block['subject'] = t('Theme');
120 $block['content'] = drupal_get_form('switchtheme_switch_form');
121 return $block;
122 }
123 elseif ($delta == 1 && $op == 'view' && user_access('switch theme')) {
124 $block['subject'] = t('Random theme');
125 $block['content'] = switchtheme_display_random_block();
126 return $block;
127 }
128 }
129
130 /**
131 * Render a random theme with screenshot to switch to.
132 */
133 function switchtheme_display_random_block() {
134 $themes = list_themes();
135 shuffle($themes);
136 foreach ($themes as $key => $theme) {
137 $theme->screenshot = dirname($theme->filename) .'/screenshot.png';
138 if ($theme->status && file_exists($theme->screenshot)) {
139 // Return the first theme with a screenshot.
140 $output = l("<img src=\"". base_path() ."$theme->screenshot\" alt=\"preview of $theme->name\"/>", $_GET['q'], array('query' => 'theme='. $theme->name, 'html' => TRUE));
141 return $output;
142 }
143 }
144 }
145
146 function switchtheme_switch_form() {
147 global $user, $custom_theme;
148
149 $form = array();
150 $form['theme'] = array(
151 '#type' => 'select',
152 '#default_value' => !empty($custom_theme) ? $custom_theme : $user->theme,
153 '#attributes' => array('title' => t('Change the way this site looks.')),
154 '#options' => switchtheme_select()
155 );
156 $form['submit'] = array('#id'=>'switchtheme-submit', '#type' => 'submit', '#value' => t('Switch'));
157 return $form;
158 }
159
160 /**
161 * Theme the block search form.
162 */
163 function theme_switchtheme_block_form($form) {
164 return '<div class="container-inline">'. form_render($form) .'</div>';
165 }
166
167 /**
168 * Process a block switchtheme form submission.
169 *
170 * We do not validate the input here, because that is done in init_theme()
171 * already.
172 */
173 function switchtheme_switch_form_submit($form, &$form_state) {
174 global $user;
175
176 $themes = switchtheme_options();
177 // Bail out if we do not have a valid theme name.
178 if (empty($form_state['values']['theme']) || !isset($themes[$form_state['values']['theme']])) {
179 return;
180 }
181 // Save the setting for authenticated users, if the "select different theme"
182 // permission has been granted.
183 if ($user->uid > 0 && user_access('select different theme')) {
184 $user = user_save($user, array('theme' => $form_state['values']['theme']));
185 }
186 // Otherwise save the setting in the user's session.
187 elseif (user_access('switch theme')) {
188 $_SESSION['custom_theme'] = $form_state['values']['theme'];
189 }
190 }
191
192 /**
193 * Create an array of enabled themes to select from.
194 *
195 * @todo Probably should come back here and cache the theme list.
196 */
197 function switchtheme_options() {
198 $themes = list_themes();
199 foreach ($themes as $name => $attr) {
200 if ($attr->status) {
201 $options[$attr->name] = $attr->name;
202 }
203 }
204 return $options;
205 }
206
207 /**
208 * Create a select list of themes and labels.
209 */
210 function switchtheme_select() {
211 $select = array();
212 $options = switchtheme_options();
213
214 foreach ($options as $option) {
215 $select[$option] = variable_get('switchtheme_'. $option, drupal_ucfirst($option));
216 }
217 asort($select);
218 return $select;
219 }
220

  ViewVC Help
Powered by ViewVC 1.1.2