<?php
// $Id: uc_addresses.install,v 1.11 2009/12/10 17:21:39 freixas Exp $

/**
 * Implementation of hook_schema().
 */
function uc_addresses_schema() {
  $schema['uc_addresses'] = array(
    'description' => t('Ubercart customer addresses'),
    'fields' => array(
      'aid' => array(
        'description' => t('The address ID'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => t('The ID of the user who owns this address'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
	'default' => 0,
      ),
      'first_name' => array(
        'description' => t('The addressee\'s first name'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'last_name' => array(
        'description' => t('The addressee\'s last name'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'phone' => array(
        'description' => t('The addressee\'s phone number'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'company' => array(
        'description' => t('The addressee\'s company name'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'street1' => array(
        'description' => t('The addressee\'s residence number and street'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'street2' => array(
        'description' => t('The addressee\'s residence number and street (continued)'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'city' => array(
        'description' => t('The addressee\'s city of residence'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'zone' => array(
        'description' => t('The addressee\'s zone of residence'),
        'type' => 'int',
        'size' => 'medium',
        'not null' => TRUE,
	'default' => 0,
      ),
      'postal_code' => array(
        'description' => t('The addressee\'s postal code'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
	'default' => '',
      ),
      'country' => array(
        'description' => t('The addressee\'s country of residence'),
        'type' => 'int',
        'size' => 'medium',
	'unsigned' => TRUE,
        'not null' => TRUE,
	'default' => 0,
      ),
      'address_name' => array(
        'description' => t('The name used to access this address'),
        'type' => 'varchar',
        'length' => 20,
        'not null' => FALSE,
      ),
      'created' => array(
        'description' => t('The date this address was created'),
        'type' => 'int',
        'not null' => TRUE,
	'default' => 0,
      ),
      'modified' => array(
        'description' => t('The date this address was last modified'),
        'type' => 'int',
        'not null' => TRUE,
	'default' => 0,
      ),
    ),
    'indexes' => array(
      'aid_uid_idx' => array('aid', 'uid'),
    ),
    'primary key' => array('aid'),
  );

  $schema['uc_addresses_defaults'] = array(
    'description' => t('Associates a user with an address: the default address'),
    'fields' => array(
      'aid' => array(
        'description' => t('The ID of the address in the uc_adresses table'),
        'type' => 'int',
        'unsigned' => 1,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => t('The ID of the user'),
        'type' => 'int',
        'unsigned' => 1,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('aid', 'uid'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function uc_addresses_install() {
  drupal_install_schema('uc_addresses');
}

/**
 * First update: remove the old incorrect sequence number name and add
 * the correct new one. Copy over the sequence number.
 */
function uc_addresses_update_1() {
  $items = array();
  $seq = db_query("SELECT * FROM {sequences} WHERE name = 'uc_addresses'");
  if (db_num_rows($seq) == 1) {
    $obj = db_fetch_object($seq);
    $items[] = update_sql("DELETE FROM {sequences} WHERE name = 'uc_addresses'");
    $items[] = update_sql("INSERT INTO {sequences} (name, id) VALUES('{uc_addresses}_aid'," . $obj->id . ")");
  }
  return $items;
}

/**
 * Second update: add nickname field.
 */
function uc_addresses_update_2() {
  $items = array();
  $items[] = update_sql("ALTER TABLE {uc_addresses} ADD COLUMN address_name VARCHAR(20) DEFAULT NULL AFTER country");
  return $items;
}

/**
 * First Drupal 6 update: standardize database definitions as per uc_orders
 */
function uc_addresses_update_6000() {
  db_drop_primary_key($ret, 'uc_addresses');
  db_drop_index($ret, 'uc_addresses', 'aid_uid_idx');
  db_change_field($ret, 'uc_addresses', 'aid', 'aid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('aid')));
  db_change_field($ret, 'uc_addresses', 'uid', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_addresses', 'first_name', 'delivery_first_name', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'last_name', 'delivery_last_name', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'phone', 'delivery_phone', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'company', 'delivery_company', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'street1', 'delivery_street1', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'street2', 'delivery_street2', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'city', 'delivery_city', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'zone', 'delivery_zone', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'medium', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_addresses', 'postal_code', 'delivery_postal_code', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_change_field($ret, 'uc_addresses', 'country', 'delivery_country', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'medium', 'not null' => TRUE, 'default' => 0));
  db_add_index($ret, 'uc_addresses', 'aid_uid_idx', array('aid', 'uid'));

  db_drop_primary_key($ret, 'uc_addresses_defaults');
  db_change_field($ret, 'uc_addresses_defaults', 'aid', 'aid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE));
  db_change_field($ret, 'uc_addresses_defaults', 'uid', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_primary_key($ret, 'uc_addresses_defaults', array('aid', 'uid'));

  return $ret;
}

/**
 * Second Drupal 6 update: undo the first Drupal 6 update. Not sure
 * why I did it. Addresses can be used for delivery OR billing. Also,
 * people who do not uc_addresses already installed will get a
 * different database than those who update.
 */
function uc_addresses_update_6001() {
  $ret = array();

// Assume if one is bad, they are all bad

  if (db_column_exists('uc_addresses', 'delivery_first_name')) {
    db_change_field($ret, 'uc_addresses', 'delivery_first_name', 'first_name', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_last_name', 'last_name', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_phone', 'phone', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_company', 'company', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_street1', 'street1', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_street2', 'street2', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_city', 'city', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_zone', 'zone', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'medium', 'not null' => TRUE, 'default' => 0));
    db_change_field($ret, 'uc_addresses', 'delivery_postal_code', 'postal_code', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    db_change_field($ret, 'uc_addresses', 'delivery_country', 'country', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'medium', 'not null' => TRUE, 'default' => 0));
  }
  return $ret;
}

/**
 * Implementation of hook_uninstall().
 */
function uc_addresses_uninstall() {
  drupal_uninstall_schema('uc_addresses');

  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_addresses%'");
}
