You are here

uc_coupon.install in Ubercart Discount Coupons 6

Same filename and directory in other branches
  1. 5 uc_coupon.install
  2. 7.3 uc_coupon.install
  3. 7.2 uc_coupon.install

Ubercart uc_coupon.module schema

File

uc_coupon.install
View source
<?php

/**
 * @file
 * Ubercart uc_coupon.module schema
 */

/**
 * Implementation of hook_schema().
 */
function uc_coupon_schema() {
  $schema = array();
  $schema['uc_coupons'] = array(
    'description' => t('Ubercart Coupons'),
    'fields' => array(
      'cid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'code' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'default' => '',
      ),
      'value' => array(
        'type' => 'numeric',
        'precision' => 10,
        'scale' => 2,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => '12',
        'not null' => TRUE,
        'default' => 'price',
      ),
      'status' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 1,
      ),
      'valid_from' => array(
        'type' => 'int',
      ),
      'valid_until' => array(
        'type' => 'int',
      ),
      'max_uses' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
      'minimum_order' => array(
        'type' => 'numeric',
        'precision' => 10,
        'scale' => 2,
        'not null' => TRUE,
        'default' => 0,
      ),
      'data' => array(
        'type' => 'text',
        'serialize' => TRUE,
      ),
      'bulk' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'bulk_seed' => array(
        'type' => 'char',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'cid',
    ),
  );
  $schema['uc_coupons_orders'] = array(
    'description' => t('Ubercart Coupons used on Orders'),
    'fields' => array(
      'cuid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'cid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'oid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'value' => array(
        'type' => 'numeric',
        'precision' => 10,
        'scale' => 2,
        'not null' => TRUE,
        'default' => 0,
      ),
      'code' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'cuid',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_install().
 */
function uc_coupon_install() {
  drupal_install_schema('uc_coupon');
}

/**
 * Implementation of hook_uninstall().
 */
function uc_coupon_uninstall() {
  drupal_uninstall_schema('uc_coupon');
  variable_del('uc_coupon_collapse_pane');
  variable_del('uc_coupon_default_usage');
  variable_del('uc_coupon_pane_description');
  variable_del('uc_coupon_purchase_order_status');
  variable_del('uc_coupon_querystring');
  variable_del('uc_coupon_show_in_cart');
  variable_del('uc_coupon_used_order_status');
}
function uc_coupon_update_6000() {
  $ret = array();
  if (!db_column_exists('uc_coupons', 'data')) {
    db_add_field($ret, 'uc_coupons', 'data', array(
      'type' => 'text',
    ));
    $result = db_query('SELECT cid, products, users, roles FROM {uc_coupons}');
    while ($row = db_fetch_object($result)) {
      $data = array(
        'wholesale' => $row->roles,
      );
      if ($row->products) {
        $data['products'] = explode(',', $row->products);
      }
      if ($row->users) {

        // substr() removes the final comma that was present in the previous strings.
        $data['users'] = explode(',', substr($row->users, 0, -1));
      }
      db_query("UPDATE {uc_coupons} SET data = '%s' WHERE cid = %d", serialize($data), $row->cid);
    }
    db_drop_field($ret, 'uc_coupons', 'products');
    db_drop_field($ret, 'uc_coupons', 'users');
    db_drop_field($ret, 'uc_coupons', 'roles');
    db_drop_field($ret, 'uc_coupons_orders', 'user');
    db_drop_field($ret, 'uc_coupons_orders', 'role');
  }
  return $ret;
}
function uc_coupon_update_6001() {
  $ret = array();
  if (!db_column_exists('uc_coupons', 'bulk')) {
    $field = array(
      'type' => 'int',
      'size' => 'tiny',
      'not null' => TRUE,
      'default' => 0,
    );
    db_add_field($ret, 'uc_coupons', 'bulk', $field);
    $field = array(
      'type' => 'char',
      'length' => 32,
      'not null' => TRUE,
      'default' => '',
    );
    db_add_field($ret, 'uc_coupons', 'bulk_seed', $field);
    $ret[] = update_sql("UPDATE {uc_coupons} SET bulk_seed = MD5(CAST(RAND() AS CHAR(32))) WHERE bulk_seed = ''");
  }
  return $ret;
}
function uc_coupon_update_6002() {
  $ret = array();
  if (!db_column_exists('uc_coupons', 'valid_from')) {
    db_add_field($ret, 'uc_coupons', 'valid_from', array(
      'type' => 'int',
    ));
    $ret[] = update_sql("UPDATE {uc_coupons} SET valid_from = 0");
  }
  return $ret;
}

// Increase coupon name length to 255 characters.
function uc_coupon_update_6003() {
  $ret = array();
  db_change_field($ret, 'uc_coupons', 'name', 'name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
  return $ret;
}

// Increase precision of value and minimum_order fields.
function uc_coupon_update_6004() {
  $ret = array();
  db_change_field($ret, 'uc_coupons', 'value', 'value', array(
    'type' => 'numeric',
    'precision' => 10,
    'scale' => 2,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_change_field($ret, 'uc_coupons', 'minimum_order', 'minimum_order', array(
    'type' => 'numeric',
    'precision' => 10,
    'scale' => 2,
    'not null' => TRUE,
    'default' => 0,
  ));
  return $ret;
}

// Increase coupon code length to 100 characters.
function uc_coupon_update_6005() {
  $ret = array();
  $field = array(
    'type' => 'varchar',
    'length' => 100,
    'not null' => TRUE,
    'default' => '',
  );
  db_change_field($ret, 'uc_coupons', 'code', 'code', $field);
  db_change_field($ret, 'uc_coupons_orders', 'code', 'code', $field);
  return $ret;
}

// Add creation timestamp.
function uc_coupon_update_6006() {
  $ret = array();
  if (!db_column_exists('uc_coupons', 'created')) {
    db_add_field($ret, 'uc_coupons', 'created', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
    $ret[] = update_sql("UPDATE {uc_coupons} SET created = " . time());
  }
  return $ret;
}