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

Drupal example source code file (uc_product_kit.install)

This example Drupal source code file (uc_product_kit.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, default, function, not, null, php, ret, sql, the, true, uc_product_kits, update_sql, vid

The uc_product_kit.install Drupal example source code

<?php
// $Id: uc_product_kit.install,v 1.11.2.12 2010/07/09 13:16:02 islandusurper Exp $

/**
 * @file
 * Install file for uc_product_kit
 */

/**
 * Implementation of hook_schema().
 */
function uc_product_kit_schema() {
  $schema['uc_product_kits'] = array(
    'description' => 'Stores product kit information.',
    'fields' => array(
      'vid' => array(
        'description' => 'The {node}.vid of the product kit.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the product kit.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'product_id' => array(
        'description' => 'The {uc_products}.nid of a product contained in the kit.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'mutable' => array(
        'description' => 'A flag indicating whether the contents of the kit can be changed by the customer. 1 => Mutable. 0 => Immutable.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'qty' => array(
        'description' => 'The number of this product contained in the kit.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'discount' => array(
        'description' => 'The adjustment to the {uc_products}.sell_price of the product.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'ordering' => array(
        'description' => 'The weight of this product in relation to other products in the kit.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
      'synchronized' => array(
        'description' => 'A flag indicating that changes to the sell price of this product will change the total price of the kit. 1 => Yes. 0 => No.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('vid', 'product_id'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function uc_product_kit_install() {
  drupal_install_schema('uc_product_kit');
}

/**
 * Implementation of hook_uninstall().
 */
function uc_product_kit_uninstall() {
  drupal_uninstall_schema('uc_product_kit');
  variable_del('uc_product_kit_mutable');
}

/**
 * Implementation of hook_enable().
 */
function uc_product_kit_enable() {
  // For some time in Drupal 5, CCK would delete its field data if a node
  // type was unavailable because its module was disabled. This function
  // worked around that by giving the product classes to node.module when
  // uc_product_kit was disabled. This is no longer necessary as of CCK
  // 5.x-1.9, but the workaround was left in to prevent accidents. This block
  // of code is here to reclaim the product kit nodes after an upgrade to
  // Drupal 6, and then should not be used again as the corresponding code in
  // uc_product_kit_disable() was removed.
  if (variable_get('uc_product_kit_enable_nodes', TRUE)) {
    $node_type = node_get_types('type', 'product_kit');
    if (is_object($node_type) && $node_type->module == 'node') {
      $node_type->module = 'uc_product_kit';
      $node_type->custom = 0;
      node_type_save($node_type);
    }
    variable_set('uc_product_kit_enable_nodes', FALSE);
  }

  // Hack the product kit node type into the catalog if this module is enabled
  // some time after uc_catalog.
  if (module_exists('uc_catalog') && $vid = variable_get('uc_catalog_vid', 0)) {
    $vocab = taxonomy_vocabulary_load($vid);
    if (!isset($vocab->nodes['product_kit'])) {
      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $vid, 'product_kit');
    }
  }

  // Add field_image_cache to product kits.
  if (module_exists('imagefield')) {
    uc_product_add_default_image_field('product_kit');
  }
}

function uc_product_kit_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_product_kits} ADD PRIMARY KEY (nid, product_id)");
      $ret[] = update_sql("ALTER TABLE {uc_product_kits} ADD COLUMN mutable tinyint(1) NOT NULL default 0 AFTER product_id");
      $ret[] = update_sql("ALTER TABLE {uc_product_kits} ADD COLUMN discount float NOT NULL default 0.0 AFTER qty");
    break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_product_kits} ADD PRIMARY KEY (nid, product_id)");
      db_add_column($ret, 'uc_product_kits', 'mutable', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'uc_product_kits', 'discount', 'float', array('not null' => TRUE, 'default' => 0.0));
    break;
  }
  return $ret;
}

function uc_product_kit_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_product_kits} ADD COLUMN ordering smallint NOT NULL default 0");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_product_kits', 'ordering', 'smallint', array('not null' => TRUE, 'default' => 0));
    break;
  }
  return $ret;
}

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

function uc_product_kit_update_4() {
  $ret = array();
  db_query("UPDATE {node} SET type = 'product_kit' WHERE type = 'kit'");
  if (module_exists('content')) {
    $ret[] = update_sql("ALTER TABLE {content_type_kit} RENAME TO {content_type_product_kit}");
  }
  node_types_rebuild();
  menu_rebuild();
  return $ret;
}

function uc_product_kit_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_product_kits} DROP data");
    break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_product_kits} DROP data");
    break;
  }
  return $ret;
}

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

  db_drop_primary_key($ret, 'uc_product_kits');
  db_change_field($ret, 'uc_product_kits', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_kits', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_kits', 'product_id', 'product_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_kits', 'qty', 'qty', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'small', 'not null' => TRUE, 'default' => 0));
  db_add_primary_key($ret, 'uc_product_kits', array('vid', 'product_id'));

  return $ret;
}

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

  variable_set('uc_image_product_kit', 'field_image_cache');

  $t = get_t();
  $ret[] = array('success' => TRUE, $t('field_image_cache set as the Ubercart image for product kits.'));

  return $ret;
}

/**
 * Update moved to 6003 to fix flawed beta5 installations.
 * See http://drupal.org/node/400720.
 */
function uc_product_kit_update_6002() {
  $ret = array();

  return $ret;
}

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

  if (!db_column_exists('uc_product_kits', 'synchronized')) {
    db_add_field($ret, 'uc_product_kits', 'synchronized', array('type' => 'int', 'size', 'tiny', 'not null' => TRUE, 'default' => 0));
    $ret[] = update_sql("UPDATE {uc_product_kits} SET synchronized = 1 WHERE discount < 0");

    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql("UPDATE {uc_product_kits} AS pk JOIN {uc_products} AS p ON pk.product_id = p.nid SET pk.discount = pk.discount - p.sell_price WHERE pk.discount >= 0;");
      break;
      case 'pgsql':
        $ret[] = update_sql("UPDATE {uc_product_kits} SET discount = discount - (SELECT p.sell_price FROM {uc_products} p JOIN {uc_product_kits} pk ON pk.product_id = p.nid WHERE pk.discount >= 0);");
      break;
    }
  }

  return $ret;
}

Other Drupal examples (source code examples)

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