<?php

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

/**
 * Implements hook_schema().
 */
function uc_role_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' => 'varchar',
        'length' => 64,
        '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' => 'varchar',
        'length' => 64,
        '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_uninstall().
 */
function uc_role_uninstall() {
  db_delete('uc_product_features')
    ->condition('fid', 'role')
    ->execute();
}
