-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdomain.module
4498 lines (4234 loc) · 134 KB
/
domain.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
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
/**
* @defgroup domain Domain Access: A domain-based access control system
*
* The core Domain Access module.
*/
/**
* @file
* Core module functions for the Domain Access suite.
* @ingroup domain
*/
/**
* Defines how to handle access permissions when installing the module.
* You may alter this variable before installing the module. See README.txt.
*/
define('DOMAIN_INSTALL_RULE', TRUE);
/**
* Defines whether to show affiliated content on all domains.
* You may alter this variable before installing the module. See README.txt.
*/
define('DOMAIN_SITE_GRANT', TRUE);
/**
* Defines whether to assign users to the default domain on install.
* You may alter this variable before installing the module. See README.txt.
*/
define('DOMAIN_ASSIGN_USERS', TRUE);
/**
* Sets a default value at which to start paginating Domain lists.
*/
define('DOMAIN_LIST_SIZE', 25);
/**
* Defines the export statuses for a domain.
*/
define('DOMAIN_EXPORT_STATUS_DATABASE', 0);
define('DOMAIN_EXPORT_STATUS_CODE', 1);
/**
* Defines constants for content settings.
*/
define('DOMAIN_ALL', 'DOMAIN_ALL');
define('DOMAIN_ACTIVE', 'DOMAIN_ACTIVE');
/**
* Ensures that our custom_url_rewrite_outbound() is loaded.
*
* @link http://drupal.org/node/529026
* @link http://drupal.org/node/820062
*/
function domain_boot() {
include_once('settings_custom_url.inc');
}
/**
* Notify other modules of our API version.
*/
function domain_api_version() {
return 3;
}
/**
* Implements hook_init().
*
* Initializes a global $_domain variable if necessary (usually that's done in
* domain_bootstrap.inc) and loads information on current domain.
*
* Also handles www stripping, checks the validity of user domains and updates
* $conf['site_name'].
*/
function domain_init() {
global $_domain;
if (!is_array($_domain)) {
$_domain = array();
}
// Error handling in case the module is not installed correctly.
if (!isset($_domain['domain_id'])) {
$_domain = domain_default(TRUE);
$_domain['error'] = 'bootstrap include';
}
// If $_domain['error'] is present, then set a message and stop.
if (!isset($error) && isset($_domain['error'])) {
$error = 'Domain access failed to load during phase: ' . $_domain['error'] . '. Please check your settings.php file and site configuration.';
// Do not show on form submissions, when enabling the module.
if (empty($_POST)) {
// Show a warning to admin users, if enabled.
// You may disable this warning by adding:
// $conf['domain_hide_errors'] = TRUE;
// to the bottom of settings.php.
$hide = config_get('domain.settings', 'domain_hide_errors');
if (user_access('administer domains') && empty($hide)) {
backdrop_set_message($error, 'error');
}
if (empty($hide)) {
watchdog('domain', $error, NULL, WATCHDOG_ERROR);
}
}
}
// End of the error handling routine.
// If coming from a node save, make sure we are on an accessible domain.
domain_node_save_redirect();
// Strip the www. off the domain, if required by the module settings.
$www_replaced = FALSE;
if (config_get('domain.settings', 'domain_www') && strpos($_domain['subdomain'], 'www.') !== FALSE) {
$_domain['subdomain'] = str_replace('www.', '', $_domain['subdomain']);
$www_replaced = TRUE;
}
// Add information from domain_lookup but keep existing values (domain_id and subdomain)
$domain = domain_lookup($_domain['domain_id'], NULL, TRUE);
if ($domain != -1) {
$_domain = array_merge($domain, $_domain);
}
// Set the initial domain record, for later reference. See http://drupal.org/node/706490.
domain_initial_domain($_domain);
// If we have replaced 'www.' in the url, redirect to the clean domain.
if ($www_replaced) {
backdrop_goto(domain_get_uri($_domain));
}
// For Domain User, we check the validity of accounts, so the 'valid' flag must be TRUE.
if (empty($_domain['valid'])) {
domain_invalid_domain_requested();
}
// Set the site name to the domain-specific name.
if (config_get('domain.settings', 'domain_sitename_override')) {
config_set('system.core', 'site_name', $_domain['sitename']);
}
}
/**
* Store the initially loaded domain, for later use.
*
* @param $domain
* The domain global value. This should only be called once.
*
* @return $initial
* A copy of the initial $_domain global value.
*
* @see domain_init()
*/
function domain_initial_domain($domain = []) {
$initial = &backdrop_static(__FUNCTION__);
if (!isset($initial) && !empty($domain)) {
$initial = $domain;
}
return $initial;
}
/**
* Unserialize an object stored in {domain_*} tables.
*
* PostgreSQL has issues with bytea fields, and while this is
* handled cleanly in cache_get(), we have our own functions
* for retrieving similar data objects. So we must be sure to
* unserialize these safely.
*
* This may have been fixed in Drupal 7.
*
* @param $object
* The serialized object.
*
* @return $data
* Properly unserialized data or an empty string if the $object
* contained no data.
*
* @see http://drupal.org/node/686146
*/
function domain_unserialize($object) {
if (empty($object)) {
return;
}
return unserialize($object);
}
/**
* Implements hook_menu().
*/
function domain_menu() {
$items = array();
$admin = user_access('administer domains');
$items['admin/structure/domain'] = [
'title' => 'Domains',
'access arguments' => ['administer domains'],
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_overview_form'],
'file' => 'domain.admin.inc',
'description' => 'Manage and configure domains.',
];
$items['admin/structure/domain/view'] = [
'title' => 'Domain list',
'access arguments' => ['administer domains'],
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_overview_form'],
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'domain.admin.inc',
'description' => 'View domains for the site.',
'weight' => -50,
];
$items['admin/structure/domain/settings'] = [
'title' => 'Settings',
'access arguments' => ['administer domains'],
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_configure',
'file' => 'domain.admin.inc',
'description' => 'Configure Domain Access settings.',
'weight' => -20,
];
$items['admin/structure/domain/create'] = [
'title' => 'Create domain',
'access arguments' => ['administer domains'],
'type' => MENU_LOCAL_ACTION,
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_form'],
'file' => 'domain.admin.inc',
'description' => 'Create new domain record.',
];
// Register the batch actions as menu callbacks
$batch = module_invoke_all('domain_batch');
if (!empty($batch)) {
$items['admin/structure/domain/batch'] = [
'title' => 'Batch updating',
'access arguments' => ['administer domains'],
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_batch',
'file' => 'domain.admin.inc',
'description' => 'Batch update domain settings.',
'weight' => -15,
];
// Get the submenu items
foreach ($batch as $key => $value) {
$items['admin/structure/domain/batch/' . $key] = [
'title' => $value['#form']['#title'],
'access arguments' => isset($value['#permission']) ? [$value['#permission']] : ['administer domains'],
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'page callback' => 'domain_batch',
'page arguments' => [$key],
'file' => 'domain.admin.inc',
'description' => isset($value['#description']) ? $value['#description'] : '',
'weight' => isset($value['#weight']) ? $value['#weight'] : 0,
];
}
}
$items['admin/structure/domain/nodes'] = [
'title' => 'Content defaults',
'access arguments' => ['administer domains'],
'type' => MENU_LOCAL_TASK,
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_nodes_form'],
'file' => 'domain.admin.inc',
'description' => 'Default domain settings for content.',
'weight' => -10,
];
$items['admin/structure/domain/roles'] = [
'title' => 'User defaults',
'access arguments' => ['administer domains'],
'type' => MENU_LOCAL_TASK,
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_roles_form'],
'file' => 'domain.admin.inc',
'description' => 'Default domain settings for users.',
'weight' => -5,
];
$items['admin/structure/domain/view/%domain'] = [
'title' => 'View',
'title callback' => 'domain_title',
'title arguments' => [4],
'access arguments' => ['administer domains'],
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_form', 4],
'description' => 'Edit domain record.',
'file' => 'domain.admin.inc',
'weight' => -10,
];
$items['admin/structure/domain/view/%domain/edit'] = [
'title' => 'Edit',
'access arguments' => ['administer domains'],
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
];
$items['admin/structure/domain/view/%domain/delete'] = [
'title' => 'Delete',
'access arguments' => ['administer domains'],
'type' => MENU_LOCAL_TASK,
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_delete_form', 4],
'description' => 'Delete domain record.',
'file' => 'domain.admin.inc',
'weight' => 50,
];
$items['admin/structure/domain/repair'] = [
'title' => 'Domain update database',
'access arguments' => ['administer domains'],
'page callback' => 'backdrop_get_form',
'page arguments' => ['domain_repair_form'],
'type' => MENU_CALLBACK,
'file' => 'domain.admin.inc',
];
return $items;
}
/**
* Set the title of a menu callback for domain edits.
*
* @param $domain
* The domain array.
*
* @return
* A title string.
*/
function domain_title($domain) {
return $domain['subdomain'];
}
/**
* Implements hook_permission().
*/
function domain_permission() {
$permissions = [
'administer domains' => [
'title' => t('Administer domain records and settings'),
'restrict access' => TRUE,
],
'access inactive domains' => [
'title' => t('Access inactive domains'),
'restrict access' => TRUE,
],
'assign domain editors' => [
'title' => t('Assign editors to domains'),
],
'set domain access' => [
'title' => t('Set domain access status for all content'),
],
'publish to any assigned domain' => [
'title' => t('Publish content to any assigned domain'),
],
'publish from assigned domain' => [
'title' => t('Publish content only from assigned domain'),
],
'publish from default domain' => [
'title' => t('Publish content only from the default domain'),
],
'edit domain content' => [
'title' => t('Edit any content on assigned domains'),
],
'delete domain content' => [
'title' => t('Delete any content on assigned domains'),
],
'view unpublished domain content' => [
'title' => t('View unpublished content on assigned domains'),
],
];
// Generate standard node permissions for all applicable node types.
foreach (node_permissions_get_configured_types() as $type) {
$permissions += domain_editor_list_permissions($type);
}
return $permissions;
}
/**
* Helper function to generate standard node permission list for a given type.
*
* Shamelessly lifted from node_list_permissions().
*
* @param $type
* The machine-readable name of the node type.
* @return array
* An array of permission names and descriptions.
*/
function domain_editor_list_permissions($type) {
$info = node_type_get_type($type);
$type = check_plain($info->type);
// Build standard list of node permissions for this type.
$perms = [
"create $type content on assigned domains" => [
'title' => t('%type_name: Create new content on assigned domains', ['%type_name' => $info->name]),
],
"update $type content on assigned domains" => [
'title' => t('%type_name: Edit any content on assigned domains', ['%type_name' => $info->name]),
],
"delete $type content on assigned domains" => [
'title' => t('%type_name: Delete any content on assigned domains', ['%type_name' => $info->name]),
],
];
return $perms;
}
/**
* Implements hook_theme().
*/
function domain_theme($existing, $type, $theme, $path) {
$themes = [
'domain_batch_form' => [
'render element' => 'form',
'file' => 'domain.admin.inc',
],
'domain_batch_title' => [
'variables' => ['batch' => []],
'file' => 'domain.admin.inc',
],
'domain_nodes_form' => [
'render element' => 'form',
'file' => 'domain.admin.inc',
],
'domain_roles_form' => [
'render element' => 'form',
'file' => 'domain.admin.inc',
],
'domain_overview_form' => [
'render element' => 'form',
'file' => 'domain.admin.inc',
],
];
return $themes;
}
/**
* Implements hook_hook_info().
*
* Allows the use of $module.domain.inc files.
*/
function domain_hook_info() {
$hooks['domain_load'] = [
'group' => 'domain',
];
$hooks['domain_insert'] = [
'group' => 'domain',
];
$hooks['domain_update'] = [
'group' => 'domain',
];
$hooks['domain_delete'] = [
'group' => 'domain',
];
$hooks['domain_reassign'] = [
'group' => 'domain',
];
$hooks['domain_cron'] = [
'group' => 'domain',
];
$hooks['domain_features_rebuild'] = [
'group' => 'domain',
];
$hooks['domain_install'] = [
'group' => 'domain',
];
$hooks['domain_ignore'] = [
'group' => 'domain',
];
// Replace with form alter?
$hooks['domain_form'] = [
'group' => 'domain',
];
$hooks['domain_warning'] = [
'group' => 'domain',
];
$hooks['domain_source_alter'] = [
'group' => 'domain',
];
$hooks['domain_source_path_alter'] = [
'group' => 'domain',
];
$hooks['domain_batch'] = [
'group' => 'domain',
];
$hooks['domain_batch_alter'] = [
'group' => 'domain',
];
// Test that these work.
$hooks['domain_bootstrap_lookup'] = [
'group' => 'domain',
];
// Test that these work.
$hooks['domain_bootstrap_full'] = [
'group' => 'domain',
];
// Rename to hook_domain_path
$hooks['domain_path'] = [
'group' => 'domain',
];
// Rename to hook_domain_warning_alter
$hooks['domain_warning_alter'] = [
'group' => 'domain',
];
// Replace with form_alter?
$hooks['domain_settings'] = [
'group' => 'domain',
];
$hooks['domain_validate_alter'] = [
'group' => 'domain',
];
return $hooks;
}
/**
* Implements hook_block_info().
*/
function domain_block_info() {
$blocks = [];
$blocks['information'] = [
'info' => t('Domain access information'),
];
$blocks['server'] = [
'info' => t('Domain access server information'),
'cache' => BACKDROP_NO_CACHE,
];
$blocks['switcher'] = [
'info' => t('Domain switcher'),
];
return $blocks;
}
/**
* Implements hook_block_view().
*/
function domain_block_view($delta = '') {
// Dispatch to sub-function.
if (empty($delta)) {
return;
}
module_load_include('inc', 'domain', 'domain.blocks');
$function = 'domain_block_view_' . $delta;
if (function_exists($function)) {
return $function();
}
}
/**
* Implements hook_user_load().
*
* Attached domain_id records to all registering users. These
* are used to determine which 'domain_editor' group that users
* with the 'edit domain content' and 'delete domain content' permissions are
* in.
*/
function domain_user_load($users) {
foreach ($users as $uid => $account) {
$users[$uid]->domain_user = domain_get_user_domains($account);
}
}
/**
* Emsures that a user object has been loaded properly.
*
* @param $account
* An account object.
*/
function domain_user_set($account) {
if (!isset($account->domain_user)) {
$accounts = [$account->uid => $account];
domain_user_load($accounts);
}
}
/**
* Implements hook_user_insert().
*/
function domain_user_insert($account) {
domain_user_save($account);
}
/**
* Implements hook_user_update().
*/
function domain_user_update($account) {
domain_user_save($account);
}
/**
* Helper function called by both hook_user_insert() and hook_user_update().
*/
function domain_user_save($account) {
// If our field element is missing, do nothing.
if (!isset($account->domain_user)) {
return;
}
// Clear and reset the {domain_editor} table.
db_delete('domain_editor')
->condition('uid', $account->uid)
->execute();
// Store the new domains.
$values = [];
foreach ($account->domain_user as $domain_id => $status) {
if ($status != 0) {
$values[] = ['uid' => $account->uid, 'domain_id' => $domain_id];
}
}
if (!empty($values)) {
$query = db_insert('domain_editor')->fields(['uid', 'domain_id']);
foreach ($values as $record) {
$query->values($record);
}
$query->execute();
}
// Clear the $edit field.
// $edit['domain_user'] = NULL;
}
/**
* Implements hook_user_delete().
*/
function domain_user_delete($account) {
db_delete('domain_editor')
->condition('uid', $account->uid)
->execute();
}
/**
* Implements hook_user_view().
*/
function domain_user_view($account, $view_mode) {
domain_user_set($account);
// Only show on full view.
if ($view_mode != 'full') {
return;
}
// Only show trusted users.
// TODO: Make this a new permission.
if (!user_access('assign domain editors')) {
return;
}
$add_roles = config_get('domain.settings', 'domain_add_roles');
// In the register case, we take the 'new user' settings.
$account->domain_user = domain_get_user_domains($account, $add_roles);
$output = '';
$account->content['domain'] = [
'#type' => 'user_profile_category',
'#weight' => 10,
'#title' => t('Domain status'),
];
if (empty($account->domain_user)) {
$output = t('This user is not assigned to a domain.');
}
else {
$items = [];
foreach (array_filter($account->domain_user) as $id) {
$domain = domain_lookup($id);
$items[] = check_plain($domain['sitename']);
}
$output = theme('item_list', ['items' => $items]);
}
$account->content['domain']['domain_settings'] = [
'#type' => 'item',
'#title' => t('Assigned domains'),
'#markup' => $output,
];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function domain_form_user_profile_form_alter(&$form, &$form_state) {
domain_form_user_form_alter($form, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function domain_form_user_register_form_alter(&$form, &$form_state) {
domain_form_user_form_alter($form, $form_state);
}
/**
* Helper function for the two user forms we care about.
*/
function domain_form_user_form_alter(&$form, &$form_state) {
$_domain = domain_get_domain();
// Get the domains for this user, but ignore roles unless told to use them.
$add_roles = config_get('domain.settings', 'domain_add_roles');
// In the register case, we take the 'new user' settings.
if ($form['#form_id'] == 'user_register_form') {
$add_roles = TRUE;
}
$account = $form['#user'];
$account->domain_user = domain_get_user_domains($account, $add_roles);
// By default, the requesting domain is assigned on registration.
if (empty($account->uid)) {
$default = [$_domain['domain_id'] => $_domain['domain_id']];
}
else {
$default = $account->domain_user;
}
if (user_access('assign domain editors')) {
// Set the form options.
$domains = domain_domains();
$options = [];
foreach ($domains as $domain) {
$options[$domain['domain_id']] = check_plain($domain['sitename']);
}
$format = domain_select_format();
$form['domain'] = [
'#type' => 'fieldset',
'#title' => t('Domain access'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => -10,
];
if (config_get('domain.settings', 'domain_vertical_tab')) {
$form['domain']['#group'] = 'additional_settings';
$form['domain']['#attributes'] = [
'class' => ['domain-access-options-form'],
];
$form['domain']['#attached'] = [
'js' => [
backdrop_get_path('module', 'domain') . '/domain.node.js',
[
'data' => ['domain' => ['fieldType' => $format]],
'type' => 'setting',
],
],
];
}
$form['domain']['domain_user'] = [
'#type' => empty($format) ? 'checkboxes' : 'select',
'#options' => $options,
'#title' => t('Domain access settings'),
'#description' => t('Select the affiliates that this user belongs to. Used to grant editing permissions for users with the "edit domain content" permission.'),
'#default_value' => $default,
];
if ($format) {
$form['domain']['domain_user']['#multiple'] = TRUE;
$form['domain']['domain_user']['#size'] = count($options) > 10 ? 10 : count($options);
}
}
else {
$form['domain'] = [
'domain_user' => [
'#type' => 'value',
'#value' => $default,
],
];
}
}
/**
* Implements hook_user_operations().
*/
function domain_user_operations() {
if (!user_access('assign domain editors')) {
return;
}
return [
'domain' => [
'label' => t('Assign users to domains'),
'callback' => 'domain_user_operation_assign',
],
];
}
/**
* Implements hook_form_alter().
*/
function domain_form_user_admin_account_alter(&$form, $form_state) {
global $_domain;
if (!user_access('assign domain editors')) {
return;
}
$form['options']['#weight'] = -2;
$_domain = domain_get_domain();
$options = [];
$format = domain_select_format();
foreach (domain_domains() as $data) {
// The domain must be valid.
if ($data['valid'] || user_access('access inactive domains')) {
// Filter checkbox output but not select list.
$options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
}
}
$form['domain'] = [
'#type' => 'fieldset',
'#title' => t('Affiliate editor options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#prefix' => '<div class="description">' . t('If you select <em>Assign users to domains</em> above, you should confirm the <em>Affiliate editor options</em> settings below.') . '</div>',
'#weight' => -1,
];
$form['domain']['behavior'] = [
'#type' => 'radios',
'#title' => t('Update behavior'),
'#options' => [
0 => t('Replace old values with new settings'),
1 => t('Add new settings to existing values'),
2 => t('Remove selected domains from existing values'),
],
'#description' => t('Defines how new grants will be applied to the updated users.'),
'#default_value' => 0,
];
$form['domain']['domains'] = [
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Assign to'),
'#options' => $options,
'#required' => FALSE,
'#description' => t('Select which affiliates these users should belong to. <em>Note: this will erase any current assignment for the selected users.</em>'),
'#default_value' => [$_domain['domain_id']],
];
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
}
// Add our domain elements.
$ops = array_pop($form['accounts']['#header']);
$form['accounts']['#header']['domain_user'] = ['data' => t('Assigned Domains')];
foreach (array_keys($form['accounts']['#options']) as $uid) {
$form['accounts']['#options'][$uid]['domain_user'] = theme('item_list', ['items' => _domain_user_list($uid)]);
}
$form['accounts']['#header']['operations'] = $ops;
$form['#submit'][] = 'domain_update_users';
}
/**
* Helper function to get the names of all domains for a user.
*
* @param $uid
* The user id.
*
* @return
* An array of domain names.
*/
function _domain_user_list($uid) {
$temp_account = new stdClass;
$temp_account->uid = $uid;
$list = domain_get_user_domains($temp_account, FALSE);
$domains = domain_domains();
$user_domains = [];
foreach ($list as $domain_id) {
$user_domains[] = check_plain($domains[$domain_id]['sitename']);
}
return $user_domains;
}
/**
* Callback for domain_content_node_operations().
*
* This callback is required, but we actually do our action inside
* of domain_update_users().
*/
function domain_user_operation_assign($accounts) {
}
/**
* FormsAPI to handle the batch update of users.
*/
function domain_update_users($form, &$form_state) {
$values = $form_state['values'];
if ($values['operation'] != 'domain') {
return;
}
// Get the domains for this user, but ignore roles unless told to use them.
$add_roles = config_get('domain.settings', 'domain_add_roles');
// Loop through the selected accounts.
$domains = array_filter($values['domains']);
foreach ($values['accounts'] as $uid) {
// If appending values, do so here.
if (!empty($form_state['values']['behavior'])) {
$account = new stdClass();
$account->uid = $uid;
$current = domain_get_user_domains($account, $add_roles);
// Behavior 1: add new domains.
if ($form_state['values']['behavior'] == 1) {
$domains += $current;
}
// Behavior 2: remove new domains.
else {
foreach ($domains as $domain_id) {
if (isset($current[$domain_id])) {
unset($current[$domain_id]);
}
}
$domains = $current;
}
}
db_delete('domain_editor')
->condition('uid', $uid)
->execute();
foreach ($domains as $domain_id) {
db_insert('domain_editor')
->fields(['uid' => $uid, 'domain_id' => $domain_id])
->execute();
}
}
}
/**
* Implements hook_cron().
*
* This function invokes hook_domain_cron() and allows
* Domain Access modules to run functions for all active affiliates.
*/
function domain_cron() {
// Check to see if this function is needed at all.
$modules = module_implements('domain_cron');
if (!empty($modules)) {
// Get the domain list.
$domains = domain_domains();
// Run the hook for each active domain.
foreach ($domains as $domain) {
domain_set_domain($domain['domain_id'], TRUE);
foreach ($modules as $module) {
module_invoke($module, 'domain_cron', $domain);
}
}
// Reset the active domain.
domain_reset_domain(TRUE);
}
}
/**
* Menu loader function.
*
* Note that this function should only be used as a menu callback,
* since the results are not cached. Use domain_lookup() instead.
*
* The passed parameter will be checked against the {domain} table for
* valid records.
*
* @param $domain_id
* The id request for a specific domain.
* @param $reset
* A boolean flag to clear the static variable if necessary.
*
* @return
* $domain array on success or FALSE on failure.
*/
function domain_load($domain_id = NULL, $reset = FALSE) {
$domain = domain_lookup($domain_id, NULL, $reset);
if ($domain == -1) {
return FALSE;
}
return $domain;
}
/**
* Save a domain record.
*
* @param $values
* Form value information required to edit a domain.
* @param $form_values
* Form values passed to the submit handler. May be used by other
* modules.
*
* @return
* $domain array on success or -1 on failure.
*/
function domain_save($values, $form_values = []) {
// Must this be the default domain?
// Used in cases where there are no domains present.
$count = (bool) db_query("SELECT COUNT(domain_id) FROM {domain} WHERE is_default = 1")->fetchField();
if (!$count) {
$values['is_default'] = 1;
}
// Ensure we have a machine name.
if (!isset($values['machine_name'])) {
$values['machine_name'] = domain_machine_name($values['subdomain']);
}
// Update or insert a record?
if (!empty($values['domain_id'])) {
$action = 'domain_update';
$update_id = ['domain_id'];
}
else {
$action = 'domain_insert';
$update_id = [];
}
// If this is the default domain, reset other domains.
if (!empty($values['is_default'])) {
db_update('domain')
->fields(['is_default' => 0])
->condition('machine_name', $values['machine_name'], '<>')
->execute();
}
// Store the data, using the machine_name to generate a numeric id.
// Note that we _must_ have a numeric key for {node_access}.
backdrop_write_record('domain_export', $values, $update_id);
backdrop_write_record('domain', $values, $update_id);