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

Drupal example source code file (uc_cart.install)

This example Drupal source code file (uc_cart.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, description, function, not, null, php, ret, sql, the, true, type

The uc_cart.install Drupal example source code

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

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

/**
 * Implementation of 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',
        'size' => 'small',
        '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'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function uc_cart_install() {
  drupal_install_schema('uc_cart');
}

/**
 * Implementation of hook_uninstall().
 */
function uc_cart_uninstall() {
  drupal_uninstall_schema('uc_cart');

  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_cart_%%'");
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_pane_%%'");
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_cap_%%'");
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_checkout_%%'");
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_msg_%%'");
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_new_customer_%%'");

  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_ce_no_cancel');
  variable_del('uc_ce_submit_disable');
}

/**
 * Standardize database definitions during upgrade to Drupal 6.
 *
 * ID fields are unsigned, regular-sized ints. "Boolean" flags are unsigned
 * tiny ints. Postgres tables will have the necessary default values, and MySQL
 * doesn't need them, so the schema can just be mismatched for that.
 */
function uc_cart_update_6000() {
  $ret = array();

  db_change_field($ret, 'uc_cart_products', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_cart_products', 'qty', 'qty', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'small', 'not null' => TRUE, 'default' => 0));

  return $ret;
}

/**
 * Remove unused variables.
 */
function uc_cart_update_6001() {
  variable_del('uc_checkout_review_button');
  variable_del('uc_checkout_submit_button');
  variable_del('uc_checkout_next_button');

  return array();
}

/**
 * Increase width of cart id to 255.
 */
function uc_cart_update_6002() {
  $ret = array();

  db_change_field($ret, 'uc_cart_products', 'cart_id', 'cart_id', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

/**
 * Add an autoincrement primary key to the uc_cart_products table
 */
function uc_cart_update_6200() {
  $ret = array();

  db_add_field($ret, 'uc_cart_products', 'cart_item_id', array(
        'description' => 'Unique identifier for cart item.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
    array('primary key' => array('cart_item_id'),)
  );

  return $ret;
}

/**
 * Add index to uc_cart_products.cart_id.
 */
function uc_cart_update_6201() {
  $ret = array();

  db_add_index($ret, 'uc_cart_products', 'cart_id', array('cart_id'));

  // Remove unnecessary error message that would occur for new installs.
  $mysql_error = check_plain("Duplicate key name 'cart_id'");
  $pg_error = check_plain('relation "uc_cart_products_cart_id_idx" already exists');
  $pg_error2 = check_plain('query: CREATE INDEX uc_cart_products_cart_id_idx ON uc_cart_products (cart_id)');
  $errors = $_SESSION['messages']['error'];
  $total = count($errors);
  for ($i = 0; $i < $total; $i++) {
    if (strpos($errors[$i], $mysql_error) || strpos($errors[$i], $pg_error) || strpos($errors[$i], $pg_error2)) {
      unset($_SESSION['messages']['error'][$i]);
      $ret[0]['success'] = TRUE;
    }
  }
  if (empty($_SESSION['messages']['error'])) {
    unset($_SESSION['messages']['error']);
  }

  return $ret;
}

Other Drupal examples (source code examples)

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