You are here

uc_usps.install in Ubercart 5

File

shipping/uc_usps/uc_usps.install
View source
<?php

function uc_usps_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {uc_usps_products} (\n        `vid` mediumint(9) unsigned NOT NULL default 0,\n        `nid` mediumint(9) unsigned NOT NULL default 0,\n        `container` varchar(255) NOT NULL,\n        PRIMARY KEY (`vid`)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {uc_usps_products} (\n        vid int_unsigned NOT NULL default 0,\n        nid int_unsigned NOT NULL default 0,\n        container varchar(255) NOT NULL default '',\n        PRIMARY KEY (vid)\n      );");
      break;
  }
}
function uc_usps_uninstall() {
  db_query("DROP TABLE {uc_usps_products}");
  variable_del('uc_usps_user_id');
  variable_del('uc_usps_services');
  variable_del('uc_usps_markup');
}
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, '%')));
  }
  else {
    if (strpos($markup, '$') !== false) {
      variable_set('uc_usps_markup_type', 'currency');
      variable_set('uc_usps_markup', floatval(ltrim($markup, '$')));
    }
    else {
      if (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;
}