You are here

uc_product.install in Ubercart 5

Database installation, uninstallation, and updates for the product module.

File

uc_product/uc_product.install
View source
<?php

/**
 * @file
 * Database installation, uninstallation, and updates for the product module.
 */

/**
 * Ubercart uc_product.module schema
 */
function uc_product_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {uc_product_classes} (\n        `pcid` varchar(32) NOT NULL,\n        `name` varchar(255) NOT NULL,\n        `description` text,\n        PRIMARY KEY  (`pcid`)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      db_query("CREATE TABLE {uc_product_features} (\n        `pfid` mediumint(9) NOT NULL default 0,\n        `nid` mediumint(9) NOT NULL default 0,\n        `fid` varchar(32) NOT NULL,\n        `description` text,\n        PRIMARY KEY (`pfid`),\n        KEY nid (nid)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      db_query("CREATE TABLE {uc_products} (\n        `vid` mediumint(9) NOT NULL default 0,\n        `nid` mediumint(9) NOT NULL default 0,\n        `model` varchar(255) NOT NULL default '',\n        `list_price` decimal(10,2) NOT NULL default 0.00,\n        `cost` decimal(10,2) NOT NULL default 0.00,\n        `sell_price` decimal(10,2) NOT NULL default 0.00,\n        `weight` float NOT NULL default 0,\n        `weight_units` varchar(255) NOT NULL default 'lb',\n        `length` float unsigned NOT NULL default 0,\n        `width` float unsigned NOT NULL default 0,\n        `height` float unsigned NOT NULL default 0,\n        `length_units` varchar(255) NOT NULL default 'in',\n        `pkg_qty` smallint unsigned NOT NULL default 1,\n        `default_qty` smallint(5) unsigned NOT NULL default 1,\n        `unique_hash` varchar(32) NOT NULL,\n        `ordering` tinyint(2) NOT NULL default 0,\n        `shippable` tinyint(2) NOT NULL default 1,\n        PRIMARY KEY  (`vid`)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      break;
    case "pgsql":
      db_query("CREATE TABLE {uc_product_classes} (\n        pcid varchar(32) NOT NULL default '',\n        name varchar(255) NOT NULL default '',\n        description text,\n        PRIMARY KEY  (pcid)\n      );");
      db_query("CREATE TABLE {uc_product_features} (\n        pfid serial NOT NULL,\n        nid integer NOT NULL default 0,\n        fid varchar(32) NOT NULL,\n        description text,\n        PRIMARY KEY (pfid)\n      );");
      db_query("CREATE INDEX {uc_product_features}_nid ON {uc_product_features} (nid)");
      db_query("CREATE TABLE {uc_products} (\n        vid integer NOT NULL default 0,\n        nid integer NOT NULL default 0,\n        model varchar(255) NOT NULL default '',\n        list_price decimal(10,2) NOT NULL default 0.00,\n        cost decimal(10,2) NOT NULL default 0.00,\n        sell_price decimal(10,2) NOT NULL default 0.00,\n        weight float NOT NULL default 0,\n        weight_units varchar(255) NOT NULL default 'lb',\n        length float NOT NULL default 0,\n        width float NOT NULL default 0,\n        height float NOT NULL default 0,\n        length_units varchar(255) NOT NULL default 'in',\n        pkg_qty smallint_unsigned NOT NULL default 1,\n        default_qty smallint_unsigned NOT NULL default 1,\n        unique_hash varchar(32) NOT NULL default '',\n        ordering smallint NOT NULL default 0,\n        shippable smallint NOT NULL default 1,\n        PRIMARY KEY (vid)\n      );");
      break;
  }
}
function uc_product_uninstall() {
  db_query("DROP TABLE {uc_product_classes}");
  db_query("DROP TABLE {uc_product_features}");
  db_query("DROP TABLE {uc_products}");
  variable_del('uc_product_nodes_per_page');
  variable_del('uc_product_add_to_cart_qty');
  variable_del('uc_product_add_to_cart_teaser');
  variable_del('uc_teaser_add_to_cart_text');
  variable_del('uc_product_add_to_cart_text');
  variable_del('uc_product_field_enabled');
  variable_del('uc_product_field_weight');
}
function uc_product_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_class_choices} CHANGE name name varchar(255) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_class_fields} CHANGE name name varchar(255) NOT NULL, DROP COLUMN title");
      $ret[] = update_sql("ALTER TABLE {uc_product_classes} CHANGE name name varchar(255) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_product_class_choices} CHANGE value value varchar(255) NOT NULL, ADD PRIMARY KEY (nid, cfid, value)");
      $ret[] = update_sql("ALTER TABLE {uc_products} CHANGE model model varchar(255) NOT NULL");
      break;
    case 'pgsql':
      db_change_column($ret, 'uc_class_choices', 'name', 'name', 'varchar(255)', array(
        'not null' => true,
        'default' => "''",
      ));
      db_change_column($ret, 'uc_class_fields', 'name', 'name', 'varchar(255)', array(
        'not null' => true,
        'default' => "''",
      ));
      $ret[] = update_sql("ALTER TABLE {uc_class_fields} DROP title");
      db_change_column($ret, 'uc_product_classes', 'name', 'name', 'varchar(255)', array(
        'not null' => true,
        'default' => "''",
      ));
      db_change_column($ret, 'uc_product_class_choices', 'value', 'value', 'varchar(255)', array(
        'not null' => true,
        'default' => "''",
      ));
      $ret[] = update_sql("ALTER TABLE {uc_product_class_choices} ADD PRIMARY KEY (nid, cfid, value)");
      db_change_column($ret, 'uc_products', 'model', 'model', 'varchar(255)', array(
        'not null' => true,
        'default' => "''",
      ));
      break;
  }
  if ($max_id = db_result(db_query_range("SELECT cfid FROM {uc_class_fields} ORDER BY cfid DESC", 0, 1))) {
    $ret[] = update_sql("INSERT INTO {sequences} (name, id) VALUES ('{uc_class_fields}_cfid', %d)", $max_id);
  }
  return $ret;
}
function uc_product_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_product_class_choices} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {uc_product_class_choices} CHANGE value value text NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_product_class_choices} ADD PRIMARY KEY (nid, cfid, value (3))");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_product_class_choices} DROP CONSTRAINT {uc_product_class_choices}_pkey");
      db_change_column($ret, 'uc_product_class_choices', 'value', 'value', 'text', array(
        'not null' => true,
        'default' => "''",
      ));
      $ret[] = update_sql("ALTER TABLE {uc_product_class_choices} ADD PRIMARY KEY (nid, cfid, value (3))");
      break;
  }
  return $ret;
}
function uc_product_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD COLUMN units varchar(255) NOT NULL default 'lbs' AFTER weight");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_products', 'units', 'varchar(255)', array(
        'not null' => true,
        'default' => 'lbs',
      ));
      break;
  }
  return $ret;
}
function uc_product_update_4() {
  $ret = array();
  $ret[] = update_sql("UPDATE {node} AS n, {uc_products} AS p, {uc_product_classes} AS pc SET n.type = REPLACE(LOWER(pc.name), ' ', '_') WHERE n.nid = p.nid AND p.pcid = pc.pcid");
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("DROP TABLE {uc_class_choices}");
      $ret[] = update_sql("DROP TABLE {uc_class_fields}");
      $ret[] = update_sql("DROP TABLE {uc_product_class_choices}");
      $ret[] = update_sql("ALTER TABLE {uc_product_classes} CHANGE pcid pcid varchar(32) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_product_classes} ADD COLUMN description text");
      $ret[] = update_sql("ALTER TABLE {uc_products} DROP COLUMN pcid");
      break;
    case 'pgsql':
      $ret[] = update_sql("DROP TABLE {uc_class_choices}");
      $ret[] = update_sql("DROP TABLE {uc_class_fields}");
      $ret[] = update_sql("DROP TABLE {uc_product_class_choices}");
      $ret[] = update_sql("DROP INDEX {uc_product_classes}_pcid_idx");
      db_change_column($ret, 'uc_product_classes', 'pcid', 'pcid', 'varchar(32)', array(
        'not null' => true,
        'default' => "''",
      ));
      db_add_column($ret, 'uc_product_classes', 'description', 'text');
      $ret[] = update_sql("ALTER TABLE {uc_products} DROP COLUMN pcid");
      break;
  }
  $ret[] = update_sql("UPDATE {uc_product_classes} SET pcid = REPLACE(LOWER(name), ' ', '_')");
  if ($vid = variable_get('uc_catalog_vid', 0)) {
    $types = module_invoke_all('product_types');
    unset($types['product']);
    foreach ($types as $type) {
      $placeholders[] = "(%d, '%s')";
      $values[] = $vid;
      $values[] = $type;
    }
    $result = db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES " . implode(',', $placeholders), $values);
    $ret[] = array(
      'success' => $result,
      'query' => t('Added the following node types to the Catalog vocabulary: %list', array(
        '%list' => implode(', ', $values),
      )),
    );
  }
  node_types_rebuild();
  return $ret;
}
function uc_product_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD COLUMN default_qty smallint(5) unsigned NOT NULL default '1' AFTER units");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_products', 'default_qty', 'smallint unsigned', array(
        'not null' => true,
        'default' => 1,
      ));
      break;
  }
  return $ret;
}
function uc_product_update_6() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD COLUMN ordering smallint(2) NOT NULL default 0");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_products', 'default_qty', 'smallint(2)', array(
        'not null' => true,
        'default' => 0,
      ));
      break;
  }
  return $ret;
}
function uc_product_update_7() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_products} CHANGE units weight_units varchar(255) NOT NULL default 'lb'");
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD length float unsigned NOT NULL default 0 AFTER weight_units");
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD width float unsigned NOT NULL default 0 AFTER length");
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD height float unsigned NOT NULL default 0 AFTER width");
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD length_units varchar(255) NOT NULL default 'in' AFTER height");
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD pkg_qty smallint unsigned NOT NULL default 1 AFTER length_units");
      break;
    case 'pgsql':
      db_change_column($ret, 'uc_products', 'units', 'weight_units', 'varchar(255)', array(
        'not null' => true,
        'default' => 'lb',
      ));
      db_add_column($ret, 'uc_products', 'pkg_qty', 'smallint unsigned', array(
        'not null' => true,
        'default' => 1,
      ));
      db_add_column($ret, 'uc_products', 'length', 'float unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      db_add_column($ret, 'uc_products', 'width', 'float unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      db_add_column($ret, 'uc_products', 'height', 'float unsigned', array(
        'not null' => true,
        'default' => 0,
      ));
      db_add_column($ret, 'uc_products', 'length_units', 'varchar(255)', array(
        'not null' => true,
        'default' => 'in',
      ));
      break;
  }
  return $ret;
}
function uc_product_update_8() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD vid mediumint(9) NOT NULL default 0 FIRST");
      $ret[] = update_sql("ALTER TABLE {uc_products} DROP PRIMARY KEY");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD PRIMARY KEY (vid)");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_products', 'vid', 'integer', array(
        'not null' => true,
        'default' => 0,
      ));
      $ret[] = update_sql("ALTER TABLE {uc_products} DROP CONSTRAINT {uc_products}_pkey");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD PRIMARY KEY (vid)");
      break;
  }
  return $ret;
}

