<?php

/**
 * @file
 * Install hooks for uc_attribute.module.
 */

/**
 * Implemenation of hook_schema()
 */
function uc_attribute_schema() {
  $schema = array();

  $schema['uc_attributes'] = array(
    'description' => 'Attributes: the decisions that need to be made about products.',
    'fields' => array(
      'aid' => array(
        'description' => 'Primary key: attribute ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Name of the attribute.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'Label to use when attribute is displayed.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'ordering' => array(
        'description' => 'Determines the list position of attributes.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'required' => array(
        'description' => 'Flag that, if set, requires a user response for attributes (disables default options).',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'display' => array(
        'description' => 'Display type of the attribute options: 0 => text fields, 1 => select box (default), 2 => radio buttons.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'description' => array(
        'description' => 'Description of the attribute.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('aid'),
  );

  $schema['uc_attribute_options'] = array(
    'description' => 'The available choices for each attribute.',
    'fields' => array(
      'aid' => array(
        'description' => 'The {uc_attributes}.aid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'oid' => array(
        'description' => 'Primary key: the option ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The name of the option.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'cost' => array(
        'description' => "The adjustment to a product's cost with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'description' => "The adjustment to a product's price with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'description' => "The adjustment to a product's physical weight with the chosen option.",
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
      ),
      'ordering' => array(
        'description' => 'Affects the list position of the options.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('oid'),
  );

  $schema['uc_class_attributes'] = array(
    'description' => 'Attributes copied to a product of a certain class when it is created.',
    'fields' => array(
      'pcid' => array(
        'description' => 'Primary key: the product {node}.type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'aid' => array(
        'description' => 'The {uc_attributes}.aid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'label' => array(
        'description' => 'Label to use when attribute is displayed.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'ordering' => array(
        'description' => 'Determines the list position of attributes.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'default_option' => array(
        'description' => 'The default value of the attribute field on the add to cart form.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'required' => array(
        'description' => "A flag indicating that, if set, requires a user response for attributes (disables default options).",
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'display' => array(
        'description' => 'Display type of the attribute options: 0 => text fields, 1 => select box (default), 2 => radio buttons',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array('pcid', 'aid'),
  );

  $schema['uc_class_attribute_options'] = array(
    'description' => 'The available choices for each attribute.',
    'fields' => array(
      'pcid' => array(
        'description' => 'Primary key: the product {node}.type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'oid' => array(
        'description' => 'The {uc_attribute_options}.oid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'cost' => array(
        'description' => "The adjustment to a product's cost with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'description' => "The adjustment to a product's price with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'description' => "The adjustment to a product's physical weight with the chosen option.",
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
      ),
      'ordering' => array(
        'description' => 'Affects the list position of the options.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('pcid', 'oid'),
  );

  $schema['uc_product_attributes'] = array(
    'description' => 'Attributes copied to a product.',
    'fields' => array(
      'nid' => array(
        'description' => 'The product {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'aid' => array(
        'description' => 'The {uc_attributes}.aid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'label' => array(
        'description' => 'Label to use when attribute is displayed',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'ordering' => array(
        'description' => 'Determines the list position of attributes.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'default_option' => array(
        'description' => 'The default value of the attribute field on the add to cart form.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'required' => array(
        'description' => "Flag that, if set, requires a user response for attributes (disables default options).",
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'display' => array(
        'description' => 'Display type of the attribute options: 0 -- text fields, 1 -- select box (default), 2 -- radio buttons',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array('nid', 'aid'),
  );

  $schema['uc_product_options'] = array(
    'description' => 'The available choices for each attribute.',
    'fields' => array(
      'nid' => array(
        'description' => 'The product {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'oid' => array(
        'description' => 'The {uc_attribute_options}.oid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'cost' => array(
        'description' => "The adjustment to a product's cost with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'description' => "The adjustment to a product's price with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'description' => "The adjustment to a product's physical weight with the chosen option.",
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
      ),
      'ordering' => array(
        'description' => 'Affects the list position of the options.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('nid', 'oid'),
  );

  $schema['uc_product_adjustments'] = array(
    'description' => "Changes to a product's SKU based on the possible combination of chosen options.",
    'fields' => array(
      'nid' => array(
        'description' => 'The product {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'combination' => array(
        'description' => 'A serialized array whose keys are attribute IDs and values are option IDs.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'model' => array(
        'description' => 'The SKU representing the product with the combination of options.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
    ),
  );

  return $schema;
}

/**
 * Implmentation of hook_install().
 */
function uc_attribute_install() {
  drupal_install_schema('uc_attribute');
}

/**
 * Implements hook_uninstall().
 */
function uc_attribute_uninstall() {
  drupal_uninstall_schema('uc_attribute');

  variable_del('uc_attribute_option_price_format');
}

/**
 * Implements hook_update_last_removed().
 */
function uc_attribute_update_last_removed() {
  return 10;
}

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

  // Standardize database definitions during upgrade to Drupal 6.
  // ID fields are unsigned, regular-sized ints.
  // "Boolean" flags are unsigned tiny ints.
  // Postgres tables will have the necessary default values, and MySQL
  // doesn't need them, so the schema can just be mismatched for that.

  // Some id columns are already auto-incremented, so we can't remove the
  // primary key beforehand in MySQL. So we add it later for Postgres.

  db_change_field($ret, 'uc_attributes', 'aid', 'aid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE));
  db_change_field($ret, 'uc_attributes', 'required', 'required', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attributes', 'display', 'display', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_add_primary_key($ret, 'uc_attributes', array('aid'));
  }

  db_change_field($ret, 'uc_attribute_options', 'aid', 'aid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attribute_options', 'oid', 'oid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE));
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_add_primary_key($ret, 'uc_attribute_options', array('oid'));
  }

  db_drop_primary_key($ret, 'uc_class_attributes');
  db_change_field($ret, 'uc_class_attributes', 'aid', 'aid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('pcid', 'aid')));
  db_change_field($ret, 'uc_class_attributes', 'default_option', 'default_option', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attributes', 'required', 'required', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attributes', 'display', 'display', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));

  db_drop_primary_key($ret, 'uc_class_attribute_options');
  db_change_field($ret, 'uc_class_attribute_options', 'oid', 'oid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('pcid', 'oid')));

  db_drop_primary_key($ret, 'uc_product_attributes');
  db_change_field($ret, 'uc_product_attributes', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'aid', 'aid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'default_option', 'default_option', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'required', 'required', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'display', 'display', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
  db_add_primary_key($ret, 'uc_product_attributes', array('nid', 'aid'));

  db_drop_primary_key($ret, 'uc_product_options');
  db_change_field($ret, 'uc_product_options', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'oid', 'oid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_primary_key($ret, 'uc_product_options', array('nid', 'oid'));

  db_drop_primary_key($ret, 'uc_product_adjustments');
  db_change_field($ret, 'uc_product_adjustments', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('indexes' => array('nid' => array('nid'))));

  return $ret;
}

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

  db_change_field($ret, 'uc_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'price', 'price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

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

  $result = db_query("SELECT * FROM {permission}");
  while($perm = db_fetch_object($result)) {
    $perm->perm = str_replace("adminster attributes", "administer attributes", $perm->perm);
    drupal_write_record('permission', $perm, 'pid');
  }
  $ret[] = array('success' => TRUE, 'query' => "Replaced all instances of 'adminster attributes' with 'administer attributes' in the permissions table.");

  return $ret;
}

/**
 * Add label to attributes to allow differently named attributes to show with
 * the same label on product add to cart forms.
 */
function uc_attribute_update_6003() {
  $ret = array();

  $spec = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  );
  db_add_field($ret, 'uc_attributes', 'label', $spec);
  db_add_field($ret, 'uc_class_attributes', 'label', $spec);
  db_add_field($ret, 'uc_product_attributes', 'label', $spec);

  $ret[] = array('success' => TRUE, 'query' => 'Added label to product attributes.');

  return $ret;
}

/**
 * Sort adjustment combination arrays by attribute id.
 */
function uc_attribute_update_6004() {
  $ret = array();

  $changes = 0;
  $result = db_query('SELECT * FROM {uc_product_adjustments}');
  while($row = db_fetch_object($result)) {
    $combination = unserialize($row->combination);
    ksort($combination);
    db_query("UPDATE {uc_product_adjustments} SET combination = '%s' WHERE nid = %d AND model = '%s' AND combination = '%s'", serialize($combination), $row->nid, $row->model, $row->combination);
    $changes++;
  }

  $ret[] = array('success' => TRUE, 'query' => 'Sorted adjustment combination arrays by attribute id.');

  return $ret;
}

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

  db_change_field($ret, 'uc_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'price', 'price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));

  return $ret;
}
