move.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. $subgroup_guid = get_input('subgroup_guid');
  3. $parent_guid = get_input('parent_guid');
  4. if( $parent_guid == -1 )
  5. {
  6. // remove any existing parent relationships
  7. au_subgroups_remove_parent_group($subgroup_guid);
  8. }
  9. else
  10. {
  11. $subgroup = get_entity($subgroup_guid);
  12. $parent = get_entity($parent_guid);
  13. $oldparent = au_subgroups_get_parent_group($subgroup);
  14. $child_groups = au_subgroups_get_all_children_guids($subgroup);
  15. //sanity check
  16. if (!elgg_instanceof($subgroup, 'group') || !elgg_instanceof($parent, 'group')) {
  17. register_error(elgg_echo('au_subgroups:error:invalid:group'));
  18. forward(REFERER);
  19. }
  20. // we need to have edit permissions all the way up
  21. if (!au_subgroups_can_move_subgroup($subgroup, $parent)) {
  22. register_error(elgg_echo('au_subgroups:error:permissions'));
  23. forward(REFERER);
  24. }
  25. // remove any existing parent relationships
  26. au_subgroups_remove_parent_group($subgroup->guid);
  27. au_subgroups_set_parent_group($subgroup->guid, $parent->guid);
  28. // determine the access_id of the new group, must be equal or more restrictive than the parent
  29. switch ($parent->access_id) {
  30. case ACCESS_PUBLIC:
  31. // only need to check that subgroup wasn't to old parent only
  32. if ($subgroup->access_id == $oldparent->group_acl) {
  33. $subgroup->access_id = $parent->group_acl;
  34. }
  35. break;
  36. case ACCESS_LOGGED_IN:
  37. // subgroup cannot be public
  38. if ($subgroup->access_id == ACCESS_PUBLIC) {
  39. $subgroup->access_id = ACCESS_LOGGED_IN;
  40. }
  41. elseif ($subgroup->access_id == $oldparent->group_acl) {
  42. $subgroup->access_id = $parent->group_acl;
  43. }
  44. break;
  45. case $parent->group_acl:
  46. default:
  47. // if none of the above, then parent is visible to it's parent
  48. // lets keep that model
  49. //hidden group, make sure subgroup can only be seen by parent, or is hidden itself
  50. if ($subgroup->access_id != $subgroup->group_acl) {
  51. $subgroup->access_id = $parent->group_acl;
  52. }
  53. break;
  54. }
  55. $subgroup->save();
  56. //now we need to make sure that all members of the new subgroup are
  57. // members of the parent group
  58. // get all members of the subgroup - any members of subgroups have to be in this anyway
  59. global $AU_SUBGROUPS_ALL_MEMBERS;
  60. $AU_SUBGROUPS_ALL_MEMBERS = array();
  61. $options = array(
  62. 'relationship' => 'member',
  63. 'relationship_guid' => $subgroup->guid,
  64. 'inverse_relationship' => TRUE,
  65. 'type' => 'user',
  66. 'limit' => false,
  67. );
  68. $batch = new ElggBatch('elgg_get_entities_from_relationship', $options, 'au_subgroups_get_all_members', 25);
  69. $AU_SUBGROUPS_ALL_MEMBERS = array_unique($AU_SUBGROUPS_ALL_MEMBERS);
  70. // array of user guids we need to invite back into the group
  71. $invite = array();
  72. foreach ($AU_SUBGROUPS_ALL_MEMBERS as $member_guid) {
  73. if (!is_group_member($parent_guid, $member_guid)) {
  74. join_group($parent_guid, $member_guid);
  75. $user = get_user($member_guid);
  76. if ($user) {
  77. // notify the user
  78. $url = elgg_normalize_url("groups/invitations/$user->username");
  79. notify_user(
  80. $member_guid,
  81. $parent->owner_guid,
  82. elgg_echo('au_subgroups:moveto:subject', array($user->name, $subgroup->name, $parent->name, $parent->name)),
  83. elgg_echo('au_subgroups:moveto:body', array(
  84. $user->name,
  85. $subgroup->name,
  86. $parent->name,
  87. $parent->name,
  88. $url,
  89. ))
  90. );
  91. }
  92. /*$invite[] = $member_guid;
  93. // the user isn't a member of the parent group
  94. // so we have to remove them from this subgroup, and all subgroups of this subgroup
  95. // and send them an invitation
  96. // we'll set a plugin setting on the user, which we'll monitor for when they accept the invitation
  97. $groups_left = array($subgroup_guid);
  98. foreach ($child_groups as $child_guid) {
  99. if (is_group_member($child_guid, $member_guid)) {
  100. $groups_left[] = $child_guid;
  101. }
  102. }
  103. leave_group($subgroup_guid, $member_guid);
  104. // now we need to set a plugin user setting for this user that we can monitor
  105. elgg_set_plugin_user_setting('invitation_' . $subgroup_guid, serialize($groups_left), $member_guid, 'au_subgroups');
  106. // invite the user
  107. add_entity_relationship($subgroup_guid, 'invited', $member_guid);
  108. $user = get_user($member_guid);
  109. if ($user) {
  110. // notify the user
  111. $url = elgg_normalize_url("groups/invitations/$user->username");
  112. notify_user(
  113. $member_guid,
  114. $subgroup->owner_guid,
  115. elgg_echo('groups:invite:subject', array($user->name, $subgroup->name)),
  116. elgg_echo('au_subgroups:invite:body', array(
  117. $user->name,
  118. $subgroup->name,
  119. $parent->name,
  120. $url,
  121. ))
  122. );
  123. }*/
  124. }
  125. }
  126. }
  127. system_message(elgg_echo('au_subgroups:move:success'));
  128. forward(REFERER);