<?php

/**
 * @file
 * Install, update, and uninstall functions for the Organic groups module.
 */

/**
 * Implements hook_install().
 */
function og_install() {
  _field_info_collate_fields(TRUE);
  _field_info_collate_types(TRUE);
  // Add group audience to users by default.
  og_create_field(OG_AUDIENCE_FIELD, 'user', 'user');

  // Add "User request" to the default group membership type.
  $field_name = OG_MEMBERSHIP_REQUEST_FIELD;
  if (!field_info_field($field_name)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'text_long',
      'entity_types' => array('og_membership'),
      'cardinality' => 1,
      // Although we would prefer to not allow deleting this field, we can not
      // lock it, as it will not allow adding it to other bundles.
      'locked' => FALSE,
    );
    $field = field_create_field($field);
  }

  if (!field_info_instance('og_membership', $field_name, OG_MEMBERSHIP_TYPE_DEFAULT)) {
    $instance = array(
      'field_name' => $field_name,
      'bundle' => OG_MEMBERSHIP_TYPE_DEFAULT,
      'entity_type' => 'og_membership',
      'label' => t('Request message'),
      'description' => t('This is the text a user may send to the group administrators.'),
      'required' => FALSE,
    );
    field_create_instance($instance);
  }
}

/**
 * Implements hook_uninstall().
 */
function og_uninstall() {
  $vars = array(
    'og_context',
    'og_group_manager_full_access',
    'og_skip_access',
    'og_update_batch_size',
    'og_upgrade_7001',
    'og_node_access_strict',
  );
  foreach ($vars as $var) {
    variable_del($var);
  }
  // Remove all fields owned by organic groups.
  foreach (field_read_fields(array('module' => 'og'), array('include_inactive' => TRUE)) as $field){
    // We need to invoke the storage_delete hook and the field_delete_instance
    // here, even though this should be done by field_delete_field().
    // The current version of this function only works on active fields,
    // and during uninstall, the fields will be inactive.
    // Can be removed once
    // http://drupal.org/node/943772
    // is fixed.
    module_invoke($field['storage']['module'], 'field_storage_delete_field', $field);
    $instances = field_read_instances(array('field_id' => $field['id']), array('include_inactive' => TRUE));
    foreach ($instances as $instance) {
      field_delete_instance($instance, FALSE);
    }
    field_delete_field($field['field_name']);
  }
}


/**
 * Implements hook_schema().
 */
function og_schema() {
  return og_schema_info();
}

