<?php
// $Id: webform_protected_downloads.install,v 1.1.2.3 2010/11/03 00:21:43 berliner Exp $

/**
 * Implementation of hook_schema().
 */

function webform_protected_downloads_schema() {
  
  $schema = array();
  
  $schema['wpd_access_hashes'] = array(
    'fields' => array(
      'sid' => array(
        'description' => 'The submission identifier',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'hash' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Hash to identify this submission if needed',
      ),
      'processed' => array(
        'description' => 'The timestamp when the submission has been processed',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'expires' => array(
        'description' => 'The timestamp when the download expires',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'used' => array(
        'description' => 'The timestamp when the hash has been last used',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('sid'),
  );
  
  $schema['wpd_protected_files'] = array(
    'fields' => array(
      'nid' => array(
        'description' => 'The node id of the protected webform',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'fid' => array(
        'description' => 'The file id that is protected',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'created' => array(
        'description' => 'The timestamp when the file has been protected',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('nid', 'fid'),
  );
  
  $schema['wpd_node_configuration'] = array(
    'fields' => array(
      'nid' => array(
        'description' => 'The node id of the protected webform',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'mail_field_cid' => array(
        'description' => 'The component id of the webform that is used as mail adress to confirm the download',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'mail_from' => array(
        'description' => 'The from address for the confimation mail',
        'type' => 'varchar',
        'length' => 255,
      ),
      'mail_subject' => array(
        'description' => 'The subject for the confimation mail',
        'type' => 'varchar',
        'length' => 255,
      ),
      'mail_body' => array(
        'description' => 'The body text for the confimation mail',
        'type' => 'text',
      ),
      'text_download_access' => array(
        'description' => 'The text for the download page, above the file listing',
        'type' => 'text',
      ),
      'text_download_access_format' => array(
        'description' => 'The input format used for the text, above the file listing',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
      'text_download_noaccess' => array(
        'description' => 'The text for the download page, if access can not be granted',
        'type' => 'text',
      ),
      'text_download_noaccess_format' => array(
        'description' => 'The input format used for the text, if access can not be granted',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
      'access_type' => array(
        'description' => 'The type of access, either 0 for one-time or 1 for repeated',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'expiration_download' => array(
        'description' => 'The timestamp when the download expires',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'expiration_session' => array(
        'description' => 'The timestamp when the session expires',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'retroactive' => array(
        'description' => 'The timestamp when the session expires',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'redirect' => array(
        'description' => 'Whether the user should be redirected to the downloads page after form submission',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('nid'),
  );
  
  return $schema;
  
}

/**
 * Add fields to store text formats.
 * Add a field to store redirect information.
 */
function webform_protected_downloads_update_7001() {
  if (!db_field_exists('wpd_node_configuration', 'text_download_access_format')) {
    db_add_field('wpd_node_configuration', 'text_download_access_format', array(
      'description' => 'The input format used for the text, above the file listing',
      'type' => 'varchar',
      'length' => '255',
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (!db_field_exists('wpd_node_configuration', 'text_download_noaccess_format')) {
    db_add_field('wpd_node_configuration', 'text_download_noaccess_format', array(
      'description' => 'The input format used for the text, if access can not be granted',
      'type' => 'varchar',
      'length' => '255',
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (!db_field_exists('wpd_node_configuration', 'redirect')) {
    db_add_field('wpd_node_configuration', 'redirect', array(
      'description' => 'Whether the user should be redirected to the downloads page after form submission',
      'type' => 'int',
      'size' => 'tiny',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ));
  }
}