You are here

function ggroup_entity_update in Group 7

Implements hook_entity_update().

File

modules/ggroup/ggroup.entity.inc, line 86
Contains all Entity API functions for the Subgroup module.

Code

function ggroup_entity_update($entity, $type) {
  if ($type == 'group_membership' && $entity->roles_changed) {

    // If an inherited membership is updated, check if it broke inheritance.
    // Furthermore, update the 'heritage' property if necessary.
    if ($entity->status == 'inherited' || $entity->status == 'inherited-overridden') {

      // Retrieve the roles the member should have according to its heritage.
      $heritage = $entity->heritage['initial'];

      // Keep a reference before modifying anything.
      $ref_granted = $entity->heritage['granted'];
      $ref_revoked = $entity->heritage['revoked'];

      // Store whatever roles were changed.
      $granted = $entity->heritage['granted'] = array_diff($entity->roles, $heritage);
      $revoked = $entity->heritage['revoked'] = array_diff($heritage, $entity->roles);

      // Flag an inherited membership as overridden if anything has changed.
      if ($entity->status == 'inherited' && ($granted || $revoked)) {
        $entity->status = 'inherited-overridden';
      }
      elseif ($entity->status == 'inherited-overridden' && !$granted && !$revoked) {
        $entity->status = 'inherited';
      }

      // Find out if anything was modified.
      $mod_granted = array_diff($ref_granted, $granted) || array_diff($granted, $ref_granted);
      $mod_revoked = array_diff($ref_revoked, $revoked) || array_diff($revoked, $ref_revoked);

      // Save the membership again and rerun inheritance if there was.
      if ($mod_granted || $mod_revoked) {
        $entity
          ->save();
      }
    }

    // If the membership's roles have changed, we need to run inheritance again.
    ggroup_run_member_inheritance($entity);
  }
}