events.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. function au_subgroups_add_parent($event, $type, $object) {
  3. // if we have an input, then we're setting the parent
  4. $parent_guid = get_input('au_subgroups_parent_guid', false);
  5. if ($parent_guid !== false) {
  6. au_subgroups_set_parent_group($object->guid, $parent_guid);
  7. }
  8. $parent = get_entity($parent_guid);
  9. // a few things that can stop subgroup creation
  10. // - no subgroups allowed
  11. // - not an admin/group-admin and members disallowed
  12. if (elgg_instanceof($parent, 'group')) {
  13. if ($parent->subgroups_enable == 'no') {
  14. return FALSE;
  15. }
  16. if ($parent->subgroups_members_create_enable == 'no') {
  17. // only group admins can create subgroups
  18. if (!$parent->canEdit()) {
  19. return FALSE;
  20. }
  21. }
  22. }
  23. }
  24. function au_subgroups_clone_layout_on_create($event, $type, $object) {
  25. if (elgg_is_active_plugin('group_custom_layout')) {
  26. $parent = au_subgroups_get_parent_group($object);
  27. if ($parent) {
  28. au_subgroups_clone_layout($object, $parent);
  29. }
  30. }
  31. }
  32. /**
  33. * when groups are created/updated, make sure subgroups have
  34. * access only by parent group acl
  35. */
  36. function au_subgroups_group_visibility($event, $type, $object) {
  37. $parent = au_subgroups_get_parent_group($object);
  38. // make sure the visibility is what was set on the form
  39. $vis = get_input('vis', false);
  40. if ($vis !== false) { // this makes sure we only update access when it's done via form
  41. switch ($vis) {
  42. case 'parent_group_acl':
  43. $access_id = $parent->group_acl;
  44. break;
  45. case ACCESS_PRIVATE:
  46. $access_id = $object->group_acl;
  47. break;
  48. default:
  49. $access_id = $vis;
  50. break;
  51. }
  52. /*
  53. * Here we have some trickiness, because save is called twice with the visibility being
  54. * reset the second time. So we have to make sure we're only updating the visibility
  55. * of the original (not a subgroup or parent) on subsequent calls.
  56. *
  57. * To do this we're setting a temporary config variable to say that yes, we've been here once
  58. * and pass the guid of the group we're concerned with in another config variable.
  59. * That way we know only to update the vis of the matching guid
  60. */
  61. if (!elgg_get_config('au_subgroups_visupdate')) {
  62. // this is the first pass, lets mark it and save the guid of the group we care about
  63. elgg_set_config('au_subgroups_visupdate', true);
  64. elgg_set_config('au_subgroups_vis_guid', $object->guid);
  65. }
  66. if (elgg_get_config('au_subgroups_vis_guid') == $object->guid) {
  67. // we need to update it - first in memory, then in the db
  68. $object->access_id = $access_id;
  69. $q = "UPDATE " . elgg_get_config('dbprefix') . "entities SET access_id = {$access_id} WHERE guid = {$object->guid}";
  70. update_data($q);
  71. // make sure our metadata follows suit
  72. metadata_update('update', 'group', $object);
  73. }
  74. // if this group has subgroups, and we're making the visibility more restrictive
  75. // we need to check the subgroups to make sure they're not more visible than this group
  76. set_time_limit(0); // this is recursive and could take a while
  77. $children = au_subgroups_get_subgroups($object, 0);
  78. if ($children) {
  79. foreach($children as $child) {
  80. switch ($access_id) {
  81. case ACCESS_PUBLIC:
  82. // do nothing, most permissive access
  83. break;
  84. case ACCESS_LOGGED_IN:
  85. // if child access is public, bump it up
  86. if ($child->access_id == ACCESS_PUBLIC) {
  87. $child->access_id = ACCESS_LOGGED_IN;
  88. $child->save();
  89. }
  90. break;
  91. default:
  92. // two options here, group->group_acl = hidden
  93. // or parent->group_acl = visible to parent group members
  94. // if the child is more permissive than the parent, we're changing the child to
  95. // the next level up - in this case, visible to parent group
  96. if (!in_array($child->access_id, array($child->group_acl, $object->group_acl))) {
  97. $child->access_id = $object->group_acl;
  98. $child->save();
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * Prevents users from joining a subgroup if they're not a member of the parent
  108. *
  109. * @param type $event
  110. * @param type $type
  111. * @param ElggRelationship $object
  112. * @return boolean
  113. */
  114. function au_subgroups_join_group($event, $type, $object) {
  115. if ($object instanceof ElggRelationship) {
  116. $user = get_entity($object->guid_one);
  117. $group = get_entity($object->guid_two);
  118. $parent = au_subgroups_get_parent_group($group);
  119. // use temp global config to decide if we should prevent joining
  120. // prevent joining if not a member of the parent group
  121. // except during a subgroup move invitation
  122. $au_subgroups_ignore_join = elgg_get_config('au_subgroups_ignore_join');
  123. if ($parent && !$au_subgroups_ignore_join) {
  124. // cover the case of moved subgroups
  125. // user will have been invited, and have a plugin setting saying which other groups to join
  126. $invited = check_entity_relationship($group->guid, 'invited', $user->guid);
  127. $children_to_join = elgg_get_plugin_user_setting('invitation_' . $group->guid, $user->guid, 'au_subgroups');
  128. if (!empty($children_to_join)) {
  129. $children_to_join = unserialize($children_to_join);
  130. }
  131. if ($invited) {
  132. elgg_set_config('au_subgroups_ignore_join', true);
  133. // we have been invited in through the back door by a subgroup move
  134. // join this user to all parent groups fo this group
  135. if (au_subgroups_join_parents_recursive($group, $user)) {
  136. // we're in, now lets rejoin the children
  137. if (is_array($children_to_join)) {
  138. $children_guids = au_subgroups_get_all_children_guids($group);
  139. foreach ($children_to_join as $child) {
  140. if (in_array($child, $children_guids)) {
  141. $child_group = get_entity($child);
  142. $child_group->join($user);
  143. }
  144. }
  145. }
  146. // delete plugin setting
  147. elgg_set_plugin_user_setting('invitation_' . $group->guid, '', $user->guid, 'au_subgroups');
  148. }
  149. else {
  150. // something went wrong with joining the groups
  151. // lets stop everything now
  152. return false;
  153. }
  154. }
  155. elseif (!$parent->isMember($user)) {
  156. register_error(elgg_echo('au_subgroups:error:notparentmember'));
  157. return false;
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * When leaving a group, make sure users are removed from any subgroups
  164. *
  165. * @param type $event
  166. * @param type $type
  167. * @param type $object
  168. */
  169. function au_subgroups_leave_group($event, $type, $params) {
  170. $guids = au_subgroups_get_all_children_guids($params['group']);
  171. foreach ($guids as $guid) {
  172. leave_group($guid, $params['user']->guid);
  173. }
  174. }
  175. function au_subgroups_pagesetup() {
  176. if (in_array(elgg_get_context(), array('au_subgroups', 'group_profile'))) {
  177. $group = elgg_get_page_owner_entity();
  178. $any_member = ($group->subgroups_members_create_enable != 'no');
  179. if (elgg_instanceof($group, 'group') && $group->subgroups_enable != 'no') {
  180. if (($any_member && $group->isMember()) || $group->canEdit()) {
  181. // register our title menu
  182. elgg_register_menu_item('title', array(
  183. 'name' => 'add_subgroup',
  184. 'href' => "groups/subgroups/add/{$group->guid}",
  185. 'text' => elgg_echo('au_subgroups:add:subgroup'),
  186. 'link_class' => 'elgg-button elgg-button-action'
  187. ));
  188. }
  189. }
  190. }
  191. }