You are here

units.install in Units of Measurement 7.2

Same filename and directory in other branches
  1. 7 units.install

Install and uninstall hooks of the Units module.

File

units.install
View source
<?php

/**
 * @file
 * Install and uninstall hooks of the Units module.
 */

/**
 * Implements hook_schema().
 */
function units_schema() {
  $schema = array();
  $schema['units_unit'] = array(
    'description' => 'Store information on unit types.',
    'fields' => array(
      'umid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique unit measure ID.',
      ),
      'measure' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => '{units_measure}.measure that this unit measure is capable of measuring.',
      ),
      'machine_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Machine readable name of unit measure.',
      ),
      'decomposition_mathematical_expression_id' => array(
        'type' => 'int',
        'length' => 10,
        'not null' => TRUE,
        'description' => 'Pointer to {units_mathematical_expression_postfix}.mathematical_expression_id that denotes decomposition of this unit.',
      ),
      'label' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Human readable name of unit measure.',
      ),
      'symbol' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Symbol that corresponds to this unit measure.',
        'default' => '',
      ),
      'description' => array(
        'description' => 'Description of measure.',
        'type' => 'text',
        'size' => 'medium',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'umid',
    ),
    'foreign keys' => array(
      'units_measure' => array(
        'table' => 'units_measure',
        'columns' => array(
          'measure' => 'measure',
        ),
      ),
    ),
    'unique keys' => array(
      'machine_name' => array(
        'machine_name',
      ),
    ),
  );
  $schema['units_measure'] = array(
    'description' => 'Store information on measures.',
    'fields' => array(
      'mid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique measure ID.',
      ),
      'measure' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Machine readable name of measure.',
      ),
      'label' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Human readable name of measure.',
      ),
      'description' => array(
        'description' => 'Description of measure.',
        'type' => 'text',
        'size' => 'medium',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'mid',
    ),
    'unique keys' => array(
      'measure' => array(
        'measure',
      ),
    ),
  );
  $schema['units_mathematical_expression_postfix'] = array(
    'description' => 'Store mathematical expressions in postfix (reverse polish notation).',
    'fields' => array(
      'eid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary key: unique expression ID.',
      ),
      'mathematical_expression_id' => array(
        'type' => 'int',
        'length' => 10,
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'ID of the mathematical expression to this this member/token belongs.',
      ),
      'type' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'size' => 'tiny',
        'description' => 'Type of this node in abstract tree. ' . UNITS_TOKEN_TYPE_CONSTANT . ' is constant. ' . UNITS_TOKEN_TYPE_UNIT . ' is unit. ' . UNITS_TOKEN_TYPE_OPERATOR . ' is operator.',
      ),
      'value_numeric' => array(
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Numeric value of this node, if numeric value for this node makes sense.',
      ),
      'value_string' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'postfix_order' => array(
        'type' => 'int',
        'length' => 10,
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Order of this member/token in its mathematical expression.',
      ),
    ),
    'primary key' => array(
      'eid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_enable().
 */
function units_enable() {
  units_mathematical_expression_recreate_stored_functions(TRUE);
}

/**
 * Implements hook_disable().
 */
function units_disable() {
  units_mathematical_expression_recreate_stored_functions(FALSE);
}

/**
 * Implements hook_update_N().
 *
 * Update to version of Units module, that supports 'symbol' for 'units_unit'
 * entity type.
 */
function units_update_7101() {

  // Adding 'symbol' column to the 'units_unit' table.
  $table = 'units_unit';
  $column = 'symbol';
  if (!db_field_exists($table, $column)) {
    $schema = drupal_get_schema($table, TRUE);
    db_add_field($table, $column, $schema['fields'][$column]);
  }
  return t('Updated settings of Units module. Now it supports <em>symbol</em> property for units.');
}

/**
 * Implements hook_update_N().
 *
 * Update to version of Units module, that supports 'exportable' feature for
 * 'units_unit' and 'units_measure' entity types.
 */
function units_update_7102() {

  // Adding 'status' and 'module' columns to the 'units_unit' and
  // 'units_measure' tables.
  $tables = array(
    'units_unit',
    'units_measure',
  );
  $columns = array(
    'status',
    'module',
  );
  foreach ($tables as $table) {
    $schema = drupal_get_schema($table, TRUE);
    foreach ($columns as $column) {
      if (!db_field_exists($table, $column)) {
        db_add_field($table, $column, $schema['fields'][$column]);
      }
    }
  }
  return t('Updated settings of Units module. Now it supports Features exportables.');
}

/**
 * Adapt existing measures to CTools units converters plugin scheme.
 */
function units_update_7103() {
  $new_field = 'converter';
  $old_field = 'convert_callback';
  db_add_field('units_measure', $new_field, array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => 'Name of cTools units converter plugin responsible for converting units in this measure.',
  ));
  $outstanding_measures = db_select('units_measure', 'm')
    ->fields('m', array(
    'label',
  ))
    ->condition($old_field, 'units_convert', '<>')
    ->execute()
    ->fetchCol();
  db_update('units_measure')
    ->fields(array(
    $new_field => 'linear',
  ))
    ->condition($old_field, 'units_convert')
    ->execute();
  db_drop_field('units_measure', $old_field);

  // For some reason without this clear cache the schema wouldn't get updated
  // to the latest columns.
  drupal_static_reset();
  _cache_get_object('cache')
    ->clear('schema', TRUE);
  if (empty($outstanding_measures)) {
    return t('Successfully migrated measures onto the new conversion logic.');
  }
  else {
    return t('Successfully migrated measures onto the new conversion logic. The following measures likely to need a new conversion logic: %measures.', array(
      '%measures' => implode(', ', $outstanding_measures),
    ));
  }
}

Functions

Namesort descending Description
units_disable Implements hook_disable().
units_enable Implements hook_enable().
units_schema Implements hook_schema().
units_update_7101 Implements hook_update_N().
units_update_7102 Implements hook_update_N().
units_update_7103 Adapt existing measures to CTools units converters plugin scheme.