<?php

/**
 * @file
 * Install, update and uninstall functions for the uc_product module.
 */

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

  $schema['uc_product_classes'] = array(
    'description' => 'The list of product node types.',
    'fields' => array(
      'pcid' => array(
        'description' => 'The node type identifier.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'name' => array(
        'description' => 'The human-readable name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'The description of the node type.',
        'type' => 'text',
      ),
    ),
    'primary key' => array('pcid'),
  );

  $schema['uc_product_features'] = array(
    'description' => 'Stores information of features added to products.',
    'fields' => array(
      'pfid' => array(
        'description' => 'Primary key: the product feature id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the product that has this feature.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'fid' => array(
        'description' => 'The type of feature.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'The description of the feature.',
        'type' => 'text',
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
    ),
    'primary key' => array('pfid'),
  );

  $schema['uc_products'] = array(
    'description' => 'Product information for nodes.',
    'fields' => array(
      'vid' => array(
        'description' => 'The {node}.vid of the product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'model' => array(
        'description' => 'SKU or model number.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'list_price' => array(
        'description' => 'Suggested retail price.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'cost' => array(
        'description' => 'The amount the store pays to sell the product.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'sell_price' => array(
        'description' => 'The amount the customer pays for the product.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'weight' => array(
        'description' => 'Physical weight.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'weight_units' => array(
        'description' => 'Unit of measure for the weight field.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'lb',
      ),
      'length' => array(
        'description' => 'Physical length of the product or its packaging.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'width' => array(
        'description' => 'Physical width of the product or its packaging.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'height' => array(
        'description' => 'Physical height of the product or its packaging.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'length_units' => array(
        'description' => 'Unit of measure for the length, width, and height.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'in',
      ),
      'pkg_qty' => array(
        'description' => 'The number of this product that fit in one package.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'default_qty' => array(
        'description' => 'The default value for the quantity field in the "Add to Cart" form.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'unique_hash' => array(
        'description' => 'A multi-site unique identifier for a product.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => md5(''),
      ),
      'ordering' => array(
        'description' => 'The sort criteria for products.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'shippable' => array(
        'description' => 'Boolean flag signifying that the product can be shipped.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
    ),
    'primary key' => array('vid'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
function uc_product_install() {
  drupal_install_schema('uc_product');
}

/**
 * Implements hook_uninstall().
 */
function uc_product_uninstall() {
  drupal_uninstall_schema('uc_product');

  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_product_shippable_%%'");

  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');
}

/**
 * Implements hook_update_last_removed().
 */
function uc_product_update_last_removed() {
  return 12;
}

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

  db_drop_primary_key($ret, 'uc_product_features');
  db_drop_index($ret, 'uc_product_features', 'nid');
  db_change_field($ret, 'uc_product_features', 'pfid', 'pfid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('pfid')));
  db_change_field($ret, 'uc_product_features', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('indexes' => array('nid' => array('nid'))));

  db_drop_primary_key($ret, 'uc_products');
  db_change_field($ret, 'uc_products', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('vid')));
  db_change_field($ret, 'uc_products', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'list_price', 'list_price', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'cost', 'cost', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'sell_price', 'sell_price', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'weight', 'weight', array('type' => 'float', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'unique_hash', 'unique_hash', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => 'd41d8cd98f00b204e9800998ecf8427e'));
  db_change_field($ret, 'uc_products', 'shippable', 'shippable', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));

  return $ret;
}

/**
 * Makes sure that those who had the faulty 6000 update have the right
 * precision and scale.
 */
function uc_product_update_6001() {
  $ret = array();

  db_change_field($ret, 'uc_products', 'list_price', 'list_price', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'cost', 'cost', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'sell_price', 'sell_price', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

/**
 * Makes the numeric fields signed for Postgres compatibility.
 */
function uc_product_update_6002() {
  $ret = array();

  db_change_field($ret, 'uc_products', 'list_price', 'list_price', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'cost', 'cost', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'sell_price', 'sell_price', array('type' => 'numeric', 'precision' => 10, 'scale' => 2, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

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

  // Geez. Can't we get these columns right?
  db_change_field($ret, 'uc_products', 'list_price', 'list_price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'sell_price', 'sell_price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

/**
 * Handles the image widget upgrade.
 */
function uc_product_update_6004() {
  $ret = array();

  if (!variable_get('uc_product_image_widget', NULL)) {
    if (module_exists('thickbox')) {
      variable_set('uc_product_image_widget', 'thickbox');
    }
    elseif (module_exists('lightbox2')) {
      variable_set('uc_product_image_widget', 'lightbox2');
    }
    $ret[] = array('success' => TRUE, variable_get('uc_product_image_widget', 'No widget') .' was set as the current image widget');
  }

  return $ret;
}

/**
 * Sets the uc_image setting to field_image_cache for all existing products.
 */
function uc_product_update_6005() {
  $ret = array();

  if (db_table_exists('content_node_field_instance')) {
    $result = db_query("SELECT type_name FROM {content_node_field_instance} WHERE field_name = 'field_image_cache'");
    while ($class = db_fetch_object($result)) {
      variable_set('uc_image_'. $class->type_name, 'field_image_cache');
    }

    $t = get_t();
    $ret[] = array('success' => TRUE, $t('field_image_cache set as the Ubercart image for products and product classes.'));
  }

  return $ret;
}

/**
 * Changes to signed floats.
 */
function uc_product_update_6006() {
  $ret = array();

  $schema = array(
    'float_spec' => array(
      'type' => 'float',
      'not null' => TRUE,
      'default' => 0.0,
    ),
  );
  db_change_field($ret, 'uc_products', 'weight', 'weight', $schema['float_spec'] + array('description' => "The product's physical weight"));
  db_change_field($ret, 'uc_products', 'length', 'length', $schema['float_spec'] + array('description' => 'Physical length of the product or its packaging.'));
  db_change_field($ret, 'uc_products', 'width', 'width', $schema['float_spec'] + array('description' => 'Physical width of the product or its packaging.'));
  db_change_field($ret, 'uc_products', 'height', 'height', $schema['float_spec'] + array('description' => 'Physical height of the product or its packaging.'));

  return $ret;
}

/**
 * Updates old product images to the imagefield widget type.
 */
function uc_product_update_6007() {
  $ret = array();

  if (db_table_exists('content_node_field_instance')) {
    $ret[] = update_sql("UPDATE {content_node_field_instance} SET widget_type = 'imagefield_widget' WHERE field_name = 'field_image_cache'");
  }

  return $ret;
}

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

  if (db_table_exists('imagecache_preset')) {
    $result = db_result(db_query("SELECT COUNT(presetid) FROM {imagecache_preset} WHERE presetname = 'product_full'"));
    if (!$result) {
      $ret[] = update_sql("INSERT INTO {imagecache_preset} (presetname) VALUES ('product_full')");
    }
  }

  return $ret;
}

/**
 * Changes currency fields to numeric(16,5).
 */
function uc_product_update_6009() {
  $ret = array();

  db_change_field($ret, 'uc_products', 'list_price', 'list_price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_products', 'sell_price', 'sell_price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));

  return $ret;
}
