units_default.module in Units of Measurement 7.2
Same filename and directory in other branches
Create common units and measures such as length, volume, weight, etc.
File
units_default/units_default.moduleView source
<?php
/**
* @file
* Create common units and measures such as length, volume, weight, etc.
*/
/**
* Implements hook_menu().
*/
function units_default_menu() {
$items = array();
$items['admin/structure/units-measure/default'] = array(
'title' => 'Administer default units',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'units_default_form',
),
'access arguments' => array(
'administer default units',
),
'file' => 'units_default.pages.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function units_default_permission() {
return array(
'administer default units' => array(
'title' => t('Administer default units'),
'description' => t('Enable and disable default units and measures.'),
),
);
}
/**
* Implements hook_default_units_measure().
*/
function units_default_default_units_measure() {
$settings = units_default_enabled();
$items = array_intersect_key(_units_default_default_units_measure(), $settings);
return $items;
}
/**
* Implements hook_default_units_unit().
*/
function units_default_default_units_unit() {
$settings = units_default_enabled();
$enabled_units = array();
foreach ($settings as $v) {
$enabled_units += $v['units'];
}
$items = array_intersect_key(_units_default_default_units_unit(), $enabled_units);
return $items;
}
/**
* Generate all default unit measures this module can provide.
*/
function _units_default_default_units_measure() {
$items = array();
$controller = entity_get_controller('units_measure');
$items['length'] = $controller
->create(array(
'measure' => 'length',
'label' => 'Length',
'description' => '',
));
$items['volume'] = $controller
->create(array(
'measure' => 'volume',
'label' => 'Volume',
'description' => '',
));
$items['weight'] = $controller
->create(array(
'measure' => 'weight',
'label' => 'Weight',
'description' => '',
));
$items['area'] = $controller
->create(array(
'measure' => 'area',
'label' => 'Area',
'description' => '',
));
$items['force'] = $controller
->create(array(
'measure' => 'force',
'label' => 'Force',
'description' => '',
));
$items['pressure'] = $controller
->create(array(
'measure' => 'pressure',
'label' => 'Pressure',
'description' => '',
));
$items['time'] = $controller
->create(array(
'measure' => 'time',
'label' => 'Time',
'description' => '',
));
$items['temperature'] = $controller
->create(array(
'measure' => 'temperature',
'label' => 'Temperature',
'description' => '',
));
return $items;
}
/**
* Generate all default units this module can provide.
*/
function _units_default_default_units_unit() {
$items = array();
$controller = entity_get_controller('units_unit');
$meter = units_unit_machine_name_load('meter');
$kilogram = units_unit_machine_name_load('kilogram');
$second = units_unit_machine_name_load('second');
$celsius = units_unit_machine_name_load('celsius');
$items['meter'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'meter',
'label' => 'meter',
'symbol' => 'm',
'description' => '',
'decomposition' => NULL,
));
// Since all length units decompose into meter, we must assure that meter
// exists before we can define the other units.
if ($meter) {
$items['millimeter'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'millimeter',
'label' => 'millimeter',
'symbol' => 'mm',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.001 meter *'),
));
$items['centimeter'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'centimeter',
'label' => 'centimeter',
'symbol' => 'cm',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.01 meter *'),
));
$items['decimeter'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'decimeter',
'label' => 'decimeter',
'symbol' => 'dm',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.1 meter *'),
));
$items['kilometer'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'kilometer',
'label' => 'kilometer',
'symbol' => 'km',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1000 meter *'),
));
$items['foot'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'foot',
'label' => 'foot',
'symbol' => 'ft',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.3048 meter *'),
));
$items['inch'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'inch',
'label' => 'inch',
'symbol' => 'in',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.0254 meter *'),
));
$items['mile'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'mile',
'label' => 'mile',
'symbol' => 'mi',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1609.344 meter *'),
));
$items['yard'] = $controller
->create(array(
'measure' => 'length',
'machine_name' => 'yard',
'label' => 'yard',
'symbol' => 'yd',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.9144 meter *'),
));
$items['square_meter'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'square_meter',
'label' => 'square meter',
'symbol' => 'm2',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('meter 2 ^'),
));
$items['acre'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'acre',
'label' => 'acre',
'symbol' => 'acre',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('4.046873e+3 meter 2 ^ *'),
));
$items['are'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'are',
'label' => 'are',
'symbol' => 'a',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.0e+2 meter 2 ^ *'),
));
$items['hectare'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'hectare',
'label' => 'hectare',
'symbol' => 'ha',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.0e+4 meter 2 ^ *'),
));
$items['cubic_meter'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'cubic_meter',
'label' => 'cubic meter',
'symbol' => 'm3',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('meter 3 ^'),
));
$items['liter'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'liter',
'label' => 'liter',
'symbol' => 'L',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.001 meter 3 ^ *'),
));
$items['milliliter'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'milliliter',
'label' => 'milliliter',
'symbol' => 'mL',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.000001 meter 3 ^ *'),
));
$items['cup'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'cup',
'label' => 'cup',
'symbol' => 'cup',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('2.365882e-04 meter 3 ^ *'),
));
$items['us_gallon'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'us_gallon',
'label' => 'US gallon',
'symbol' => 'gal',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('3.785412e-03 meter 3 ^ *'),
));
$items['us_fluid_ounce'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'us_fluid_ounce',
'label' => 'US fluid ounce',
'symbol' => 'fl oz',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('2.957353e-05 meter 3 ^ *'),
));
$items['us_liquid_pint'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'us_liquid_pint',
'label' => 'US pint (liquid)',
'symbol' => 'pt',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('4.731765e-04 meter 3 ^ *'),
));
$items['us_liquid_quart'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'us_liquid_quart',
'label' => 'US quart (liquid)',
'symbol' => 'qt',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('9.463529e-04 meter 3 ^ *'),
));
$items['tablespoon'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'tablespoon',
'label' => 'tablespoon',
'symbol' => 'tbsp',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.478676e-05 meter 3 ^ *'),
));
$items['teaspoon'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'teaspoon',
'label' => 'teaspoon',
'symbol' => 'tspn',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('4.928922E-06 meter 3 ^ *'),
));
}
if (units_unit_machine_name_load('foot')) {
$items['square_foot'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'square_foot',
'label' => 'square foot',
'symbol' => 'ft2',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('foot 2 ^'),
));
$items['cubic_foot'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'cubic_foot',
'label' => 'cubic foot',
'symbol' => 'ft3',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('foot 3 ^'),
));
}
if (units_unit_machine_name_load('inch')) {
$items['square_inch'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'square_inch',
'label' => 'square inch',
'symbol' => 'in2',
'description' => '',
'decomposition_postfix' => units_mathematical_expression_create_from_postfix('inch 2 ^'),
));
$items['cubic_inch'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'cubic_inch',
'label' => 'cubic inch',
'symbol' => 'in3',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('inch 3 ^'),
));
}
if (units_unit_machine_name_load('kilometer')) {
$items['square_kilometer'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'square_kilometer',
'label' => 'square kilometer',
'symbol' => 'km2',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('kilometer 2 ^'),
));
}
if (units_unit_machine_name_load('mile')) {
$items['square_mile'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'square_mile',
'label' => 'square mile',
'symbol' => 'mi2',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('mile 2 ^'),
));
$items['cubic_mile'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'cubic_mile',
'label' => 'cubic mile',
'symbol' => 'mi3',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('mile 3 ^'),
));
}
if (units_unit_machine_name_load('yard')) {
$items['square_yard'] = $controller
->create(array(
'measure' => 'area',
'machine_name' => 'square_yard',
'label' => 'square yard',
'symbol' => 'yd2',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('yard 2 ^'),
));
$items['cubic_yard'] = $controller
->create(array(
'measure' => 'volume',
'machine_name' => 'cubic_yard',
'label' => 'cubic yard',
'symbol' => 'yd3',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('yard 3 ^'),
));
}
$items['kilogram'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'kilogram',
'label' => 'kilogram',
'symbol' => 'kg',
'description' => '',
'decomposition' => NULL,
));
if ($kilogram) {
$items['milligram'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'milligram',
'label' => 'milligram',
'symbol' => 'mg',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.000001 kilogram *'),
));
$items['gram'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'gram',
'label' => 'gram',
'symbol' => 'g',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('0.001 kilogram *'),
));
$items['carat'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'carat',
'label' => 'carat',
'symbol' => 'CD',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('2.0e-4 kilogram *'),
));
$items['ounce'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'ounce',
'label' => 'ounce',
'symbol' => 'oz',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('2.834952e-02 kilogram *'),
));
$items['pennyweight'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'pennyweight',
'label' => 'pennyweight',
'symbol' => 'dwt',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.555174e-03 kilogram *'),
));
$items['pound'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'pound',
'label' => 'pound',
'symbol' => 'lb',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('4.535924e-01 kilogram *'),
));
$items['ton'] = $controller
->create(array(
'measure' => 'weight',
'machine_name' => 'ton',
'label' => 'ton',
'symbol' => 't',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.0e+3 kilogram *'),
));
}
if ($meter && $second && $kilogram) {
$items['newton'] = $controller
->create(array(
'measure' => 'force',
'machine_name' => 'newton',
'label' => 'newton',
'symbol' => 'N',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('kilogram meter * second 2 ^ /'),
));
$items['pascal'] = $controller
->create(array(
'measure' => 'pressure',
'machine_name' => 'pascal',
'label' => 'pascal',
'symbol' => 'pa',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('kilogram meter / second 2 ^ /'),
));
}
if (units_unit_machine_name_load('pascal')) {
$items['torr'] = $controller
->create(array(
'measure' => 'pressure',
'machine_name' => 'torr',
'label' => 'torr',
'symbol' => 'Torr',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.333224e+2 pascal *'),
));
$items['bar'] = $controller
->create(array(
'measure' => 'pressure',
'machine_name' => 'bar',
'label' => 'bar',
'symbol' => 'bar',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.0e+5 pascal *'),
));
$items['millibar'] = $controller
->create(array(
'measure' => 'pressure',
'machine_name' => 'millibar',
'label' => 'millibar',
'symbol' => 'mb',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('1.0e+2 pascal *'),
));
$items['psi'] = $controller
->create(array(
'measure' => 'pressure',
'machine_name' => 'psi',
'label' => 'psi',
'symbol' => 'lbf/in2',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('6.894757e+3 pascal *'),
));
}
$items['second'] = $controller
->create(array(
'measure' => 'time',
'machine_name' => 'second',
'label' => 'second',
'symbol' => 's',
'description' => '',
'decomposition' => NULL,
));
if ($second) {
$items['minute'] = $controller
->create(array(
'measure' => 'time',
'machine_name' => 'minute',
'label' => 'minute',
'symbol' => 'min',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('60 second *'),
));
$items['hour'] = $controller
->create(array(
'measure' => 'time',
'machine_name' => 'hour',
'label' => 'hour',
'symbol' => 'h',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('3600 second *'),
));
$items['day'] = $controller
->create(array(
'measure' => 'time',
'machine_name' => 'day',
'label' => 'day',
'symbol' => 'd',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('86400 second *'),
));
$items['year'] = $controller
->create(array(
'measure' => 'time',
'machine_name' => 'year',
'label' => 'year',
'symbol' => 'yr',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix('31536000 second *'),
));
}
$items['celsius'] = $controller
->create(array(
'measure' => 'temperature',
'machine_name' => 'celsius',
'label' => 'celsius',
'symbol' => "°C",
'description' => '',
'decomposition' => NULL,
));
if ($celsius) {
$items['fahrenheit'] = $controller
->create(array(
'measure' => 'temperature',
'machine_name' => 'fahrenheit',
'label' => 'fahrenheit',
'symbol' => "°F",
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix(UNITS_QUANTITY . ' 32 - 5 * 9 / celsius *'),
));
$items['kelvin'] = $controller
->create(array(
'measure' => 'temperature',
'machine_name' => 'kelvin',
'label' => 'kelvin',
'symbol' => 'K',
'description' => '',
'decomposition' => units_mathematical_expression_create_from_postfix(UNITS_QUANTITY . ' 273.15 - celsius *'),
));
}
return $items;
}
/**
* Retrieve all enabled default units.
*
* @return array
* Array of the information about all enabled default units. The array will
* only include the enabled units. Keys of this array are machine names of
* enabled measures, whereas values will be arrays with the following
* structure:
* - units: (array) Array of units enabled within this measure. Its keys and
* values both will be machine names of enabled units
*/
function units_default_enabled() {
return variable_get('units_default_enabled', array(
'length' => array(
'measure' => 'length',
'units' => array(
'meter' => 'meter',
'foot' => 'foot',
'centimeter' => 'centimeter',
'inch' => 'inch',
),
),
));
}
Functions
Name | Description |
---|---|
units_default_default_units_measure | Implements hook_default_units_measure(). |
units_default_default_units_unit | Implements hook_default_units_unit(). |
units_default_enabled | Retrieve all enabled default units. |
units_default_menu | Implements hook_menu(). |
units_default_permission | Implements hook_permission(). |
_units_default_default_units_measure | Generate all default unit measures this module can provide. |
_units_default_default_units_unit | Generate all default units this module can provide. |