function og_schema_info() {
  $schema = array();

  $schema['og'] = array(
    'description'     => 'Store information related to the groups.',
    'fields'          => array(
      'gid' => array(
        'description' => "The group's unique ID.",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'etid' => array(
        'description' => "The entity ID.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => "The entity label (e.g. node's title).",
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
      'state' => array(
        'description' => 'The state of the group (i.e. active or disabled).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('gid'),
    'indexes' => array(
      // Entity index; When searching for an entity, we use both the id and type.
      'entity' => array('etid', 'entity_type'),
    ),
  );

  $schema['og_role_permission'] = array(
    'description' => 'Stores the permissions assigned to user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Foreign Key: {role}.rid.',
      ),
      'permission' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'A single permission granted to the role identified by rid.',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The module declaring the permission.",
      ),
    ),
    'primary key' => array('rid', 'permission'),
    'indexes' => array(
      'permission' => array('permission'),
    ),
    'foreign keys' => array(
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  $schema['og_role'] = array(
    'description' => 'Stores user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique role ID.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Unique role name per group.',
      ),
    ),
    'primary key' => array('rid'),
  );

  $schema['og_users_roles'] = array(
    'description' => 'Maps users to roles.',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {users}.uid for user.',
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {og_role}.rid for role.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
    ),
    'primary key' => array('uid', 'rid', 'gid'),
    'indexes' => array(
      'rid' => array('rid'),
    ),
    'foreign keys' => array(
      'user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  $schema['og_membership_type'] = array(
    'description' => 'The group membership type.',
    'fields' => array(
      // Although the "name" should be enough as the primary key, the numeric ID
      // is required for the internal use of entity API.
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Numeric group membership type ID.',
      ),
      'name' => array(
        'description' => 'The unified identifier for a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'Description for this group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // This is equivilent to ENTITY_CUSTOM.
        'default' =>  0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'name' => array('name'),
    ),
  );

  $schema['og_membership'] = array(
    'description' => 'The group membership table.',
    'fields' => array(
      'id' => array(
        'description' => "The group membership's unique ID.",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'Reference to a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'etid' => array(
        'description' => "The entity ID.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'state' => array(
        'description' => 'The state of the group content.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group content was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
    'indexes' => array(
      // Entity index; When searching for an entity, we use both the id and type.
      'entity' => array('etid', 'entity_type'),
      'gid' => array('gid'),
    ),
    'foreign keys' => array(
      'og_membership_type' => array(
        'table' => 'og_membership_type',
        'columns' => array('name' => 'name'),
      ),
      'group' => array(
        'table' => 'og',
        'columns' => array('gid' => 'gid'),
      ),
    ),
  );

  return $schema;
}

/**
 * Return the schema for upgrade 7000.
 */
function og_schema_7000_info() {
  $schema = array();

  $schema['og'] = array(
    'description'     => 'Store information related to the groups.',
    'fields'          => array(
      'gid' => array(
        'description' => "The group's unique ID.",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'etid' => array(
        'description' => "The entity ID.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => "The entity label (e.g. node's title).",
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
      'state' => array(
        'description' => 'The state of the group (i.e. active or disabled).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('gid'),
  );

  $schema['og_role_permission'] = array(
    'description' => 'Stores the permissions assigned to user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Foreign Key: {role}.rid.',
      ),
      'permission' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'A single permission granted to the role identified by rid.',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The module declaring the permission.",
      ),
    ),
    'primary key' => array('rid', 'permission'),
    'indexes' => array(
      'permission' => array('permission'),
    ),
    'foreign keys' => array(
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  $schema['og_role'] = array(
    'description' => 'Stores user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique role ID.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Unique role name per group.',
      ),
    ),
    'primary key' => array('rid'),
  );

  $schema['og_users_roles'] = array(
    'description' => 'Maps users to roles.',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {users}.uid for user.',
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {og_role}.rid for role.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
    ),
    'primary key' => array('uid', 'rid', 'gid'),
    'indexes' => array(
      'rid' => array('rid'),
    ),
    'foreign keys' => array(
      'user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  $schema['og_membership_type'] = array(
    'description' => 'The group membership type.',
    'fields' => array(
      // Although the "name" should be enough as the primary key, the numeric ID
      // is required for the internal use of entity API.
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Numeric group membership type ID.',
      ),
      'name' => array(
        'description' => 'The unified identifier for a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'Description for this group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // This is equivilent to ENTITY_CUSTOM.
        'default' =>  0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'name' => array('name'),
    ),
  );

  $schema['og_membership'] = array(
    'description' => 'The group membership table.',
    'fields' => array(
      'id' => array(
        'description' => "The group membership's unique ID.",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Reference to a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'etid' => array(
        'description' => "The entity ID.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'state' => array(
        'description' => 'The state of the group content.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group content was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
    'foreign keys' => array(
      'og_membership_type' => array(
        'table' => 'og_membership_type',
        'columns' => array('name' => 'name'),
      ),
      'group' => array(
        'table' => 'og',
        'columns' => array('gid' => 'gid'),
      ),
    ),
  );

  return $schema;
}

/**
 * Implements hook_field_schema().
 */
function og_field_schema($field) {
  $columns = array(
    'gid' => array(
      'description' => 'The group unique ID.',
      'type' => 'float',
      'unsigned' => TRUE,
      'not null' => FALSE,
    ),
    // This columns should be deprecated and removed, as the group membership
    // entity takes care of it. However, there is currently no way to remove
    // them.
    'state' => array(
      'description' => 'The state of the group content.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
      'default' => '',
    ),
    'created' => array(
      'description' => 'The Unix timestamp when the group content was created.',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ),
  );
  return array(
    'columns' => $columns,
    'indexes' => array(
      'gid' => array('gid'),
    ),
    'foreign keys' => array(
      'og' => array(
        'table' => 'og',
        'columns' => array('gid' => 'gid'),
      ),
    ),
  );
}

/**
 * Upgrade from Organic groups 6 to 7.
 */
function og_update_7000(&$sandbox) {
  if (db_field_exists('og', 'nid') && !db_table_exists('d6_og')) {
    // Rename the old table, so we can populate the new {og} table using API
    // functions, that assume the new DB structure.
    db_rename_table('og', 'd6_og');
    db_rename_table('og_ancestry', 'd6_og_ancestry');
    db_rename_table('og_uid', 'd6_og_uid');

    // Add serial ID to d6_og* tables so we can keep track of records that were
    // processed.
    foreach (array('d6_og', 'd6_og_ancestry', 'd6_og_uid') as $table) {
      // Drop the current primary key, as we are adding a serial column.
      db_drop_primary_key($table);
      db_add_field($table, 'upgrade_id', array(
        'description' => 'This a serial ID that keeps track of records that were processed.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      array('primary key' => array('upgrade_id')));
    }

    $schema = og_schema_7000_info();
    foreach (array('og', 'og_role_permission', 'og_role', 'og_users_roles') as $table) {
      db_create_table($table, $schema[$table]);
    }

    // We can't call og_needs_migrate() directly, so just set the variable.
    variable_set('og_needs_migrate', TRUE);
    return t('Renamed Organic groups 6 tables. Enable Organic groups migrate module to continue the migration of data.');
  }
  return t('No change needed in Organic groups schema.');
}

/**
 * Add group membership entities instead of field data.
 */
function og_update_7001(&$sandbox) {
  $schema = og_schema_info();

  // Due to changes in the upgrade path (e.g. moving data migration to
  // og-migrate) some users already have the tables, so be careful about the
  // creation of these tables.
  if (!db_table_exists('og_membership')) {
    db_create_table('og_membership', $schema['og_membership']);
  }

  if (!db_table_exists('og_membership_type')) {
    db_create_table('og_membership_type', $schema['og_membership_type']);
  }
  // We can't call og_needs_migrate() directly, so just set the variable.
  variable_set('og_needs_migrate', TRUE);
  return t('Organic groups added group membership tables. Enable Organic groups migrate module to continue the migration of data.');
}

/**
 * Add indexes to og and og_membership tables.
 */
function og_update_7100(&$sandbox) {
  // Make sure we don't try to re-add an index that was added in previous
  // updates.
  if (!db_index_exists('og_membership', 'entity')) {
    db_add_index('og_membership', 'entity', array('etid', 'entity_type'));
  }
  if (!db_index_exists('og_membership', 'gid')) {
    db_add_index('og_membership', 'gid', array('gid'));
  }
  if (!db_index_exists('og', 'entity')) {
    db_add_index('og', 'entity', array('etid', 'entity_type'));
  }
  return t('Added table indexes for the following tables: @tables', array('@tables' => 'og, og_membership'));
}

/**
 * Rename the 'name' column to 'type' in {OG membership}.
 */
function og_update_7101() {
  if (db_field_exists('og_membership', 'type')) {
    // Return early if field already created in og_update_7000().
    return;
  }
  $column = array(
    'description' => 'Reference to a group membership type.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  );
  db_change_field('og_membership', 'name', 'type', $column);
}

/**
 * Add default value function to all audience field instances.
 */
function og_update_7102() {
  $instances = field_read_instances(array('field_name' => 'group_audience'));
  foreach ($instances as $instance) {
    $instance['default_value_function'] = 'og_field_audience_default_value';
    field_update_instance($instance);
  }
}

/**
 * Extend the length of permission field in og_role_permission.
 */
function og_update_7103() {
  $column = array(
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
    'description' => 'A single permission granted to the role identified by rid.',
  );

  if (db_field_exists('og_role_permission', 'permission')) {
    db_change_field('og_role_permission', 'permission', 'permission', $column);
  }

  return t('Updated column definition for {og_role_permission} table.');
}
