<?php
// $Id: uc_followup.install,v 1.1.2.1 2010/06/30 00:19:49 neochief Exp $

/**
 * @file
 * UC Follow Uncompleted install and uninstall routine.
 */
 
/**
 * Implementation of hook_schema().
 */
function uc_followup_schema() {
  $schema['uc_followup'] = array(
    'fields' => array(
      'followup_id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'status' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'order_status' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'hours_past' => array(
        'type' => 'int',
        'default' => 24,
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'is_recurring' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'hours_recurring' => array(
        'type' => 'int',
        'default' => 24,
        'unsigned' => FALSE,
        'not null' => TRUE,
      ),
      'repeat_after' => array(
        'type' => 'int',
        'default' => 0,
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'repeat_max' => array(
        'type' => 'int',
        'default' => 0,
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'sender' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'subject' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE
      ),
    ),
    'primary key' => array('followup_id'),
  );

  $schema['uc_followup_sent'] = array(
    'fields' => array(
      'follow_id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'order_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'followup_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'manual' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'date' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'email' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'sender' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'subject' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE
      ),
    ),
    'primary key' => array('follow_id'),
    'indexes' => array(
      'order_id' => array('order_id'),
      'order_id_followup_id' => array('order_id', 'followup_id'),
    ),
  );

  return $schema;
}


/**
 * Implementation of hook_install()
 */
function uc_followup_install() {
  drupal_install_schema('uc_followup');
}

/**
 * Implementation of hook_uninstall()
 */
function uc_followup_uninstall() {
  drupal_uninstall_schema('uc_followup');
}


/**
 * Implementation of hook_update_N().
 * Support recurring orders.
 */
function uc_followup_update_6001() {
  $ret = array();
  
  db_add_field($ret, 'uc_followup', 'is_recurring', array(
    'type' => 'int',
    'size' => 'tiny',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ));

  db_add_field($ret, 'uc_followup', 'hours_recurring', array(
    'type' => 'int',
    'default' => 24,
    'unsigned' => FALSE,
    'not null' => TRUE,
  ));
  
  $ret[] = array('success' => TRUE, 'query' => t('Updated Schema to match recurring fields.'));
  
  return $ret;
}
