You are here

function og_field_crud_group in Organic groups 7

Create update or delete a group, based on the field CRUD.

TODO: Remove this an move to hook_entity_presave().

See also

og_field_attach_insert().

og_field_attach_update().

og_field_attach_delete().

3 calls to og_field_crud_group()
og_field_attach_delete in ./og.field.inc
Implements hook_field_attach_delete().
og_field_attach_insert in ./og.field.inc
Implements hook_field_attach_insert().
og_field_attach_update in ./og.field.inc
Implements hook_field_attach_update().

File

./og.field.inc, line 497
Field module functionality for the Organic groups module.

Code

function og_field_crud_group($op, $entity_type, $entity) {
  $property = OG_GROUP_FIELD;

  // If the entity is a translated node, we can return early, as all operations
  // happen on the source node.
  if ($entity_type == 'node' && (!empty($entity->tnid) && $entity->tnid != $entity->nid || !empty($entity->translation_source->tnid)) && module_exists('translation')) {
    return;
  }
  if (!empty($entity->{$property}) && empty($entity->og_skip_group_create)) {
    $wrapper =& $entity->{$property}[LANGUAGE_NONE];

    // Get the entity ID.
    list($id) = entity_extract_ids($entity_type, $entity);
    $group = og_get_group($entity_type, $id, TRUE, array(
      OG_STATE_ACTIVE,
      OG_STATE_PENDING,
    ));
    if ($op == 'delete') {
      if (!empty($group->gid)) {

        // Remove group.
        $group
          ->delete();
      }
    }
    else {

      // Check group is new.
      if (empty($group->gid)) {
        if (!empty($wrapper[0]['value'])) {

          // Save the group to get the group ID.
          $group
            ->save();

          // Subscribe the entity author, if exists.
          if (!empty($entity->uid) && ($account = user_load($entity->uid))) {
            $values = array(
              'entity' => $account,
            );
            og_group($group->gid, $values);
          }
        }
      }
      else {

        // Existing group.
        $save = FALSE;
        if ($group->state == OG_STATE_ACTIVE && empty($wrapper[0]['value'])) {
          $group->state = OG_STATE_PENDING;
          $save = TRUE;
        }
        elseif ($group->state == OG_STATE_PENDING && !empty($wrapper[0]['value'])) {
          $group->state = OG_STATE_ACTIVE;
          $save = TRUE;
        }

        // Check if the entity label has changed.
        $label = og_entity_label($entity_type, $entity);
        if ($group->label != $label) {
          $group->label = $label;
          $save = TRUE;
        }
        if ($save) {
          $group
            ->save();
        }
      }

      // Determine if field has changed and roles should be overridden, or
      // reverted, by comparing the default access field of the entity being
      // saved, and its original state.
      $property = OG_DEFAULT_ACCESS_FIELD;

      // The field exists.
      if (isset($entity->{$property})) {
        if (!empty($entity->{$property}[LANGUAGE_NONE][0]['value'])) {
          og_roles_override($group->gid);
        }
        elseif (empty($group->is_new)) {

          // If the field is set to be using default access and there are
          // already overridden roles we delete them.
          og_delete_user_roles_by_group($group->gid);
        }
      }
    }
  }
}