forked from phase2/oa_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oa_core.module
1254 lines (1151 loc) · 45 KB
/
oa_core.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
/**
* @file
* Code for the OpenAtrium Core feature.
*/
include_once 'oa_core.features.inc';
include_once 'includes/oa_core.util.inc';
include_once 'includes/oa_core.access.inc';
include_once 'includes/oa_core.login.inc';
include_once 'includes/oa_core.theme.inc';
/**
* Name of default OpenAtrium Space content type.
*/
define('OA_SPACE_TYPE', 'oa_space');
/**
* Name of OpenAtrium Section content type.
*/
define('OA_SECTION_TYPE', 'oa_section');
/**
* Name of OpenAtrium Group content type.
*/
define('OA_GROUP_TYPE', 'oa_group');
/**
* Name of OpenAtrium Team content type.
*/
define('OA_TEAM_TYPE', 'oa_team');
/**
* Name of default OpenAtrium Section field (for Organic Groups Fields UI).
*/
define('OA_SECTION_FIELD', 'oa_section_ref');
/**
* Name of default OpenAtrium Group field (for Organic Groups Fields UI).
*/
define('OA_SPACE_FIELD', 'og_group_ref');
/**
* The access realm of space member.
*/
define('OA_ACCESS_REALM', 'oa_access');
/**
* The access realm of unpublished content.
*/
define('OA_UNPUBLISHED_REALM', 'oa_unpublished');
/**
* Implements hook_ctools_plugin_directory().
*/
function oa_core_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
elseif ($owner == 'entityreference') {
return "plugins/entityreference/$plugin_type";
}
}
/**
* Implementations hook_menu().
*/
function oa_core_menu() {
// Add an administration page for Open Atrium
$items['admin/openatrium'] = array(
'title' => 'Open Atrium',
'description' => 'Administer Open Atrium.',
'weight' => 0,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/openatrium/section-templates'] = array(
'title' => 'Section Templates',
'description' => 'Configure panelizer section templates',
'weight' => 0,
'access arguments' => array('administer site configuration'),
'page callback' => 'oa_core_section_template',
);
$items['admin/openatrium/space-templates'] = array(
'title' => 'Space Templates',
'description' => 'Configure panelizer space templates',
'weight' => 0,
'access arguments' => array('administer site configuration'),
'page callback' => 'oa_core_space_template',
);
$items['admin/openatrium/setting'] = array(
//changing from /settings since Drupal not refreshing parent of existing items
'title' => 'Plugin Settings',
'description' => 'Configuration related to Open Atrium plugins',
'weight' => 0,
'page callback' => 'drupal_get_form',
'page arguments' => array('oa_core_configure_form'),
'access arguments' => array('administer site configuration'),
);
$items['admin/openatrium/groups'] = array(
'title' => 'Groups',
'description' => 'Configure Open Atrium Groups',
'weight' => 0,
'access arguments' => array('administer site configuration'),
'page callback' => 'oa_core_show_groups',
);
$items['group/%/%/remove/%/%'] = array(
'title' => 'Remove member',
'type' => MENU_CALLBACK,
'page callback' => 'oa_core_remove_member',
'page arguments' => array(1, 2, 4, 5),
'access callback' => 'og_ui_user_access_group',
'access arguments' => array('manage members', 1, 2),
);
$items['group/%/%/block/%'] = array(
'title' => 'Block member',
'type' => MENU_CALLBACK,
'page callback' => 'oa_core_block_member',
'page arguments' => array(1, 2, 4),
'access callback' => 'og_ui_user_access_group',
'access arguments' => array('manage members', 1, 2),
);
$items['group/%/%/add-member/%'] = array(
'title' => 'Add member',
'type' => MENU_CALLBACK,
'page callback' => 'oa_core_add_member',
'page arguments' => array(1, 2, 4),
'access callback' => 'user_is_logged_in',
);
$items['group/%/%/add-admin/%'] = array(
'title' => 'Add Admin',
'type' => MENU_CALLBACK,
'page callback' => 'oa_core_add_admin',
'page arguments' => array(1, 2, 4),
'access callback' => 'og_ui_user_access_group',
'access arguments' => array('manage members', 1, 2),
);
$items['group/%/%/remove-admin/%'] = array(
'title' => 'Add Admin',
'type' => MENU_CALLBACK,
'page callback' => 'oa_core_remove_admin',
'page arguments' => array(1, 2, 4),
'access callback' => 'og_ui_user_access_group',
'access arguments' => array('manage members', 1, 2),
);
$items['node/add/oa-space/%'] = array(
'title' => 'Create Space',
'page callback' => 'oa_core_create_space_page_callback',
'page arguments' => array(OA_SPACE_TYPE, 3),
'access callback' => 'node_access',
'access arguments' => array('create', OA_SPACE_TYPE),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback for the 'Create space' page.
*
* This a copy of 'page_manager_node_add' but with the title using the
* space_type name rather than just the content type name.
*
* @param string $type
* The node content type name (ie. oa_space, oa_section).
* @param integer $space_tid
* The taxonomy term ID of the space_type or section_type.
*
* @see page_manager_node_add
*/
function oa_core_create_space_page_callback($type, $space_tid) {
global $user;
if ($space_type = taxonomy_term_load($space_tid)) {
// Initialize settings:
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $type,
'language' => LANGUAGE_NONE,
);
$types = node_type_get_types();
drupal_set_title(t('Create @name @type', array(
'@name' => $space_type->name,
'@type' => $types[$type]->name,
)));
// Load the file with page_manager_node_edit().
module_load_include('inc', 'ctools', 'page_manager/plugins/tasks/node_edit');
return page_manager_node_edit($node);
}
return MENU_NOT_FOUND;
}
/**
* Implements hook_menu_alter().
*/
function oa_core_menu_alter(&$items) {
if (!empty($items['node/%/group'])) {
$items['node/%/group']['title'] = 'Config';
}
}
/**
* Implements hook_views_data().
*/
function oa_core_views_data() {
$data = array();
$data['field_data_oa_parent']['is_parent'] = array(
'title' => t('Is Parent'),
'help' => t('Content is a parent'),
'filter' => array(
'handler' => 'views_handler_filter_is_parent',
),
);
return $data;
}
/**
* Configuration Form for Open Atrium
*/
function oa_core_configure_form($form, &$form_state) {
// blank for for now. Other modules can alter this to add stuff
$form = array();
return system_settings_form($form);
}
/**
* Implements hook_og_fields_info().
*/
function oa_core_og_fields_info() {
$items[OA_SECTION_FIELD] = array(
'no ui' => TRUE,
'type' => array('group content'),
'description' => t('Determine to which Open Atrium section this space content is assigned to.'),
'field' => array(
'field_name' => OA_SECTION_FIELD,
'type' => 'entityreference',
'cardinality' => 1,
'module' => 'entityreference',
'settings' => array(
'handler' => 'base',
'handler_settings' => array(
'behaviors' => array(
'views-select-list' => array(
'status' => 0,
),
),
'sort' => array(
'type' => 'none',
),
'target_bundles' => array(
OA_SECTION_TYPE => OA_SECTION_TYPE,
),
),
'target_type' => 'node',
),
'translatable' => '0',
'type' => 'entityreference',
),
'instance' => array(
'label' => t('Open Atrium Section'),
'widget' => array(
'active' => 0,
'module' => 'options',
'settings' => array(
'match_operator' => 'CONTAINS',
'path' => '',
'size' => 60,
),
'type' => 'options_select',
'weight' => '7',
),
),
);
return $items;
}
/**
* Implements hook_form_alter for node edit forms
*/
function oa_core_form_node_form_alter(&$form, &$form_state, $form_id) {
// we are in an edit form
// autofill empty group audience field
if (isset($form[OA_SPACE_FIELD]) && empty($form[OA_SPACE_FIELD][LANGUAGE_NONE][0]['default']['#default_value'])) {
if (!empty($_GET['og_group_ref'])) {
$form[OA_SPACE_FIELD][LANGUAGE_NONE][0]['default']['#default_value'] = check_plain($_GET['og_group_ref']);
}
elseif (!empty($_SESSION['og_context']['gid'])) {
$form[OA_SPACE_FIELD][LANGUAGE_NONE][0]['default']['#default_value'] = $_SESSION['og_context']['gid'];
}
}
// modify label on Panelizer layout field
if (isset($form['panelizer'])) {
$form['panelizer']['page_manager']['name']['#title'] = t('Select Layout');
}
}
/**
* Implements hook_views_pre_render().
*/
function oa_core_views_pre_render(&$view) {
if ($view->name == 'oa_core_space_types') {
if (count($view->result) == 1) {
// If there is only one Space Blueprint, then we jump to it automatically.
$args = $_GET;
unset($args['q']);
drupal_goto('node/add/oa-space/' . $view->result[0]->tid, array('query' => $args));
}
elseif (user_access('administer taxonomy')) {
$view->attachment_before = '<p>' . t('Manage existing <em>Space Blueprints</em> or add new ones by <a href="!url">administrating the taxonomy</a>.', array('!url' => url('admin/structure/taxonomy/space_type'))) . '</p>';
}
}
}
/**
* Implements hook_views_pre_build().
*/
function oa_core_views_pre_build(&$view) {
if ($view->name == 'oa_recent_activity') {
// Set the override path to the current page. This will redirect the back view to
// the current page when submitted in case AJAX fails.
$view->override_path = $_GET['q'];
}
}
/**
* Implements hook_preprocess_views_view_fields().
* Perform field-level replacement/processing here
*/
function oa_core_preprocess_views_view_fields(&$vars) {
$vars['index'] = $vars['view']->row_index;
$vars['display'] = $vars['view']->current_display;
foreach ($vars['fields'] as $id => $field) {
$vars[$id] = $field->content;
switch ($id) {
case 'timestamp':
$vars[$id . '_raw'] = $field->raw;
break;
case 'field_user_picture':
// check for missing user image in a view
if (!strip_tags($field->content, '<img>')) {
$image_style = 'oa_small_thumbnail';
if (isset($field->handler->options['settings']['image_style'])) {
$image_style = $field->handler->options['settings']['image_style'];
}
$vars[$id] = oa_users_picture(NULL, $image_style);
}
break;
}
}
}
/**
* Replace "group" term in OG with "space"
*/
function oa_core_replace_group(&$field, $context) {
if (isset($field)) {
if (isset($context['entity']->type) && ($context['entity']->type == OA_SPACE_TYPE)) {
$field = str_replace(t('group'), t('space'), $field);
$field = str_replace(t('Group'), t('Space'), $field);
}
}
}
/**
* Implements hook_field_attach_view_alter
*/
function oa_core_field_attach_view_alter(&$output, $context) {
if ($context['entity_type'] == 'node') {
if (!empty($output['group_group'][0]['#title'])) {
oa_core_replace_group($output['group_group'][0]['#title'], $context);
}
if (!empty($output['group_group'][0]['#markup'])) {
oa_core_replace_group($output['group_group'][0]['#markup'], $context);
}
}
}
/**
* Menu callback for Section Templates
*/
function oa_core_section_template() {
ctools_include('export-ui');
$handler = panelizer_entity_plugin_get_handler('node');
return panelizer_export_ui_switcher_page($handler, OA_SECTION_TYPE . '.page_manager', 'panelizer_defaults', 'list');
}
/**
* Menu callback for Section Templates
*/
function oa_core_space_template() {
ctools_include('export-ui');
$handler = panelizer_entity_plugin_get_handler('node');
return panelizer_export_ui_switcher_page($handler, OA_SPACE_TYPE . '.page_manager', 'panelizer_defaults', 'list');
}
/**
* Menu callback for Groups
*/
function oa_core_show_groups() {
drupal_goto('groups');
}
/**
* Helper function to add a user to a group
* @param [type] $group_type [description]
* @param [type] $gid [description]
* @param [type] $uid [description]
* @param [type] $override if TRUE, add pending members to space
* @return [type] [description]
*/
function oa_core_add_member_api($group_type, $gid, $uid, $override = FALSE) {
$account = user_load($uid);
$group = entity_load_single($group_type, $gid);
$label = entity_label($group_type, $group);
$entity_type = 'node';
$message = '';
$params = array();
$params['@user'] = format_username($account);
// Show the group name only if user has access to it.
$params['@group'] = entity_access('view', $entity_type, $group) ? entity_label($entity_type, $group) : t('Private group');
if (!$uid) {
// Anonymous user can't request membership.
$dest = drupal_get_destination();
if (variable_get('user_register', 1)) {
$message = t('In order to join any group, you must <a href="!login">login</a>. After you have successfully done so, you will need to request membership again.',
array('!login' => url("user/login", array('query' => $dest))));
}
else {
$message = t('In order to join any group, you must <a href="!login">login</a> or <a href="!register">register</a> a new account. After you have successfully done so, you will need to request membership again.',
array('!register' => url("user/register", array('query' => $dest)), '!login' => url("user/login", array('query' => $dest))));
}
}
elseif (!$override && og_is_member($entity_type, $gid, 'user', $account, array(OG_STATE_BLOCKED))) {
// User is blocked, access denied.
$message = t('@user is blocked and cannot be addded.', $params);
}
elseif (!$override && og_is_member($entity_type, $gid, 'user', $account, array(OG_STATE_PENDING))) {
// User is pending, return them back.
$message = t('@user already has a pending membership for the the group @group.', $params);
}
elseif (og_is_member($entity_type, $gid, 'user', $account, array(OG_STATE_ACTIVE))) {
// User is already a member, return them back.
$message = t('@user is already a member of the group @group.', $params);
}
if (empty($message)) {
// Ungroup user, in case they were already registered.
og_ungroup('node', $group->nid, 'user', $account);
// add user to group
og_group('node', $group->nid, array('entity' => $account));
$message = t('%user has been added to the space %title.', array('%user' => format_username($account), '%title' => $label));
}
drupal_set_message($message);
return $message;
}
/**
* Menu callback to add user to group
* Mostly taken from og_ui_subscribe in og_ui_pages.inc
* @param $group_type
* @param $gid
* @param $uid
*/
function oa_core_add_member($group_type, $gid, $uid, $field_name = NULL, $type = 'nojs') {
global $user;
if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], $group_type . '_' . $gid . '_' . $uid)) {
return MENU_ACCESS_DENIED;
}
$message = '';
$account = user_load($uid);
$is_user = ($user->uid == $account->uid) && ($user->uid != 1);
if (module_exists('og_ui') && ($type != 'ajax') && $is_user) {
module_load_include('inc', 'og_ui', 'og_ui.pages');
// user is requesting their own membership, so show the confirmation form
if (og_user_access('node', $gid, 'subscribe', $account) || og_user_access('node', $gid, 'subscribe without approval', $account)) {
// Show the user a subscription confirmation.
return drupal_get_form('og_ui_confirm_subscribe', 'node', $gid, $account, $field_name);
}
drupal_access_denied();
}
$message = oa_core_add_member_api($group_type, $gid, $uid, !$is_user);
if ($type != 'ajax') {
drupal_goto();
}
else {
$commands = array();
$commands[] = ajax_command_append('#oa-core-messages', theme('status_messages'));
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}
return;
}
/**
* Menu callback to remove user from group
* @param $group_type
* @param $gid
* @param $uid
*/
function oa_core_remove_member($group_type, $gid, $from_children, $uid, $type = 'nojs') {
if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], $group_type . '_' . $gid . '_' . $uid)) {
return MENU_ACCESS_DENIED;
}
$account = user_load($uid);
$group = entity_load_single($group_type, $gid);
$label = entity_label($group_type, $group);
$username = format_username($account);
og_ungroup('node', $group->nid, 'user', $account);
if ($from_children == 'all' && module_exists('oa_subspaces') && og_user_access('node', $group->nid, 'administer group') && ($children = og_subgroups_children_load('node', $group->nid)) && ($user_groups = og_get_entity_groups('user', $account))) {
if ($remove = og_subgroups_intersect_groups($children, $user_groups)) {
foreach ($remove as $entity_type => $ids) {
foreach ($ids as $id) {
$child_group = entity_load_single($entity_type, $id);
og_ungroup($entity_type, $id, 'user', $account);
$child_label = entity_label($entity_type, $child_group);
drupal_set_message(t('%user has been removed from child @type %title.', array('@type' => drupal_strtolower(node_type_get_name($child_group->type)), '%user' => $username, '%title' => $child_label)));
}
}
}
}
drupal_set_message(t('%user has been removed from the @type %title.', array('@type' => drupal_strtolower(node_type_get_name($group->type)), '%user' => $username, '%title' => $label)));
if ($type != 'ajax') {
drupal_goto();
}
else {
$commands = array();
$commands[] = ajax_command_append('#oa-core-messages', theme('status_messages'));
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}
}
/**
* Menu callback to block a user from group
* @param $group_type
* @param $gid
* @param $uid
*/
function oa_core_block_member($group_type, $gid, $uid, $type = 'nojs') {
if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], $group_type . '_' . $gid . '_' . $uid)) {
return MENU_ACCESS_DENIED;
}
$account = user_load($uid);
$group = entity_load_single($group_type, $gid);
$label = entity_label($group_type, $group);
$username = format_username($account);
og_ungroup('node', $group->nid, 'user', $account);
// add user to group
og_group('node', $group->nid, array('entity' => $account, 'state' => OG_STATE_BLOCKED));
drupal_set_message(t('%user has been blocked.', array('%user' => format_username($account), '%title' => $label)));
if ($type != 'ajax') {
drupal_goto();
}
else {
$commands = array();
$commands[] = ajax_command_append('#oa-core-messages', theme('status_messages'));
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}
}
/**
* Menu callback to make user an admin
* @param $group_type
* @param $gid
* @param $uid
*/
function oa_core_add_admin($group_type, $gid, $uid, $type = 'nojs') {
if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], $group_type . '_' . $gid . '_' . $uid)) {
return MENU_ACCESS_DENIED;
}
$account = user_load($uid);
$group = entity_load_single($group_type, $gid);
$label = entity_label($group_type, $group);
$og_roles = og_roles($group_type, $group->type, $gid, FALSE, FALSE);
$rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
if ($rid > 0) {
og_role_grant($group_type, $gid, $uid, $rid);
og_invalidate_cache();
drupal_set_message(t('%user has been added as an Admin to the space %title.', array('%user' => format_username($account), '%title' => $label)));
}
if ($type != 'ajax') {
drupal_goto();
}
else {
$commands = array();
$commands[] = ajax_command_append('#oa-core-messages', theme('status_messages'));
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}
}
/**
* Menu callback to remove user as admin
* @param $group_type
* @param $gid
* @param $uid
*/
function oa_core_remove_admin($group_type, $gid, $uid, $type = 'nojs') {
if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], $group_type . '_' . $gid . '_' . $uid)) {
return MENU_ACCESS_DENIED;
}
$account = user_load($uid);
$group = entity_load_single($group_type, $gid);
$label = entity_label($group_type, $group);
$og_roles = og_roles($group_type, $group->type, $gid, FALSE, FALSE);
$rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
if ($rid > 0) {
og_role_revoke($group_type, $gid, $uid, $rid);
og_invalidate_cache();
drupal_set_message(t('%user has been removed as Admin of space %title.', array('%user' => format_username($account), '%title' => $label)));
}
if ($type != 'ajax') {
drupal_goto();
}
else {
$commands = array();
$commands[] = ajax_command_append('#oa-core-messages', theme('status_messages'));
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}
return;
}
/**
* Copies GET arguments to the end of a link on a view.
*
* Should be used in hook_views_pre_view().
*
* @param object &$view
* Views object to modify.
* @param string $display_name
* (Optional) The Views display to modify.
* @param string $field_name
* (Optional) The Views field name to modify.
*
* @see hook_views_pre_view()
*/
function oa_core_views_copy_get_arguments_to_link(&$view, $display_name = 'default', $field_name = 'name') {
// Get the GET arguments from the current page.
$args = $_GET;
unset($args['q']);
if (count($args) > 0) {
$query = http_build_query($args);
if (!empty($view->display[$display_name]->handler->options['fields'][$field_name]['alter']['path'])) {
$view->display[$display_name]->handler->options['fields'][$field_name]['alter']['path'] .= '?' . $query;
}
}
}
/**
* Implements hook_views_pre_view().
*/
function oa_core_views_pre_view(&$view) {
// Use page_1 display's style option so it allows sorting by table headers.
if ($view->name == 'open_atrium_content' && !empty($view->display_handler->options['pane_conf']['view_settings']) &&$view->display_handler->options['pane_conf']['view_settings'] == 'table') {
$view->display_handler->options['defaults']['style_options'] = FALSE;
$view->display_handler->options['style_options'] = $view->display['page_1']->display_options['style_options'];
}
elseif ($view->name == 'oa_core_space_types') {
oa_core_views_copy_get_arguments_to_link($view);
}
if (!empty($view->display_handler->options['pane_conf']['exposed']['oa_core_default_to_current_user'])) {
global $user;
// @todo There has to be a way to make this more elegant.
$exposed = $view->get_exposed_input();
if (!isset($_GET['uid']) && empty($exposed['uid'])) {
$exposed['uid'] = $user->name;
}
$view->set_exposed_input($exposed);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function oa_core_form_views_content_views_panes_content_type_edit_form_alter(&$form, &$form_state, $form_id) {
$conf = $form_state['conf'];
$pane = $form_state['pane'];
// Add in te Active options
// @todo be a bit smarter on this, check type of field.
$group = oa_core_get_space_context();
$has_active_space = ($group_node = node_load($group)) && ($group_node->type == OA_SPACE_TYPE);
if (isset($form['exposed']['filter-oa_parent_space_target_id']['oa_parent_space_target_id'])) {
$form['exposed']['filter-oa_parent_space_target_id']['oa_parent_space_target_id']['#options'] = array('' => t('- Active Group -')) + $form['exposed']['filter-oa_parent_space_target_id']['oa_parent_space_target_id']['#options'];
if (!isset($pane->configuration['exposed']['oa_parent_space_target_id'])) {
$pane->configuration['exposed']['oa_parent_space_target_id'] = '';
}
}
if (isset($form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id'])) {
$form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id']['#options'] = array('' => t('- Active Space -')) + $form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id']['#options'];
if (!isset($pane->configuration['exposed']['og_group_ref_target_id'])) {
$pane->configuration['exposed']['og_group_ref_target_id'] = '';
}
$form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id_mine'] = array(
'#title' => t('Limit to groups of current user'),
'#type' => 'checkbox',
'#default_value' => !empty($conf['exposed']['og_group_ref_target_id_mine']),
'#parents' => array('exposed', 'og_group_ref_target_id_mine'),
);
$form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id']['#states'] = array(
'visible' => array(
':input[name="exposed[og_group_ref_target_id_mine]"]' => array('checked' => FALSE),
),
);
}
if (isset($form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id'])) {
// Add a process that change ajax path so can return multiple changes.
if (isset($form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id'])) {
$form['buttons']['#process'] = array('oa_core_form_views_content_views_panes_content_type_edit_process', 'form_process_actions', 'form_process_container');
}
// Refresh the sections with currently selected group if not currently active group but not empty.
if (!empty($pane->configuration['exposed']['og_group_ref_target_id']) && !isset($_POST['exposed']['og_group_ref_target_id']) && $pane->configuration['exposed']['og_group_ref_target_id'] != $group && is_numeric($pane->configuration['exposed']['og_group_ref_target_id'])) {
_oa_sections_get_current_selected_space(NULL, $pane->configuration['exposed']['og_group_ref_target_id']);
}
$form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id']['#after_build'][] = 'oa_core_refresh_section_values';
$form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id']['#options'] = array('' => t('- Active Section -')) + $form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id']['#options'];
if (!isset($pane->configuration['exposed']['oa_section_ref_target_id'])) {
$pane->configuration['exposed']['oa_section_ref_target_id'] = '';
}
}
if (!empty($form['exposed']['filter-uid'])) {
$form['exposed']['filter-uid']['oa_core_default_to_current_user'] = array(
'#type' => 'checkbox',
'#title' => t('Default to current user'),
'#parents' => array('exposed', 'oa_core_default_to_current_user'),
'#default_value' => !empty($conf['exposed']['oa_core_default_to_current_user']),
);
$form['exposed']['filter-uid']['uid']['#states'] = array(
'visible' => array(
':input[name="exposed[oa_core_default_to_current_user]"]' => array('checked' => FALSE),
),
);
}
// Set exposed form to be disabled at first.
if (isset($form['show_exposed_form'])) {
$form['show_exposed_form']['#default_value'] = isset($form_state['conf']['show_exposed_form']) ? $form_state['conf']['show_exposed_form'] : FALSE;
$form['display_settings']['show_exposed_form'] = $form['show_exposed_form'];
$form['display_settings']['show_exposed_form']['#weight'] = 10;
unset($form['show_exposed_form']);
if (!isset($pane->configuration['show_exposed_form'])) {
$pane->configuration['show_exposed_form'] = FALSE;
}
}
}
/**
* Update the section values in a way that skips cache, etc.
*/
function oa_core_refresh_section_values($element) {
$element['#options'] = array_intersect_key($element['#options'], array(NULL => '', 'All' => ''));
$values = entityreference_get_selection_handler(field_info_field('oa_section_ref'))->getReferencableEntities();
if ($values) {
$element['#options'] += current($values);
}
if ($element['#value'] && !array_key_exists($element['#value'], $element['#options'])) {
$element['#value'] = 'All';
}
return $element;
}
/**
* Only way I figured out to change the #ajax of this w/o changing the weight.
* Can't hook_form_alter cause go before panopoly_magic and panopoly_magic does
* module implements alter and puts itself last. Argh!
*/
function oa_core_form_views_content_views_panes_content_type_edit_process($element, &$form_state) {
if (isset($element['preview']['#ajax'])) {
$element['preview']['#ajax']['callback'] = 'oa_core_refresh_oa_section_ref_target_id';
}
return $element;
}
/**
* Replaces any changed elements on the form on ajax.
*/
function oa_core_refresh_oa_section_ref_target_id($form, $form_state) {
$messages = theme('status_messages');
$messages = $messages ? $messages : '';
$return = array(
'#type' => 'ajax',
'#commands' => array(),
);
$return['#commands'][] = ajax_command_replace('#panopoly-form-widget-preview', $messages . panopoly_magic_ajax_update_preview($form, $form_state));
if (isset($form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id'])) {
$form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id']['#theme_wrappers'] = array();
$form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id']['#attributes']['id'] = 'edit-exposed-oa-section-ref-target-id';
$return['#commands'][] = ajax_command_replace('#edit-exposed-oa-section-ref-target-id', drupal_render($form['exposed']['filter-oa_section_ref_target_id']['oa_section_ref_target_id']));
}
// The "my spaces" may have changed this, so update it.
if (isset($form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id'])) {
$form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id']['#theme_wrappers'] = array();
$form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id']['#attributes']['id'] = 'edit-exposed-og-group-ref-target-id';
$return['#commands'][] = ajax_command_replace('#edit-exposed-og-group-ref-target-id', drupal_render($form['exposed']['filter-og_group_ref_target_id']['og_group_ref_target_id']));
}
return $return;
}
/**
* See which views to setthe default value for exposed filters.
*/
function _oa_core_set_default_value_for_view($name) {
return in_array($name, array('open_atrium_content', 'oa_recent_activity'));
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function oa_core_form_views_exposed_form_alter(&$form, $form_state) {
if (_oa_core_set_default_value_for_view($form_state['view']->name) && $form_state['view']->display_handler->get_url() != $_GET['q']) {
// Change exposed form to submit to given page instead of redirecting to
// view all.
$form['#action'] = url($_GET['q']);
}
if (!empty($form['og_group_ref_target_id'])) {
$form['#after_build'][] = 'oa_core_views_exposed_form_rearrange';
}
// If a Space was chosen, and we have a Section selector - then we need to
// change the available options to be the Sections in that Space (rather
// than the current Space). This is necessary to make the customizations
// work that were done in:
// oa_core_form_views_content_views_panes_content_type_edit_form_alter()
// Otherwise you'll get the 'An illegal choice has been detected.' message
// when selecting a Section in another Space.
if (!empty($form_state['input']['og_group_ref_target_id']) && !empty($form['oa_section_ref_target_id'])) {
$sections = array(
'' => t('- Active Section -'),
'All' => t('- Any -')
) + oa_core_space_sections($form_state['input']['og_group_ref_target_id'], NULL, TRUE);
$form['oa_section_ref_target_id']['#options'] = $sections;
}
}
function oa_core_views_exposed_form_rearrange($form) {
if (!empty($form['og_group_ref_target_id_mine']) || !empty($form['og_subspaces_view_all'])) {
$form['og_group_ref_target_id'] = array(
'og_group_ref_target_id' => $form['og_group_ref_target_id'],
);
if (!empty($form['og_group_ref_target_id_mine'])) {
$form['og_group_ref_target_id']['og_group_ref_target_id_mine'] = $form['og_group_ref_target_id_mine'];
unset($form['og_group_ref_target_id_mine']);
}
if (!empty($form['og_subspaces_view_all'])) {
$form['og_group_ref_target_id']['og_subspaces_view_all'] = $form['og_subspaces_view_all'];
unset($form['og_subspaces_view_all']);
}
}
return $form;
}
/**
* Implements hook_token_info().
*/
function oa_core_token_info() {
$node['group-or-section-alias'] = array(
'name' => t("URL"),
'description' => t("The alias to the section (or group if no section of given content.)"),
);
return array(
'tokens' => array('node' => $node),
);
}
/**
* Return what the defined alias will eventually be for $nid
*/
function oa_core_get_alias($nid) {
$cache = &drupal_static('oa_core_alias', array());
if (isset($cache[$nid])) {
return $cache[$nid];
}
$node = node_load($nid);
$uri = entity_uri('node', $node);
$path = drupal_get_path_alias($uri['path']);
if (!isset($node->path['pathauto'])) {
module_load_include('inc', 'pathauto');
// return what the proper alias for this node will eventually be
// don't use 'update' yet since the drupal_get_path_alias() will still be cached
$path = pathauto_create_alias('node', 'return', $uri['path'], array('node' => $node), $node->type, $node->language);
}
$cache[$nid] = $path;
return $path;
}
/**
* Implements hook_tokens().
*/
function oa_core_tokens($type, $tokens, array $data = array(), array $options = array()) {
if ($type == 'node' && !empty($data['node']) && isset($tokens['group-or-section-alias'])) {
$node = $data['node'];
$replacements = array();
$original = $tokens['group-or-section-alias'];
$section = field_get_items('node', $node, OA_SECTION_FIELD);
$space = field_get_items('node', $node, OA_SPACE_FIELD);
$section_id = !empty($section[0]['target_id']) ? $section[0]['target_id'] : 0;
$space_id = !empty($space[0]['target_id']) ? $space[0]['target_id'] : 0;
if ($section_id > 0) {
$path = drupal_get_path_alias('node/' . $section_id);
if (module_exists('pathauto') && preg_match('#^node/(\d+)#', $path)) {
// section alias not defined yet, so define it
$path = oa_core_get_alias($section_id);
}
if (!empty($path)) {
$replacements[$original] = $path;
}
}
elseif ($space_id > 0) {
$path = drupal_get_path_alias('node/' . $space_id);
if (module_exists('pathauto') && preg_match('#^node/(\d+)#', $path)) {
// space alias not defined yet, so define it
$path = oa_core_get_alias($space_id);
}
if (!empty($path)) {
$replacements[$original] = $path;
if ($node->type != OA_SECTION_TYPE) {
$replacements[$original] .= '/content';
}
}
}
else {
$replacements[$original] = 'content';
}
return $replacements;
}
}
/**
* Implements hook_preprocess_views_exposed_form().
*/
function oa_core_preprocess_views_exposed_form(&$vars) {
// Add options from a filters to the variables for use in our template.
$vars['collapsed_filter'] = empty($vars['form']['#collapsed_filter']) ? FALSE : TRUE;
}
/**
* Allowed values callback for using particular space types.
*
* @param array $field
* Field instance array for the given field.
*
* @return array
* Associative array keyed by Taxonomy term id containing the term names.
*/
function oa_core_get_allowed_space_types($field) {
$options = array();
$gid = oa_core_get_space_context();
// If no gid, may be using this as a filter or such.
// @see oa_subspaces_form_node_form_alter().
if (!$gid || !module_exists('oa_subspaces') || user_access('administer group') || user_access('create oa_space content') || og_user_access('node', $gid, 'use any oa button space_type') || og_user_access('node', $gid, 'administer group')) {
return taxonomy_allowed_values($field);
}
if ($gid) {
foreach ($field['settings']['allowed_values'] as $tree) {
if ($vocabulary = oa_core_taxonomy_vocabulary($tree['vocabulary'])) {
if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'])) {
foreach ($terms as $term) {
if (og_user_access('node', $gid, _oa_buttons_perm_name($tree['vocabulary'], $term->tid))) {
$options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
}
}
}
}
}
}
return $options;
}
/**
* Gets all the options for space_type or section_type.
*