<?php namespace AU\SubGroups; $subgroup_guid = get_input('subgroup_guid'); $parent_guid = get_input('parent_guid'); $subgroup = get_entity($subgroup_guid); $parent = get_entity($parent_guid); $oldparent = get_parent_group($subgroup); $child_groups = get_all_children_guids($subgroup); //sanity check if (!elgg_instanceof($subgroup, 'group') || !elgg_instanceof($parent, 'group')) { register_error(elgg_echo('au_subgroups:error:invalid:group')); forward(REFERER); } // we need to have edit permissions all the way up if (!can_move_subgroup($subgroup, $parent)) { register_error(elgg_echo('au_subgroups:error:permissions')); forward(REFERER); } // remove any existing parent relationships remove_parent_group($subgroup->guid); set_parent_group($subgroup->guid, $parent->guid); // determine the access_id of the new group, must be equal or more restrictive than the parent switch ($parent->access_id) { case ACCESS_PUBLIC: // only need to check that subgroup wasn't to old parent only if ($subgroup->access_id == $oldparent->group_acl) { $subgroup->access_id = $parent->group_acl; } break;
/** * Determines if a subgroup could potentially be moved * To a parent group * Makes sure permissions are in order, and that the subgroup isn't already a parent * of the parent or anything weird like that * * @param type $user ElggUser * @param type $subgroup_guid * @param type $parentgroup_guid */ function can_move_subgroup($subgroup, $parent, $user = NULL) { if (!elgg_instanceof($user, 'user')) { $user = elgg_get_logged_in_user_entity(); } if (!$user) { return false; } // make sure they're really groups if (!elgg_instanceof($subgroup, 'group') || !elgg_instanceof($parent, 'group')) { return false; } // make sure we can edit them if (!$subgroup->canEdit($user->guid) || !$parent->canEdit($user->guid)) { return false; } // make sure we can edit all the way up, and we're not trying to move a group into itself if (!can_edit_recursive($subgroup) || $subgroup->guid == $parent->guid) { return false; } // make sure we're not moving a group into it's existing parent $current_parent = get_parent_group($subgroup); if ($current_parent && $current_parent->guid == $parent->guid) { return false; } // also make sure the potential parent isn't a subgroup of the subgroup $children = get_all_children_guids($subgroup); if (in_array($parent->guid, $children)) { return false; } return true; }
function delete_group_event($event, $type, $group) { $content_policy = get_input('au_subgroups_content_policy', false); if (!$content_policy) { return true; } $guid = get_input('guid'); if (!$guid) { $guid = get_input('group_guid'); } // protects against recursion on the group/delete action if (!$guid || $group->guid != $guid) { return true; } $parent = get_parent_group($group); // this is the top level to delete, so if transferring content to parent, it's the parent of this // apply content policy recursively, then delete all subgroups recursively // this could take a while... set_time_limit(0); $guids = get_all_children_guids($group); $guids[] = $group->guid; if (is_array($guids) && count($guids)) { if ($content_policy != 'delete' && is_array($guids) && count($guids)) { $options = array('container_guids' => $guids, 'au_subgroups_content_policy' => $content_policy, 'au_subgroups_parent_guid' => $parent->guid, 'limit' => 0); $batch = new \ElggBatch('elgg_get_entities', $options, null, 25); $ia = elgg_set_ignore_access(true); foreach ($batch as $content) { if ($content_policy == 'owner') { $container_guid = $content->owner_guid; } else { $container_guid = $parent->guid; } $content->container_guid = $container_guid; $content->save(); } elgg_set_ignore_access($ia); } // now delete the groups themselves $options = array('guids' => $guids, 'types' => array('group'), 'limit' => 0); $batch = new \ElggBatch('elgg_get_entities', $options, null, 25, false); foreach ($batch as $e) { if ($e->guid == $group->guid) { continue; // the action itself will take care of this } $e->delete(); } } }