home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (uc_taxes.install)

This example Drupal source code file (uc_taxes.install) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

alter, array, case, database, default, function, not, null, ret, sql, table, true, uc_taxes, update_sql

The uc_taxes.install Drupal example source code

<?php
// $Id: uc_taxes.install,v 1.10.2.9 2010/03/19 21:27:18 tr Exp $

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

/**
 * Implementation of hook_schema().
 */
function uc_taxes_schema() {
  $schema = array();

  $schema['uc_taxes'] = array(
    'description' => 'Stores tax information.',
    'fields' => array(
      'id' => array(
        'description' => 'Primary key: Unique tax rate id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The tax rate name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'rate' => array(
        'description' => 'The tax rate multiplier.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'shippable' => array(
        'description' => 'Flag that describes how this rate applies to shippable and non-shippable products. 0 => Disregard shipability. 1 => Apply tax to shippable products only.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'taxed_product_types' => array(
        'description' => 'Serialized array of node types to be taxed.',
        'type' => 'text',
        'serialize' => TRUE,
      ),
      'taxed_line_items' => array(
        'description' => 'Serialized array of line item types to be taxed.',
        'type' => 'text',
        'serialize' => TRUE,
      ),
      'weight' => array(
        'description' => 'The weight of this tax rate in relation to other rates.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function uc_taxes_install() {
  drupal_install_schema('uc_taxes');
}

/**
 * Implementation of hook_uninstall().
 */
function uc_taxes_uninstall() {
  drupal_uninstall_schema('uc_taxes');
}

function uc_taxes_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP INDEX taxes");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP pcid");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN id mediumint(9) NOT NULL FIRST");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD PRIMARY KEY id (id)");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN name varchar(255) NOT NULL AFTER id");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN area area varchar(255) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN type type enum('code','zone','country') NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN standalone cumulative tinyint(1) NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN weight tinyint(2) NOT NULL default '0'");
    break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP CONSTRAINT {uc_taxes}_pcid_area_key");
      $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP pcid");
      db_add_column($ret, 'uc_taxes', 'id', 'integer', array('not null' => TRUE, 'default' => 0));
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD PRIMARY KEY (id)");
      $ret[] = update_sql("CREATE INDEX {uc_taxes}_id ON {uc_taxes} (id)");
      db_add_column($ret, 'uc_taxes', 'name', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
      db_change_column($ret, 'uc_taxes', 'area', 'area', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
      db_change_column($ret, 'uc_taxes', 'type', 'type', "enum('code','zone','country')", array('not null' => TRUE, 'default' => 'code'));
      db_change_column($ret, 'uc_taxes', 'standalone', 'cumulative', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'uc_taxes', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
    break;
  }
  return $ret;
}

function uc_taxes_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} CHANGE COLUMN shipping taxed_line_items text NOT NULL");
    break;
    case 'pgsql':
      db_change_column($ret, 'uc_taxes', 'shipping', 'taxed_line_items', 'text', array('not null' => TRUE, 'default' => "''"));
    break;
  }
  return $ret;
}

function uc_taxes_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN conditions text NOT NULL");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_taxes', 'conditions', 'text', array('not null' => TRUE, 'default' => "''"));
    break;
  }

  return $ret;
}

function uc_taxes_update_4() {
  $result = db_query("SELECT id, taxed_line_items FROM {uc_taxes} WHERE cumulative <> 0");
  while ($tax = db_fetch_object($result)) {
    if ($taxed_line_items = unserialize($tax->taxed_line_items) && is_array($taxed_line_items)) {
      $taxed_line_items['tax'] = 'tax';
      db_query("UPDATE {uc_taxes} SET taxed_line_items = '%s' WHERE id = %d", serialize($taxed_line_items), $tax->id);
    }
  }

  $ret = array();
  $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP cumulative");

  return $ret;
}

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

  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN taxed_product_types text NOT NULL AFTER rate");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_taxes', 'taxed_product_types', 'text', array('not null' => TRUE, 'default' => "''"));
    break;
  }

  $types = array();
  foreach (module_invoke_all('product_types') as $type) {
    $types[$type] = $type;
  }
  db_query("UPDATE {uc_taxes} SET taxed_product_types = '%s'", serialize($types));

  return $ret;
}

function uc_taxes_update_6() {
  $ret = array();
  if (module_exists('workflow_ng_ui')) {
    $configurations = array();
    $taxes = uc_taxes_rate_load();
    foreach ($taxes as $tax) {
      $configurations['uc_taxes_'. $tax->id] = array(
        '#name' => 'uc_taxes_'. $tax->id,
        '#label' => $tax->name,
        '#event' => 'calculate_tax_'. $tax->id,
        '#module' => 'uc_taxes',
        '#active' => TRUE,
      );
      $action = workflow_ng_use_action('uc_taxes_action_apply_tax', array(
        '#label' => t('Apply @tax', array('@tax' => $tax->name)),
      ));
      switch ($tax->type) {
        case 'code':
          $area_condition = workflow_ng_use_condition('uc_order_condition_delivery_postal_code', array(
            '#label' => t('Is in postal code area @code', array('@code' => $tax->area)),
            '#settings' => array(
              'pattern' => $tax->area,
            ),
          ));
        break;
        case 'zone':
          $zone = uc_get_zone_code($tax->area);
          $area_condition = workflow_ng_use_condition('uc_order_condition_delivery_zone', array(
            '#label' => t('Is in @zone', array('@zone' => $zone)),
            '#settings' => array(
              'zones' => array($tax->area),
            ),
          ));
        break;
        case 'country':
          $country = uc_get_country_data(array('country_id' => $tax->area));
          $country_name = $country[0]['country_name'];
          $area_condition = workflow_ng_use_condition('uc_order_condition_delivery_country', array(
            '#label' => t('Is in @country', array('@country' => $country_name)),
            '#settings' => array(
              'countries' => array($tax->area),
            ),
          ));
        break;
      }
      $configurations['uc_taxes_'. $tax->id] = workflow_ng_configure($configurations['uc_taxes_'. $tax->id], $action, $area_condition);
      workflow_ng_ui_save_configuration($configurations['uc_taxes_'. $tax->id]);
    }
    $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP area");
    $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP type");
    $ret[] = update_sql("ALTER TABLE {uc_taxes} DROP conditions");
  }
  return $ret;
}

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

  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_taxes} ADD COLUMN shippable tinyint UNSIGNED NOT NULL default 0");
      break;
    case 'pgsql':
      db_add_column($ret, 'uc_taxes', 'shippable', 'smallint_unsigned', array('not null' => TRUE, 'default' => 0));
      break;
  }

  return $ret;
}

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

  db_drop_primary_key($ret, 'uc_taxes');
  db_change_field($ret, 'uc_taxes', 'id', 'id', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('id')));

  return $ret;
}

/**
 * Change to signed floats.
 */
function uc_taxes_update_6001() {
  $ret = array();

  $schema = array(
    'rate' => array(
      'description' => t('The rate of taxation.'),
      'type' => 'float',
      'not null' => TRUE,
      'default' => 0.0,
    ),
  );
  db_change_field($ret, 'uc_taxes', 'rate', 'rate', $schema['rate']);

  return $ret;
}

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

  if (!db_column_exists('uc_taxes', 'shippable')) {
    db_add_field($ret, 'uc_taxes', 'shippable', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  }

  return $ret;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal uc_taxes.install source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.