-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathog.module
More file actions
executable file
·2328 lines (2077 loc) · 67.9 KB
/
Copy pathog.module
File metadata and controls
executable file
·2328 lines (2077 loc) · 67.9 KB
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
// $Id$
/**
* @file
* Enable users to create and manage groups.
*/
// Load all Field module hooks for organic groups.
require(drupal_get_path('module', 'og') . '/og.field.inc');
// site admin chooses in og_settings() whether group creator can put his group on the registration form
define('OG_REGISTRATION_NEVER', 0);
define('OG_REGISTRATION_ALWAYS', 1);
define('OG_REGISTRATION_CHOOSE_TRUE', 2);
define('OG_REGISTRATION_CHOOSE_FALSE', 3);
// site admin chooses in og_settings() whether group creator can put his group in the Groups directory
define('OG_DIRECTORY_NEVER', 0);
define('OG_DIRECTORY_ALWAYS', 1);
define('OG_DIRECTORY_CHOOSE_TRUE', 2);
define('OG_DIRECTORY_CHOOSE_FALSE', 3);
// Dispositioning of content and memberships after deletion of a group node.
define('OG_DELETE_NOTHING', 0);
define('OG_DELETE_ORPHANS', 1);
define('OG_DELETE_MOVE_NODES', 2);
define('OG_DELETE_MOVE_NODES_MEMBERSHIPS', 3);
/**
* Define active group post states.
*/
define('OG_STATE_ACTIVE', 'active');
/**
* Define pending group post states. The user is subscribed to the group but
* isn't an active member yet.
*/
define('OG_STATE_PENDING', 'pending');
/**
* Define blocked group post states. The user is rejected from the group.
*/
define('OG_STATE_BLOCKED', 'blocked');
/**
* Modules should return this value from hook_og_access() to allow access to a
* group menu item.
*/
define('OG_ACCESS_ALLOW', 'allow');
/**
* Modules should return this value from hook_og_access() to deny access to a
* group menu item.
*/
define('OG_ACCESS_DENY', 'deny');
/**
* Modules should return this value from hook_og_access() to not affect group
* menu item access.
*/
define('OG_ACCESS_IGNORE', NULL);
/**
* Group audience field.
*/
define('OG_AUDIENCE_FIELD', 'og_audience');
/**
* The role name of group non-members.
*/
define('OG_ANONYMOUS_ROLE', 'anonymous member');
/**
* The role name of group member.
*/
define('OG_AUTHENTICATED_ROLE', 'authenticated member');
/**
* The role name of group administrator.
*/
define('OG_ADMINISTRATOR_ROLE', 'administrator');
/**
* The role ID of the default group non-members.
*/
define('OG_ANONYMOUS_DEFAULT_RID', 1);
/**
* The role ID of the default group member.
*/
define('OG_AUTHENTICATED_DEFAULT_RID', 2);
/**
* The role ID of the default group administrator.
*/
define('OG_ADMINISTRATOR_DEFAULT_RID', 3);
/*******************************************************************************
* Hook implementations
******************************************************************************/
/**
* Implement hook_menu().
*/
function og_menu() {
$items['og/%/%/subscribe'] = array(
'type' => MENU_CALLBACK,
'file' => 'og.pages.inc',
'page callback' => 'og_subscribe',
'page arguments' => array(1),
'access callback' => 'og_access',
'access arguments' => array('subscribe', 1),
// We don't add the group name, as it might be private.
'title' => 'Join group'
);
$items['og/opml'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'og_opml',
'access callback' => 'user_is_logged_in',
'title' => 'OPML',
);
$items['og/%/%/unsubscribe'] = array(
'type' => MENU_CALLBACK,
'file' => 'og.pages.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_confirm_unsubscribe', 1, 2),
'access callback' => 'og_access',
'access arguments' => array('unsubscribe', 1, 2),
'title' => 'Leave group',
);
$items['og/%/%/approve/%user/%'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'og_approve',
'page arguments' => array(1, 2, 3, 4),
'access callback' => 'og_access',
'access arguments' => array('approve membership', 1, 2, 3),
'title' => 'Approve membership request',
'file' => 'og.pages.inc',
);
$items['og/%/%/deny/%user/%'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'og_deny',
'page arguments' => array(1, 2, 4, 5),
'access callback' => 'og_access',
'access arguments' => array('deny membership', 1, 2, 4, 5),
'title' => 'Deny membership request',
);
$items['og/%/%/invite'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('og_invite_form', 1, 2),
'access callback' => 'og_access',
'access arguments' => array('invite', 1, 2),
'title' => 'Send invitation',
'type' => MENU_CALLBACK,
'file' => 'og.pages.inc',
);
$items['og/%/%/add-user'] = array(
'page callback' => 'drupal_get_form',
'title' => 'Add members',
'page arguments' => array('og_add_users', 1, 2),
'type' => MENU_LOCAL_TASK,
'file' => 'og.pages.inc',
'weight' => 5,
'access callback' => 'og_access',
'access arguments' => array('add user', 1, 2),
);
$items["og/%/%/manage"] = array(
'page callback' => 'og_manage',
'page arguments' => array(1),
'access callback' => 'og_access',
'access arguments' => array('manage', 1, 2),
'title' => 'Manage membership',
'type' => MENU_CALLBACK,
'file' => 'og.pages.inc',
);
// User listing pages.
$items['og/%/%/admin/people'] = array(
'title arguments' => array('People in group @group', 1, 2),
'title callback' => 'og_menu_title_callback',
'description' => 'Find and manage group members.',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_user_admin_account', 1, 2),
'access callback' => 'og_access',
'access arguments' => array('administer group', 1, 2),
'weight' => -4,
'file' => 'og.admin.inc',
);
// Permission administration pages.
$items['og/%/%/admin/people/roles'] = array(
'title arguments' => array('Roles for group @group', 1, 2),
'title callback' => 'og_menu_title_callback',
'description' => 'List, edit, or add user roles.',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_user_admin_new_role', 1, 2),
'access callback' => 'og_access',
'access arguments' => array('administer group', 1, 2),
'file' => 'og.admin.inc',
'weight' => -9,
);
$items['og/%/%/admin/people/roles/edit'] = array(
'title' => 'Edit role',
'page arguments' => array('og_user_admin_role', 1, 2),
'access callback' => 'og_access',
'access arguments' => array('administer group', 1, 2),
'type' => MENU_CALLBACK,
);
$items['og/%/%/admin/people/permissions'] = array(
'title arguments' => array('Permissions for group @group', 1, 2),
'title callback' => 'og_menu_title_callback',
'description' => 'Determine access to features by selecting permissions for roles.',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_user_admin_permissions', 1, 2),
'access callback' => 'og_access',
'access arguments' => array('administer group', 1, 2),
'file' => 'og.admin.inc',
'weight' => -8,
);
$items['node/%/og'] = array(
'title' => 'Group',
'page callback' => 'og_group_admin_overview',
'page arguments' => array('node', 1),
'access callback' => 'og_organic_groups_tab_access',
'access arguments' => array('node', 1),
'type' => MENU_LOCAL_TASK,
'file' => 'og.admin.inc',
);
$items['admin/config/og'] = array(
'title' => 'Organic groups',
'description' => 'Administer the suite of Organic groups modules.',
'position' => 'right',
'weight' => -5,
'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/config/og/permissions'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('og_user_admin_default_permissions'),
'title' => 'Organic groups default permissions',
'access arguments' => array('administer site configuration'),
'description' => 'Configure organic groups default permissions when a new group is created.',
'file' => 'og.admin.inc',
'file path' => drupal_get_path('module', 'og'),
'weight' => -5,
);
return $items;
}
/**
* Return TRUE if a node is a group node and the user has administrator rights.
*/
function og_organic_groups_tab_access($obj_type, $oid) {
return ($group = og_get_group($obj_type, $oid)) && og_user_access($group->gid, 'administer group');
}
/**
* Implement hook_init().
*/
function og_init() {
if ($group_node = og_determine_context()) {
og_set_context($group_node);
}
}
/**
* Implement hook_theme().
*/
function og_theme() {
return array(
'og_user_admin_permissions' => array(
'render element' => 'form',
'file' => 'og.admin.inc',
),
'og_user_admin_new_role' => array(
'render element' => 'form',
'file' => 'og.admin.inc',
),
);
}
/**
* Implement hook_forms().
*/
function og_forms() {
$forms['og_user_admin_new_role']['callback'] = 'og_user_admin_role';
// Form to define the default permissions state for new groups.
$forms['og_user_admin_default_permissions']['callback'] = 'og_user_admin_permissions';
return $forms;
}
/**
* Implement hook_form_FORM_ID_alter().
*/
function og_form_node_type_form_alter(&$form, &$form_state) {
$node_type = $form['#node_type']->type;
$form['og'] = array(
'#type' => 'fieldset',
'#title' => t('Organic groups'),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
'#description' => t('Specify how organic groups should treat content of this type. Content may behave as a group, as group posts, or may not participate in organic groups at all.'),
);
$form['og']['og_group_type'] = array(
'#type' => 'radios',
'#title' => t('Group'),
'#default_value' => og_is_group_type('node', $node_type) ? 'group' : 'omitted',
'#options' => array('omitted' => t('Not a group type'), 'group' => t('Group type')),
);
$form['og']['og_group_post_type'] = array(
'#type' => 'radios',
'#title' => t('Group post'),
'#default_value' => og_is_group_post_type('node', $node_type) ? 'group_post' : 'omitted',
'#options' => array('omitted' => t('Not a group post type'), 'group_post' => t('Group post type')),
);
}
/**
* Implement hook_node_type_delete().
*/
function og_node_type_delete($info) {
variable_del('og_group_type_' . $info->type);
variable_del('og_group_post_type_' . $info->type);
}
/**
* Implement hook_node_type_insert().
*/
function og_node_type_insert($info) {
og_node_type_save($info->type);
}
/**
* Implement hook_node_type_update().
*/
function og_node_type_update($info) {
og_node_type_save($info->type);
}
/**
* Add group and group posts fields to new content types.
*
* @param $bundle_name
* The content type name.
*/
function og_node_type_save($bundle_name) {
if (variable_get('og_group_type_' . $bundle_name, 'omitted') == 'group') {
og_create_fields_group($bundle_name);
// Delete the variable, as we will rely on the presence of th field.
variable_del('og_group_type_' . $bundle_name);
}
if (variable_get('og_group_post_type_' . $bundle_name, 'omitted') == 'group_post') {
og_create_fields_group_posts($bundle_name);
// Delete the variable, as we will rely on the presence of th field.
variable_del('og_group_post_type_' . $bundle_name);
}
}
/**
* Implement hook_node_insert().
*/
function og_node_insert($node) {
if (og_is_group_type('node', $node->type)) {
// Add group.
$group = og_set_group('node', $node->nid);
// Subscribe the node author to the group.
$account = user_load($node->uid);
og_subscribe_user(array(array('value' => $group->gid, 'state' => OG_STATE_ACTIVE)), $account);
og_set_new_group_roles($group->gid);
}
}
/**
* Implement hook_node_update().
*
* In most cases group will be already registered on og_node_insert() however if
* the content already exists, but wasn't defined as a group node, then we must
* make sure it is registered.
*/
function og_node_update($node) {
if (og_is_group_type('node', $node->type) && !og_get_group('node', $node->nid)) {
og_node_insert($node);
}
}
/**
* Implement hook_node_delete().
*/
function og_node_delete($node) {
if ($group = og_get_group('node', $node->nid) && og_is_group_type('node', $node->type)) {
// Remove group.
og_delete_group($group->gid);
}
}
/**
* Implement hook_node_view().
*/
function og_node_view($node, $build_mode) {
global $user;
$obj_type = 'node';
$oid = $node->nid;
if (($group = og_get_group($obj_type, $oid)) && $node->uid != $user->uid) {
// Check user association with the group.
$links = array();
if ($user_group = og_get_group_from_object($group->gid, 'user', $user)) {
// check the user state.
if ($user_group['state'] == OG_STATE_PENDING && og_user_access($group->gid, 'subscribe')) {
$links['og_subscribe']['title'] = t('Re-request group membership');
$links['og_subscribe']['href'] = "og/$obj_type/$oid/subscribe";
}
elseif ($user_group['state'] == OG_STATE_ACTIVE && og_user_access($group->gid, 'unsubscribe')) {
$links['og_subscribe']['title'] = t('Unsubscribe from group');
$links['og_subscribe']['href'] = "og/$obj_type/$oid/unsubscribe";
}
}
else {
if (og_user_access($group->gid, 'subscribe without approval')) {
$links['og_subscribe']['title'] = t('Subscribe to group');
}
elseif (og_user_access($group->gid, 'subscribe')) {
$links['og_subscribe']['title'] = t('Request group membership');
}
$links['og_subscribe']['href'] = "og/$obj_type/$oid/subscribe";
}
if (!empty($links['og_subscribe']['title'])) {
$node->content['links']['og'] = array(
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
}
}
/**
* Implement og_permission
*/
function og_og_permission() {
return array(
'subscribe' => array(
'title' => t('Subscribe user to group'),
'description' => t("Allow user to be a member of a group (approval required)."),
'roles' => array(OG_ANONYMOUS_ROLE),
),
'subscribe without approval' => array(
'title' => t('Subscribe user to group (no approval required).'),
'description' => t("Allow user to be a member of a group without an approval of the group administrator."),
'roles' => array(OG_ANONYMOUS_ROLE),
),
'unsubscribe' => array(
'title' => t('Unsubscribe user from group'),
'description' => t("Allow user to be remove membership from a group."),
'roles' => array(OG_AUTHENTICATED_ROLE),
),
'approve and deny subscription' => array(
'title' => t('Approve and deny subscription'),
'description' => t("Allow user to allow or deny another user's subscription request."),
),
'add user' => array(
'title' => t('Add user'),
'description' => t("Add a new user to the group."),
),
'update group' => array(
'title' => t('Edit group'),
'description' => t('Edit the group content.'),
),
'update own group posts' => array(
'title' => t('Edit own group posts'),
'description' => t('Edit own group posts that belong to the group.'),
),
'update any group posts' => array(
'title' => t('Edit any group posts'),
'description' => t('Edit any group posts that belong to the group.'),
),
'delete own group posts' => array(
'title' => t('Delete own group posts'),
'description' => t('Delete own group posts that belong to the group.'),
),
'delete any group posts' => array(
'title' => t('Delete any group posts'),
'description' => t('Delete any of the group posts that belong to the group.'),
),
'administer group' => array(
'title' => t('Administer group'),
'description' => t('Manage or block users, and manage their role assignments in the group.'),
),
);
}
/**
* Implementation of hook_og_permission_default().
*/
function og_og_permission_default() {
return array(
OG_ANONYMOUS_ROLE => array('subscribe', 'subscribe without approval'),
OG_AUTHENTICATED_ROLE => array('unsubscribe', 'update own group posts', 'delete own group posts'),
OG_ADMINISTRATOR_ROLE => array('approve and deny subscription', 'add user', 'update group', 'update any group posts', 'delete any group posts', 'administer group'),
);
}
/**
* Implement hook_og_access().
*/
function og_og_access($op, $node, $acting_user, $account) {
if (og_is_group_type('node', $node->type)) {
if (og_user_access($node->nid, $op, $acting_user)) {
return OG_ACCESS_ALLOW;
}
}
return OG_ACCESS_IGNORE;
}
/**
* Implement hook_og_node_access()
*/
function og_og_node_access($node, $op, $account) {
if (og_is_group_type('node', $node->type)) {
if (og_user_access($node->nid, $op . ' group', $account)) {
return NODE_ACCESS_ALLOW;
}
}
else {
// We don't have a context, so we need to get all the permissions
// of all the groups. We don't intersect with the user's group, as
// groups might allow anonymous members access.
$gids = array_keys(og_get_object_groups('node', $node));
foreach ($gids as $gid) {
if (og_user_access($gid, $op . ' group posts', $account) || og_user_access($gid, $op . ' any group posts', $account) || (og_user_access($gid, $op . ' own group posts', $account) && $node->uid == $account->uid)) {
return NODE_ACCESS_ALLOW;
}
}
}
}
/**
* Implement hook_node_access()
*/
function og_node_access($node, $op, $account) {
$type = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type);
if ($op != 'create' && (og_is_group_type('node', $type) || og_is_group_post_type('node', $type))) {
$access = module_invoke_all('og_node_access', $node, $op, $account);
if (in_array(OG_ACCESS_DENY, $access, TRUE)) {
return NODE_ACCESS_DENY;
}
elseif (in_array(OG_ACCESS_ALLOW, $access, TRUE)) {
return NODE_ACCESS_ALLOW;
}
}
// Not an organic group node type.
return NODE_ACCESS_IGNORE;
}
/**
* Implement hook_element_info().
*
* Define a group selection form element.
*
* Keyes definition:
* - read_only: Determine if the form element should allow a user input, or only
* show the group audience.
* - opt_group: If TRUE then the group lists will be consisted of "my groups"
* and "other groups". This is set automatically to TRUE, by the processing
* function, for administrator users.
* - opt_group_access_check: If TRUE then the opt_group will be determined
* automatically, otherwise, it will use the opt_group value.
* - user_groups: Array with the group the user is subscribed to keyed by the
* group node ID and the group node title.
* - user_groups: Array with the group the user is not subscribed to keyed by
* the group node ID and the group node title.
* - excludes: Array with the group node IDs that should be excluded from the
* list.
* - includes: Array with group IDs that should be included in the selection.
* Array is keyed with 'content group' and 'other groups'.
* - minimum_for_select_list: The number of groups in the "user_groups" array
* that should be the minimum for showing a select list instead of
* checkboxes or radios.
* - multiple: Determine if multiple groups can be selected, or only a single
* one.
* - uid: The user ID. This can be used in order to created a form element for
* a different user than the acting user.
*/
function og_element_info() {
return array(
'og_audience' => array(
'#read_only' => FALSE,
'#opt_group' => FALSE,
'#opt_group_access_check' => TRUE,
'#user_groups' => array(),
'#other_groups' => array(),
'#excludes' => array(),
'#includes' => array('content groups' => array(), 'other groups' => array()),
'#minimum_for_select_list' => 20,
'#multiple' => TRUE,
'#uid' => NULL,
'#process' => array('og_elements_process'),
),
);
}
/**
* Implement hook_og_types_info().
*/
function og_og_types_info() {
return array(
'group' => array(
'type' => 'group',
'description' => t('Group node'),
),
'group post' => array(
'type' => 'group post',
'description' => t('Group post'),
),
);
}
/**
* Implementation of hook_og_entity_get_label_info().
*/
function og_og_entity_get_label_info() {
return array(
'comment' => array(
'table' => 'comment',
'id' => 'cid',
'label' => 'subject',
'url callback' => 'og_entity_format_url_comment',
),
'node' => array(
'table' => 'node',
'id' => 'nid',
'label' => 'title',
'url' => 'node/@id',
),
'user' => array(
'table' => 'users',
'id' => 'uid',
'label' => 'name',
'url' => 'user/@id',
),
);
}
/*******************************************************************************
* API functions
******************************************************************************/
/**
* Get a group object by the content ID.
*
* @param $oid
* The group content ID.
* @param $obj_type
* The group object type. "node" is the default value. Pass "gid" if the the
* content ID is equal to the group unique ID.
*/
function og_get_group($obj_type, $oid) {
$group = &drupal_static(__FUNCTION__, array());
$result = array();
if (empty($group[$obj_type][$oid])) {
$query = db_select('og_groups', 'og_groups');
$query->fields('og_groups')
->condition('oid', $oid);
if ($obj_type != 'gid') {
// The object type is not a real object - it's actually the group ID.
$query->condition('obj_type', $obj_type);
}
if ($result = $query->execute()->fetchObject()) {
// Unserialize the data.
if (!empty($result->data)) {
$result->data = unserialize($result->data);
}
// Allow other modules to change the group object.
drupal_alter('og_get_group', $result);
}
$group[$obj_type][$oid] = $result;
}
return $group[$obj_type][$oid];
}
/**
* Set a group object.
*
* @param $oid
* The group content ID.
* @param $obj_type
* The group content ID. "node" is the default value.
* @param $data
* Optional; An array with data related to the group.
*
* @return
* The group object populated with the group ID.
*/
function og_set_group($obj_type, $oid, $data = array()) {
$group = new stdClass();
$group->oid = $oid;
$group->obj_type = $obj_type;
$group->data = $data;
// Allow other modules to change the group object.
drupal_alter('og_set_group', $group);
// Delete an existing record.
og_delete_group_by_content($obj_type, $oid);
// Write the new record.
drupal_write_record('og_groups', $group);
return $group;
}
/**
* Delete a group object by it's unique ID.
*
* @param $gid
* The group unique ID.
*/
function og_delete_group($gid) {
db_delete('og_groups')
->condition('gid', $gid)
->execute();
}
/**
* Delete a group object by it's content ID.
*
* @param $oid
* The group content ID.
* @param $obj_type
* The group content ID. "node" is the default value.
*/
function og_delete_group_by_content($obj_type, $oid) {
db_delete('og_groups')
->condition('oid', $oid)
->condition('obj_type', $obj_type)
->execute();
}
/**
* Get all the users belonging to a group.
*
* @param $gid
* The group unique ID.
* @param $count
* @param $cursor
*
* @return
* Array keyed with the object IDs and the value as array with the field
* values.
*/
function og_get_group_users($gid, $states = array(OG_STATE_ACTIVE), $count = FIELD_QUERY_NO_LIMIT, &$cursor = 0) {
$return = array();
$field = field_info_field('og_audience');
$conditions = array(
array('value', $gid),
array('type', 'user'),
);
// Get the objects IDs.
if ($result = field_attach_query($field['id'], $conditions, array('limit' => $count, 'cursor' => $cursor))) {
$uids = array();
foreach ($result['user'] as $key => $value) {
$uids[$key] = $key;
}
// Load the users.
$accounts = user_load_multiple($uids);
// Prepare the return values.
foreach ($accounts as $account) {
$group = (object)og_get_group_from_object($gid, 'user', $account);
// Check if the user's state is valid.
if (!empty($states) && !in_array($group->state, $states)) {
continue;
}
// OG audience may be multiple field, but the group can appear only once,
// so we extract only the value with our requested group.
$return[$account->uid] = $group;
// Add back the user ID and user name.
$return[$account->uid]->uid = $account->uid;
$return[$account->uid]->name = $account->name;
}
}
return $return;
}
/**
* Extract a specific group from an object.
*
* @param $object
* The object.
* @param $gid
* The group unqie ID.
* @return
* Array with the field's value.
*/
function og_get_group_from_object($gid, $obj_type, $object, &$key = NULL) {
$object = og_load_object($obj_type, $object);
if (!empty($object->{OG_AUDIENCE_FIELD}[FIELD_LANGUAGE_NONE])) {
foreach ($object->{OG_AUDIENCE_FIELD}[FIELD_LANGUAGE_NONE] as $key_index => $value) {
if ($value['value'] == $gid) {
// Save the position of the value in the field.
$key = $key_index;
return $value;
}
}
}
}
/**
* Get the groups a content is associated with.
*
* @param $obj_type
* The object type (e.g. "node" or "user").
* @param $object
* The object can be a user, node or any fieldable entity.
* @param $states
* Optioanl; Array with the state to return. If empty all state will return.
* @return
* An array with the group, or an empty array.
*/
function og_get_object_groups($obj_type, $object, $states = array(OG_STATE_ACTIVE)) {
// Load object to get the fields.
$object = og_load_object($obj_type, $object);
$groups = array();
if (!empty($object->{OG_AUDIENCE_FIELD}[FIELD_LANGUAGE_NONE])) {
foreach ($object->{OG_AUDIENCE_FIELD}[FIELD_LANGUAGE_NONE] as $group) {
if (!empty($states) && !in_array($group['state'], $states)) {
// Don't register the group if it's state isn't the one we look for.
continue;
}
$groups[$group['value']] = $group['value'];
}
}
return $groups;
}
/**
* Get an array with all the types of group or group posts available.
*/
function og_get_og_types() {
$og_types = &drupal_static(__FUNCTION__, array());
if (!empty($og_types)) {
return $og_types;
}
$og_types = module_invoke_all('og_types_info');
// Add a non organic groups type.
$og_types['omitted'] = array('type' => 'omitted', 'description' => t('May not be posted into a group.'));
return $og_types;
}
/**
* Return all content types which meet a specified organic group type.
*
* @param $og_types
* Optional; An array with the organic group types, for example "group" or
* "group_post".
* @return
* An array keyed with content type and organic group type as value, or all
* the content types if $type is empty.
*/
function og_get_node_type_by_og_type($og_types = array()) {
$return = array();
if (empty($og_types)) {
// All organic groups types.
$og_types = array_keys(og_get_og_types());
}
foreach (node_type_get_types() as $type => $value) {
$usage = og_get_group_type($type);
if (in_array($usage, $og_types)) {
$return[$type] = $usage;
}
}
return $return;
}
/**
* Return the group type (i.e. "group" or "group_post") of an object.
*
* @param $bundle_name
* The bundle name to be checked.
* @param $obj_type
* The object type.
* @param $type
* The grou usage type. Must be "group" or "group post".
*
* @return
* The group type or an "omitted" if node type doesn't participate in
* organic groups.
*/
function og_get_group_type($obj_type, $bundle_name, $type = 'group') {
if ($type == 'group') {
return (bool)field_info_instance($obj_type, 'og_group', $bundle_name);
}
elseif ($type == 'group post') {
return (bool)field_info_instance($obj_type, 'og_audience', $bundle_name);
}
}
/**
* Return TRUE if the node type is a "group" type.
*
* This is a wrapper function around og_get_group_type().
*
* @param $node_type
* The node type to be checked.
*/
function og_is_group_type($obj_type, $bundle_name) {
return og_get_group_type($obj_type, $bundle_name);
}
/**
* Return TRUE if the node type is a "group post" type.
*
* This is a wrapper function around og_get_group_type().
*
* @param $node_type
* The node type to be checked.
*/
function og_is_group_post_type($obj_type, $bundle_name) {
return og_get_group_type($obj_type, $bundle_name, 'group post');
}
/**
* Subscribe a user to groups.
*
* @param $groups
* Array of the groups to subscribe the user.
* @param $account
* Optional; A user object.
* @param $replace
* Optioanl; True if the group lists should replace the existing subscrptions
* of the user.
* @param $request
* Optioanl; A message created by the requesting user that should be sent to
* the group admins.
*/
function og_subscribe_user($groups = array(), $account = NULL, $replace = FALSE, $request = '') {
if (empty($account)) {
global $user;
$account = $user;
}
og_set_association('user', $account, $groups, $replace, $request);
}
/**
* Unsubscribe a user from groups.
*
* @param $gids
* Array of the groups to unsubscribe the user.
* @param $account
* Optional; A user object.
*/
function og_unsubscribe_user($gids = array(), $account = NULL) {
if (empty($account)) {
global $user;
$account = $user;
}
og_delete_association('user', $account, $gids);
// Delete any roles the user had.
foreach ($gids as $gid) {
foreach (og_get_user_roles($gid, $account->uid) as $rid) {