<?php

/**
 * @file
 * masquerade.install
 *
 * Install, uninstall and update hooks for the Masquarade module.
 */

/**
 * Implements hook_schema().
 *
 * @return array
 */
function masquerade_schema() {
  return array(
    'masquerade' => array(
      'description' => 'Each masquerading user has their session recorded into the masquerade table. Each record represents a masquerading user.',
      'fields' => array(
        'sid' => array(
          'description' => 'The current session for this masquerading user corresponding to their {sessions}.sid.',
          'type' => 'varchar',
          'length' => '64',
          'not null' => TRUE,
          'default' => ''),
        'uid_from' => array(
          'description' => 'The {users}.uid corresponding to a session.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => '10'),
        'uid_as' => array(
          'description' => 'The {users}.uid this session is masquerading as.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => '10'),
      ),
      'indexes' => array(
        'sid' => array('sid', 'uid_from'),
        'sid_2' => array('sid', 'uid_as'),
      ),
    ),
    'masquerade_users' => array(
      'description' => 'Per-user permission table granting permissions to switch as a specific user.',
      'fields' => array(
        'uid_from' => array(
          'description' => 'The {users}.uid that can masquerade as {masquerade_users}.uid_to.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => 10,
        ),
        'uid_to' => array(
          'description' => 'The {users}.uid that {masquerade_users}.uid_from can masquerade as.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => 10,
        ),
      ),
      'primary key' => array('uid_from', 'uid_to'),
    ),
  );
}

/**
 * Implements hook_install().
 */
function masquerade_install() {
  db_update('system')
    ->fields(array('weight' => -10))
    ->condition('name', 'masquerade')
    ->execute();
}

/**
 * Implements hook_uninstall().
 */
function masquerade_uninstall() {
  variable_del('masquerade_test_user');
  variable_del('masquerade_admin_roles');
  variable_del('masquerade_quick_switches');
}

/**
 * Reformat variable value.
 */
function masquerade_update_6001() {
  variable_set('masquerade_quick_switches', implode(',', variable_get('masquerade_quick_switches', array())));
}

/**
 * Make the sid column match the length of the core sessions table (64 characters).
 */
function masquerade_update_6002() {
  db_drop_index('masquerade', 'sid');
  db_drop_index('masquerade', 'sid_2');
  db_change_field('masquerade', 'sid', 'sid', array(
    'type' => 'varchar',
    'length' => '64',
    'not null' => TRUE,
    'default' => '')
  );
  db_add_index('masquerade', 'sid', array('sid', 'uid_from'));
  db_add_index('masquerade', 'sid_2', array('sid', 'uid_as'));
}

/**
 * Change masquerade_quick_switches variable to store a serialized array of
 * user ID's. Reverts update 6001.
 */
function masquerade_update_6003() {
  $users = variable_get('masquerade_quick_switches', NULL);
  if (!empty($users)) {
    $user_ids = drupal_explode_tags($users);
    if (!empty($user_ids)) {
      variable_set('masquerade_quick_switches', $user_ids);
    }
  }
  else {
    variable_del('masquerade_quick_switches');
  }
}

/**
 * Set the weight of the masquerade module to -10, but only if it hasn't
 * previously been changed.
 */
function masquerade_update_6004() {
  db_update('system')
    ->fields(array('weight' => -10))
    ->condition('name', 'masquerade')
    ->execute();
}

/**
 * Add a table storing specific user pairings a user can masquerade as.
 */
function masquerade_update_6005() {
  $schema = array(
    'masquerade_users' => array(
      'fields' => array(
        'uid_from' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => 10,
        ),
        'uid_to' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => 10,
        ),
      ),
      'primary key' => array('uid_from', 'uid_to'),
    )
  );
  db_create_table('masquerade_users', $schema['masquerade_users']);
}

/**
 * Update masquerade block delta.
 */
function masquerade_update_7000() {
  db_update('block')
    ->fields(array('delta' => 'masquerade'))
    ->condition('module', 'masquerade')
    ->condition('delta', 0)
    ->execute();
}

/**
 * Update masquerade block caching.
 */
function masquerade_update_7001() {
  db_update('block')
    ->fields(array('cache' => DRUPAL_NO_CACHE))
    ->condition('module', 'masquerade')
    ->condition('delta', 'masquerade')
    ->execute();
}
