<?php

/**
 * @file
 * Installation hooks for the jplayer_protect module.
 */

/**
 * Implements hook_schema().
 */
function jplayer_protect_schema() {
  $schema = array();
  $schema['jplayer_protect_denied'] = array(
    'description' => 'Contains user statistics for when a user is blocked from downloading a file.',
    'fields' => array(
      'did' => array(
        'description' => 'Primary Key: Unique denied event ID.',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'The user ID of the user.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'fid' => array(
        'description' => 'The file ID that was denied access.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'hostname' => array(
        'description' => 'The hostname of the user that was denied access.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'timestamp' => array(
        'description' => 'The last time this user was denied access.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('did'),
    'indexes' => array(
      'uid' => array('uid'),
      'fid' => array('fid'),
      'hostname' => array('hostname'),
      'timestamp' => array('timestamp'),
    ),
  );

  return $schema;
}

/**
 * Add a serial column for the denied statistics.
 */
function jplayer_protect_update_7001() {
  $ret = array();
  $spec = array(
    'description' => 'Primary Key: Unique denied event ID.',
    'type' => 'serial',
    'not null' => TRUE,
  );
  $keys_new = array(
    'primary key' => array('did'),
  );

  db_drop_primary_key('jplayer_protect_denied');
  db_add_field('jplayer_protect_denied', 'did', $spec, $keys_new);

  return $ret;
}

/**
 * Implements hook_uninstall().
 */
function jplayer_protect_uninstall() {
  variable_del('jplayer_protect');
}

