<?php

/**
 * @file
 * Module install/uninstall script for pubdlcnt.
 *
 * Copyright 2009 Hideki Ito <hide@pixture.com> Pixture Inc.
 * Copyright 2017 Corey Halpin <chalpin@scout.wisc.edu>, Internet Scout
 *   Research Group
 * See LICENSE.txt for licensing terms.
 */

/**
 * Implements hook_install().
 */
function pubdlcnt_install() {
  $valid_extensions = '7z bz bz2 gz gzip hqx iso lha lzh pkg pdf rpm sea sit tar tbz tgz zip';

  // Set valid extensions to database:
  variable_set('pubdlcnt_valid_extensions', $valid_extensions);

  // Change module's weight:
  db_update('system')
    ->fields(array('weight' => 100))
    ->condition('name', 'pubdlcnt')
    ->execute();
}

/**
 * Implements hook_schema().
 */
function pubdlcnt_schema() {
  $schema['pubdlcnt'] = array(
    'fields' => array(
      'fid' => array(
        'description' => 'file ID (unique number)',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'node ID of which the file exists',
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'file name',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'url' => array(
        'description' => 'URL of the file',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'utime' => array(
        'description' => 'dowload date in unix timestamp',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'count' => array(
        'description' => 'total download count',
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('fid'),
  );
  $schema['pubdlcnt_history'] = array(
    'fields' => array(
      'id' => array(
        'description' => 'record ID (unique number)',
        'type' => 'serial',
        'unsigned' => FALSE,
        'not null' => TRUE,
      ),
      'fid' => array(
        'description' => 'file ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'utime' => array(
        'description' => 'dowload date in unix timestamp',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'count' => array(
        'description' => 'total download count',
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
  );

  $schema['pubdlcnt_ip'] = array(
    'fields' => array(
      'id' => array(
        'description' => 'record ID (unique number)',
        'type' => 'serial',
        'unsigned' => FALSE,
        'not null' => TRUE,
      ),
      'fid' => array(
        'description' => 'file ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'ip' => array(
        'description' => 'remote host IP address',
        'type' => 'varchar',
        'length' => 20,
        'not null' => FALSE,
        'default' => '',
      ),
      'utime' => array(
        'description' => 'download date in Unix timestamp',
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function pubdlcnt_uninstall() {
  // Remove tables.
  db_delete('variable')
    ->condition('name', 'pubdlcnt%', 'LIKE')
    ->execute();

  cache_clear_all('variables', 'cache');
}
