-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_theme_switcher.module
212 lines (171 loc) · 6.23 KB
/
menu_theme_switcher.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* Implementation of hook_menu()
*/
function menu_theme_switcher_menu() {
$items = array();
$items['admin/build/themes/switch'] = array(
'title' => 'Theme Switching',
'description' => 'Allow active theme to be switched based on menu conditions.',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_theme_switcher_settings_form', NULL),
'access arguments' => array('administer site configuration')
);
return $items;
}
/**
* Implementation of hook_init()
*/
function menu_theme_switcher_init(){
$item = menu_get_item();
if(module_exists('menutrails')){
$object = menu_get_object();
if($object->nid && $object->path == substr(url($_GET['q']), 1)){
$node_trails = variable_get('menutrails_node_types', array());
$term_trails = variable_get('menutrails_terms', array());
if(!empty($node_trails[$object->type])){
$item = menutrails_node_location($object);
}
if (!empty($object->taxonomy)) {
foreach ($object->taxonomy as $term) {
if (!empty($term_trails[$term->tid])) {
$item = menutrails_node_location($object);
}
}
}
}
}
menu_theme_switcher_check_active_conditions($item);
}
/**
* Implementation of preprocess_page().
*/
function menu_theme_switcher_preprocess_page(&$vars) {
global $custom_theme;
$vars['body_classes'] .= !empty($custom_theme) ? ' ' . $custom_theme : '';
}
/**
* Determine whether we need to switch themes
*/
function menu_theme_switcher_check_active_conditions($item){
//
// Check if our request is being done through ajax.
// If so, just return -- otherwise the 'theme information' option
// in views won't respect the proper templates
//
if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) {
return;
}
if ( !isset($item['href']) || !$item['href'] ) {
return false;
}
global $custom_theme;
$theme_switcher_settings = variable_get('menu_theme_switcher_settings', null);
$current_mlids = array();
$current_plids = array();
$get_menu_object = db_query('SELECT * FROM {menu_links} WHERE link_path = "%s"', $item['href']);
while($menu_object = db_fetch_object($get_menu_object)){
$current_mlids[] = $menu_object->mlid;
$current_plids = menu_theme_switcher_build_current_plids($menu_object);
}
if(!empty($theme_switcher_settings)){
foreach($theme_switcher_settings as $idx => $settings){
$menu_trigger = $settings['menu_trigger'];
$menu_triggers_parts = explode(':', $menu_trigger);
$menu_trigger_mlid = $menu_triggers_parts[1];
if(in_array($menu_trigger_mlid, $current_mlids) || ($settings['include_children'] && in_array($menu_trigger_mlid, $current_plids))){
$custom_theme = $settings['theme_reaction'];
}
}
}
if( !empty($custom_theme) ){
init_theme();
}
}
/**
* Iterate through the menu parent columns to find all of our relevant menu parents
* Used to determine which theme to display
*/
function menu_theme_switcher_build_current_plids($menu_object){
$plids = array();
$get_columns = db_query('SHOW columns FROM {menu_links} WHERE Field LIKE "p%"');
while($column = db_fetch_object($get_columns)){
$field_name = $column->Field;
if(preg_match('/^p[0-9]+$/', $field_name)){
if($menu_object->$field_name){
$plids[] = $menu_object->$field_name;
}
}
}
return $plids;
}
/**
* Theme Switcher settings form
* @ingroup forms
* @see system_themes_form_submit()
*/
function menu_theme_switcher_settings_form(){
$menu_options = array_merge(array('0' => 'None'), menu_parent_options(menu_get_menus(), 0));
$active_themes = array( '0' => 'None' );
foreach(list_themes() as $name => $theme){
if($theme->status){
$active_themes[$name] = $name;
}
}
$theme_switcher_settings = variable_get('menu_theme_switcher_settings', null);
$theme_settings_count = 1;
if( isset($theme_switcher_settings) && !empty($theme_switcher_settings) ){
$theme_settings_count += count($theme_switcher_settings);
}
for( $i = 0; $i < $theme_settings_count; $i++ ){
$settings = $theme_switcher_settings[$i];
$form['theme_switcher_settings_' . $i] = array(
'#type' => 'fieldset',
'#weight' => -($theme_settings_count - $i),
'#title' => t('Theme Switcher Settings Group !theme_settings_count', array( '!theme_settings_count' => $i + 1)),
'#collapsible' => FALSE
);
$form['theme_switcher_settings_' . $i]['menu_trigger_' . $i] = array(
'#type' => 'select',
'#options' => $menu_options,
'#default_value' => $settings['menu_trigger'] ? $settings['menu_trigger'] : 0,
'#title' => t('Menu Item Condition')
);
$form['theme_switcher_settings_' . $i]['include_children_' . $i] = array(
'#type' => 'checkbox',
'#default_value' => $settings['include_children'] ? $settings['include_children'] : 0,
'#title' => t('Include children of this menu item?')
);
$form['theme_switcher_settings_' . $i]['theme_reaction_' . $i] = array(
'#type' => 'select',
'#options' => $active_themes,
'#default_value' => $settings['theme_reaction'] ? $settings['theme_reaction'] : 0,
'#title' => t('Active Theme Reaction')
);
}
$form['theme_switcher_settings_count'] = array(
'#type' => 'hidden',
'#default_value' => $theme_settings_count
);
$form = system_settings_form($form);
$form['#submit'][] = 'menu_theme_switcher_settings_form_submit';
return $form;
}
/**
* Process theme switcher settings form submissions.
*/
function menu_theme_switcher_settings_form_submit($form, &$form_state) {
$theme_switcher_settings = array();
$idx = 0;
for( $i = 0; $i < $form_state['values']['theme_switcher_settings_count']; $i++ ){
if($form_state['values']['menu_trigger_' . $i]){
$theme_switcher_settings[$idx]['menu_trigger'] = $form_state['values']['menu_trigger_' . $i];
$theme_switcher_settings[$idx]['theme_reaction'] = $form_state['values']['theme_reaction_' . $i];
if($form_state['values']['include_children_' . $i]){
$theme_switcher_settings[$idx]['include_children'] = $form_state['values']['include_children_' . $i];
}
$idx++;
}
}
variable_set('menu_theme_switcher_settings', $theme_switcher_settings);
}