You are here

function physical_dimensions_convert in Physical Fields 7

Converts a dimension field value array to the specified unit.

Parameters

$dimensions: A dimension field value array including the 'length', 'width', 'height' and 'unit'.

$unit: The dimensions unit type to convert to.

Return value

A dimension field value array including the converted 'length', 'width', and 'height' amounts in the target 'unit' type.

File

./physical.module, line 1222
Defines fields (e.g. weight and dimensions) to support describing the physical attributes of entities.

Code

function physical_dimensions_convert($dimensions, $unit) {
  if ($dimensions['unit'] == $unit) {
    return $dimensions;
  }

  // Convert the dimension amounts based on the target and source unit type.
  switch ($unit) {
    case 'in':
      if ($dimensions['unit'] == 'ft') {
        $multiplier = 12;
      }
      elseif ($dimensions['unit'] == 'mm') {
        $multiplier = 0.0393700787;
      }
      elseif ($dimensions['unit'] == 'cm') {
        $multiplier = 0.393700787;
      }
      elseif ($dimensions['unit'] == 'm') {
        $multiplier = 39.3700787;
      }
      break;
    case 'ft':
      if ($dimensions['unit'] == 'in') {
        $multiplier = 0.08333333330000001;
      }
      elseif ($dimensions['unit'] == 'mm') {
        $multiplier = 0.0032808399;
      }
      elseif ($dimensions['unit'] == 'cm') {
        $multiplier = 0.032808399;
      }
      elseif ($dimensions['unit'] == 'm') {
        $multiplier = 3.2808399;
      }
      break;
    case 'mm':
      if ($dimensions['unit'] == 'in') {
        $multiplier = 25.4;
      }
      elseif ($dimensions['unit'] == 'ft') {
        $multiplier = 304.8;
      }
      elseif ($dimensions['unit'] == 'cm') {
        $multiplier = 10;
      }
      elseif ($dimensions['unit'] == 'm') {
        $multiplier = 1000;
      }
      break;
    case 'cm':
      if ($dimensions['unit'] == 'in') {
        $multiplier = 2.54;
      }
      elseif ($dimensions['unit'] == 'ft') {
        $multiplier = 30.48;
      }
      elseif ($dimensions['unit'] == 'mm') {
        $multiplier = 0.1;
      }
      elseif ($dimensions['unit'] == 'm') {
        $multiplier = 100;
      }
      break;
    case 'm':
      if ($dimensions['unit'] == 'in') {
        $multiplier = 0.0254;
      }
      elseif ($dimensions['unit'] == 'ft') {
        $multiplier = 0.3048;
      }
      elseif ($dimensions['unit'] == 'mm') {
        $multiplier = 0.001;
      }
      elseif ($dimensions['unit'] == 'cm') {
        $multiplier = 0.01;
      }
      break;
  }

  // Update each of the dimensions using the multiplier.
  $dimensions['length'] *= $multiplier;
  $dimensions['width'] *= $multiplier;
  $dimensions['height'] *= $multiplier;

  // Update the unit type to the target type.
  $dimensions['unit'] = $unit;
  return $dimensions;
}