<?php

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

/**
 * Implements 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'),
    'foreign keys' => array(
      'aid' => array('uc_attributes' => 'aid'),
    ),
  );

  $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'),
    'foreign keys' => array(
      'pcid' => array('uc_product_classes' => 'pcid'),
      'aid' => array('uc_attributes' => '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'),
    'foreign keys' => array(
      'pcid' => array('uc_product_classes' => 'pcid'),
      'oid' => array('uc_attribute_options' => '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'),
    'foreign keys' => array(
      'nid' => array('uc_products' => 'nid'),
      'aid' => array('uc_attributes' => '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'),
    'foreign keys' => array(
      'nid' => array('uc_products' => 'nid'),
      'oid' => array('uc_attribute_options' => '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' => '',
        'serialize' => TRUE,
      ),
      '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'),
    ),
    'foreign keys' => array(
      'nid' => array('uc_product' => 'nid'),
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function uc_attribute_uninstall() {
  variable_del('uc_attribute_option_price_format');
}

/**
 * Implements hook_update_dependencies().
 */
function uc_attribute_update_dependencies() {
  $dependencies['uc_attribute'][7000] = array(
    'filter' => 7005,
  );

  return $dependencies;
}

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

/**
 * Add description format for attributes.
 */
function uc_attribute_update_7000() {
  db_add_field('uc_attributes', 'format', array(
    'description' => 'Format of the attribute description.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));

  db_update('uc_attributes')
    ->fields(array('format' => 0))
    ->execute();
}

/**
 * Change default description format to NULL.
 */
function uc_attribute_update_7001() {
  db_change_field('uc_attributes', 'format', 'format', array(
    'description' => 'Format of the attribute description.',
    'type' => 'int',
    'not null' => FALSE,
    'default' => NULL,
  ));

  db_update('uc_attributes')
    ->fields(array('format' => filter_fallback_format()))
    ->execute();
}

/**
 * Remove attribute description format..
 */
function uc_attribute_update_7002() {
  db_drop_field('uc_attributes', 'format');
}
