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

Drupal example source code file (uc_usps.install)

This example Drupal source code file (uc_usps.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

array, database, function, mail, nid, null, php, ret, sql, table, true, uc_usps_products, update_sql, vid

The uc_usps.install Drupal example source code

<?php
// $Id: uc_usps.install,v 1.5.2.8 2010/03/19 21:27:17 tr Exp $

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

/**
 * Implementation of hook_requirements().
 */
function uc_usps_requirements($phase) {
  $t = get_t();

  $has_simplexml = class_exists('SimpleXMLElement');
  $requirements['uc_usps_simplexml'] = array(
    'title' => $t('SimpleXML'),
    'value' => $has_simplexml ? $t('Enabled') : $t('Not found'),
  );
  if (!$has_simplexml) {
    $requirements['uc_usps_simplexml']['severity'] = REQUIREMENT_ERROR;
    $requirements['uc_usps_simplexml']['description'] = $t('USPS requires the PHP <a href="!simplexml_url">SimpleXML</a> library.', array('!simplexml_url' => 'http://php.net/manual/en/simplexml.setup.php'));
  }

  return $requirements;
}

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

  $schema['uc_usps_products'] = array(
    'description' => 'Stores product information for USPS shipping quotes.',
    'fields' => array(
      'vid' => array(
        'description' => 'The {uc_products}.vid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {uc_products}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'container' => array(
        'description' => 'The package type in which the product will be shipped.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('vid'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function uc_usps_install() {
  drupal_install_schema('uc_usps');
}

/**
 * Implementation of hook_uninstall().
 */
function uc_usps_uninstall() {
  drupal_uninstall_schema('uc_usps');
  variable_del('uc_usps_user_id');
  variable_del('uc_usps_services');
  variable_del('uc_usps_markup');
}

function uc_usps_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN qty");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN length");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN width");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN height");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP COLUMN units");
    break;
  }
  return $ret;
}

function uc_usps_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} ADD COLUMN vid mediumint(9) unsigned NOT NULL default 0 FIRST");
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP INDEX nid");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_usps_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} ADD PRIMARY KEY (vid)");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_usps_products', 'vid', 'integer unsigned', array('not null' => TRUE, 'default' => 0));
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} DROP CONSTRAINT {uc_usps_products}_nid_key");
      $result = db_query("SELECT nid, vid FROM {node}");
      while ($product = db_fetch_object($result)) {
        db_query("UPDATE {uc_usps_products} SET vid = %d WHERE nid = %d", $product->vid, $product->nid);
      }
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} ADD PRIMARY KEY (vid)");
    break;
  }
  return $ret;
}

function uc_usps_update_3() {
  $ret = array();
  $markup = variable_get('uc_usps_markup', '0%');
  if (strpos($markup, '%') !== FALSE) {
    variable_set('uc_usps_markup_type', 'percentage');
    variable_set('uc_usps_markup', floatval(rtrim($markup, '%')));
  }
  elseif (strpos($markup, '$') !== FALSE) {
    variable_set('uc_usps_markup_type', 'currency');
    variable_set('uc_usps_markup', floatval(ltrim($markup, '$')));
  }
  elseif (floatval($markup)) {
    variable_set('uc_usps_markup_type', 'multiplier');
    variable_set('uc_usps_markup', floatval($markup));
  }
  $ret[] = array('success' => TRUE, 'query' => 'Added USPS markup type.');
  return $ret;
}

function uc_usps_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_usps_products} CHANGE container container varchar(255) NOT NULL");
    break;
    case 'pgsql':
      db_change_column($ret, 'uc_usps_products', 'container', 'container', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
    break;
  }
  return $ret;
}

function uc_usps_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_change_column($ret, 'uc_usps_prodcts', 'vid', 'vid', 'int_unsigned', array('not null' => TRUE, 'default' => 0));
      db_change_column($ret, 'uc_usps_prodcts', 'nid', 'nid', 'int_unsigned', array('not null' => TRUE, 'default' => 0));
    break;
  }
  return $ret;
}

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

  db_drop_primary_key($ret, 'uc_usps_products');
  db_change_field($ret, 'uc_usps_products', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('vid')));
  db_change_field($ret, 'uc_usps_products', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

/**
 * Set default weight for new shipping type.
 */
function uc_usps_update_6001() {
  $shipping_type_weights = variable_get('uc_quote_type_weight', NULL);
  if (isset($shipping_type_weights)) {
    $shipping_type_weights['envelope'] = -1;
    variable_set('uc_quote_type_weight', $shipping_type_weights);
  }

  $services = variable_get('uc_usps_services', NULL);
  if (isset($services)) {
    $env_services = array(
      'zero' => t('U.S.P.S. First-Class Mail'),
      'zeroFlat' => t('U.S.P.S. First-Class Flat'),
      12 => t('U.S.P.S. First-Class Postcard Stamped'),
      1 => t('U.S.P.S. Priority Mail'),
      16 => t('U.S.P.S. Priority Mail Flat-Rate Envelope'),
      2 => t('U.S.P.S. Express Mail PO to PO'),
      3 => t('U.S.P.S. Express Mail PO to Addressee'),
      13 => t('U.S.P.S. Express Mail Flat-Rate Envelope'),
    );
    $new_services = array();
    foreach ($env_services as $id => $value) {
      $new_services[$id] = $services[$id];
      unset($services[$id]);
    }
    variable_set('uc_usps_env_services', $new_services);
    variable_set('uc_usps_services', $services);
  }

  $services = variable_get('uc_usps_intl_services', NULL);
  if (isset($services)) {
    $intl_env_services = array(
      13 => t('First Class Mail International Letter'),
      14 => t('First Class Mail International Flat'),
      2 => t('Priority Mail International'),
      8 => t('Priority Mail International Flat Rate Envelope'),
      4 => t('Global Express Guaranteed'),
      1 => t('Express Mail International (EMS)'),
      10 => t('Express Mail International (EMS) Flat Rate Envelope'),
    );
    $new_services = array();
    foreach ($intl_env_services as $id => $value) {
      $new_services[$id] = $services[$id];
      unset($services[$id]);
    }
    variable_set('uc_usps_intl_env_services', $new_services);
    variable_set('uc_usps_intl_services', $services);
  }

  return array();
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal uc_usps.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.