function ggroup_update_7004 in Group 7
Add the heritage column to {group_membership}.
File
- modules/
ggroup/ ggroup.install, line 103 - Install, update and uninstall functions for the Subgroup project.
Code
function ggroup_update_7004() {
db_add_field('group_membership', 'heritage', _ggroup_heritage());
// Load all 'inherited' memberships and set 'initial'.
foreach (group_memberships(array(
'status' => 'inherited',
)) as $group_membership) {
$group_membership->heritage = array(
'initial' => $group_membership->roles,
'granted' => array(),
'revoked' => array(),
);
$group_membership
->save();
}
// Load all 'inherited-overridden' memberships and get funky.
foreach (group_memberships(array(
'status' => 'inherited-overridden',
)) as $group_membership) {
// Create an EMW to reduce the code below.
$emw = entity_metadata_wrapper('group_membership', $group_membership);
// Retrieve the membership's group type machine name.
$type = $emw->group->group_type
->raw();
// Retrieve the parent membership.
$parent = $emw->parent_membership
->value();
// Retrieve the parent membership's group type configuration array.
$config = $emw->parent_membership->group->group_type
->value()->config;
// If no configuration was found, something is wrong. Don't touch it.
if (empty($config['subgroup'][$type])) {
continue;
}
// Stores the roles the membership should have gotten.
$inherited = array();
// Check if the membership triggers any inherited roles.
foreach ($config['subgroup'][$type] as $role => $inherits) {
// If the member had the required role, grant the inherited roles.
if ($role == 'member' || in_array($role, $parent->roles)) {
$inherited += array_filter($inherits);
}
}
// We don't need to set 'member' explicitly, so remove it.
unset($inherited['member']);
// Create a proper roles array.
$inherited = array_keys($inherited);
// Set the 'heritage' data correctly.
$group_membership->heritage = array(
'initial' => $inherited,
'granted' => array_diff($group_membership->roles, $inherited),
'revoked' => array_diff($inherited, $group_membership->roles),
);
$group_membership
->save();
}
}