start.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. // definitions
  3. define('AU_SUBGROUPS_RELATIONSHIP', 'au_subgroup_of');
  4. // include our functions
  5. require_once 'lib/events.php';
  6. require_once 'lib/functions.php';
  7. require_once 'lib/hooks.php';
  8. elgg_register_event_handler('init', 'system', 'au_subgroups_init');
  9. function au_subgroups_init() {
  10. // add in our own css
  11. elgg_extend_view('css/elgg', 'au_subgroups/css');
  12. elgg_extend_view('forms/groups/edit', 'forms/au_subgroups/edit');
  13. elgg_extend_view('navigation/breadcrumbs', 'au_subgroups/breadcrumb_override', 1);
  14. elgg_extend_view('group/elements/summary', 'au_subgroups/group/elements/summary');
  15. elgg_extend_view('groups/tool_latest', 'au_subgroups/group_module');
  16. elgg_extend_view('groups/sidebar/members', 'au_subgroups/sidebar/subgroups');
  17. elgg_extend_view('groups/edit', 'au_subgroups/group/transfer');
  18. elgg_extend_view('groups/profile/fields', 'au_subgroups/groups/profile/fields');
  19. // register the edit page's JavaScript
  20. $js = elgg_get_simplecache_url('js', 'au_subgroups/edit_js');
  21. elgg_register_js('au_subgroups_edit.js', $js);
  22. elgg_register_simplecache_view('js/au_subgroups/edit_js');
  23. // after group creation or editing we need to check the permissions
  24. elgg_register_event_handler('update', 'group', 'au_subgroups_group_visibility');
  25. elgg_register_event_handler('create', 'member', 'au_subgroups_join_group', 0);
  26. elgg_register_event_handler('leave', 'group', 'au_subgroups_leave_group');
  27. // break up the create/update events to be more manageable
  28. elgg_register_event_handler('create', 'group', 'au_subgroups_add_parent', 1000);
  29. elgg_register_event_handler('create', 'group', 'au_subgroups_clone_layout_on_create', 1000);
  30. elgg_register_event_handler('create', 'group', 'au_subgroups_group_visibility', 1000);
  31. elgg_register_event_handler('pagesetup', 'system', 'au_subgroups_pagesetup');
  32. // replace the existing groups library so we can push some display options
  33. elgg_register_library('elgg:groups', elgg_get_plugins_path() . 'au_subgroups/lib/groups.php');
  34. add_group_tool_option('subgroups', elgg_echo('au_subgroups:group:enable'));
  35. add_group_tool_option('subgroups_members_create', elgg_echo('au_subgroups:group:memberspermissions'));
  36. // route some urls that go through 'groups' handler
  37. elgg_register_plugin_hook_handler('route', 'groups', 'au_subgroups_groups_router', 499);
  38. // make sure river entries have the correct access
  39. elgg_register_plugin_hook_handler('creating', 'river', 'au_subgroups_river_permissions');
  40. // admins of the parent group can edit the sub-group
  41. elgg_register_plugin_hook_handler('permissions_check', 'group', 'au_subgroups_group_canedit');
  42. // sort out what happens when a parent group is deleted
  43. elgg_register_plugin_hook_handler('action', 'groups/delete', 'au_subgroups_delete_group');
  44. // prevent users from being invited into a subgroup they can't join
  45. elgg_register_plugin_hook_handler('action', 'groups/invite', 'au_subgroups_group_invite');
  46. // remove 'join' and 'request membership' title links on subgroups for people not members of the parent
  47. elgg_register_plugin_hook_handler('register', 'menu:title', 'au_subgroups_titlemenu');
  48. // register our widget
  49. elgg_register_widget_type('au_subgroups', elgg_echo('au_subgroups'), elgg_echo('au_subgroups:widget:description'), 'groups');
  50. //elgg_register_plugin_hook_handler("register", "menu:entity", "group_member_entity_menu_handler");
  51. elgg_register_ajax_view('au_subgroups/search_results');
  52. // group entity menu
  53. //elgg_register_plugin_hook_handler('register', 'menu:entity', 'subgroups_entity_menu_setup');
  54. // actions
  55. elgg_register_action('au_subgroups/move', dirname(__FILE__) . '/actions/move.php');
  56. // fix some problems
  57. if (elgg_is_admin_logged_in()) {
  58. run_function_once('au_subgroups_bugfix_20121024a');
  59. }
  60. }
  61. function group_member_entity_menu_handler($hook, $type, $returnvalue, $params){
  62. $page_owner = elgg_get_page_owner_entity();
  63. $user = elgg_get_logged_in_user_entity();
  64. if (!($page_owner instanceof ElggGroup) || empty($user) || ($page_owner->owner_guid != $user->getGUID() ))
  65. return $returnvalue;
  66. $result = $returnvalue;
  67. if(!empty($params) && is_array($params) && ($user = elgg_get_logged_in_user_entity())){
  68. $entity = elgg_extract("entity", $params);
  69. if(elgg_instanceof($entity, "user") && ($entity->getGUID() != $user->getGUID())){
  70. if(!empty($result) && !is_array($result)){
  71. $result = array($result);
  72. } elseif(empty($result)){
  73. $result = array();
  74. }
  75. if(check_entity_relationship($page_owner->getGUID(), "manager", $entity->getGUID())){
  76. // pending request
  77. $result[] = ElggMenuItem::factory(array(
  78. "name" => "set_group_manager",
  79. "text" => elgg_echo("group:setmanager"),
  80. "href" => "groups/setmanager/" . $page_owner->getGUID(). "/".$entity->getGUID(),
  81. "is_action" => true,
  82. "priority" => 503
  83. ));
  84. } else {
  85. // add as friend
  86. $result[] = ElggMenuItem::factory(array(
  87. "name" => "set_group_manager",
  88. "text" => elgg_echo("group:removemanager"),
  89. "href" =>"groups/removemanager/" . $page_owner->getGUID(). "/".$entity->getGUID(),
  90. "is_action" => true,
  91. "priority" => 503
  92. ));
  93. }
  94. }
  95. }
  96. return $result;
  97. }
  98. function au_subgroups_bugfix_20121024a() {
  99. $options = array(
  100. 'types' => 'group',
  101. 'limit' => 0
  102. );
  103. // using ElggBatch because there may be many, many groups in the installation
  104. // try to avoid oom errors
  105. $batch = new ElggBatch('elgg_get_entities', $options, 'au_subgroups_fix_acls_20121024a', 50);
  106. }
  107. function au_subgroups_fix_acls_20121024a($result, $getter, $options) {
  108. if ($result->group_acl === NULL) {
  109. // group has no acl... create it and add all the members
  110. $ac_name = elgg_echo('groups:group') . ": " . $result->name;
  111. $group_acl = create_access_collection($ac_name, $result->guid);
  112. $result->group_acl = $group_acl;
  113. // now add all members of the group to the acl
  114. $members = $result->getMembers(0, 0, false);
  115. if (is_array($members) && count($members)) {
  116. foreach ($members as $member) {
  117. add_user_to_access_collection($member->guid, $group_acl);
  118. }
  119. }
  120. }
  121. }
  122. function subgroups_entity_menu_setup($hook, $type, $return, $params) {
  123. if (elgg_in_context('widgets')) {
  124. return $return;
  125. }
  126. /* @var ElggGroup $entity */
  127. $entity = $params['entity'];
  128. $handler = elgg_extract('handler', $params, false);
  129. if ($handler != 'groups') {
  130. return $return;
  131. }
  132. // group members
  133. if ($entity->isMember(elgg_get_logged_in_user_entity())) {
  134. if ($entity->getOwnerGUID() != elgg_get_logged_in_user_guid()) {
  135. // leave
  136. $url = elgg_get_site_url() . "action/groups/leave?group_guid={$entity->getGUID()}";
  137. $url = elgg_add_action_tokens_to_url($url);
  138. //$actions[$url] = 'groups:leave';
  139. $options = array(
  140. 'name' => 'leave',
  141. 'text' => elgg_echo('groups:leave'),
  142. 'href' => $url,
  143. 'priority' => 201,
  144. );
  145. }
  146. } elseif (elgg_is_logged_in()) {
  147. // join - admins can always join.
  148. $url = elgg_get_site_url() . "action/groups/join?group_guid={$entity->getGUID()}";
  149. $url = elgg_add_action_tokens_to_url($url);
  150. if ($entity->isPublicMembership() || $entity->canEdit()) {
  151. $text = elgg_echo('groups:join');
  152. } else {
  153. // request membership
  154. $text = elgg_echo('groups:joinrequest');
  155. }
  156. $options = array(
  157. 'name' => 'join',
  158. 'text' => $text,
  159. 'href' => $url,
  160. 'priority' => 202,
  161. );
  162. }
  163. $return[] = ElggMenuItem::factory($options);
  164. }