-
Notifications
You must be signed in to change notification settings - Fork 1
/
themekey_ui_admin.inc
286 lines (244 loc) · 8.93 KB
/
themekey_ui_admin.inc
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* @file
* Adds options to ThemeKey's adminstration back end and alters drupal forms
* to add theme select boxes.
*
* @see themekey_ui.module
*
* @author Markus Kalkbrenner | Cocomore AG
* @see http://drupal.org/user/124705
*
* @author profix898
* @see http://drupal.org/user/35192
*/
require_once(drupal_get_path('module', 'themekey') .'/themekey_base.inc');
require_once(drupal_get_path('module', 'themekey_ui') .'/themekey_ui_helper.inc');
/**
* ThemeKey UI settings form
*
* @see themekey_ui_settings_form_submit()
*
* @ingroup forms
*/
function themekey_ui_settings_form() {
$form['themekey_ui'] = array(
'#type' => 'fieldset',
'#title' => t('UI Settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
if (module_exists('path')) {
$form['themekey_ui']['themekey_ui_pathalias'] = array(
'#type' => 'checkbox',
'#title' => t('Show theme option in the \'URL aliases\' administration'),
'#default_value' => variable_get('themekey_ui_pathalias', 0),
'#description' => t('Assign themes to paths/path aliases from the \'URL aliases\' administration pages.')
);
}
//
$nodeform = variable_get('themekey_ui_nodeform', 0);
$form['themekey_ui']['themekey_ui_nodeform'] = array(
'#type' => 'checkbox',
'#title' => t('Show theme option in create/edit node forms'),
'#default_value' => $nodeform,
'#description' => t('Assign themes from create/edit node forms. This will show a \'Theme\' section on create/edit node pages.')
);
if ($nodeform) {
$form['themekey_ui']['content_type'] = array(
'#type' => 'fieldset',
'#title' => t('Show \'Theme\' option for nodes of type'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['themekey_ui']['content_type']['table'] = array(
'#theme' => 'themekey_ui_table',
'#header' => array(t('Content Type'), t('Enabled'))
);
foreach (node_get_types('names') as $type => $title) {
$form['themekey_ui']['content_type']['table'][$type]['title'] = array('#value' => $title);
$form['themekey_ui']['content_type']['table'][$type]['themekey_ui_nodeform|'. $type] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('themekey_ui_nodeform|'. $type, 1),
);
}
}
$form['themekey_ui']['themekey_ui_author'] = array(
'#type' => 'checkbox',
'#title' => t('Show theme option in user profile'),
'#default_value' => variable_get('themekey_ui_author', 0),
'#description' => t('Assign themes from user profile. All nodes created by a user will be shown to all visitors using the theme he selected in his profile.')
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Form submission handler for themekey_ui_settings_form().
*
* @see themekey_ui_settings_form()
*/
function themekey_ui_settings_form_submit($form, &$form_state) {
//
foreach ($form_state['values'] as $key => $value) {
$pos = strpos($key, 'themekey_ui_');
if ($pos !== FALSE && $pos == 0) {
variable_set($key, $value);
}
}
themekey_update_static_rule('themekey_ui:node_triggers_theme', $form_state['values']['themekey_ui_nodeform']);
themekey_update_static_rule('themekey_ui:node_author_triggers_theme', $form_state['values']['themekey_ui_author']);
drupal_set_message(t('The configuration options have been saved.'));
}
/**
* Adds theme selector to a form
*
* @see system_theme_select_form()
*
* @param $form
* form array
*
* @param $title
* title as string
*
* @param $description
* description as string
*
* @param $weight
* integer
*
* @param $collapsed
* boolean
*/
function themekey_ui_theme_select_form(&$form, $title, $description, $default = 'default', $weight = NULL, $collapsed = TRUE) {
$themes = list_themes();
$theme_options = themekey_theme_options();
$form['themekey_ui_themes'] = array(
'#type' => 'fieldset',
'#title' => $title,
'#description' => $description,
'#collapsible' => TRUE,
'#collapsed' => $collapsed,
'#theme' => 'themekey_ui_theme_select_form'
);
$form['themekey_ui_themes']['default']['screenshot'] = array();
$form['themekey_ui_themes']['default']['description'] = array('#type' => 'item', '#value' => t("don't switch the theme"));
$options = array('default' => '');
foreach ($themes as $info) {
if (!array_key_exists($info->name, $theme_options)) {
continue;
}
$options[$info->name] = '';
$screenshot = NULL;
$theme_key = $info->name;
while ($theme_key) {
if (file_exists($themes[$theme_key]->info['screenshot'])) {
$screenshot = $themes[$theme_key]->info['screenshot'];
break;
}
$theme_key = isset($themes[$theme_key]->info['base theme']) ? $themes[$theme_key]->info['base theme'] : NULL;
}
$screenshot = $screenshot ? theme('image', $screenshot, t('Screenshot for %theme theme', array('%theme' => $info->name)), '', array('class' => 'screenshot'), FALSE) : t('no screenshot');
$form['themekey_ui_themes'][$info->name]['screenshot'] = array('#value' => $screenshot);
$form['themekey_ui_themes'][$info->name]['description'] = array('#type' => 'item', '#title' => $info->info['name'], '#value' => dirname($info->filename));
}
$form['themekey_ui_themes']['themekey_ui_theme'] = array('#type' => 'radios', '#options' => $options, '#default_value' => $default);
if (!is_null($weight)) {
$form['themekey_ui_themes']['#weight'] = $weight;
}
}
/**
* Adds theme select box to url alias form
*
* @see path_admin_form()
* @see themekey_ui_pathalias_submit()
*
* @ingroup forms
*/
function themekey_ui_pathalias(&$form) {
if (!isset($form['#alias'])) {
return;
}
list($id, $theme) = themekey_ui_get_path_theme($form['#alias']['dst']);
themekey_ui_theme_select_form($form, t('Theme configuration'), t('Select a theme that will be used whenever content will be requested using this path alias.'), $theme, $form['submit']['weight'] - 1);
$form['themekey_ui_themes']['themekey_rule_id'] = array(
'#type' => 'value',
'#value' => $id,
);
array_unshift($form['#submit'], 'themekey_ui_pathalias_submit');
}
/**
* Form submission handler for themekey_ui_pathalias().
*
* @see themekey_ui_pathalias()
*/
function themekey_ui_pathalias_submit($form, &$form_state) {
if ((empty($form_state['values']['themekey_ui_theme']) || 'default' == $form_state['values']['themekey_ui_theme']) && $form_state['values']['themekey_rule_id']) {
themekey_ui_del_path_theme($form_state['values']['themekey_rule_id']);
}
elseif (!empty($form_state['values']['themekey_ui_theme']) && 'default' != $form_state['values']['themekey_ui_theme']) {
themekey_ui_set_path_theme($form_state['values']['dst'], $form_state['values']['themekey_ui_theme'], $form_state['values']['themekey_rule_id']);
}
if ($form['src']['#default_value'] == $form_state['values']['src'] &&
$form['dst']['#default_value'] == $form_state['values']['dst'] &&
$form['themekey_ui_themes']['themekey_ui_theme']['#default_value'] != $form_state['values']['themekey_ui_theme']) {
// only theme changed => clear page cache
// REVIEW this might be a performance issue on large sites
cache_clear_all('%' . $form_state['values']['dst'], 'cache_page', TRUE);
}
}
/**
* Formats a table with checkboxes used by ThemeKey UI settings form.
*
* @param $form
* array() containing form elements to be
* formatted as table
*
* @ingroup themeable
*/
function theme_themekey_ui_table($form) {
$header = isset($form['#header']) ? $form['#header'] : array();
$attributes = isset($form['#attributes']) ? $form['#attributes'] : array();
$rows = array();
foreach (element_children($form) as $key) {
$row = array();
foreach (element_children($form[$key]) as $item) {
$row[] = drupal_render($form[$key][$item]);
}
$rows[] = $row;
}
if (empty($rows)) {
$message = check_plain(isset($form['#empty']) ? $form['#empty'] : t('There are no items in the table.'));
$rows[] = array(array('data' => $message, 'colspan' => count($header), 'align' => 'center', 'class' => 'message'));
}
return count($rows) ? theme('table', $header, $rows, $attributes) : '';
}
/**
* Theme the theme select form.
*
* @see theme_system_theme_select_form()
*
* @param $form
* An associative array containing the structure of the form.
*
* @ingroup themeable
*/
function theme_themekey_ui_theme_select_form($form) {
$rows = array();
foreach (element_children($form) as $key) {
$row = array();
if (isset($form[$key]['description']) && is_array($form[$key]['description'])) {
$row[] = drupal_render($form[$key]['screenshot']);
$row[] = drupal_render($form[$key]['description']);
$row[] = drupal_render($form['themekey_ui_theme'][$key]);
}
$rows[] = $row;
}
if (!empty($rows)) {
$header = array(t('Screenshot'), t('Name'), t('Selected'));
$output = theme('table', $header, $rows);
return $output;
}
}