You are here

function ggroup_get_inheritance in Group 7

Determines the inheritance a membership triggers.

Parameters

GroupMembership $group_membership: The membership to get inheritance data for.

Return value

array An array of roles to inherit, keyed by the group id. If a membership should be inherited without any roles, an empty array will be returned.

1 call to ggroup_get_inheritance()
ggroup_run_member_inheritance in modules/ggroup/ggroup.module
Trigger the inheritance downwards for a membership.

File

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

Code

function ggroup_get_inheritance(GroupMembership $group_membership) {

  // Create an EMW to reduce the code below.
  $emw = entity_metadata_wrapper('group_membership', $group_membership);

  // Retrieve the child entities for the membership's group.
  $entities = $emw->group
    ->value()
    ->getEntities();

  // Retrieve the membership's group type configuration array.
  $config = $emw->group->group_type
    ->value()->config;

  // Without subgroups or subgroup configuration, there is nothing to do.
  if (empty($entities['group']) || empty($config['subgroup'])) {
    return array();
  }

  // Inheritance data will be stored here.
  $memberships = array();

  // Check inheritance for the configured subgroups.
  foreach ($config['subgroup'] as $type => $data) {

    // Only act if there are subgroups of this type.
    if (empty($entities['group'][$type])) {
      continue;
    }

    // Reset the inherited roles on every iteration.
    $inherited = array();

    // Check if the membership triggers any inherited roles.
    foreach ($data as $role => $inherits) {

      // If the member had the required role, grant the inherited roles.
      if ($role == 'member' || in_array($role, $group_membership->roles)) {
        $inherited += array_filter($inherits);
      }
    }

    // Add the inherited roles to the list.
    if (!empty($inherited)) {

      // We don't need to set 'member' explicitly, so remove it.
      unset($inherited['member']);

      // Turn the $inherited array into a valid roles array.
      $inherited = array_keys($inherited);

      // Set the actual roles array for every inherited membership.
      foreach (array_keys($entities['group'][$type]) as $gid) {
        $memberships[$gid] = $inherited;
      }
    }
  }
  return $memberships;
}