// Update to add in product feature support.
function uc_product_update_9() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {uc_product_features} (\n        `pfid` mediumint(9) NOT NULL default 0,\n        `nid` mediumint(9) NOT NULL default 0,\n        `fid` varchar(32) NOT NULL,\n        `description` text,\n        PRIMARY KEY (`pfid`),\n        KEY nid (nid)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ;");
      break;
  }
  return $ret;
}

// Update to add the shippable column to the product table.
function uc_product_update_10() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_products} ADD shippable tinyint(2) NOT NULL default 1");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_products', 'shippable', 'tinyint', array(
        'not null' => true,
        'default' => 1,
      ));
      break;
  }
  return $ret;
}
function uc_product_update_11() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_change_column($ret, 'uc_products', 'pkg_qty', 'pkg_qty', 'smallint_unsigned', array(
        'not null' => true,
        'default' => 1,
      ));
      db_change_column($ret, 'uc_products', 'default_qty', 'defautl_qty', 'smallint_unsigned', array(
        'not null' => true,
        'default' => 1,
      ));
      break;
  }
  return $ret;
}
function uc_product_update_12() {
  $ret = array();
  if (module_exists('imagecache')) {
    $preset_id = db_next_id('{imagecache_preset}_presetid');
    $action_id = db_next_id('{imagecache_action}_actionid');
    db_query("INSERT INTO {imagecache_preset} (presetid, presetname) VALUES (%d, 'uc_thumbnail')", $preset_id);
    db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, 0, '%s')", $action_id, $preset_id, 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:2:"35";s:6:"height";s:2:"35";}');
    cache_clear_all('imagecache:presets', 'cache');
    $ret[] = array(
      'success' => TRUE,
      'query' => "INSERT INTO {imagecache_preset} (presetid, presetname) VALUES (" . $preset_id . ", 'uc_thumbnail')",
    );
    $ret[] = array(
      'success' => TRUE,
      'query' => "INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (" . $action_id . ", " . $preset_id . ", 0, '" . 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:2:"35";s:6:"height";s:2:"35";}' . "')",
    );
  }
  return $ret;
}