forked from mkalkbrenner/themekey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
themekey_admin.inc
788 lines (660 loc) · 31 KB
/
themekey_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
<?php
/**
* @file
* Contains all form manipulations to required by ThemeKey.
*
* @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') .'/themekey_build.inc');
/**
* Form builder for the rule chain.
*
* The form will not be validated. All changes will be saved immediately.
* Validation will happen when the form displays the stored configuration.
* Otherwise all the drag'n'drop stuff will not work.
*
* @see themekey_form_alter()
* @see themekey_rule_chain_form_submit()
* @see themekey_rule_chain_form_set_error()
*
* @ingroup forms
*/
function themekey_rule_chain_form() {
// scan for new properties provided by other modules
themekey_rebuild();
$properties = variable_get('themekey_properties', array());
if (empty($properties)) {
drupal_goto('admin/settings/themekey/settings');
}
$themes = themekey_theme_options(TRUE, TRUE);
$attributes = variable_get('themekey_attributes', array());
$operators = array(
'=' => '=',
'!' => '!',
'<' => '<',
'<=' => '<=',
'>' => '>',
'>=' => '>=',
'~' => '~',
);
$form = array('#tree' => TRUE);
$items = themekey_load_rules();
$parent_options = array_merge(array(0), array_keys($items));
$parent_options = array_combine($parent_options, $parent_options);
foreach ($items as $item) {
$form['old_items'][$item['id']]['depth'] = array(
'#type' => 'value',
'#value' => $item['depth'],
);
$form['old_items'][$item['id']]['id'] = array(
'#type' => 'hidden',
'#value' => $item['id'],
);
$property = $item['property'];
$wildcard = '';
$static = FALSE;
if (!in_array($property, $properties)) {
if (!empty($attributes[$property]['static'])) {
$static = TRUE;
$form['old_items'][$item['id']]['property'] = array(
'#type' => 'hidden',
'#default_value' => $property,
'#value' => $property,
'#prefix' => '<span class="themekey-fadeable">' . $property . '</span>',
);
$form['old_items'][$item['id']]['operator'] = array(
'#type' => 'hidden',
'#default_value' => '=',
'#value' => '=',
);
$form['old_items'][$item['id']]['value'] = array(
'#type' => 'hidden',
'#default_value' => 'static',
'#value' => 'static',
);
$form['old_items'][$item['id']]['theme'] = array(
'#type' => 'select',
'#default_value' => 'default',
'#options' => array('default' => t('Triggered')),
);
}
else {
$property = 'drupal:path:wildcard';
$wildcard = $item['property'];
}
}
if (!isset($form['old_items'][$item['id']]['property'])) {
$form['old_items'][$item['id']]['property'] = array(
'#type' => 'select',
'#default_value' => $property,
'#options' => $properties,
);
}
$form['old_items'][$item['id']]['wildcard'] = array(
'#type' => 'textfield',
'#default_value' => $wildcard,
'#size' => 10,
'#maxlength' => 255,
);
if (!isset($form['old_items'][$item['id']]['operator'])) {
$form['old_items'][$item['id']]['operator'] = array(
'#type' => 'select',
'#default_value' => $item['operator'],
'#options' => $operators,
);
}
if (!isset($form['old_items'][$item['id']]['value'])) {
$form['old_items'][$item['id']]['value'] = array(
'#type' => 'textfield',
'#default_value' => $item['value'],
'#size' => 20,
'#maxlength' => 255,
);
}
$form['old_items'][$item['id']]['parent'] = array(
'#type' => 'select',
'#default_value' => $item['parent'],
'#options' => $parent_options
);
if (!isset($form['old_items'][$item['id']]['theme'])) {
$form['old_items'][$item['id']]['theme'] = array(
'#type' => 'select',
'#default_value' => $item['theme'],
'#options' => $themes,
);
}
$form['old_items'][$item['id']]['enabled'] = array(
'#type' => 'checkbox',
'#default_value' => $item['enabled'],
);
$form['old_items'][$item['id']]['weight'] = array(
'#type' => 'weight',
'#delta' => 50,
'#default_value' => $item['weight'],
);
$form['old_items'][$item['id']]['delete'] = array(
'#value' => $static ? '' : l(t('delete'), 'admin/settings/themekey/properties/delete/' . $item['id']),
);
}
$form['new_item']['property'] = array(
'#type' => 'select',
'#default_value' => !empty($_GET['property']) ? check_plain($_GET['property']) : '',
'#options' => $properties,
);
$form['new_item']['wildcard'] = array(
'#type' => 'textfield',
'#default_value' => '',
'#size' => 10,
'#maxlength' => 255,
);
$form['new_item']['operator'] = array(
'#type' => 'select',
'#default_value' => '=',
'#options' => $operators,
);
$form['new_item']['value'] = array(
'#type' => 'textfield',
'#default_value' => !empty($_GET['value']) ? check_plain($_GET['value']) : '',
'#size' => 25,
'#maxlength' => 255
);
$form['new_item']['theme'] = array(
'#type' => 'select',
'#default_value' => 'default',
'#options' => $themes,
);
$form['new_item']['enabled'] = array(
'#type' => 'checkbox',
'#default_value' => TRUE,
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Implements hook_form_alter().
*
* Alters
* @see themekey_rule_chain_form()
* Validation will happen here when the form displays the stored configuration.
* Otherwise all the drag'n'drop stuff will not work.
*
* @see themekey_rule_chain_form()
* @see themekey_rule_chain_form_set_error()
*/
function themekey_form_alter(&$form, $form_state, $form_id) {
if ('themekey_rule_chain_form' == $form_id && empty($form_state['post'])) {
module_invoke_all('themekey_load_validators');
$attributes = variable_get('themekey_attributes', array());
if (!empty($form['old_items'])) {
foreach ($form['old_items'] as $key_1 => $value_1) {
if ($value_1['enabled']['#default_value']) {
if (!themekey_check_theme_enabled($value_1['theme']['#default_value'])) {
themekey_rule_chain_form_set_error($form, $key_1, 'theme', t('Theme is not activated'));
}
if (0 == drupal_strlen($value_1['value']['#default_value'])) {
themekey_rule_chain_form_set_error($form, $key_1, 'value', t('You must enter a value'));
}
elseif (!empty($attributes[$value_1['property']['#default_value']]['validator'])) {
//validate rule with custom validator
$validator = $attributes[$value_1['property']['#default_value']]['validator'];
if (function_exists($validator)) {
$rule = array(
'property' => $value_1['property']['#default_value'],
'wildcard' => $value_1['wildcard']['#default_value'],
'operator' => $value_1['operator']['#default_value'],
'value' => $value_1['value']['#default_value'],
);
$errors = $validator($rule);
foreach ($errors as $element => $msg) {
themekey_rule_chain_form_set_error($form, $key_1, $element, $msg);
}
}
else {
themekey_rule_chain_form_set_error($form, $key_1, 'property', t('ThemeKey requested an unknown validator called %validator to validate property %property', array('%validator' => $validator, '%property' => $value_1['property']['#default_value'])));
}
}
foreach ($form['old_items'] as $key_2 => $value_2) {
if ($key_2 == $key_1) {
continue;
}
if ($value_2['enabled']['#default_value'] &&
!empty($value_1['value']['#default_value']) &&
$value_2['property']['#default_value'] == $value_1['property']['#default_value'] &&
$value_2['operator']['#default_value'] == $value_1['operator']['#default_value'] &&
$value_2['value']['#default_value'] == $value_1['value']['#default_value'] &&
($value_2['parent']['#default_value'] == $value_1['parent']['#default_value'] ||
$value_2['parent']['#default_value'] == $value_1['id']['#value'])) {
if ('drupal:path:wildcard' != $value_2['property']['#default_value'] ||
('drupal:path:wildcard' == $value_2['property']['#default_value'] && $value_2['wildcard']['#default_value'] == $value_1['wildcard']['#default_value'])) {
// We have two identical rules with same 'indention' in a chain.
// This is allowed only if first one has childs and second one has none and one isn't the parent of the other
if (!$value_2['parent']['#default_value'] == $value_1['id']['#value']) {
$has_childs_1 = FALSE;
$has_childs_2 = FALSE;
foreach ($form['old_items'] as $key_3 => $value_3) {
if ($value_3['parent']['#default_value'] == $value_1['id']['#value']) {
$has_childs_1 = TRUE;
}
if ($value_3['parent']['#default_value'] == $value_2['id']['#value']) {
$has_childs_2 = TRUE;
}
if ($has_childs_1 && $has_childs_2) {
break;
}
}
if ((($value_1['weight']['#default_value'] < $value_2['weight']['#default_value']) && $has_childs_1 && !$has_childs_2) ||
(($value_1['weight']['#default_value'] > $value_2['weight']['#default_value']) && !$has_childs_1 && $has_childs_2)) {
// no error
continue;
}
elseif (($value_1['weight']['#default_value'] > $value_2['weight']['#default_value']) && $has_childs_1 && !$has_childs_2) {
themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule could never be reached'));
continue;
}
elseif (($value_1['weight']['#default_value'] < $value_2['weight']['#default_value']) && !$has_childs_1 && $has_childs_2) {
themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule hides a later one'));
continue;
}
elseif (($value_1['weight']['#default_value'] < $value_2['weight']['#default_value']) && $has_childs_1 && $has_childs_2) {
themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule should be combined with an identical one below'));
continue;
}
elseif (($value_1['weight']['#default_value'] > $value_2['weight']['#default_value']) && $has_childs_1 && $has_childs_2) {
themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule should be combined with an identical one above'));
continue;
}
}
themekey_rule_chain_form_set_error($form, $key_1, 'property', t('You entered two identical theme switching rules in the chain'));
themekey_rule_chain_form_set_error($form, $key_2, 'property', t('You entered two identical theme switching rules in the chain'));
}
}
}
}
}
}
// dump all errors
themekey_rule_chain_form_set_error($form);
}
}
/**
* Implements an error stack for hemekey_rule_chain_form()
*
* @see hemekey_rule_chain_form()
* @see themekey_form_alter()
*
* @param $form
* drupal form as reference
*
* @param $rule_id
* the id of a ThemeKey rule
*
* @param $element
* the id of an errounous form element as string
*
* @param $msg
* error message as string
*/
function themekey_rule_chain_form_set_error(&$form, $rule_id = 0, $element = '', $msg = '') {
static $error_stack = array();
if (!empty($rule_id)) {
$error_stack[$rule_id][] = array('old_items][' . $rule_id . '][' . $element, $msg);
}
else {
$error_counter = 0;
foreach ($error_stack as $rule_id => $errors) {
$error_counter++;
$form['old_items'][$rule_id]['id']['#prefix'] = '<span class="error">' . $error_counter . '</span>';
foreach ($errors as $error) {
form_set_error($error[0], $error_counter .') ' . $error[1]);
}
}
if (0 < $error_counter) {
drupal_set_message(t("Your current configuration of Theme Switching Rules contains at least one logical error. Nevertheless this configuration is stored and active."), 'warning');
}
}
}
/**
* Form submission handler for themekey_rule_chain_form().
*
* @see themekey_rule_chain_form()
*/
function themekey_rule_chain_form_submit($form, &$form_state) {
$max_weight = 0;
if (!empty($form_state['values']['old_items'])) {
foreach ($form_state['values']['old_items'] as $id => $item) {
if ($item['weight'] > $max_weight) {
$max_weight = $item['weight'];
}
$item['id'] = $id;
if ('drupal:path:wildcard' == $item['property']) {
$item['property'] = $item['wildcard'];
}
unset($item['wildcard']);
themekey_rule_set($item);
}
}
if (!empty($form_state['values']['new_item']['value']) || '0' === $form_state['values']['new_item']['value']) {
$item = $form_state['values']['new_item'];
$item['parent'] = 0;
$item['weight'] = $max_weight + 1;
if ('drupal:path:wildcard' == $item['property']) {
$item['property'] = $item['wildcard'];
}
unset($item['wildcard']);
themekey_rule_set($item);
}
drupal_set_message(t('The configuration options have been saved.'));
// fast deletion of page cache (truncate)
cache_clear_all('*', 'cache_page', TRUE);
}
/**
* Form builder for the ThemeKey settings form.
*
* @ingroup forms
*/
function themekey_settings_form() {
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('General Settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
$form['settings']['themekey_path_case_sensitive'] = array(
'#type' => 'checkbox',
'#title' => t('Property drupal:path is case sensitive'),
'#default_value' => variable_get('themekey_path_case_sensitive', 0),
'#description' => t('Drupal paths are case insesitive by default. Modules like Global Redirect might change this behavior.')
);
$form['settings']['themekey_allthemes'] = array(
'#type' => 'checkbox',
'#title' => t('Provide all themes for selection'),
'#default_value' => variable_get('themekey_allthemes', 0),
'#description' => t('Make all installed themes available for selection, not enabled ones only.')
);
$form['settings']['themekey_override_custom_theme'] = array(
'#type' => 'checkbox',
'#title' => t('Force custom theme overriding'),
'#default_value' => variable_get('themekey_override_custom_theme', 0),
'#description' => t('Select this option to force ThemeKey to set the custom theme. Otherwise ThemeKey will set the custom theme only if it\'s not already set by a different module.<br /><b>Note: If you activate this feature it turns off theme switching of some other modules like system (administration theme) or organic groups.</b>'),
);
$form['settings']['themekey_theme_maintain'] = array(
'#type' => 'checkbox',
'#title' => t('Retain the theme until a new theme is set'),
'#default_value' => variable_get('themekey_theme_maintain', 0),
'#description' => t('Select this option to have users logged in stay in the same theme until they browse to a new page with an explicit theme set.'),
);
$form['settings']['themekey_theme_maintain_anonymous'] = array(
'#type' => 'checkbox',
'#title' => t('Retain the theme until a new theme is set for anonymous users'),
'#default_value' => variable_get('themekey_theme_maintain_anonymous', 0),
'#description' => t('Select this option to have anonymous users stay in the same theme until they browse to a new page with an explicit theme set.
<br /><b>Warning: This feature is only working correctly if you turn off page caching or use a drupal core that supports lazy session initialization like !cdclink or !pflink.</b>',
array('!cdclink' => l(t('!path', array('!path' => 'Cocomore Drupal Core')), 'http://drupal.cocomore.com/node/175', array('attributes' => array('target' => '_blank'))), '!pflink' => l(t('!path', array('!path' => 'Pressflow')), 'https://launchpad.net/pressflow/6.x/', array('attributes' => array('target' => '_blank'))))),
);
if (module_exists('forum')) {
$form['settings']['themekey_module_forum_triggers_taxonomy_vid'] = array(
'#type' => 'checkbox',
'#title' => t('Forum pages trigger property taxonomy:vid'),
'#default_value' => variable_get('themekey_module_forum_triggers_taxonomy_vid', 0),
'#description' => t('Property taxonomy:vid is set when a single node is shown (p.e. /node/17). If this option is selected, forum pages like /forum/28 will set taxonomy:vid as well.')
);
}
$form['settings']['themekey_cron_page_cache'] = array(
'#type' => 'checkbox',
'#title' => t('Cron cleans up page cache'),
'#default_value' => variable_get('themekey_cron_page_cache', 1),
'#description' => t('Select this option if ThemeKey should check rules containing time based properties when cron runs. ThemeKey will carefully clean up the page cache if necessary to provide the right theme to anonymous users automatically, p.e. a Christmas theme.'),
);
return system_settings_form($form);
}
/**
* Themes hemekey_rule_chain_form() and adds drag'n'drop features.
*
* @ingroup themeable
*/
function theme_themekey_rule_chain_form($form) {
themekey_admin_theme_warning();
$output = '';
$rows = array();
if (!empty($form['old_items'])) {
$num_childs = array();
$parents_disabled = array();
$attributes = variable_get('themekey_attributes', array());
foreach ($form['old_items'] as $key => $item) {
if (isset($item['property'])) {
$num_childs[$key] = 0;
$parents_disabled[$key] = FALSE;
if (!empty($parents_disabled[$item['parent']['#value']])) {
$form['old_items'][$key]['enabled']['#value'] = 0;
}
if (!$form['old_items'][$key]['enabled']['#value']) {
$parents_disabled[$key] = TRUE;
}
elseif ($item['parent']['#value']) {
$num_childs[$item['parent']['#value']]++;
}
}
}
foreach ($form['old_items'] as $key => $item) {
if (isset($item['property'])) {
$row = (isset($form['old_items'][$key]['#attributes']) && is_array($form['old_items'][$key]['#attributes'])) ? $form['old_items'][$key]['#attributes'] : array();
// Add special classes to be used for tabledrag.js.
$form['old_items'][$key]['id']['#attributes']['class'] = 'themekey-property-id';
$form['old_items'][$key]['parent']['#attributes']['class'] = 'themekey-property-parent';
$form['old_items'][$key]['weight']['#attributes']['class'] = 'themekey-property-weight';
// Add special classes to be used for themekey-properties.js.
$form['old_items'][$key]['property']['#attributes']['class'] = 'themekey-property-property themekey-fadeable';
$form['old_items'][$key]['wildcard']['#attributes']['class'] = 'themekey-property-wildcard themekey-fadeable';
$form['old_items'][$key]['operator']['#attributes']['class'] = 'themekey-fadeable';
$form['old_items'][$key]['value']['#attributes']['class'] = 'themekey-fadeable';
$form['old_items'][$key]['enabled']['#attributes']['class'] = 'themekey-property-enabled';
$form['old_items'][$key]['theme']['#attributes']['class'] = 'themekey-property-theme themekey-fadeable';
// form items of type markup don't have attributes
$form['old_items'][$key]['delete']['#value'] = str_replace('<a', '<a class="themekey-rule-delete-link"', $form['old_items'][$key]['delete']['#value']);
if ('drupal:path:wildcard' != $item['property']['#value']) {
$form['old_items'][$key]['wildcard']['#attributes']['style'] = 'display: none';
}
if ($num_childs[$key]) {
$form['old_items'][$key]['theme']['#attributes']['style'] = 'display: none';
// form items of type markup don't have attributes
$form['old_items'][$key]['delete']['#value'] = str_replace('<a', '<a style="display: none"', $form['old_items'][$key]['delete']['#value']);
}
if (!empty($parents_disabled[$item['parent']['#value']])) {
$form['old_items'][$key]['enabled']['#attributes']['style'] = 'display: none';
$form['old_items'][$key]['enabled']['#default_value'] = 0;
}
$elements = array();
$description = htmlentities(strip_tags($attributes[$form['old_items'][$key]['property']['#default_value']]['description']));
$description = str_replace('\'', '', $description);
$elements[] = array('data' =>
theme('indentation', $form['old_items'][$key]['depth']['#value']) .
drupal_render($form['old_items'][$key]['id']) .
drupal_render($form['old_items'][$key]['property']) .
drupal_render($form['old_items'][$key]['wildcard']) .
drupal_render($form['old_items'][$key]['operator']) .
drupal_render($form['old_items'][$key]['value']) .
'<a name="anchor-' . $key .'" href="#anchor-' . $key .'" id="' . form_clean_id('edit-old-items-' . $key . '-value-help') . '" class="themekey-fadeable" title="' . $description . '" onClick="alert(\'' . $description . '\')">?</a>',
'class' => 'themekey-properties-row');
$elements[] = array('data' =>
drupal_render($form['old_items'][$key]['theme']));
$elements[] = array('data' =>
drupal_render($form['old_items'][$key]['enabled']));
$elements[] = array('data' =>
drupal_render($form['old_items'][$key]['delete']));
$elements[] = array('data' =>
drupal_render($form['old_items'][$key]['parent']));
$elements[] = array('data' =>
drupal_render($form['old_items'][$key]['weight']));
$page_cache = $attributes[$form['old_items'][$key]['property']['#default_value']]['page cache'];
if ($form['old_items'][$key]['enabled']['#value']) {
themekey_page_cache_warning($page_cache);
}
$elements[] = array('data' =>
'<div id="' . form_clean_id('edit-old-items-' . $key . '-page-cache-icon') . '">' .
theme('themekey_page_cache_icon', $page_cache) .
'</div>');
$row['class'] = !empty($row['class']) ? $row['class'] .' draggable' : 'draggable';
if (!$form['old_items'][$key]['enabled']['#value']) {
$row['class'] .= ' themekey-fade-out';
}
if (!$form['old_items'][$key]['parent']['#value']) {
$row['class'] .= ' themekey-top-level';
}
$row['id'] = 'themekey-properties-row-' . $key;
$row['data'] = $elements;
$rows[] = $row;
}
}
}
if (!empty($rows)) {
if (empty($form['pager']['#value'])) {
drupal_add_tabledrag('themekey-properties', 'match', 'parent', 'themekey-property-parent', 'themekey-property-parent', 'themekey-property-id', TRUE);
drupal_add_tabledrag('themekey-properties', 'order', 'sibling', 'themekey-property-weight');
}
$header = array(
array('data' => t('Theme Switching Rule Chain')),
array('data' => t('Theme')),
array('data' => t('Enabled')),
array('data' => t('Operation')),
array('data' => t('Parent')),
array('data' => t('Weight')),
array('data' => t('Page<br />Cache')),
);
$output .= theme('table', $header, $rows, array('id' => 'themekey-properties'));
foreach ($num_childs as $parent => $num) {
$output .= '<input id="themekey-num-childs-' . $parent . '" type="hidden" value="' . $num . '"></input>';
}
}
$rows = array();
if (!empty($form['new_item'])) {
if (isset($form['new_item']['property'])) {
$row = (isset($form['new_item']['#attributes']) && is_array($form['new_item']['#attributes'])) ? $form['new_item']['#attributes'] : array();
// Add special classes to be used for themekey-properties.js.
$form['new_item']['property']['#attributes']['class'] = 'themekey-property-property';
$form['new_item']['wildcard']['#attributes']['class'] = 'themekey-property-wildcard';
if ('drupal:path:wildcard' != $form['new_item']['property']['#value']) {
$form['new_item']['wildcard']['#attributes']['style'] = 'display: none';
}
$elements = array();
$elements[] = t('New Rule:');
$elements[] = array('data' =>
drupal_render($form['new_item']['property']) .
drupal_render($form['new_item']['wildcard']) .
drupal_render($form['new_item']['operator']) .
drupal_render($form['new_item']['value']),
'class' => 'themekey-properties-row');
$elements[] = array('data' => drupal_render($form['new_item']['theme']));
$elements[] = array('data' => drupal_render($form['new_item']['enabled']));
$row['data'] = $elements;
$rows[] = $row;
}
}
if (!empty($rows)) {
$header = array(
'',
array('data' => t('Theme Switching Rule')),
array('data' => t('Theme')),
array('data' => t('Enabled')),
);
$output .= '<a name="themekey_new_rule"></a>';
$output .= theme('table', $header, $rows, array('id' => 'themekey-new-item'));
}
$output .= drupal_render($form);
drupal_add_css(drupal_get_path('module', 'themekey') . '/themekey_properties.css');
drupal_add_js(drupal_get_path('module', 'themekey') . '/themekey_properties.js');
return $output;
}
/**
* Detects if there's an active administration theme and displays a warning
*/
function themekey_admin_theme_warning() {
if (variable_get('admin_theme', '0')) {
drupal_set_message(t('%admin_theme is configured as administration theme at !link. This setting is more powerful than a corresponding ThemeKey rule.',
array('%admin_theme' => variable_get('admin_theme', '0'), '!link' => l(t('!path', array('!path' => 'admin/settings/admin')), 'admin/settings/admin'))), 'warning');
if (variable_get('node_admin_theme', '0')) {
drupal_set_message(t('As configured at !link adding or editing a node will use the administration theme %admin_theme.',
array('%admin_theme' => variable_get('admin_theme', '0'), '!link' => l(t('!path', array('!path' => 'admin/settings/admin')), 'admin/settings/admin'))), 'warning');
}
}
}
/**
* Detects if page cache is active and incompatible properties are used
* in theme switching rule chain
*/
function themekey_page_cache_warning($page_cache_support) {
static $warnings = array();
if (variable_get('cache', CACHE_DISABLED)) {
// CACHE_NORMAL, CACHE_AGGRESSIVE, CACHE_EXTERNAL
switch ($page_cache_support) {
case THEMEKEY_PAGECACHE_UNSUPPORTED:
if (!isset($warnings[THEMEKEY_PAGECACHE_UNSUPPORTED])) {
drupal_set_message(t('!page_cache_icon Your site uses !page_caching, but the Theme Switching Rule Chain contains properties that do not support page caching. You have to ensure that any rule using those properties are wrapped by a different rule that excludes anonymous users like "user:uid > 0".',
array('!page_caching' => l(t('page caching'), 'admin/settings/performance'), '!page_cache_icon' => theme('themekey_page_cache_icon', $page_cache_support))), 'warning');
$warnings[THEMEKEY_PAGECACHE_UNSUPPORTED] = TRUE;
}
break;
case THEMEKEY_PAGECACHE_TIMEBASED:
if (!isset($warnings[THEMEKEY_PAGECACHE_TIMEBASED])) {
if (variable_get('themekey_cron_page_cache', 1)) {
drupal_set_message(t('!page_cache_icon Your site uses !page_caching and the Theme Switching Rule Chain contains properties that require your cron to run frequently enough. During the cron run ThemeKey deletes the page cache if necessary depending on your Theme Switching Rules.',
array('!page_caching' => l(t('page caching'), 'admin/settings/performance'), '!page_cache_icon' => theme('themekey_page_cache_icon', $page_cache_support))), 'warning');
}
else {
drupal_set_message(t('!page_cache_icon Your site uses !page_caching and the Theme Switching Rule Chain contains properties that require "!cron_page_cache" to be enabled which has been disabled.',
array('!page_caching' => l(t('page caching'), 'admin/settings/performance'), '!cron_page_cache' => l(t('Cron cleans up page cache'), 'admin/settings/themekey/settings'), '!page_cache_icon' => theme('themekey_page_cache_icon', $page_cache_support))), 'error');
}
$warnings[THEMEKEY_PAGECACHE_TIMEBASED] = TRUE;
}
break;
}
}
}
/**
* Menu callback -- ask for confirmation of ThemeKey rule deletion
*/
function themekey_admin_delete_rule_confirm(&$form_state, $arg, $themekey_property_id) {
$form['themekey_property_id'] = array(
'#type' => 'value',
'#value' => $themekey_property_id,
);
$title = themekey_format_rule_as_string($themekey_property_id);
return confirm_form($form,
t('Are you sure you want to delete ThemeKey rule %title?', array('%title' => $title)),
'admin/settings/themekey/properties',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Execute ThemeKey rule deletion
*/
function themekey_admin_delete_rule_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
themekey_rule_del($form_state['values']['themekey_property_id']);
}
$form_state['redirect'] = 'admin/settings/themekey/properties';
}
function theme_themekey_page_cache_icon($page_cache_support) {
static $module_path = '', $page_cache_support_desriptions = array();
if (empty($module_path)) {
$module_path = drupal_get_path('module', 'themekey');
$page_cache_support_desriptions = themekey_get_page_cache_support_desriptions();
}
return '<img src="/' . $module_path .'/img/page_cache_' . $page_cache_support . '.png" width="16" height="16" title="' . htmlentities(strip_tags($page_cache_support_desriptions[$page_cache_support]), ENT_COMPAT, 'UTF-8') . '"></img>';
}
function themekey_get_page_cache_support_desriptions() {
return array(
THEMEKEY_PAGECACHE_UNSUPPORTED => t('Unsupported! If you enable page caching on your system you have to ensure that any rule using this property is wrapped by a different rule that excludes anonymous users like "user:uid > 0".'),
THEMEKEY_PAGECACHE_SUPPORTED => t('Supported'),
THEMEKEY_PAGECACHE_TIMEBASED => t('Supported if you enable "Cron cleans up page cache" at !link and run your cron frequently enough. During the cron run ThemeKey deletes the page cache if necessary depending on your Theme Switching Rules.', array('!link' => l(t('!path', array('!path' => '/admin/settings/themekey/settings')), 'admin/settings/themekey/settings'))),
);
}