-
Notifications
You must be signed in to change notification settings - Fork 5
/
domain.admin.inc
1397 lines (1320 loc) · 49.5 KB
/
domain.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Administration functions for the domain module.
*
* These functions only need to be accessed from admin/structure/domain, so we
* put them in a separate include file.
* @ingroup domain
*/
/**
* Create an overview form for sorting domains and other quick actions.
*/
function domain_overview_form($form, &$form_state) {
$check = (bool) db_query("SELECT COUNT(domain_id) FROM {domain}")->fetchField();
if (empty($check)) {
domain_set_primary_domain();
}
// Check for the 7.x.3 update.
domain_check_for_update();
// Set the form.
$default = domain_default();
$active_domain = domain_get_domain();
$form = [];;
$form['top'] = [
'#markup' => t('<p>The following domains have been created for your site. The currently active domain <strong>is shown in boldface</strong>. You
may click on a domain to change the currently active domain. Your default domain is %name, which will be used for all requests that fail to resolve to a registered domain.</p>', ['%name' => $default['subdomain']]),
];
// Cannot use domain_domains() here because we need to sort the output.
$domains = [];
$header = [
['data' => t('Order')],
['data' => t('Weight')],
['data' => t('Name')],
['data' => t('Domain')],
['data' => t('Id')],
['data' => t('Active')],
['data' => t('Default')],
['data' => t('Operations')],
];
// Set up the base query.
$limit = config_get('domain.settings', 'domain_list_size');
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$query = db_select('domain', 'd')
->fields('d', [
'domain_id',
'sitename',
'subdomain',
'scheme',
'valid',
'weight',
'is_default',
])
->orderBy('weight')
->extend('PagerDefault')
->limit($limit);
// Get the domains.
$result = $query->execute();
while ($domain = $result->fetchAssoc()) {
$domains[$domain['domain_id']] = domain_api($domain);
}
if (empty($domains)) {
$form['error'] = [
'#markup' => t('No domains have been configured. <a href="!url">Add a new domain</a>.', ['!url' => url('admin/structure/domain/create')]),
];
return $form;
}
// Get the count of all domains, which may be paginated.
$delta = count(domain_domains());
$form['domain']['#tree'] = TRUE;
$form['header'] = [
'#type' => 'value',
'#value' => $header,
];
$options = [];
$i = -1;
// Now build the form elements.
foreach ($domains as $domain_id => $domain) {
$options[$domain_id] = '';
$form['domain'][$domain_id]['values'] = [
'#type' => 'value',
'#value' => $domain,
];
$form['domain'][$domain_id]['weight'] = [
'#type' => 'weight',
'#default_value' => ($page * $limit) + $i++,
'#delta' => $delta,
'#attributes' => ['class' => ['domain-weight']],
];
$form['domain'][$domain_id]['sitename'] = [
'#type' => 'markup',
'#markup' => ($active_domain['domain_id'] == $domain_id) ? '<strong>' . check_plain($domain['sitename']) . '</strong>' : check_plain($domain['sitename']),
];
$form['domain'][$domain_id]['subdomain'] = [
'#type' => 'markup',
'#markup' => ($active_domain['domain_id'] == $domain_id) ? '<strong>' . l($domain['subdomain'], domain_get_uri($domain)) . '</strong>' : l($domain['subdomain'], domain_get_uri($domain)),
];
$form['domain'][$domain_id]['domain_id'] = [
'#type' => 'markup',
'#markup' => check_plain($domain_id),
];
$form['domain'][$domain_id]['valid'] = [
'#type' => 'checkbox',
// '#title' => !empty($domain['valid']) ? t('Active') : t('Inactive'),
'#default_value' => !empty($domain['valid']),
];
$form['domain_actions'][$domain_id] = [
'#type' => 'markup',
'#markup' => l(t('edit domain'), 'admin/structure/domain/view/' . $domain_id . '/edit'),
];
}
$form['default_domain'] = [
'#type' => 'radios',
'#options' => $options,
'#default_value' => $default['domain_id'],
];
$form['pager'] = [
'#markup' => theme('pager'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
];
return $form;
}
/**
* Validate handler for the domain overview form.
*/
function domain_overview_form_validate($form, &$form_state) {
$values = $form_state['values']['domain'];
$default = $form_state['values']['default_domain'];
if (isset($values[$default])) {
$domain = $values[$default]['values'];
$error = domain_check_response($domain);
if ($error) {
$error = t('%domain may not be set as your primary domain.', ['%domain' => domain_get_path($domain)]) . '<br />' . $error;
form_set_error('default_domain', $error);
}
}
}
/**
* Submit handler for the domain overview form.
*/
function domain_overview_form_submit($form, &$form_state) {
$values = $form_state['values']['domain'];
foreach ($values as $domain_id => $value) {
$default = 0;
$valid = $value['valid'];
// Process the default domain.
if ($form_state['values']['default_domain'] == $domain_id) {
$default = 1;
$valid = 1;
// Indicate a message if the value changed.
if ($domain_id != $form_state['complete form']['default_domain']['#default_value']) {
backdrop_set_message(t('The primary domain has been set to %domain.', ['%domain' => domain_get_path($value['values'])]));
}
}
db_update('domain')
->fields([
'weight' => $value['weight'],
'is_default' => $default,
'valid' => $valid,
])
->condition('domain_id', $domain_id)
->execute();
}
backdrop_set_message(t('Domain settings updated'));
}
/**
* Module settings and behaviors.
*/
function domain_configure() {
if (empty($_POST)) {
// Is the module installed correctly?
module_invoke_all('domain_install');
}
// Return the configuration form.
return backdrop_get_form('domain_configure_form');
}
/**
* FormsAPI for configuring the domain module.
*/
function domain_configure_form($form, &$form_state, $user_submitted = FALSE) {
$form['domain_sitename_override'] = [
'#type' => 'checkbox',
'#title' => t('Override site name with name of domain.'),
'#default_value' => config_get('domain.settings', 'domain_sitename_override'),
'#description' => t('When enabled, the site name is overridden with the domain name set on the domain edit page.'),
];
$form['domain_debug'] = [
'#type' => 'radios',
'#title' => t('Debugging status'),
'#required' => TRUE,
'#default_value' => config_get('domain.settings', 'domain_debug'),
'#options' => [
0 => t('Do not show debugging output'),
1 => t('Show debugging output on node view'),
],
'#description' => t('If set, users with the <em>set domain access</em> permission will be able to view the node access rules for each node. See the README for more details.'),
];
$form['domain_force_admin'] = [
'#type' => 'radios',
'#title' => t('Enforce rules on administrators'),
'#required' => TRUE,
'#default_value' => config_get('domain.settings', 'domain_force_admin'),
'#options' => [
0 => t('Do not enforce'),
1 => t('Restrict node views for administrators'),
],
'#description' => t('If set, users with the <em>administer nodes</em> permission and user 1 <em>will view the site with Domain Access restrictions enforced</em>. See the README for more details.'),
];
$form['domain_list_size'] = [
'#type' => 'select',
'#title' => t('Domain list size'),
'#required' => TRUE,
'#default_value' => config_get('domain.settings', 'domain_list_size'),
'#options' => backdrop_map_assoc([
5,
10,
20,
25,
30,
40,
50,
75,
100,
150,
200,
250,
500,
750,
1000,
]),
'#description' => t('Sets a break point for the size of domain lists shown to users. After this point, user interfaces will use tables, pagination, and select lists to prevent too many domains from appearing in a list. <em>Note: setting this value higher than 200 may cause memory and display issues for your site.</em>'),
];
$form['domain_vertical_tab'] = [
'#type' => 'radios',
'#title' => t('Display in vertical tabs'),
'#required' => TRUE,
'#default_value' => config_get('domain.settings', 'domain_vertical_tab'),
'#options' => [t('No'), t('Yes')],
'#description' => t('Present the domain form options in a vertical tab.'),
];
$form['domain_collapse_options'] = [
'#type' => 'radios',
'#title' => t('Collapse domain form options'),
'#required' => TRUE,
'#default_value' => config_get('domain.settings', 'domain_collapse_options'),
'#options' => [t('No'), t('Yes')],
'#description' => t('When not displayed in a vertical tab, collapse the domain form options fieldset.'),
];
$form['domain_select_format'] = [
'#type' => 'select',
'#title' => t('Domain selection format'),
'#required' => TRUE,
'#default_value' => domain_select_format(),
'#options' => [t('Checkboxes'), t('Select list')],
'#description' => t('Determines the display format of form elements that display domain lists.'),
];
$form['domain_advanced'] = [
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['domain_advanced']['domain_www'] = [
'#type' => 'radios',
'#title' => t('WWW prefix handling'),
'#default_value' => config_get('domain.settings', 'domain_www'),
'#options' => [
0 => t('Process all host requests normally'),
1 => t('Treat www.*.example.com as an alias of *.example.com'),
],
'#description' => t('If set, calls to www.* will be treated as if the www. did not exist.
<em>Users will be taken from www.example.com to example.com, so your domains must be registered without the www. prefix.</em>'),
];
$form['domain_advanced']['domain_search'] = [
'#type' => 'radios',
'#title' => t('Search settings'),
'#default_value' => config_get('domain.settings', 'domain_search'),
'#options' => [
0 => t('Search content for the current domain only'),
1 => t('Search all domains from any URL'),
],
'#description' => t('Options for content searching.'),
];
$form['domain_advanced']['domain_seo'] = [
'#type' => 'radios',
'#title' => t('Search engine optimization'),
'#default_value' => config_get('domain.settings', 'domain_seo'),
'#options' => [
0 => t('Do not rewrite URLs'),
1 => t('Rewrite all URLs to point to a single source'),
],
'#description' => t('If rewrite is turned on, all node links will point to a single instance of the node. This
option reduces the chance that search engines will recognize duplicate content.'),
];
$form['domain_advanced']['domain_edit_on_primary'] = [
'#type' => 'checkbox',
'#title' => t('Force domain editing from the primary domain'),
'#default_value' => config_get('domain.settings', 'domain_edit_on_primary'),
'#description' => t('When editing a domain, you will be redirected to the primary domain. Doing so ensures that
the server can handle changes to the domain record. <br /><em>Disabling this feature may make development environments
easier to support. Use at your own risk.</em>'),
];
$options = ['-1' => t('Do not change domain')];
foreach (domain_domains() as $data) {
// The domain must be valid.
if ($data['valid']) {
$options[$data['domain_id']] = $data['sitename'];
}
}
$form['domain_advanced']['domain_default_source'] = [
'#type' => 'select',
'#title' => t('Default source domain'),
'#options' => $options,
'#default_value' => config_get('domain.settings', 'domain_default_source'),
'#description' => t('When rewriting urls, nodes assigned to all affiliates will be sent to this domain. <em>NOTE: This option only fires if you enable SEO rewrites or use the Domain Source module.</em>'),
];
$form['domain_all'] = [
'#type' => 'fieldset',
'#title' => t('Special page requests'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['domain_all']['domain_grant_all'] = [
'#type' => 'textarea',
'#rows' => 5,
'#cols' => 40,
'#default_value' => config_get('domain.settings', 'domain_grant_all'),
'#description' => t('Content on these pages should be viewable on any domain. Enter one path per line.
You may use the * as a wildcard. Use this for aggregate pages like those provided by <a href="!url">MySite</a> or if you
intend to show all user posts on a specific page. See the README for more details.', ['!url' => 'http://drupal.org/project/mysite']),
];
$form['domain_all']['domain_cron_rule'] = [
'#type' => 'checkbox',
'#default_value' => config_get('domain.settings', 'domain_cron_rule'),
'#title' => t('Treat cron.php as a special page request.'),
'#description' => t('Normally, you should leave this setting active. See the README for more information.'),
];
$form['domain_all']['domain_xmlrpc_rule'] = [
'#type' => 'checkbox',
'#default_value' => config_get('domain.settings', 'domain_xmlrpc_rule'),
'#title' => t('Treat xmlrpc.php as a special page request.'),
'#description' => t('Enable this setting if you have trouble with remote data calls over XMLRPC. See the README for more information.'),
];
$form['domain_paths'] = [
'#type' => 'fieldset',
'#title' => t('Node link patterns'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['domain_paths']['domain_paths'] = [
'#type' => 'textarea',
'#rows' => 5,
'#cols' => 40,
'#default_value' => config_get('domain.settings', 'domain_paths'),
'#description' => t('When using SEO or other path rewrites, the following link paths should be turned into absolute URLs. Enter
the Backdrop path of the link, using the <em>%n</em> placeholder to represent the node id.
Enter one path per line. See the README for more details.'),
];
$form['domain_classes'] = [
'#type' => 'fieldset',
'#title' => t('Domain-specific CSS classes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['domain_classes']['domain_classes'] = [
'#type' => 'textarea',
'#rows' => 5,
'#cols' => 40,
'#default_value' => config_get('domain.settings', 'domain_classes'),
];
if (module_exists('token')) {
$form['domain_classes']['domain_classes']['#description'] = t('Add the following classes to the <em>body</em> tag of every page. Enter one class per line. This field supports tokens.');
$form['domain_classes']['token_help'] = [
'#theme' => 'token_tree',
'#token_types' => ['current-domain'],
'#global_types' => FALSE,
];
}
else {
$tokens = domain_get_tokens(['current-domain']);
$form['domain_classes']['domain_classes']['#description'] = t('Add the following classes to the <em>body</em> tag of every page. Enter one class per line. The following token replacements are allowed: !tokens', ['!tokens' => theme('item_list', ['items' => $tokens])]);
}
// Allow submodules to add elements to the form.
$modules = module_implements('domain_form');
if (!empty($modules)) {
foreach ($modules as $module) {
$func = $module . '_domain_form';
$func($form);
}
}
$form['#submit'][] = 'domain_configure_form_submit';
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save configuration'),
];
return $form;
}
function domain_configure_form_submit($form, &$form_state, $user_submitted = FALSE) {
config_set('domain.settings', 'domain_sitename_override', $form_state['values']['domain_sitename_override']);
config_set('domain.settings', 'domain_debug', $form_state['values']['domain_debug']);
config_set('domain.settings', 'domain_force_admin', $form_state['values']['domain_force_admin']);
config_set('domain.settings', 'domain_list_size', $form_state['values']['domain_list_size']);
config_set('domain.settings', 'domain_vertical_tab', $form_state['values']['domain_vertical_tab']);
config_set('domain.settings', 'domain_collapse_options', $form_state['values']['domain_collapse_options']);
config_set('domain.settings', 'domain_select_format', $form_state['values']['domain_select_format']);
config_set('domain.settings', 'domain_www', $form_state['values']['domain_www']);
config_set('domain.settings', 'domain_search', $form_state['values']['domain_search']);
config_set('domain.settings', 'domain_seo', $form_state['values']['domain_seo']);
config_set('domain.settings', 'domain_edit_on_primary', $form_state['values']['domain_edit_on_primary']);
config_set('domain.settings', 'domain_default_source', $form_state['values']['domain_default_source']);
config_set('domain.settings', 'domain_grant_all', $form_state['values']['domain_grant_all']);
config_set('domain.settings', 'domain_cron_rule', $form_state['values']['domain_cron_rule']);
config_set('domain.settings', 'domain_xmlrpc_rule', $form_state['values']['domain_xmlrpc_rule']);
config_set('domain.settings', 'domain_paths', $form_state['values']['domain_paths']);
config_set('domain.settings', 'domain_classes', $form_state['values']['domain_classes']);
if (module_exists('domain_settings') && isset($form_state['values']['domain_settings_behavior']) ) {
config_set('domain_settings.settings', 'domain_settings_behavior', $form_state['values']['domain_settings_behavior']);
config_set('domain_settings.settings', 'domain_settings_form_visibility', $form_state['values']['domain_settings_form_visibility']);
config_set('domain_settings.settings', 'domain_settings_forms', $form_state['values']['domain_settings_forms']);
}
}
/**
* Return an array of values suitable for both create and update sql statements
*
* The last element on this array is ignored by the create sql because it has
* one fewer placeholder argument as it doesn't require a WHERE clause.
*
* @param $form_state
* From state from the submit_hook
*
* @return array
* Array of domain values for insert and update sql operations
*/
function domain_values_from_form_state($form_state) {
return [
'subdomain' => $form_state['values']['subdomain'],
'sitename' => $form_state['values']['sitename'],
'scheme' => $form_state['values']['scheme'],
'valid' => $form_state['values']['valid'],
'domain_id' => $form_state['values']['domain_id'],
'weight' => $form_state['values']['weight'],
'is_default' => $form_state['values']['is_default'],
'machine_name' => $form_state['values']['machine_name'],
];
}
/**
* FormsAPI for editing a domain record
*
* @param $form
* The current form, passed by FormsAPI.
* @param $form_state
* The current form state, passed by FormsAPI.
* @param $domain
* An array containing the record from the {domain} table.
* @param $arguments
* An array of additional hidden key/value pairs to pass to the form.
* Used by child modules to control behaviors.
* Currently supported arguments are:
* 'user_submitted' => TRUE
* Indicates that a form should not process administrative messages and paths
* upon form submit. Used by the Domain User module.
*/
function domain_form($form, &$form_state, $domain = [], $arguments = []) {
$_domain = domain_get_domain();
// This action should be performed from the primary domain unless overridden.
$error = FALSE;
if (!isset($arguments['ignore_domain_status_check'])) {
if (config_get('domain.settings', 'domain_edit_on_primary')) {
domain_check_primary();
}
if (!empty($domain)) {
$error = domain_check_response($domain);
if ($error && empty($_POST)) {
backdrop_set_message($error, 'warning', FALSE);
}
}
}
// Ensure indexes are set.
foreach ($_domain as $key => $value) {
if (!isset($domain[$key])) {
$domain[$key] = NULL;
}
}
$form = [];
backdrop_set_title(t('Edit !domain', ['!domain' => $domain['subdomain']]));
$form['#domain'] = $domain;
// The $arguments array allows other modules to pass values to change the behavior
// of submit and validate functions.
if (!empty($arguments)) {
$form['domain_arguments'] = ['#type' => 'value', '#value' => $arguments];
}
$form['domain_id'] = ['#type' => 'value', '#value' => $domain['domain_id']];
if (!config_get('domain.settings', 'domain_allow_non_ascii')) {
$description = t('Must contain only dots, lowercase alphanumeric characters, dashes and a colon (if using alternative ports).');
}
else {
$description = t('Must contain only dots, lowercase alphanumeric and ASCII characters, dashes and a colon (if using alternative ports).');
}
$form['subdomain'] = [
'#type' => 'textfield',
'#title' => t('Domain'),
'#size' => 40,
'#maxlength' => 80,
'#required' => TRUE,
'#default_value' => !is_null($domain['subdomain']) ? $domain['subdomain'] : '',
'#description' => t('The allowed domain, using the full <em>path.example.com</em> format.') . '<br />' . t('Leave off the http:// and the trailing slash and do not include any paths.') . '<br />' . $description,
];
$form['machine_name'] = [
'#type' => 'machine_name',
'#machine_name' => [
'source' => ['subdomain'],
'exists' => 'domain_check_machine_name',
],
'#default_value' => $domain['machine_name'],
];
$form['sitename'] = [
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 40,
'#maxlength' => 80,
'#required' => TRUE,
'#default_value' => !is_null($domain['sitename']) ? $domain['sitename'] : '',
'#description' => t('The human-readable name of this domain.'),
];
$form['scheme'] = [
'#type' => 'radios',
'#title' => t('Domain URL scheme'),
'#required' => TRUE,
'#options' => ['http' => 'http://', 'https' => 'https://'],
'#default_value' => !empty($domain['scheme']) ? $domain['scheme'] : 'http',
'#description' => t('The URL scheme for accessing this domain.'),
];
$form['valid'] = [
'#type' => 'radios',
'#title' => t('Domain status'),
'#required' => TRUE,
'#options' => [1 => t('Active'), 0 => t('Inactive')],
'#default_value' => isset($domain['valid']) ? $domain['valid'] : 1,
'#description' => t('Must be set to "Active" for users to navigate to this domain.'),
];
$next_weight = 0;
foreach (domain_domains() as $record) {
if ($record['weight'] > $next_weight) {
$next_weight = $record['weight'];
}
}
$form['weight'] = [
'#type' => 'weight',
'#title' => t('Weight'),
'#required' => TRUE,
'#delta' => count(domain_domains()) + 1,
'#default_value' => isset($domain['weight']) ? $domain['weight'] : $next_weight + 1,
'#description' => t('The sort order for this record. Lower values display first.'),
];
$form['is_default'] = [
'#type' => 'checkbox',
'#title' => t('Default domain'),
'#default_value' => isset($domain['is_default']) ? $domain['is_default'] : 0,
'#description' => t('If a URL request fails to match a domain record, the settings for this domain will be used.'),
];
// If the server did not respond properly, do not allow this record to be set
// as the default domain.
if ($error) {
$form['is_default']['#description'] .= '<br />' . t('<strong>This domain did not respond correctly. It cannot be set as your primary domain unless you select <em>Ignore server response warning</em></strong>.');
$form['ignore'] = [
'#type' => 'checkbox',
'#title' => t('Ignore server response warning'),
'#default_value' => 0,
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Save domain record'),
'#prefix' => '<br />',
];
$form['#validate'][] = 'domain_form_validate';
$form['#redirect'] = ['admin/structure/domain/view'];
return $form;
}
/**
* Implement domain_form validate hook.
*/
function domain_form_validate($form, &$form_state) {
$subdomain = $form_state['values']['subdomain'];
$error_list = [];
$domain_changed = (bool) strcmp($form_state['values']['subdomain'], $form['subdomain']['#default_value']);
if ($domain_changed && !domain_unique_domain($subdomain)) {
$error_list[] = t('The domain value must be unique.');
}
$error = domain_valid_domain($subdomain);
if (!empty($error)) {
$error_list[] = $error;
}
foreach ($error_list as $error) {
form_set_error('subdomain', $error);
}
if (!empty($form_state['values']['is_default']) && empty($form_state['values']['ignore'])) {
$error = domain_check_response($form_state['values']);
if ($error) {
$error = t('This domain may not be set as your primary domain.') . '<br />' . $error;
form_set_error('is_default', $error);
}
}
}
/**
* Implement domain_form submit hook.
*/
function domain_form_submit($form, &$form_state) {
$values = domain_values_from_form_state($form_state);
// Set the proper message.
if (!empty($values['domain_id'])) {
$message = t('Domain record updated.');
}
else {
$message = t('Domain record created.');
}
if (!empty($values['is_default'])) {
$message .= ' ' . t('Set as primary domain.');
}
// Run the save routine.
$domain = domain_save($values, $form_state);
// If return is not a $domain array, something went wrong.
if ($domain == -1) {
$message = t('Domain record failed.');
}
else {
// Update the domain_id in $form_state for any submit handlers that need it.
if (empty($form_state['values']['domain_id'])) {
$form_state['values']['domain_id'] = $domain['domain_id'];
}
}
// Hide the message for the Domain User module.
if (empty($form_state['values']['domain_arguments']['user_submitted'])) {
backdrop_set_message($message);
}
return $domain;
}
/**
* A delete confirmation form.
*
* @param $form_state
* The current form state, passed by FormsAPI.
* @param $domain
* An array containing the record from the {domain} table.
* @param $arguments
* An array of additional hidden key/value pairs to pass to the form.
* Used by child modules to control behaviors.
* Currently supported arguments are:
* 'user_submitted' => TRUE
* Indicates that a form should not process administrative messages and
* paths upon form submit. Used by the Domain User module.
*/
function domain_delete_form($form, &$form_state, $domain, $arguments = []) {
// This action should be performed from the primary domain.
domain_check_primary();
backdrop_set_title(t('Delete !domain', ['!domain' => $domain['sitename']]));
$form = [];
// The $arguments array allows other modules to pass values to change the behavior
// of submit and validate functions.
if (!empty($arguments)) {
$form['domain_arguments'] = ['#type' => 'value', '#value' => $arguments];
}
$form['domain'] = ['#type' => 'value', '#value' => $domain];
if (!empty($domain['is_default'])) {
backdrop_set_message(t('The primary domain may not be deleted. <a href="!url">Select a new primary domain</a> before deleting this record.', ['!url' => url('admin/structure/domain')]), 'warning');
return $form;
}
// Allow for data resets.
$domains = domain_domains();
$options = ['none' => t('Do not reassign')];
foreach ($domains as $record) {
if ($record['domain_id'] != $domain['domain_id']) {
$options[$record['domain_id']] = $record['sitename'];
}
}
$form['domain_access'] = [
'#title' => t('Reassign content'),
'#type' => 'select',
'#options' => $options,
'#default_value' => domain_default_id(),
'#description' => t('All content assigned to %domain will be re-assigned to your selection.', ['%domain' => $domain['sitename']]),
];
$form['domain_editor'] = [
'#title' => t('Reassign editors'),
'#type' => 'select',
'#options' => $options,
'#default_value' => domain_default_id(),
'#description' => t('All users assigned to %domain will be re-assigned to your selection.', ['%domain' => $domain['sitename']]),
];
return confirm_form(
$form,
t('Are you sure you wish to delete the domain record for <strong>%domain</strong>?', ['%domain' => $domain['subdomain']]),
'admin/structure/domain/view',
NULL,
t('Delete domain record'),
t('Cancel')
);
}
/**
* Implement domain_delete_form submit hook.
*/
function domain_delete_form_submit($form, &$form_state) {
// Delete the domain.
domain_delete($form_state['values']['domain'], $form_state['values']);
// Hide the message from the Domain User module.
if (empty($form_state['values']['domain_arguments']['user_submitted'])) {
backdrop_set_message(t('Domain record deleted.'));
$form_state['redirect'] = 'admin/structure/domain/view';
}
}
/**
* Allows for the batch update of certain elements.
*
* @param $action
* The name of the action to perform; corresponds to the keys of the $batch
* array returned by hook_domain_batch().
*
* @return
* The appropriate form, or a list of actions, or an error.
*/
function domain_batch($action = NULL) {
// We must have the module configured correctly.
$domains = domain_domains();
if (empty($domains)) {
return t('There are no domains configured for this site.');
}
$batch = domain_batch_actions();
// We must have some actions, otherwise, no point.
if (empty($batch)) {
return t('There are no batch actions configured.');
}
// If we are on the main page, just list the actions.
if (empty($action)) {
return domain_batch_list($batch);
}
// If we are doing a delete action, only valid domains can be acted upon.
$allowed = [];
if (!empty($batch[$action]['#table'])) {
$table = db_escape_table($batch[$action]['#table']);
$result = db_query("SELECT domain_id FROM {$table}");
foreach ($result as $test) {
$allowed[] = $domains[$test->domain_id];
}
if (empty($allowed)) {
return t('There are no valid domains on which to perform this action. The likely reason is that no records exist in the specified table.');
}
}
else {
$allowed = $domains;
}
// If we passed all the checks, generate the form.
return backdrop_get_form('domain_batch_form', $action, $batch[$action], $allowed);
}
/**
* Lists available batch updates for the domain module.
*
* @param $batch
* An array of batch actions, as defined by hook_domain_batch().
*
* @return
* A themed table of links and descriptions for batch actions.
*/
function domain_batch_list($batch) {
$build = [];
$header = [t('Action'), t('Description')];
$rows = [];
foreach ($batch as $key => $value) {
if (!isset($value['#module'])) {
$batch[$key]['#module'] = t('Other');
}
}
uasort($batch, 'domain_batch_sort');
$group = '';
foreach ($batch as $key => $value) {
$permission = isset($value['#permission']) ? $value['#permission'] : 'administer domains';
if (!user_access($permission)) {
continue;
}
if ($group != $value['#module']) {
$rows[] = [
[
'data' => '<strong>' . t('%module options', ['%module' => $value['#module']]) . '</strong>',
'colspan' => 2,
],
];
$group = $value['#module'];
}
$rows[] = [
l($value['#form']['#title'], 'admin/structure/domain/batch/' . $key),
$value['#meta_description'],
];
}
$output = '<p>' . t('Batch updates allow you to edit values for multiple domains at one time. These functions are helpful when moving your sites from staging to production, or any time you need to make mass changes quickly. The following batch update actions may be performed.') . '</p>';
$output .= '<p><em>' . t('Note that you will only be shown domains on which each action may be performed. If the module is not yet configured, some actions may be empty.') . '</em></p>';
// Return the build array.
$build['header'] = [
'#markup' => $output,
];
$build['content'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => ['id' => 'domain-list'],
];
return $build;
}
/**
* Sorting function for domain batch options.
*/
function domain_batch_sort($a, $b) {
if ($a['#module'] != $b['#module']) {
return strcmp($a['#module'], $b['#module']);
}
if ($a['#weight'] == $b['#weight']) {
return strcmp($a['#form']['#title'], $b['#form']['#title']);
}
else {
return ($a['#weight'] < $b['#weight']) ? -1 : 1;
}
}
/**
* Generate the form data for a batch update.
*
* @param $form_state
* The current form state, passed by FormsAPI.
* @param $action
* The name of the action to perform; corresponds to the keys of the $batch
* array returned by hook_domain_batch().
* @param $batch
* The batch data for this $action, as defined by hook_domain_batch().
* @param $domains
* The current settings for each domain.
*
* @return
* A themed table of links and descriptions for batch actions.
*/
function domain_batch_form($form, &$form_state, $action, $batch, $domains) {
$default = [];
backdrop_set_title($batch['#form']['#title']);
$form = [];
$form['message'] = [
'#markup' => theme('domain_batch_title', ['batch' => $batch]),
];
// For some values, allow every record to be updated.
if (!empty($batch['#update_all'])) {
$form['domain_batch_all'] = [
'#type' => 'fieldset',
'#title' => t('Update value for all domains'),
'#weight' => -10,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('By entering a value below and checking the <strong>Apply to all domains</strong> option, you can set the desired value for all domains.'),
];
$form['domain_batch_all']['batch_all_setting'] = $batch['#form'];
$form['domain_batch_all']['batch_all_setting']['#required'] = FALSE;
$form['domain_batch_all']['batch_all_setting']['#default_value'] = isset($batch['#system_default']) ? $batch['#system_default'] : NULL;
$form['domain_batch_all']['batch_override'] = [
'#type' => 'checkbox',
'#title' => t('Apply to all domains'),
];
$form['domain_batch_all']['submit'] = [
'#type' => 'submit',
'#value' => t('Update all domain settings'),
];
}
$form['domain_batch'] = [
'#tree' => TRUE,
'#title' => $batch['#form']['#title'],
'#description' => $batch['#meta_description'],
];
if ($batch['#domain_action'] == 'domain_conf' && $batch['#form']['#type'] == 'select') {
$batch['#form']['#options'] = ['domain-conf-ignore' => t('Use primary domain settings')] + $batch['#form']['#options'];
}
foreach ($domains as $domain) {
// Set the current value according to the stored values.
if (isset($domain[$action])) {
$default[$domain['domain_id']] = $domain[$action];
}
if ($batch['#domain_action'] == 'domain_conf') {
// Set the default value to the main settings.
// In rare cases, variable_get() returns unusable data, so we override it.
if (empty($batch['#override_default'])) {
$default[$domain['domain_id']] = empty(config_get('domain.settings', $action)) || is_null(config_get('domain.settings', $action)) ? $batch['#system_default'] : config_get('domain.settings', $action);
}
else {
$default[$domain['domain_id']] = $batch['#system_default'];
}
// Check for domain-specific settings.
$settings = domain_conf_data_get($domain['domain_id']);
if (isset($settings[$action])) {
$default[$domain['domain_id']] = $settings[$action];
}
}
elseif ($batch['#domain_action'] == 'custom' && isset($batch['#lookup'])) {
$default[$domain['domain_id']] = isset($batch['#system_default']) ? $batch['#system_default'] : NULL;
$func = $batch['#lookup'];
$setting = $func($domain);
if ($setting != -1) {
$default[$domain['domain_id']] = $setting;
}
}
// Take the settings from the $batch array.
$form['domain_batch'][$domain['domain_id']] = $batch['#form'];
// Add the domain-specific elements.
$form['domain_batch'][$domain['domain_id']]['#sitename'] = check_plain($domain['sitename']);
$form['domain_batch'][$domain['domain_id']]['#subdomain'] = check_plain($domain['subdomain']);
if (isset($default[$domain['domain_id']])) {
if (is_array($default[$domain['domain_id']])) {
foreach ($default[$domain['domain_id']] as $fieldname => $value) {
$form['domain_batch'][$domain['domain_id']][$fieldname]['#default_value'] = $value;
}
}
else {
$form['domain_batch'][$domain['domain_id']]['#default_value'] = $default[$domain['domain_id']];
}
}
}
$api_keys = ['variable', 'table', 'data_type'];
// These are optional elements, only passed if present.
foreach ($api_keys as $key) {
if (isset($batch['#' . $key])) {
$form[$key] = ['#type' => 'value', '#value' => $batch['#' . $key]];
}
}
// Custom submit and validate handlers.
foreach (['submit', 'validate'] as $key) {
if (isset($batch['#' . $key])) {
$form[$key . '_handler'] = [
'#type' => 'value',
'#value' => $batch['#' . $key],
];
}
}
$form['handler'] = ['#type' => 'value', '#value' => $batch['#domain_action']];
$form['batch_item'] = ['#type' => 'value', '#value' => $action];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Update domain settings'),
];
return $form;
}
/**
* FormsAPI theming.
*/
function theme_domain_batch_form($variables) {
$form = $variables['form'];