<?php

/**
 * @file
 * Install hooks for uc_usps.module.
 */

/**
 * Implements hook_requirements().
 */
function uc_usps_requirements($phase) {
  $t = get_t();

  $has_simplexml = extension_loaded('SimpleXML');
  $requirements['uc_usps_simplexml'] = array(
    'title' => $t('SimpleXML'),
    'value' => $has_simplexml ? $t('Enabled') : $t('Not found'),
  );
  if (!$has_simplexml) {
    $requirements['uc_usps_simplexml']['severity'] = REQUIREMENT_ERROR;
    $requirements['uc_usps_simplexml']['description'] = $t('USPS requires the PHP <a href="!simplexml_url">SimpleXML</a> library.', array('!simplexml_url' => 'http://www.php.net/simplexml'));
  }

  return $requirements;
}

/**
 * Implements hook_schema().
 */
function uc_usps_schema() {
  $schema = array();

  $schema['uc_usps_products'] = array(
    'description' => 'Stores product information for USPS shipping quotes.',
    'fields' => array(
      'vid' => array(
        'description' => 'The {uc_products}.vid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {uc_products}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'container' => array(
        'description' => 'The package type in which the product will be shipped.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('vid'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
function uc_usps_install() {
  drupal_install_schema('uc_usps');
}

/**
 * Implements hook_uninstall().
 */
function uc_usps_uninstall() {
  drupal_uninstall_schema('uc_usps');
  variable_del('uc_usps_markup');

  variable_del('uc_usps_user_id');
  variable_del('uc_usps_services');
  variable_del('uc_usps_env_services');
  variable_del('uc_usps_intl_services');
  variable_del('uc_usps_intl_env_services');
  variable_del('uc_usps_rate_markup');
  variable_del('uc_usps_rate_markup_type');
  variable_del('uc_usps_weight_markup');
  variable_del('uc_usps_weight_markup_type');
  variable_del('uc_usps_all_in_one');
  variable_del('uc_usps_insurance');
  variable_del('uc_usps_delivery_confirmation');
  variable_del('uc_usps_signature_confirmation');
  variable_del('uc_usps_online_rates');
}

function uc_usps_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN qty");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN length");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN width");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN height");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN units");
    break;
  }
  return $ret;
}

function uc_usps_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} ADD COLUMN vid mediumint(9) unsigned NOT NULL default 0 FIRST");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP INDEX nid");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_usps_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} ADD PRIMARY KEY (vid)");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_usps_products', 'vid', 'integer unsigned', array('not null' => TRUE, 'default' => 0));
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP CONSTRAINT {uc_usps_products}_nid_key");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_usps_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} ADD PRIMARY KEY (vid)");
    break;
  }
  return $ret;
}

function uc_usps_update_3() {
  $ret = array();
  $markup = variable_get('uc_usps_markup', '0%');
  if (strpos($markup, '%') !== FALSE) {
    variable_set('uc_usps_markup_type', 'percentage');
    variable_set('uc_usps_markup', floatval(rtrim($markup, '%')));
  }
  elseif (strpos($markup, '$') !== FALSE) {
    variable_set('uc_usps_markup_type', 'currency');
    variable_set('uc_usps_markup', floatval(ltrim($markup, '$')));
  }
  elseif (floatval($markup)) {
    variable_set('uc_usps_markup_type', 'multiplier');
    variable_set('uc_usps_markup', floatval($markup));
  }
  $ret[] = array('success' => TRUE, 'query' => 'Added USPS markup type.');
  return $ret;
}

function uc_usps_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} CHANGE container container varchar(255) NOT NULL");
    break;
    case 'pgsql':
      db_change_column($ret, 'uc_usps_products', 'container', 'container', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
    break;
  }
  return $ret;
}

function uc_usps_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_change_column($ret, 'uc_usps_prodcts', 'vid', 'vid', 'int_unsigned', array('not null' => TRUE, 'default' => 0));
      db_change_column($ret, 'uc_usps_prodcts', 'nid', 'nid', 'int_unsigned', array('not null' => TRUE, 'default' => 0));
    break;
  }
  return $ret;
}

function uc_usps_update_6000() {
  $ret = array();

  db_drop_primary_key($ret, 'uc_usps_products');
  db_change_field($ret, 'uc_usps_products', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('vid')));
  db_change_field($ret, 'uc_usps_products', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

/**
 * Set default weight for new shipping type.
 */
function uc_usps_update_6001() {
  $shipping_type_weights = variable_get('uc_quote_type_weight', NULL);
  if (isset($shipping_type_weights)) {
    $shipping_type_weights['envelope'] = -1;
    variable_set('uc_quote_type_weight', $shipping_type_weights);
  }

  $services = variable_get('uc_usps_services', NULL);
  if (isset($services)) {
    $env_services = array(
      'zero' => t('U.S.P.S. First-Class Mail'),
      'zeroFlat' => t('U.S.P.S. First-Class Flat'),
      12 => t('U.S.P.S. First-Class Postcard Stamped'),
      1 => t('U.S.P.S. Priority Mail'),
      16 => t('U.S.P.S. Priority Mail Flat-Rate Envelope'),
      2 => t('U.S.P.S. Express Mail PO to PO'),
      3 => t('U.S.P.S. Express Mail PO to Addressee'),
      13 => t('U.S.P.S. Express Mail Flat-Rate Envelope'),
    );
    $new_services = array();
    foreach ($env_services as $id => $value) {
      $new_services[$id] = $services[$id];
      unset($services[$id]);
    }
    variable_set('uc_usps_env_services', $new_services);
    variable_set('uc_usps_services', $services);
  }

  $services = variable_get('uc_usps_intl_services', NULL);
  if (isset($services)) {
    $intl_env_services = array(
      13 => t('First Class Mail International Letter'),
      14 => t('First Class Mail International Flat'),
      2 => t('Priority Mail International'),
      8 => t('Priority Mail International Flat Rate Envelope'),
      4 => t('Global Express Guaranteed'),
      1 => t('Express Mail International (EMS)'),
      10 => t('Express Mail International (EMS) Flat Rate Envelope'),
    );
    $new_services = array();
    foreach ($intl_env_services as $id => $value) {
      $new_services[$id] = $services[$id];
      unset($services[$id]);
    }
    variable_set('uc_usps_intl_env_services', $new_services);
    variable_set('uc_usps_intl_services', $services);
  }

  return array();
}
