You are here

uc_flatrate.install in Ubercart 5

File

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

function uc_flatrate_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {uc_flatrate_products} (\n        `vid` mediumint(9) unsigned NOT NULL default 0,\n        `nid` mediumint(9) unsigned NOT NULL default 0,\n        `mid` mediumint(9) unsigned NOT NULL default 0,\n        `rate` decimal(10,2) NOT NULL default 0.00,\n        PRIMARY KEY (`vid`, `mid`)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      db_query("CREATE TABLE {uc_flatrate_methods} (\n        `mid` mediumint(9) unsigned NOT NULL default 0,\n        `title` varchar(255) NOT NULL default '',\n        `label` varchar(255) NOT NULL default '',\n        `base_rate` decimal(10,2) NOT NULL default 0.00,\n        `product_rate` decimal(10,2) NOT NULL default 0.00,\n        PRIMARY KEY (`mid`)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {uc_flatrate_products} (\n        vid int_unsigned NOT NULL default 0,\n        nid int_unsigned NOT NULL default 0,\n        mid int_unsigned NOT NULL default 0,\n        rate decimal(10,2) NOT NULL default 0.00,\n        PRIMARY KEY (vid, mid)\n      );");
      db_query("CREATE TABLE {uc_flatrate_methods} (\n        mid serial NOT NULL,\n        title varchar(255) NOT NULL default '',\n        label varchar(255) NOT NULL default '',\n        base_rate decimal(10,2) NOT NULL default 0.00,\n        product_rate decimal(10,2) NOT NULL default 0.00,\n        PRIMARY KEY (mid)\n      );");
      db_query("CREATE INDEX {uc_flatrate_methods}_mid_idx ON {uc_flatrate_methods}(mid)");
      break;
  }
}
function uc_flatrate_uninstall() {
  db_query("DROP TABLE {uc_flatrate_products}");
  db_query("DROP TABLE {uc_flatrate_methods}");
}
function uc_flatrate_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} ADD COLUMN vid mediumint(9) unsigned NOT NULL default 0 FIRST");
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} DROP INDEX nid");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_flatrate_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} ADD PRIMARY KEY (vid)");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_flatrate_products', 'vid', 'integer unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} DROP CONSTRAINT {uc_flatrate_products}_nid_key");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_flatrate_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} ADD PRIMARY KEY (vid)");
      break;
  }
  return $ret;
}
function uc_flatrate_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} CHANGE rate rate decimal(10,2) NOT NULL default 0.00");
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} ADD COLUMN mid mediumint(9) unsigned NOT NULL default 0 AFTER nid");
      $ret[] = update_sql("CREATE TABLE {uc_flatrate_methods} (\n        `mid` mediumint(9) unsigned NOT NULL default 0,\n        `title` varchar(255) NOT NULL default '',\n        `label` varchar(255) NOT NULL default '',\n        `base_rate` decimal(10,2) NOT NULL default 0.00,\n        `product_rate` decimal(10,2) NOT NULL default 0.00,\n        PRIMARY KEY (`mid`)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      break;
    case 'pgsql':
      db_change_column($ret, 'uc_flatrate_products', 'rate', 'rate', 'decimal(10,2)', array(
        'not null' => true,
        'default' => 0,
      ));
      db_add_column($ret, 'uc_flatrate_products', 'mid', 'mediumint(9) unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      $ret[] = update_sql("CREATE TABLE {uc_flatrate_methods} (\n        mid mediumint(9) unsigned NOT NULL default 0,\n        title varchar(255) NOT NULL default '',\n        label varchar(255) NOT NULL default '',\n        base_rate decimal(10,2) NOT NULL default 0.00,\n        product_rate decimal(10,2) NOT NULL default 0.00,\n        PRIMARY KEY (mid)\n      );");
      $ret[] = update_sql("CREATE INDEX {uc_flatrate_methods}_mid_idx ON {uc_flatrate_methods}(mid)");
      break;
  }
  $set_var = false;
  $enabled = variable_get('uc_quote_enabled', array());
  $weight = variable_get('uc_quote_method_weight', array(
    'flatrate' => 0,
    'order_rate' => 1,
  ));
  $base_rate = variable_get('uc_flatrate_product_base_rate', 0);
  $product_rate = variable_get('uc_flatrate_product_default', 0);
  $changes = db_num_rows(db_query("SELECT * FROM {uc_flatrate_products}"));
  $mid = db_next_id('{uc_flatrate_methods}_mid');
  $ret[] = update_sql("INSERT INTO {uc_flatrate_methods} (mid, title, label, base_rate, product_rate) VALUES (" . $mid . ", '" . t('Flat rate per product') . "', '" . t('Shipping rate') . "', " . $base_rate . ", " . $product_rate . ")");
  $ret[] = update_sql("UPDATE {uc_flatrate_products} SET mid = " . $mid);
  $enabled['flatrate_' . $mid] = $enabled['flatrate'];
  $weight['flatrate_' . $mid] = $weight['flatrate'];
  unset($enabled['flatrate'], $weight['flatrate']);
  if (db_table_exists('workflow_ng_cfgs') && ($configuration = db_fetch_object(db_query("SELECT data FROM {workflow_ng_cfgs} WHERE name = '%s'", 'uc_flatrate_get_quote')))) {
    $configuration->name = 'uc_flatrate_get_quote_' . $mid;
    $configuration->data = unserialize($configuration->data);
    $configuration->data['#event'] = 'get_quote_from_flatrate_' . $mid;
    $result = db_query("UPDATE {workflow_ng_cfgs} SET name = '" . $configuration->name . "', data = '" . serialize($configuration->data) . "' WHERE name = 'uc_flatrate_get_quote'");
    $ret[] = array(
      'success' => $result !== FALSE,
      'query' => check_plain("UPDATE {workflow_ng_cfgs} SET name = '" . $configuration->name . "', data = '" . serialize($configuration->data) . "' WHERE name = 'uc_flatrate_get_quote'"),
    );
  }
  else {
    $ret[] = array(
      'success' => FALSE,
      'query' => check_plain("UPDATE {workflow_ng_cfgs} SET name = '" . $configuration->name . "', data = '" . serialize($configuration->data) . "' WHERE name = 'uc_flatrate_get_quote'"),
    );
  }
  $order_rate = variable_get('uc_flatrate_order_rate', 0);
  $mid = db_next_id('{uc_flatrate_methods}_mid');
  $ret[] = update_sql("INSERT INTO {uc_flatrate_methods} (mid, title, label, base_rate) VALUES (" . $mid . ", '" . t('Flat rate per order') . "', '" . t('Shipping rate') . "', " . $order_rate . ")");
  $enabled['flatrate_' . $mid] = $enabled['order_rate'];
  $weight['flatrate_' . $mid] = $weight['order_rate'];
  unset($enabled['order_rate'], $weight['order_rate']);
  if (db_table_exists('workflow_ng_cfgs') && ($configuration = db_fetch_object(db_query("SELECT data FROM {workflow_ng_cfgs} WHERE name = '%s'", 'uc_flatrate_get_quote_by_order')))) {
    $configuration->name = 'uc_flatrate_get_quote_' . $mid;
    $configuration->data = unserialize($configuration->data);
    $configuration->data['#event'] = 'get_quote_from_flatrate_' . $mid;
    $result = db_query("UPDATE {workflow_ng_cfgs} SET name = '" . $configuration->name . "', data = '" . serialize($configuration->data) . "' WHERE name = 'uc_flatrate_get_quote_by_order'");
    $ret[] = array(
      'success' => $result !== FALSE,
      'query' => check_plain("UPDATE {workflow_ng_cfgs} SET name = '" . $configuration->name . "', data = '" . serialize($configuration->data) . "' WHERE name = 'uc_flatrate_get_quote_by_order'"),
    );
  }
  else {
    $ret[] = array(
      'success' => FALSE,
      'query' => check_plain("UPDATE {workflow_ng_cfgs} SET name = '" . $configuration->name . "', data = '" . serialize($configuration->data) . "' WHERE name = 'uc_flatrate_get_quote_by_order'"),
    );
  }
  variable_set('uc_quote_enabled', $enabled);
  variable_set('uc_quote_method_weight', $weight);
  if (module_exists('workflow_ng')) {
    workflow_ng_clear_cache();
  }
  variable_del('uc_flatrate_product_base_rate');
  variable_del('uc_flatrate_product_default');
  variable_del('uc_flatrate_order_rate');
  return $ret;
}
function uc_flatrate_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} DROP PRIMARY KEY, ADD PRIMARY KEY (`vid`, `mid`)");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_flatrate_products} DROP CONSTRAINT {uc_flatrate_products}_pkey, ADD PRIMARY KEY (vid, mid)");
      break;
  }
  return $ret;
}
function uc_flatrate_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_change_column($ret, 'uc_flatrate_prodcts', 'vid', 'vid', 'int_unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      db_change_column($ret, 'uc_flatrate_prodcts', 'nid', 'nid', 'int_unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      db_change_column($ret, 'uc_flatrate_prodcts', 'mid', 'mid', 'int_unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      break;
  }
  return $ret;
}