<?php

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

/**
 * Implements hook_schema().
 */
function uc_roles_schema() {
  $schema['uc_roles_products'] = array(
    'description' => 'Maps purchasable roles to Ubercart products.',
    'fields' => array(
      'rpid' => array(
        'description' => 'Primary key: the role-product id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'pfid' => array(
        'description' => 'The {uc_product_features}.pfid of the product feature this is attached to.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the node this role feature is attached to.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'model' => array(
        'description' => 'The product model.',
        'type' => 'varchar',
        'length' => 255,
        'default' => NULL,
      ),
      'rid' => array(
        'description' => 'The {role}.rid that is purchased with the attached product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),

      // Start of expiration period
      // Not actually implemented yet, this is a placeholder.
      'start_override' => array(
        'description' => 'Override the store default start time? 1 => Yes. 0 => No.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => FALSE,
        'default' => 0,
      ),
      'start_time' => array(
        'description' => 'Role expiration start time. 0 signifies to start at product purchase.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
      ),

      // End of expiration period
      'end_override' => array(
        'description' => 'Override the default end time? 1 => Yes. 0 => No.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => FALSE,
        'default' => 0,
      ),
      'end_time' => array(
        'description' => 'Role expiration end time. 0 signifies to use a relative expiration.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
      ),
      'duration' => array(
        'description' => 'The duration of the granted role, using the value of granualarity.',
        'type' => 'int',
        'size' => 'small',
      ),
      'granularity' => array(
        'description' => 'The units of time of duration.',
        'type' => 'varchar',
        'length' => 32,
      ),
      'shippable' => array(
        'description' => 'Is this role feature shippable? 1 => Yes. 0 => No.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'by_quantity' => array(
        'description' => 'Multiply any relative expiration by the quantity purchased? 1 => Yes. 0 => No.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
      'model' => array('model'),
      'rid' => array('rid'),
    ),
    'primary key' => array('rpid'),
    'foreign keys' => array(
      'uc_product_features' => array(
        'table' => 'uc_product_features',
        'columns' => array('pfid' => 'pfid'),
      ),
      'uc_products' => array(
        'table' => 'uc_products',
        'columns' => array('nid' => 'nid'),
      ),
      'role' => array(
        'table' => 'role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  $schema['uc_roles_expirations'] = array(
    'description' => 'Store expiration dates of purchased roles.',
    'fields' => array(
      'reid' => array(
        'description' => 'Primary key: the unique expiration id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'The {users}.uid owning the role.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'rid' => array(
        'description' => 'The {role}.rid of the purchased role.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'expiration' => array(
        'description' => 'The Unix timestamp indicating when the role will be removed from the user account.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'notified' => array(
        'description' => 'A flag indicating that the user was warned that the role will be removed soon.',
        'type' => 'int',
        'size' => 'tiny',
      ),
    ),
    'indexes' => array(
      'uid' => array('uid'),
      'rid' => array('rid'),
    ),
    'primary key' => array('reid'),
    'foreign keys' => array(
      'users' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'role' => array(
        'table' => 'role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  return $schema;
}

/**
 * Implements hook_update_last_removed().
 */
function uc_roles_update_last_removed() {
  // 7.x-3.0-beta2 and earlier were installed with schema version 0,
  // which causes update.php to fail.
  return drupal_get_installed_schema_version('uc_roles') == 0 ? 0 : 6004;
}

/**
 * Implements hook_uninstall().
 */
function uc_roles_uninstall() {
  db_delete('uc_product_features')
    ->condition('fid', 'role')
    ->execute();
  db_delete('variable')
    ->condition('name', 'uc_roles_%', 'LIKE')
    ->execute();
}

/**
 * Remove unused variables.
 */
function uc_roles_update_7300() {
  variable_del('uc_roles_default_expiration_header');
  variable_del('uc_roles_default_expiration_message');
  variable_del('uc_roles_default_expiration_title');
}
