You are here

function ggroup_run_member_inheritance in Group 7

Trigger the inheritance downwards for a membership.

Parameters

GroupMembership $parent: The membership to check and run inheritance for towards subgroups.

array $filter: (optional) An array of group ids to limit the inheritance run to. Can be used to boost performance when targeting specific subgroups to (re)run inheritance towards.

3 calls to ggroup_run_member_inheritance()
ggroup_entity_insert in modules/ggroup/ggroup.entity.inc
Implements hook_entity_insert().
ggroup_entity_update in modules/ggroup/ggroup.entity.inc
Implements hook_entity_update().
ggroup_group_type_batch in modules/ggroup/ggroup.module
Batch callback for mass running membership inheritance.

File

modules/ggroup/ggroup.module, line 130
Contains Subgroup functionality for the Group module.

Code

function ggroup_run_member_inheritance(GroupMembership $parent, array $filter = array()) {

  // Retrieve the membership status info.
  $status_info = group_membership_status_info();

  // Inactive memberships are not inherited.
  if (empty($status_info[$parent->status]['active'])) {
    return;
  }

  // Check for any possible inherited memberships.
  $inherited = ggroup_get_inheritance($parent);

  // Create the inherited memberships along with the original one.
  foreach ($inherited as $gid => $roles) {

    // Skip if the group id isn't in the filter.
    if (!empty($filter) && !in_array($gid, $filter)) {
      continue;
    }

    // Take special care if the membership exists already.
    if ($group_membership = group_membership_load($gid, $parent->uid)) {

      // Update the roles for clean inherited memberships.
      if ($group_membership->status == 'inherited') {
        $group_membership->heritage['initial'] = $roles;
        $group_membership->roles = $roles;
        $group_membership
          ->save();
      }
      elseif ($group_membership->status == 'inherited-overridden') {
        $granted = $group_membership->heritage['granted'];
        $revoked = $group_membership->heritage['revoked'];

        // Update the initial heritage data.
        $group_membership->heritage['initial'] = $roles;

        // Check for previous grants that are now part of the heritage package
        // and remove them from the heritage data.
        $obsolete = array_intersect($granted, $roles);
        $group_membership->heritage['granted'] = array_diff($granted, $obsolete);

        // Check for previous revoked roles that are no longer part of the
        // heritage package and remove them from the heritage data.
        $obsolete = array_diff($revoked, $roles);
        $group_membership->heritage['revoked'] = array_diff($revoked, $obsolete);

        // Rebuild the roles property based on the newly calculated heritage.
        $roles = array_merge($roles, $group_membership->heritage['granted']);
        $roles = array_diff($roles, $group_membership->heritage['revoked']);

        // Finally, update the inherited membership.
        $group_membership->roles = $roles;
        $group_membership
          ->save();
      }
    }
    else {
      $values = array(
        'gid' => $gid,
        'uid' => $parent->uid,
        'roles' => $roles,
        'status' => 'inherited',
        'parent_mid' => $parent->mid,
        'heritage' => array(
          'initial' => $roles,
          'granted' => array(),
          'revoked' => array(),
        ),
      );
      $group_membership = entity_create('group_membership', $values);
      $group_membership
        ->save();
    }
  }
}