You are here

function og_group in Organic groups 7

Same name and namespace in other branches
  1. 7.2 og.module \og_group()

Set an association (e.g. subscribe) an entity to a group.

Parameters

$gid: The group ID.

$values: Array with the information to pass along, until it is processed in og_entity_presave(). Keys are:

  • "entity type": Optional; The entity type (e.g. "node" or "user"). Defaults to 'user'
  • "entity" :Optional; The entity to set the association. Defaults to the current user if the $entity_type property is set to 'user'.
  • "state": Optional; The state of the association. Can be:
  • "save": Optional; TRUE if fields value should be saved. Defaults to TRUE.
  • "force reload": Optional; Determine if og_load_entity() should be used on the passed entity. This can be used when you pass an entity that has been saved yet to the database, but you want to assign it groups. Defaults to TRUE. // TODO: Adds docs on rest of keys.

Return value

The entity with the fields updated.

17 calls to og_group()
generate-og-d7-content-update-7001.php in scripts/generate-og-d7-content-update-7001.php
OgGroupAndUngroup::testGroupAndUngroup in ./og.test
Test group and ungroup of content.
OgGroupApi::testGetEntityGroups in ./og.test
Test the og_get_entity_groups() API function.
OgGroupApi::testOgAccessEntity in ./og.test
Verify og_user_access_entity() returns correct value.
OgGroupAudienceField::testOgAudienceFieldDeletedGroup in ./og.test
Test the group-audience field for a group that was deleted, but there are still users associated with that group.

... See full list

1 string reference to 'og_group'
og_context_create_og_group in og_context/og_context.module
Return OG group form context.

File

./og.module, line 1615
Enable users to create and manage groups with roles and permissions.

Code

function og_group($gid, $values = array()) {

  // Set default values.
  $values += array(
    'entity type' => 'user',
    'entity' => FALSE,
    'state' => OG_STATE_ACTIVE,
    'save' => TRUE,
    'force reload' => TRUE,
    'membership type' => OG_MEMBERSHIP_TYPE_DEFAULT,
    'membership fields' => array(),
    'created' => time(),
  );
  $entity_type = $values['entity type'];
  $entity = $values['entity'];
  $state = $values['state'];
  $save = $values['save'];
  if ($entity_type == 'user' && empty($entity)) {
    global $user;
    $entity = clone $user;
  }
  if ($values['force reload']) {
    $entity = og_load_entity($entity_type, $entity);
  }
  $property = OG_AUDIENCE_FIELD;

  // Check the audience field exists in the entity.
  if (in_array($entity_type, array_keys(og_get_all_group_content_entity()))) {
    $wrapper =& $entity->{$property}[LANGUAGE_NONE];
    list($id) = entity_extract_ids($entity_type, $entity);

    // Check if the entity is new or an existing one.
    // TODO: make sure is_new is on all entity types.
    $op = empty($entity->is_new) ? 'update' : 'insert';
    if ($op == 'insert') {
      $field_values = array(
        'gid' => $gid,
        'state' => $state,
        'created' => $values['created'],
        'membership type' => $values['membership type'],
        'membership fields' => $values['membership fields'],
      );
      $wrapper[] = $field_values;
    }
    else {
      $existing_key = FALSE;
      if (!empty($wrapper)) {
        foreach ($wrapper as $key => $value) {
          if ($gid == $value['gid']) {
            $existing_key = $key;
            break;
          }
        }
      }
      if ($existing_key === FALSE) {

        // Update field with new value.
        $field_values = array(
          'gid' => $gid,
          // This values won't be saved, just passed along.
          // @see og_entity_presave().
          'state' => $state,
          'membership type' => $values['membership type'],
          'membership fields' => $values['membership fields'],
        );
        $entity->{$property}[LANGUAGE_NONE][] = $field_values;
      }
      else {
        $group_membership = og_get_group_membership($wrapper[$existing_key]['gid'], $entity_type, $id);
        if ($group_membership->state != $state || $group_membership->type != $values['membership type']) {
          $wrapper[$existing_key]['state'] = $state;

          // This value won't be saved in the field, just passed along and
          // processed later on.
          // @see og_entity_insert()
          // @see og_og_entity_insert()
          $wrapper[$existing_key] += array(
            'membership type' => $values['membership type'],
            'membership fields' => $values['membership fields'],
          );
        }
        else {

          // Nothing changed, so no need to save.
          $save = FALSE;
        }
      }
    }
    if ($save) {

      // Make sure a group isn't created for this entity. This is used for cases
      // that a user object can be a group, however we don't want
      // og_field_crud_group() to actually make it a group when the field
      // attachers are invoked.
      $entity->og_skip_group_create = TRUE;
      entity_save($entity_type, $entity);
      og_invalidate_cache();

      // Unset our temporary property.
      unset($entity->og_skip_group_create);
    }
  }
  return $entity;
}