<?php

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

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

  $schema['uc_cart_products'] = array(
    'description' => 'Stores products placed in shopping carts.',
    'fields' => array(
      'cart_item_id' => array(
        'description' => 'Unique identifier for cart item.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'cart_id' => array(
        'description' => 'A user-specific cart ID. For authenticated users, their {users}.uid. For anonymous users, a token.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '0',
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'qty' => array(
        'description' => 'The number of this product in the cart.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp indicating the time the product in the cart was changed.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'data' => array(
        'description' => 'A serialized array of extra cart data for the product.',
        'type' => 'text',
        'serialize' => TRUE,
      ),
    ),
    'indexes' => array(
      'cart_id' => array('cart_id'),
    ),
    'primary key' => array('cart_item_id'),
    'foreign keys' => array(
      'nid' => array('node' => 'nid'),
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function uc_cart_uninstall() {
  db_delete('variable')
    ->condition('name', 'uc_cart_%', 'LIKE')
    ->condition('name', 'uc_pane_%', 'LIKE')
    ->condition('name', 'uc_cap_%', 'LIKE')
    ->condition('name', 'uc_checkout_%', 'LIKE')
    ->condition('name', 'uc_msg_%', 'LIKE')
    ->condition('name', 'uc_new_customer_%', 'LIKE')
    ->execute();

  variable_del('uc_minimum_subtotal');
  variable_del('uc_add_item_redirect');
  variable_del('uc_continue_shopping_url');
  variable_del('uc_continue_shopping_text');
  variable_del('uc_continue_shopping_type');
  variable_del('uc_use_next_buttons');
  variable_del('uc_collapse_current_pane');
  variable_del('uc_checkout_email_customer');
  variable_del('uc_checkout_email_admin');
}

/**
 * Implements hook_update_last_removed().
 */
function uc_cart_update_last_removed() {
  // 7.x-3.0-beta2 and earlier were installed with schema version 0,
  // which causes update.php to fail.
  return drupal_get_installed_schema_version('uc_cart') == 0 ? 0 : 6201;
}

/**
 * Unify cart block title setting.
 */
function uc_cart_update_6202() {
  variable_del('uc_cart_block_title');
}

/**
 * Remove unused text format variables.
 */
function uc_cart_update_7000() {
  variable_del('uc_checkout_instructions_format');
  variable_del('uc_checkout_review_instructions_format');
  variable_del('uc_msg_order_submit_format');
  variable_del('uc_msg_order_logged_in_format');
  variable_del('uc_msg_order_existing_user_format');
  variable_del('uc_msg_order_new_user_format');
  variable_del('uc_msg_continue_shopping_format');
}

/**
 * Increase maximum cart item quantity.
 */
function uc_cart_update_7001() {
  db_change_field('uc_cart_products', 'qty', 'qty', array(
    'description' => 'The number of this product in the cart.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
}
