<?php
// $Id: uc_fedex.install,v 1.16 2010/03/17 08:30:27 tr Exp $

/**
 * @file
 * Database installation, uninstallation, and update functions for the
 * FedEx Shipping Quotes module
 *
 * @author Tim Rohaly.    <http://drupal.org/user/202830>
 * @version $Id: uc_fedex.install,v 1.16 2010/03/17 08:30:27 tr Exp $
 */


/**
 * Implementation of hook_requirements().
 */
function uc_fedex_requirements($phase) {
  $t = get_t();

  // Check that the PHP SOAP extension is loaded
  $has_soap = extension_loaded('soap');

  $requirements['uc_fedex_soap'] = array(
    'title' => $t('SOAP'),
    'value' => $has_soap ? $t('Installed') : $t('Not Installed'),
  );

  if (!$has_soap) {
    $requirements['uc_fedex_soap']['severity'] = REQUIREMENT_ERROR;
    $requirements['uc_fedex_soap']['description'] = $t("The FedEx API requires the PHP <a href='!soap_url'>SOAP</a> library.", array('!soap_url' => 'http://www.php.net/soap'));
  }

  return $requirements;
}


/**
 * Implementation of hook_schema().
 */
function uc_fedex_schema() {
  $schema = array();

  $schema['uc_fedex_products'] = array(
    'description' => 'Stores product information for FedEx 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,
      ),
      'pkg_type' => array(
        'description' => 'The type of package in which the product will be shipped.',
        'type'     => 'varchar',
        'length'   => 2,
        'not null' => TRUE,
        'default'  => '',
      ),
    ),
    'primary key' => array('vid'),
  );

  return $schema;
}


/**
 * Implementation of hook_install().
 */
function uc_fedex_install() {
  drupal_install_schema('uc_fedex');
}


/**
 * Implementation of hook_uninstall().
 *
 * Removes all tables and variables inserted into the
 * database by this module.
 */
function uc_fedex_uninstall() {

  drupal_uninstall_schema('uc_fedex');

  // Remove all module variables from the database
  variable_del('uc_fedex_user_credential_key');
  variable_del('uc_fedex_user_credential_password');
  variable_del('uc_fedex_account_number');
  variable_del('uc_fedex_freight_account_number');
  variable_del('uc_fedex_meter_number');
  variable_del('uc_fedex_server_role');

  variable_del('uc_fedex_ground_services');
  variable_del('uc_fedex_express_services');
  variable_del('uc_fedex_freight_services');
  variable_del('uc_fedex_quote_type');
  variable_del('uc_fedex_dropoff_type');
  variable_del('uc_fedex_package_type');
  variable_del('uc_fedex_all_in_one');
  variable_del('uc_fedex_insurance');

  variable_del('uc_fedex_rate_markup');
  variable_del('uc_fedex_rate_markup_type');
  variable_del('uc_fedex_weight_markup');
  variable_del('uc_fedex_weight_markup_type');

  variable_del('uc_fedex_address_validation');
  variable_del('uc_fedex_residential_quotes');
  variable_del('uc_fedex_checkout_validation');

  variable_del('uc_fedex_label_image_format');
  variable_del('uc_fedex_label_stock');
  variable_del('uc_fedex_label_orientation');
  variable_del('uc_fedex_label_lifetime');
}


/**
 * Implementation of hook_update().
 */
function uc_fedex_update_6200() {
  $ret = array();
  $schema = drupal_get_schema('uc_fedex_products', TRUE);
  db_create_table($ret, 'uc_fedex_products', $schema);

  return $ret;
}


/**
 * Implementation of hook_update().
 *
 * Refactor uc_fedex shipping method into three separate methods:
 * Ground, Express, and Freight.  Ensure that service selections
 * are transferred from the old method to the new methods. If FedEx
 * was enabled at admin/store/settings/quotes/methods, figure out
 * which of the new methods should be enabled based on the service
 * selections.
 */
function uc_fedex_update_6201() {
  $ret = array();

  $services = variable_get('uc_fedex_services', NULL);
  if (isset($services)) {
    $ground_services  = array();
    $freight_services = array();

    // Remove ground services from list of services
    if (isset($services['FEDEX_GROUND'])) {
        $ground_services['FEDEX_GROUND'] = $services['FEDEX_GROUND'];
      unset($services['FEDEX_GROUND']);
    }
    if (isset($services['GROUND_HOME_DELIVERY'])) {
        $ground_services['GROUND_HOME_DELIVERY'] = $services['GROUND_HOME_DELIVERY'];
      unset($services['GROUND_HOME_DELIVERY']);
    }

    // Remove freight services from list of services
    if (isset($services['FEDEX_1_DAY_FREIGHT'])) {
        $freight_services['FEDEX_1_DAY_FREIGHT'] = $services['FEDEX_1_DAY_FREIGHT'];
      unset($services['FEDEX_1_DAY_FREIGHT']);
    }
    if (isset($services['FEDEX_2_DAY_FREIGHT'])) {
        $freight_services['FEDEX_2_DAY_FREIGHT'] = $services['FEDEX_2_DAY_FREIGHT'];
      unset($services['FEDEX_2_DAY_FREIGHT']);
    }
    if (isset($services['FEDEX_3_DAY_FREIGHT'])) {
        $freight_services['FEDEX_3_DAY_FREIGHT'] = $services['FEDEX_3_DAY_FREIGHT'];
      unset($services['FEDEX_3_DAY_FREIGHT']);
    }
    if (isset($services['INTERNATIONAL_ECONOMY_FREIGHT'])) {
        $freight_services['INTERNATIONAL_ECONOMY_FREIGHT'] = $services['INTERNATIONAL_ECONOMY_FREIGHT'];
      unset($services['INTERNATIONAL_ECONOMY_FREIGHT']);
    }
    if (isset($services['INTERNATIONAL_PRIORITY_FREIGHT'])) {
        $freight_services['INTERNATIONAL_PRIORITY_FREIGHT'] = $services['INTERNATIONAL_PRIORITY_FREIGHT'];
      unset($services['INTERNATIONAL_PRIORITY_FREIGHT']);
    }
    if (isset($services['INTERNATIONAL_DISTRIBUTION_FREIGHT'])) {
        $freight_services['INTERNATIONAL_DISTRIBUTION_FREIGHT'] = $services['INTERNATIONAL_DISTRIBUTION_FREIGHT'];
      unset($services['INTERNATIONAL_DISTRIBUTUION_FREIGHT']);
    }

    // Create new variables with services separated out
    variable_set('uc_fedex_ground_services',  $ground_services);
    variable_set('uc_fedex_express_services', $services);
    variable_set('uc_fedex_freight_services', $freight_services);

    variable_del('uc_fedex_services', $services);

    // If FedEx quotes are enabled, figure out which services are
    // being used and make sure the correct new methods are enabled
    $enabled  = variable_get('uc_quote_enabled', array());
    if ($enabled['fedex']) {
      if (count($ground_services))  $enabled['fedex_ground']  = TRUE;
      if (count($services) == 0)    $enabled['fedex']         = FALSE;
      if (count($freight_services)) $enabled['fedex_freight'] = TRUE;
      variable_set('uc_quote_enabled', $enabled);
    }
  }

  return $ret;
}
