forked from mkalkbrenner/themekey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
themekey_ui.module
299 lines (258 loc) · 10 KB
/
themekey_ui.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* @file
* ThemeKey UI is an extension for ThemeKey
*
* ThemeKey UI adds a form element to node create and edit forms
* to assign a theme to a node.
*
* ThemeKey UI adds a theme option to the 'URL aliases' administration
* if module "Path" is enabled.
*
* ThemeKey UI adds a form element to user profile for theming all
* nodes created by this user.
*
* @see themekey.module
*
* @author Markus Kalkbrenner | Cocomore AG
* @see http://drupal.org/user/124705
*
* @author profix898
* @see http://drupal.org/user/35192
*/
/**
* Implements hook_themekey_properties()
*
* Provides additional properties for module ThemeKey:
* themekey_ui:node_triggers_theme
*
* @return
* array of themekey properties
*/
function themekey_ui_themekey_properties() {
// Attributes for properties
$attributes = array();
$attributes['themekey_ui:node_triggers_theme'] = array(
'description' => t("Property themekey_ui:node_triggers_theme could not be selected from the property drop down. You get this static property by activating !link. Afterwards you can move the property to any position in the rule chain. When done it triggers the switch to the theme assigned to the current node using ThemeKey UI.",
array('!link' => l(t('Show theme option in create/edit node forms'), 'admin/settings/themekey/settings/ui'))),
'page cache' => THEMEKEY_PAGECACHE_SUPPORTED,
'static' => TRUE,
);
$attributes['themekey_ui:node_author_triggers_theme'] = array(
'description' => t("Property themekey_ui:node_author_triggers_theme could not be selected from the property drop down. You get this static property by activating !link. Afterwards you can move the property to any position in the rule chain. When done it triggers the switch to the theme the author selected for his nodes in his user profile.",
array('!link' => l(t('Show theme option in user profile'), 'admin/settings/themekey/settings/ui'))),
'page cache' => THEMEKEY_PAGECACHE_SUPPORTED,
'static' => TRUE,
);
// Mapping functions
$maps = array();
$maps[] = array('src' => 'node:nid',
'dst' => 'themekey_ui:node_triggers_theme',
'callback' => 'themekey_ui_nid2theme');
$maps[] = array('src' => 'node:nid',
'dst' => 'themekey_ui:node_author_triggers_theme',
'callback' => 'themekey_ui_author2theme');
return array('attributes' => $attributes, 'maps' => $maps);
}
/**
* This function implements the interface of a ThemeKey
* mapping function but doesn't set a ThemeKey property's
* value. It sets the global variable $custom_theme to a
* theme directly which will cause ThemeKey to use this
* theme.
*
* @param $nid
* a node id, current value of ThemeKey property node:nid
*
* @return
* string "static" if global custom theme has been set
* or NULL if no theme has been assigned to the node
*/
function themekey_ui_nid2theme($nid) {
global $custom_theme;
// node_load() must not be called from hook_init().
// Therefor we have to execute SQL here using hook_nodeapi().
$node = new stdClass();
$node->nid = $nid;
$node->vid = themekey_node_get_simple_node_property($nid, 'vid');
$node->type = themekey_node_get_simple_node_property($nid, 'type');
themekey_ui_nodeapi($node, 'load');
if (!empty($node->themekey_ui_theme) && themekey_check_theme_enabled($node->themekey_ui_theme)) {
$custom_theme = $node->themekey_ui_theme;
return 'static';
}
return NULL;
}
/**
* This function implements the interface of a ThemeKey
* mapping function but doesn't set a ThemeKey property's
* value. It sets the global variable $custom_theme to a
* theme directly which will cause ThemeKey to use this
* theme.
*
* @param $nid
* a node id, current value of ThemeKey property node:nid
*
* @return
* string "static" if global custom theme has been set
* or NULL if no theme has been assigned to the node
*/
function themekey_ui_author2theme($nid) {
global $custom_theme;
// node_load() must not be called from hook_init().
// Therefor we have to execute SQL here.
if ($theme = db_result(db_query("SELECT theme FROM {node} JOIN {themekey_ui_author_theme} USING (uid) WHERE nid = %d", $nid))) {
if ('default' != $theme) {
$custom_theme = $theme;
return 'static';
}
}
return NULL;
}
/**
* Implements hook_theme().
*/
function themekey_ui_theme() {
return array(
'themekey_ui_table' => array(
'arguments' => array('form' => NULL),
),
'themekey_ui_theme_select_form' => array(
'arguments' => array('form' => NULL),
),
);
}
/**
* Implements hook_perm().
*/
function themekey_ui_perm() {
return array('assign node themes', 'assign path alias themes', 'assign theme to own nodes');
}
/**
* Implements hook_menu().
*/
function themekey_ui_menu() {
$items = array();
$items['admin/settings/themekey/settings/ui'] = array(
'title' => 'User Interface',
'access callback' => 'user_access',
'access arguments' => array('administer themekey settings'),
'file' => 'themekey_ui_admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('themekey_ui_settings_form'),
'type' => MENU_LOCAL_TASK,
'weight' => 1
);
return $items;
}
/**
* Implements hook_form_alter().
*/
function themekey_ui_form_alter(&$form, $form_state, $form_id) {
if ('path_admin_form' == $form_id) {
// path aliases form
if (user_access('assign path alias themes') && variable_get('themekey_ui_pathalias', 0)) {
module_load_include('inc', 'themekey_ui', 'themekey_ui_admin');
themekey_ui_pathalias($form);
}
}
elseif ('user_profile_form' == $form_id) {
if (user_access('assign theme to own nodes') && variable_get('themekey_ui_author', 0)) {
module_load_include('inc', 'themekey_ui', 'themekey_ui_admin');
// to avoid a sql query to load his nodes themes every time a user is loaded we do this query here
$theme = FALSE;
if (!empty($form['#uid'])) {
$theme = db_result(db_query("SELECT theme FROM {themekey_ui_author_theme} WHERE uid = %d", $form['#uid']));
}
themekey_ui_theme_select_form($form, t('Theme configuration for my nodes'), t('Every node I create will be shown to other users using this theme.'), $theme ? $theme : 'default', $form['theme_select']['#weight'], FALSE);
}
}
elseif ('themekey_help_tutorials_form' == $form_id) {
module_load_include('inc', 'themekey_ui', 'themekey_ui_help');
themekey_ui_help_tutorials($form);
}
else {
// node form?
if (variable_get('themekey_ui_nodeform', 0) && user_access('assign node themes')) {
$type = isset($form['type']['#value']) ? $form['type']['#value'] : FALSE;
if ($form_id == $type .'_node_form' && variable_get('themekey_ui_nodeform|'. $type, 1)) {
module_load_include('inc', 'themekey_ui', 'themekey_ui_admin');
$description = t('Theme configuration for this node');
if (module_exists('og') && !og_is_omitted_type($type)) {
$description .= '<p><b>' . t('Note:') . '</b> ' . t('This content type is used in organic groups. A Theme you select here will only be used to display this node if you set the theme for the organic group to %theme', array('%theme' => variable_get('theme_default', 'garland') . ' ' . t('(site default theme)'))) . '</p>';
}
themekey_ui_theme_select_form($form, t('Theme configuration for this node'), $description, !empty($form['#node']->themekey_ui_theme) ? $form['#node']->themekey_ui_theme : 'default');
}
}
}
}
/**
* Implements hook_nodeapi().
*/
function themekey_ui_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'load':
if (variable_get('themekey_ui_nodeform', 0) && variable_get('themekey_ui_nodeform|'. $node->type, 1)) {
if (!empty($node->vid)) {
$theme = db_result(db_query('SELECT theme FROM {themekey_ui_node_theme} WHERE nid = %d AND vid = %d', $node->nid, $node->vid));
if ($theme) {
$node->themekey_ui_theme = $theme;
}
else {
$node->themekey_ui_theme = 'default';
}
}
}
break;
case 'insert':
if (!empty($node->themekey_ui_theme)) {
$item = array('nid' => $node->nid, 'vid' => $node->vid, 'theme' => $node->themekey_ui_theme);
drupal_write_record('themekey_ui_node_theme', $item);
}
break;
case 'update':
if (!empty($node->themekey_ui_theme)) {
$item = array('nid' => $node->nid, 'vid' => $node->vid, 'theme' => $node->themekey_ui_theme);
$key = array();
if (!$node->revision && db_result(db_query("SELECT 1 FROM {themekey_ui_node_theme} WHERE nid = %d AND vid = %d", $node->nid, $node->vid))) {
$key = array('nid', 'vid');
}
drupal_write_record('themekey_ui_node_theme', $item, $key);
}
break;
case 'delete':
db_query('DELETE FROM {themekey_ui_node_theme} WHERE nid = %d', $node->nid);
break;
}
}
/**
* Implements hook_user()
*/
function themekey_ui_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
case 'update':
if (user_access('assign theme to own nodes') && variable_get('themekey_ui_author', 0) && !empty($edit['themekey_ui_theme'])) {
$author_theme = array(
'uid' => $account->uid,
'theme' => $edit['themekey_ui_theme'],
);
if (db_result(db_query("SELECT 1 FROM {themekey_ui_author_theme} WHERE uid = %d", $account->uid))) {
drupal_write_record('themekey_ui_author_theme', $author_theme, array('uid'));
}
else {
drupal_write_record('themekey_ui_author_theme', $author_theme);
}
if ($edit['themekey_ui_theme'] != $account->themekey_ui_theme) {
// theme settings changed
// fast deletion of page cache (truncate)
cache_clear_all('*', 'cache_page', TRUE);
}
$edit['themekey_ui_theme'] = NULL;
}
break;
case 'delete':
db_query("DELETE FROM {themekey_ui_author_theme} WHERE uid = %d", $account->uid);
break;
}
}