forked from Gizra/og
-
Notifications
You must be signed in to change notification settings - Fork 0
/
og.module
executable file
·3757 lines (3343 loc) · 111 KB
/
og.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
* Enable users to create and manage groups with roles and permissions.
*/
// Add field widget related code.
require DRUPAL_ROOT . '/' . drupal_get_path('module', 'og') . '/includes/og.field.inc';
/**
* Define active group content states.
*
* When a user has this membership state they are considered to be of
* "member" role.
*/
define('OG_STATE_ACTIVE', 1);
/**
* Define pending group content states. The user is subscribed to the group
* but isn't an active member yet.
*
* When a user has this membership state they are considered to be of
* "non-member" role.
*/
define('OG_STATE_PENDING', 2);
/**
* Define blocked group content states. The user is rejected from the group.
*
* When a user has this membership state they are denided access to any
* group related action. This state, however, does not prevent user to
* access a group or group content node.
*/
define('OG_STATE_BLOCKED', 3);
/**
* Group audience field.
*/
define('OG_AUDIENCE_FIELD', 'og_group_ref');
/**
* Group field.
*/
define('OG_GROUP_FIELD', 'group_group');
/**
* Group default roles and permissions field.
*/
define('OG_DEFAULT_ACCESS_FIELD', 'og_roles_permissions');
/**
* The role name of group non-members.
*/
define('OG_ANONYMOUS_ROLE', 'non-member');
/**
* The role name of group member.
*/
define('OG_AUTHENTICATED_ROLE', 'member');
/**
* The role name of group administrator.
*/
define('OG_ADMINISTRATOR_ROLE', 'administrator member');
/**
* The default group membership type that is the bundle of group membership.
*/
define('OG_MEMBERSHIP_TYPE_DEFAULT', 'og_membership_type_default');
/**
* The name of the user's request field in the default group membership type.
*/
define('OG_MEMBERSHIP_REQUEST_FIELD', 'og_membership_request');
/**
* Implements hook_help().
*/
function og_help($path, $arg) {
switch ($path) {
case 'admin/help#og':
$path = drupal_get_path('module', 'og');
$output = '<p>' . t("Read the <a href='@url'>README.txt</a> file in the Organic groups module directory.", array('@url' => "/$path/README.txt")) . '</p>';
$output .= '<p>' . t("Information about Organic Groups can also be found on the module's<a href='@og'>documentation page</a>.", array('@og' => 'http://drupal.org/documentation/modules/og')) . '</p>';
return $output;
}
}
/**
* Implements hook_menu().
*/
function og_menu() {
$items = array();
// Add our own autocomplete callback to pass also the group and
// vocabulary info.
$items['og/autocomplete/single/%/%/%/%'] = array(
'title' => 'Entity Reference Autocomplete',
'page callback' => 'og_entityreference_autocomplete_callback',
'page arguments' => array(2, 3, 4, 5, 6),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['og/autocomplete/tags/%/%/%/%'] = array(
'title' => 'Entity Reference Autocomplete',
'page callback' => 'og_entityreference_autocomplete_callback',
'page arguments' => array(2, 3, 4, 5, 6),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_entity_info().
*/
function og_entity_info() {
$items['og_membership_type'] = array(
'label' => t('OG membership type'),
'controller class' => 'EntityAPIControllerExportable',
'entity class' => 'OgMembershipType',
'base table' => 'og_membership_type',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'label' => 'description',
'name' => 'name',
),
'exportable' => TRUE,
'export' => array(
'default hook' => 'default_og_membership_type',
),
'bundle of' => 'og_membership',
'module' => 'og',
'metadata controller class' => 'EntityDefaultMetadataController',
'views controller class' => 'EntityDefaultViewsController',
'access callback' => 'og_membership_type_access',
'entity cache' => module_exists('entitycache'),
);
if (class_exists('OgMembershipTypeUIController')) {
$items['og_membership_type'] += array(
// Enable the entity API's admin UI.
'admin ui' => array(
// TODO: This path doesn't exist before OG-ui.
'path' => 'admin/config/group/group-membership',
'file' => 'includes/og.admin.inc',
'controller class' => 'OgMembershipTypeUIController',
),
);
}
$items['og_membership'] = array(
'label' => t('OG membership'),
'entity class' => 'OgMembership',
'controller class' => 'EntityAPIController',
'base table' => 'og_membership',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
// The message has no label.
'label' => FALSE,
'bundle' => 'type',
),
'label callback' => 'og_membership_label',
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'name',
),
'module' => 'og',
'metadata controller class' => 'OgMembershipMetadataController',
'views controller class' => 'OgMembershipViewsController',
'access callback' => 'og_membership_access',
'entity cache' => module_exists('entitycache'),
);
// Add bundle info but bypass entity_load() as we cannot use it here.
if (db_table_exists('og_membership_type')) {
$memberships = db_select('og_membership_type', 'g')
->fields('g')
->execute()
->fetchAllAssoc('name');
foreach ($memberships as $type_name => $type) {
$items['og_membership']['bundles'][$type_name] = array(
'label' => $type->name,
'admin' => array(
'path' => 'admin/config/group/group-membership/manage/%og_membership_type',
'real path' => 'admin/config/group/group-membership/manage/' . $type->name,
'bundle argument' => 5,
'access arguments' => array('administer group'),
),
);
}
}
return $items;
}
/**
* Implements hook_entity_property_info().
*/
function og_entity_property_info() {
$info = array();
// Add OG membership metadata for every bundle that is a group content.
foreach (og_get_all_group_content_bundle() as $entity_type => $bundles) {
foreach ($bundles as $bundle => $bundle_value) {
$info[$entity_type]['bundles'][$bundle]['properties']['og_membership'] = array(
'label' => t("OG memberships"),
'type' => 'list<og_membership>',
'description' => t("A list of all OG memberships of the @name entity.", array('@name' => $entity_type)),
'getter callback' => 'og_get_og_membership_properties',
);
// Add per-state properties.
$general = $info[$entity_type]['bundles'][$bundle]['properties']['og_membership'];
foreach (og_group_content_states() as $state => $state_label) {
$params = array('@state' => $state_label, '@name' => $entity_type);
$info[$entity_type]['bundles'][$bundle]['properties']['og_membership__' . $state] = $general;
$info[$entity_type]['bundles'][$bundle]['properties']['og_membership__' . $state]['label'] = t('@state OG membership', $params);
$info[$entity_type]['bundles'][$bundle]['properties']['og_membership__' . $state]['description'] = t("A list of all OG memberships of the @name entity with @state state.", $params);
}
// Add OG membership per field in a bundle.
foreach (og_get_group_audience_fields($entity_type, $bundle) as $field_name => $label) {
$params = array('@label' => $label);
$field_info = field_info_field($field_name);
$group_type = $field_info['settings']['target_type'];
$info[$entity_type]['bundles'][$bundle]['properties'][$field_name . '__og_membership'] = array(
'label' => t('OG membership from field @label', $params),
'type' => 'list<og_membership>',
// The bundle in this context means the OG membership type.
'bundle' => $field_info['settings']['handler_settings']['membership_type'],
'description' => t('A list of all OG memberships registered in field @label.', $params),
'getter callback' => 'og_get_field_og_membership_properties',
);
// Add per-state properties.
$general = $info[$entity_type]['bundles'][$bundle]['properties'][$field_name . '__og_membership'];
foreach (og_group_content_states() as $state => $state_label) {
$params = array(
'@label' => $label,
'@label' => $label,
'@state' => $state_label,
);
$info[$entity_type]['bundles'][$bundle]['properties'][$field_name . '__og_membership__' . $state] = $general;
$info[$entity_type]['bundles'][$bundle]['properties'][$field_name . '__og_membership__' . $state]['label'] = t('@state OG memberships from field @label', $params);
$info[$entity_type]['bundles'][$bundle]['properties'][$field_name . '__og_membership__' . $state]['description'] = t('A list of all OG memberships with @state registered in field @label.', $params);
}
}
}
}
foreach (og_get_all_group_bundle() as $entity_type => $bundles) {
foreach ($bundles as $bundle => $bundle_value) {
$info[$entity_type]['bundles'][$bundle]['properties']['members'] = array(
'label' => t("Group members"),
'type' => 'list<user>',
'description' => t("A list group members of the @name entity.", array('@name' => $entity_type)),
'getter callback' => 'og_get_group_members_properties',
);
// Add per-state properties.
$general = $info[$entity_type]['bundles'][$bundle]['properties']['members'];
foreach (og_group_content_states() as $state => $state_label) {
$params = array('@state' => $state_label, '@name' => $entity_type);
$info[$entity_type]['bundles'][$bundle]['properties']['members__' . $state] = $general;
$info[$entity_type]['bundles'][$bundle]['properties']['members__' . $state]['label'] = t('@state group members', $params);
$info[$entity_type]['bundles'][$bundle]['properties']['members__' . $state]['description'] = t("A list of all users of the @name entity with @state state.", $params);
}
}
}
return $info;
}
/**
* Property getter callback for group members.
*
* @see og_entity_property_info()
*/
function og_get_group_members_properties($entity, array $options, $name, $type) {
$args = explode('__', $name);
$state = !empty($args[1]) ? $args[1] : FALSE;
list($id) = entity_extract_ids($type, $entity);
$cache = &drupal_static(__FUNCTION__, array());
if (isset($cache[$type][$id][$state])) {
// Return the cached result.
return $cache[$type][$id][$state];
}
$cache[$type][$id][$state] = array();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'og_membership')
->propertyCondition('group_type', $type, '=')
->propertyCondition('gid', $id, '=')
->propertyCondition('entity_type', 'user', '=');
if ($state) {
$query->propertyCondition('state', $state, '=');
}
$result = $query->execute();
if (!empty($result['og_membership'])) {
$og_memberships = og_membership_load_multiple(array_keys($result['og_membership']));
foreach ($og_memberships as $og_membership) {
$cache[$type][$id][$state][] = $og_membership->etid;
}
}
return $cache[$type][$id][$state];
}
/**
* Property getter callback for OG membership.
*
* @see og_entity_property_info()
*/
function og_get_og_membership_properties($entity, array $options, $name, $type) {
// Get the state from name, if exists.
if ($name == 'og_membership') {
$state = array();
}
else {
$args = explode('__', $name);
$state = array($args[1]);
}
$ids = array();
if ($gids = og_get_entity_groups($type, $entity, $state)) {
$ids = array();
foreach ($gids as $group_type => $values) {
$ids = array_merge($ids, array_keys($values));
}
}
return $ids;
}
/**
* Property getter callback for OG membership per field.
*
* @see og_entity_property_info()
*/
function og_get_field_og_membership_properties($entity, array $options, $name, $type) {
$args = explode('__', $name);
// Field name might have double underscore as-well, so we need to make
// sure we get it right.
$last_char = substr($name, -1);
$state = is_numeric($last_char) ? $last_char : FALSE;
// The number of characters to ignore in the name (i.e. remove the
// "__og_membership" or "__og_membership__0").
$remove_char = $state ? -18 : -15;
$field_name = substr($name, 0, $remove_char);
$field_name = $args[0];
$field = field_info_field($field_name);
$states = count($args) == 2 ? FALSE : array($args[2]);
$result = og_get_entity_groups($type, $entity, $states, $field_name);
$target_type = $field['settings']['target_type'];
return !empty($result[$target_type]) ? array_keys($result[$target_type]) : array();
}
/**
* Getter callback to load the 'entity' or 'group' property from OG membership.
*
* We have to return the entity wrapped.
*/
function og_entity_getter($object, array $options, $property_name) {
// Check that property name contains double underscores, ignoring ones that
// start with a double underscore.
if (strpos($property_name, '__')) {
list($real_property, $sub_property) = explode('__', $property_name, 2);
}
else {
$real_property = $property_name;
}
switch ($real_property) {
case 'entity':
if (empty($sub_property) || $sub_property == $object->entity_type) {
return entity_metadata_wrapper($object->entity_type, $object->etid);
}
break;
case 'group':
if (empty($sub_property) || $sub_property == $object->group_type) {
return entity_metadata_wrapper($object->group_type, $object->gid);
}
break;
}
}
/**
* Entity property info setter callback to set the "entity" property for groups
* and memberships.
*
* As the property is of type entity, the value will be passed as a wrapped
* entity.
*/
function og_entity_setter($object, $property_name, $wrapper) {
// Check that property name contains double underscores, ignoring ones that
// start with a double underscore.
if (strpos($property_name, '__')) {
list($real_property, $sub_property) = explode('__', $property_name, 2);
}
else {
$real_property = $property_name;
}
switch ($real_property) {
case 'entity':
$object->entity_type = $wrapper->type();
$object->etid = $wrapper->getIdentifier();
break;
case 'group':
$object->group_type = $wrapper->type();
$object->gid = $wrapper->getIdentifier();
break;
}
}
/**
* Implements hook_default_og_membership_type().
*/
function og_default_og_membership_type() {
$items = array();
$items['og_membership_type_default'] = entity_import('og_membership_type', '{
"name" : "og_membership_type_default",
"description" : "Default",
"rdf_mapping" : []
}');
return $items;
}
/**
* Implements hook_modules_uninstalled().
*/
function og_modules_uninstalled($modules) {
// Delete module's permissions.
og_permissions_delete_by_module($modules);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function og_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools') {
return 'plugins/' . $plugin;
}
elseif ($module == 'entityreference') {
return "plugins/entityreference/$plugin";
}
}
/**
* Implements hook_permission().
*/
function og_permission() {
return array(
'administer group' => array(
'title' => t('Administer Organic groups permissions'),
'description' => t('Administer all groups and permissions.'),
),
);
}
/**
* Implements hook_og_permission().
*/
function og_og_permission() {
// Generate standard node permissions for all applicable node types.
$perms = array();
$perms['update group'] = array(
'title' => t('Edit group'),
'description' => t('Edit the group. Note: This permission controls only node entity type groups.'),
'default role' => array(OG_ADMINISTRATOR_ROLE),
);
$perms['administer group'] = array(
'title' => t('Administer group'),
'description' => t('Manage group members and content in the group.'),
'default role' => array(OG_ADMINISTRATOR_ROLE),
'restrict access' => TRUE,
);
foreach (node_permissions_get_configured_types() as $type) {
$perms = array_merge($perms, og_list_permissions($type));
}
return $perms;
}
/**
* Implements hook_og_default_roles().
*/
function og_og_default_roles() {
return array(OG_ADMINISTRATOR_ROLE);
}
/**
* Implements 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_content_type('node', $type)) {
// Save some legwork if the user has the core permission and strict node
// access is not set.
if (!variable_get('og_node_access_strict', TRUE) && user_access("create $type content", $account)) {
// We just ignore: core access will take care of it.
return NODE_ACCESS_IGNORE;
}
if (user_access('administer group', $account)) {
return NODE_ACCESS_ALLOW;
}
// We can't check if user has create permissions using og_user_access(), as
// there is no group context. However, we can check if there are any groups
// the user will be able to select, and if not, we don't allow access.
// @see OgSelectionHandler::getReferencableEntities()
$required = FALSE;
foreach (og_get_group_audience_fields('node', $type) as $field_name => $label) {
$field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, $type);
// Set the "field mode" to default, before passing it to the
// selection handler.
$instance['field_mode'] = 'default';
if (entityreference_get_selection_handler($field, $instance)->countReferencableEntities()) {
return NODE_ACCESS_ALLOW;
}
// Allow users to create content outside of groups, if none of the
// audience fields is required.
if ($instance['required']) {
$required = TRUE;
}
}
// If no group audience field is required, we ignore.
if (!$required) {
return NODE_ACCESS_IGNORE;
}
// Otherwise, ignore or deny based on whether strict node access is set.
return variable_get('og_node_access_strict', TRUE) ? NODE_ACCESS_DENY : NODE_ACCESS_IGNORE;
}
elseif (in_array($op, array('update', 'delete'))) {
$access = og_user_access_entity('administer group', 'node', $node, $account);
if (is_null($access)) {
// The node isn't in an OG context, so no need to keep testing.
return NODE_ACCESS_IGNORE;
}
else {
$access = $access ||
// Any content.
og_user_access_entity("$op any $type content", 'node', $node, $account) ||
// Own content.
($account->uid == $node->uid && og_user_access_entity("$op own $type content", 'node', $node, $account));
}
if (!$access && $op == 'update' && og_is_group('node', $node)) {
// The node is a group, so check "update group" permission.
$access = og_user_access_entity('update group', 'node', $node, $account);
}
if ($access) {
return NODE_ACCESS_ALLOW;
}
// Check if OG should explicitly deny access or not.
return variable_get('og_node_access_strict', TRUE) ? NODE_ACCESS_DENY : NODE_ACCESS_IGNORE;
}
return NODE_ACCESS_IGNORE;
}
/**
* Implements hook_field_access().
*
* Hide group-audience fields from user's edit profile for non-privileged users.
*/
function og_field_access($op, $field, $entity_type, $entity, $account) {
global $user;
if (empty($entity)) {
// We are in field settings page.
return;
}
if (!$user->uid) {
// User is anonymous, and user register might try to add the
// group-audience field.
return;
}
if ($op != 'edit') {
return;
}
$field_name = $field['field_name'];
list($id, $vid, $bundle_name) = entity_extract_ids($entity_type, $entity);
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
if ($field_name == OG_GROUP_FIELD) {
$wrapper = entity_metadata_wrapper($entity_type, $entity);
if ($wrapper->getIdentifier() && !$wrapper->{OG_GROUP_FIELD}->value()) {
// Entity isn't an active group.
return;
}
if (!empty($instance['widget']['settings']['og_hide'])) {
return FALSE;
}
return;
}
if (!og_is_group_audience_field($field_name)) {
return;
}
$field = field_info_field($field_name);
$settings = $field['settings']['handler_settings'];
// Check if we are editing the user entity.
if ($entity_type == 'user') {
if (!empty($instance['settings']['behaviors']['og_widget']['access_override'])) {
return;
}
return user_access('administer group', $account);
}
}
/**
* Implements hook_views_api().
*/
function og_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'og') . '/includes/views',
);
}
/**
* Implements hook_field_create_instance().
*
* - Create default OG roles per entity-type and bundle.
* - Create a group audience field on the user's entity, referencing the first
* group defined.
*/
function og_field_create_instance($instance) {
if ($instance['field_name'] != OG_GROUP_FIELD) {
return;
}
// Create default roles per entity-type per bundle.
og_roles_override($instance['entity_type'], $instance['bundle'], 0);
// Check if we need to add a group audience on the user's entity.
// We add a different field, so each field can be set differently.
$entity_type = $instance['entity_type'];
$bundle = $instance['bundle'];
foreach (array_keys(og_get_group_audience_fields()) as $field_name) {
$field = field_info_field($field_name);
if ($field['settings']['target_type'] == $entity_type && empty($field['settings']['handler_settings']['target_bundles'])) {
return;
}
if ($field['settings']['target_type'] == $entity_type && in_array($bundle, $field['settings']['handler_settings']['target_bundles'])) {
return;
}
}
// If we reached here, it means we need to create a field.
// Pick an unused name.
$field_name = substr("og_user_$entity_type", 0, 32);
$i = 1;
while (field_info_field($field_name)) {
$field_name = substr("og_user_$entity_type", 0, 32 - strlen($i)) . $i;
++$i;
}
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = $entity_type;
$og_field['instance']['label'] = t('Group membership');
// If the user entity type has multiple bundles, make sure to attach a field
// instance to all of them.
$entity_info = entity_get_info('user');
foreach (array_keys($entity_info['bundles']) as $user_bundle) {
og_create_field($field_name, 'user', $user_bundle, $og_field);
}
}
/**
* Implements field_delete_instance().
*
* - Invalidate OG's static cache if a group-audience field is deleted.
* - Delete the default OG roles per entity-type and bundle.
*/
function og_field_delete_instance($instance) {
if (og_is_group_audience_field($instance['field_name'])) {
og_invalidate_cache();
}
if ($instance['field_name'] != OG_GROUP_FIELD) {
return;
}
// Get the per-bundle roles.
$roles = og_roles($instance['entity_type'], $instance['bundle']);
foreach ($roles as $rid => $name) {
og_role_delete($rid);
}
}
/**
* Implements hook_field_attach_form().
*/
function og_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
list(,, $bundle) = entity_extract_ids($entity_type, $entity);
if (og_get_group_audience_fields($entity_type, $bundle)) {
$form['#validate'][] = 'og_form_group_reference_validate';
}
if ($entity_type == 'user' || !og_is_group_type($entity_type, $bundle)) {
return;
}
$form['#validate'][] = 'og_form_group_manager_validate';
}
/**
* Validate handler; Make sure group-only content permissions are honored.
*
* If a user does not have site-wide node permissions, throw an error if they
* try to post site-wide instead of within a group.
*
* Note: This function does not check group -access- just if a group has been
* Selected.
*/
function og_form_group_reference_validate($form, &$form_state) {
global $user;
$entity_type = $form['#entity_type'];
if (empty($form_state[$entity_type])) {
// We are inside field settings page.
return;
}
$account = user_load($user->uid);
$bundle = $form['#bundle'];
$entity = isset($form["#$entity_type"]) ? $form["#$entity_type"] : $form['#entity'];
list($id) = entity_extract_ids($entity_type, $entity);
$op = empty($id) ? 'create' : 'update';
if ($entity_type == 'node') {
$node = empty($id) ? $bundle : $entity;
// We call node_node_access() directly as we just want to check the
// permissions using user_acces().
if (node_node_access($node, $op, $account)) {
// User has site-wide permissions to create or edit the node.
return;
}
}
elseif (entity_access($op, $entity_type, $entity, $account)) {
// User has site-wide permissions to create or edit the entity.
return;
}
foreach (array_keys(og_get_group_audience_fields($entity_type, $bundle)) as $field_name) {
// If there is at least one group selected, return.
if (!empty($form_state['values'][$field_name][LANGUAGE_NONE])) {
return;
}
}
// No group selected, throw an error.
form_set_error('og', t('You must select one or more groups for this content.'));
}
/**
* Validate handler; Make sure a group can be created.
*
* We check if the group manager has a matching group-audience field for the
* OG membership to be created in.
*/
function og_form_group_manager_validate($form, &$form_state) {
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
if (empty($form_state[$entity_type])) {
// We are inside field settings page.
return;
}
$entity = $form_state[$entity_type];
$langcode = $form_state['values']['language'];
if (!isset($form_state['values']['uid']) || !isset($entity->uid)) {
// There is no user ID property on the entity.
return;
}
if (isset($form_state['values'][OG_GROUP_FIELD]) && empty($form_state['values'][OG_GROUP_FIELD][$langcode][0]['value'])) {
// Not a group.
return;
}
if (!isset($form_state['values'][OG_GROUP_FIELD])) {
// Field doesn't appear in the form, so it is probably hidden by
// hook_field_access(). So check the default value of the field.
$field = field_info_field(OG_GROUP_FIELD);
$instance = field_info_instance($entity_type, OG_GROUP_FIELD, $bundle);
$items = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
if (empty($items[0]['value'])) {
// Default value is not a group.
return;
}
}
if ($entity_type == 'node') {
// A user might assign the node author by entering a user name in the
// node form, which we then need to translate to a user ID.
// However, this happens later on, in node_submit(), so we do a special
// check for the node entity.
if (!$account = user_load_by_name($form_state['values']['name'])) {
// Invalid username.
return;
}
}
else {
$account = user_load($form_state['values']['uid']);
}
list($id) = entity_extract_ids($entity_type, $entity);
if ($id && $entity->uid == $account->uid) {
// The entity's user ID hasn't changed.
return;
}
if ($access = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) {
// Matching group audience field found.
return;
}
form_error($form, t("Can't save entity as group, because user @name can't be subscribed to group and become a manager.", array('@name' => format_username($account))));
}
/**
* Implements hook_entity_insert().
*/
function og_entity_insert($entity, $entity_type) {
if (!og_is_group($entity_type, $entity)) {
return;
}
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
if (!empty($entity->uid)) {
// Subscribe the group manager.
og_group($entity_type, $id, array('entity' => $entity->uid));
// Assign roles to group manager.
$name = 'og_group_manager_default_rids_' . $entity_type . '_' . $bundle;
if ($rids = variable_get($name)) {
foreach ($rids as $rid) {
og_role_grant($entity_type, $id, $entity->uid, $rid);
}
}
}
if (!og_is_group_default_access($entity_type, $entity)) {
// Override default roles.
og_roles_override($entity_type, $bundle, $id);
}
}
/**
* Implements hook_entity_update().
*/
function og_entity_update($entity, $entity_type) {
if (!og_is_group($entity_type, $entity)) {
return;
}
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (!empty($entity->uid) && !og_is_member($entity_type, $entity, 'user', $entity->uid)) {
// Subscribe the group manager, in case the owner changed.
og_group($entity_type, $id, array('entity' => $entity->uid));
// Assign roles to group manager.
$name = 'og_group_manager_default_rids_' . $entity_type . '_' . $bundle;
if ($rids = variable_get($name)) {
foreach ($rids as $rid) {
og_role_grant($entity_type, $id, $entity->uid, $rid);
}
}
}
$original_entity = $entity->original;
$property = OG_DEFAULT_ACCESS_FIELD;
if (!empty($entity->{$property}) && $entity->{$property} != $original_entity->{$property}) {
if (!og_is_group_default_access($entity_type, $entity)) {
// Override default roles.
og_roles_override($entity_type, $bundle, $id);
}
else {
// Delete overridden roles.
og_delete_user_roles_by_group($entity_type, $entity);
}
}
}
/**
* Implements hook_field_attach_insert().
*/
function og_field_attach_insert($entity_type, $entity) {
_og_update_entity_fields($entity_type, $entity);
}
/**
* Implements hook_field_attach_update().
*/
function og_field_attach_update($entity_type, $entity) {
_og_update_entity_fields($entity_type, $entity);
}
/**
* Update the field values in the entity, to reflect the membership.
*
* This is used to allow other modules that save a new/ existing entity
* to act on the field values, even before hook_field_load() is called.
*/
function _og_update_entity_fields($entity_type, $entity) {
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
if (!og_is_group_content_type($entity_type, $bundle)) {
return;
}
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach (og_get_group_audience_fields($entity_type, $bundle) as $field_name => $label) {
$field = field_info_field($field_name);
$gids = array();
if ($field['cardinality'] == 1) {
if ($og_membership = $wrapper->{$field_name . '__og_membership'}->value()) {
// Wrapper return an array.
$gids = $og_membership[0]->gid;
}
}
else {
$target_type = $field['settings']['target_type'];
$gids = og_get_entity_groups($entity_type, $entity, array(), $field_name);
$gids = !empty($gids[$target_type]) ? array_values($gids[$target_type]) : array();
}
if ($gids) {
$wrapper->{$field_name}->set($gids);
}
}
}
/**
* Implements hook_entity_delete().
*/
function og_entity_delete($entity, $entity_type) {
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (og_is_group($entity_type, $entity)) {
og_delete_user_roles_by_group($entity_type, $entity);
og_membership_delete_by_group($entity_type, $entity);
}
if (og_is_group_content_type($entity_type, $bundle)) {
// As the field attachers are called after hook_entity_presave() we
// can't delete the OG memberships here. So we just mark the entity
// as being deleted, and we will do the actual delete in
// OgBehaviorHandler::delete().
$entity->delete_og_membership = TRUE;
}
}
/**
* Implements hook_og_membership_insert().
*/
function og_og_membership_insert($og_membership) {
if ($og_membership->entity_type == 'user' && module_exists('rules')) {
rules_invoke_event('og_user_insert', $og_membership, entity_metadata_wrapper('user', $og_membership->etid));
}
}
/**
* Implements hook_og_membership_update().
*/
function og_og_membership_update($og_membership) {
if ($og_membership->entity_type == 'user' && module_exists('rules')) {