hooks.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * Called when a group is attempting to be deleted
  4. * Check if there are subgroups and sort out what happens them and content
  5. */
  6. function au_subgroups_delete_group($hook, $type, $return, $params) {
  7. $guid = get_input('guid');
  8. if (!$guid) {
  9. $guid = get_input('group_guid');
  10. }
  11. $group = get_entity($guid);
  12. if (elgg_instanceof($group, 'group')) {
  13. // determine if the group has any child groups
  14. $child = au_subgroups_get_subgroups($group, 1);
  15. $parent = au_subgroups_get_parent_group($group);
  16. if ($child || $parent) {
  17. // here we are, we're deleting something with subgroups or a parent
  18. // if we've already sorted out what happens to content
  19. // we'll have a special input
  20. $content_policy = get_input('au_subgroups_content_policy', false);
  21. if (!$content_policy) {
  22. forward(elgg_get_site_url() . "groups/subgroups/delete/{$group->guid}");
  23. }
  24. // this is the top level to delete, so if transferring content to parent, it's the parent of this
  25. // apply content policy recursively, then delete all subgroups recursively
  26. // this could take a while...
  27. set_time_limit(0);
  28. $guids = au_subgroups_get_all_children_guids($group);
  29. if (is_array($guids) && count($guids)) {
  30. if ($content_policy != 'delete' && is_array($guids) && count($guids)) {
  31. $options = array(
  32. 'container_guids' => $guids,
  33. 'au_subgroups_content_policy' => $content_policy,
  34. 'au_subgroups_parent_guid' => $parent->guid,
  35. 'limit' => 0
  36. );
  37. $batch = new ElggBatch('elgg_get_entities', $options, 'au_subgroups_move_content', 25);
  38. }
  39. // now delete the groups themselves
  40. $options = array(
  41. 'guids' => $guids,
  42. 'types' => array('group'),
  43. 'limit' => 0
  44. );
  45. $batch = new ElggBatch('elgg_get_entities', $options, 'au_subgroups_delete_entities', 25, false);
  46. }
  47. }
  48. }
  49. }
  50. function au_subgroups_group_canedit($hook, $type, $return, $params) {
  51. $group = $params['entity'];
  52. $user = $params['user'];
  53. $parent = au_subgroups_get_parent_group($group);
  54. if ($parent) {
  55. if ($parent->canEdit($user->guid)) {
  56. return true;
  57. }
  58. }
  59. }
  60. /**
  61. * prevent users from being invited to subgroups they can't join
  62. */
  63. function au_subgroups_group_invite($hook, $type, $return, $params) {
  64. $user_guid = get_input('user_guid');
  65. $group_guid = get_input('group_guid');
  66. $group = get_entity($group_guid);
  67. $parent = au_subgroups_get_parent_group($group);
  68. // if $parent, then this is a subgroup they're being invited to
  69. // make sure they're a member of the parent
  70. if ($parent) {
  71. if (!is_array($user_guid)) {
  72. $user_guid = array($user_guid);
  73. }
  74. $invalid_users = array();
  75. foreach($user_guid as $guid) {
  76. $user = get_user($guid);
  77. if ($user && !$parent->isMember($user)) {
  78. $invalid_users[] = $user;
  79. }
  80. }
  81. if (count($invalid_users)) {
  82. $error_suffix = "<ul>";
  83. foreach($invalid_users as $user) {
  84. $error_suffix .= "<li>{$user->name}</li>";
  85. }
  86. $error_suffix .= "</ul>";
  87. register_error(elgg_echo('au_subgroups:error:invite') . $error_suffix);
  88. return false;
  89. }
  90. }
  91. }
  92. /**
  93. * re/routes some urls that go through the groups handler
  94. */
  95. function au_subgroups_groups_router($hook, $type, $return, $params) {
  96. au_subgroups_breadcrumb_override($return);
  97. // subgroup options
  98. if ($return['segments'][0] == 'subgroups') {
  99. elgg_load_library('elgg:groups');
  100. $group = get_entity($return['segments'][2]);
  101. if (!elgg_instanceof($group, 'group') || ($group->subgroups_enable == 'no')) {
  102. return $return;
  103. }
  104. elgg_set_context('groups');
  105. elgg_set_page_owner_guid($group->guid);
  106. switch ($return['segments'][1]) {
  107. case 'add':
  108. set_input('au_subgroup', true);
  109. set_input('au_subgroup_parent_guid', $group->guid);
  110. if (include(elgg_get_plugins_path() . 'au_subgroups/pages/add.php')) {
  111. //if dont exit, the page will show site '' page
  112. exit;
  113. return true;
  114. }
  115. break;
  116. case 'delete':
  117. if (include(elgg_get_plugins_path() . 'au_subgroups/pages/delete.php')) {
  118. exit;
  119. return true;
  120. }
  121. break;
  122. case 'list':
  123. if (include(elgg_get_plugins_path() . 'au_subgroups/pages/list.php')) {
  124. exit;
  125. return true;
  126. }
  127. break;
  128. }
  129. }
  130. // need to redo closed/open tabs provided by group_tools - if it's installed
  131. if ($return['segments'][0] == 'all' && elgg_is_active_plugin('group_tools')) {
  132. $filter = get_input('filter', false);
  133. if(empty($filter) && ($default_filter = elgg_get_plugin_setting("group_listing", "group_tools"))){
  134. $filter = $default_filter;
  135. set_input("filter", $default_filter);
  136. }
  137. if(in_array($filter, array("open", "closed", "alpha"))){
  138. au_subgroups_handle_openclosed_tabs();
  139. return true;
  140. }
  141. }
  142. }
  143. function au_subgroups_river_permissions($hook, $type, $return, $params) {
  144. $group = get_entity($return['object_guid']);
  145. $parent = au_subgroups_get_parent_group($group);
  146. if ($parent) {
  147. // it is a group, and it has a parent
  148. $return['access_id'] = $group->access_id;
  149. }
  150. return $return;
  151. }
  152. function au_subgroups_titlemenu($h, $t, $r, $p) {
  153. if (in_array(elgg_get_context(), array('group_profile', 'groups'))) {
  154. $group = elgg_get_page_owner_entity();
  155. // make sure we're dealing with a group
  156. if (!elgg_instanceof($group, 'group')) {
  157. return $r;
  158. }
  159. // make sure the group is a subgroup
  160. $parent = au_subgroups_get_parent_group($group);
  161. if (!$parent) {
  162. return $r;
  163. }
  164. // see if we're a member of the parent group
  165. if ($parent->isMember()) {
  166. return $r;
  167. }
  168. // we're not a member, so we need to remove any 'join'/'request membership' links
  169. foreach ($r as $key => $item) {
  170. if (in_array($item->getName(), array('groups:join', 'groups:joinrequest'))) {
  171. unset($r[$key]);
  172. }
  173. }
  174. return $r;
  175. }
  176. }