--- /dev/null
+<?php\r
+// +----------------------------------------------------------------------+\r
+// | PHP version 4.0 |\r
+// +----------------------------------------------------------------------+\r
+// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |\r
+// +----------------------------------------------------------------------+\r
+// | This source file is subject to version 2.0 of the PHP license, |\r
+// | that is bundled with this package in the file LICENSE, and is |\r
+// | available at through the world-wide-web at |\r
+// | http://www.php.net/license/2_02.txt. |\r
+// | If you did not receive a copy of the PHP license and are unable to |\r
+// | obtain it through the world-wide-web, please send a note to |\r
+// | license@php.net so we can mail you a copy immediately. |\r
+// +----------------------------------------------------------------------+\r
+// | Authors: Stanislav Okhvat <stanis@ngs.ru> |\r
+// | Co-authored by : CVH, Chris Hansel <chris@cpi-service.com> |\r
+// +----------------------------------------------------------------------+\r
+//\r
+// $Id: UnitConvertor.php,v 1.00 2002/02/20 11:40:00 stasokhvat Exp $\r
+\r
+/**\r
+* UnitConvertor is able to convert between different units and currencies.\r
+*\r
+* @author Stanislav Okhvat <stanis@sibfair.nsk.su, stanis@ngs.ru>\r
+* @version $Id: UnitConvertor.php,v 1.00 2002/03/01 17:00:00 stasokhvat Exp $\r
+* @package UnitConvertor\r
+* @access public\r
+* @history 01.03.2002 Implemented the code for regular and offset-based\r
+* conversions\r
+*\r
+* 13.12.2004\r
+* By Chris Hansel (CVH): changed getConvSpecs in order to have it look up \r
+* intermediary conversions (also see comments in check_key).\r
+*\r
+* Intermediary conversions are useful when no conversion ratio is specified\r
+* between two units when we calculate between the two. For example, we want\r
+* to convert between Fahrenheit and Kelvin, and we have only\r
+* specified how to convert Centigrade<->Fahrenheit and\r
+* Centigrade<->Kelvin. While a direct (Fahrenheit->Kelvin) or\r
+* reverse (Kelvin->Fahrenheit) lookups fail, looking for an intermediary\r
+* unit linking the two (Centigrade) helps us do the conversion.\r
+*\r
+* 13.12.2004\r
+* Chris Hansel (CVH): $to_array argument of addConversion method can now\r
+* contain units as 'unit1/unit2/unit3', when all units stand for the same\r
+* thing. See examples in unitconv.php\r
+*/\r
+class UnitConvertor\r
+{\r
+ /**\r
+ * Stores conversion ratios.\r
+ *\r
+ * @var array\r
+ * @access private\r
+ */\r
+ var $conversion_table = array();\r
+\r
+ /**\r
+ * Decimal point character (default is "." - American - set in constructor).\r
+ *\r
+ * @var string\r
+ * @access private\r
+ */\r
+ var $decimal_point;\r
+\r
+ /**\r
+ * Thousands separator (default is "," - American - set in constructor).\r
+ *\r
+ * @var string\r
+ * @access private\r
+ */\r
+ var $thousand_separator;\r
+\r
+ /**\r
+ * For future use\r
+ *\r
+ * @var array\r
+ * @access private\r
+ */\r
+ var $bases = array();\r
+\r
+ /**\r
+ * Constructor. Initializes the UnitConvertor object with the most important\r
+ * properties.\r
+ *\r
+ * @param string decimal point character\r
+ * @param string thousand separator character\r
+ * @return void\r
+ * @access public\r
+ */\r
+ function UnitConvertor($dec_point = '.', $thousand_sep = ',')\r
+ {\r
+ $this->decimal_point = $dec_point;\r
+ $this->thousand_separator = $thousand_sep;\r
+\r
+ } // end func UnitConvertor\r
+\r
+ /**\r
+ * Adds a conversion ratio to the conversion table.\r
+ *\r
+ * @param string the name of unit from which to convert\r
+ * @param array array(\r
+ * "pound"=>array("ratio"=>'', "offset"=>'')\r
+ * )\r
+ * "pound" - name of unit to set conversion ration to\r
+ * "ratio" - 'double' conversion ratio which, when\r
+ * multiplied by the number of $from_unit units produces\r
+ * the result\r
+ * "offset" - an offset from 0 which will be added to\r
+ * the result when converting (needed for temperature\r
+ * conversions and defaults to 0).\r
+ * @return boolean true if successful, false otherwise\r
+ * @access public\r
+ */\r
+ function addConversion($from_unit, $to_array)\r
+ {\r
+ if (!isset($this->conversion_table[$from_unit])) {\r
+ while(list($key, $val) = each($to_array))\r
+ {\r
+ if (strstr($key, '/'))\r
+ {\r
+ $to_units = explode('/', $key);\r
+ foreach ($to_units as $to_unit)\r
+ {\r
+ $this->bases[$from_unit][] = $to_unit;\r
+\r
+ if (!is_array($val))\r
+ {\r
+ $this->conversion_table[$from_unit."_".$to_unit] = array("ratio"=>$val, "offset"=>0);\r
+ }\r
+ else\r
+ {\r
+ $this->conversion_table[$from_unit."_".$to_unit] =\r
+ array(\r
+ "ratio"=>$val['ratio'],\r
+ "offset"=>(isset($val['offset']) ? $val['offset'] : 0)\r
+ );\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ $this->bases[$from_unit][] = $key;\r
+\r
+ if (!is_array($val))\r
+ {\r
+ $this->conversion_table[$from_unit."_".$key] = array("ratio"=>$val, "offset"=>0);\r
+ }\r
+ else\r
+ {\r
+ $this->conversion_table[$from_unit."_".$key] =\r
+ array(\r
+ "ratio"=>$val['ratio'],\r
+ "offset"=>(isset($val['offset']) ? $val['offset'] : 0)\r
+ );\r
+ }\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+ return false;\r
+\r
+ } // end func addConversion\r
+\r
+ /**\r
+ * Converts from one unit to another using specified precision.\r
+ *\r
+ * @param double value to convert\r
+ * @param string name of the source unit from which to convert\r
+ * @param string name of the target unit to which we are converting\r
+ * @param integer double precision of the end result\r
+ * @return void\r
+ * @access public\r
+ */\r
+ function convert($value, $from_unit, $to_unit, $precision)\r
+ {\r
+ if ($this->getConvSpecs($from_unit, $to_unit, $value, $converted ))\r
+ {\r
+ return number_format($converted , (int)$precision, $this->decimal_point, $this->thousand_separator);\r
+ } else {\r
+ return false;\r
+ }\r
+ } // end func\r
+\r
+ /**\r
+ * CVH : changed this Function getConvSpecs in order to have it look up \r
+ * intermediary Conversions from the \r
+ * "base" unit being that one that has the highest hierarchical order in one\r
+ * "logical" Conversion_Array\r
+ * when taking $conv->addConversion('km',\r
+ * array('meter'=>1000, 'dmeter'=>10000, 'centimeter'=>100000,\r
+ * 'millimeter'=>1000000, 'mile'=>0.62137, 'naut.mile'=>0.53996,\r
+ * 'inch(es)/zoll'=>39370, 'ft/foot/feet'=>3280.8, 'yd/yard'=>1093.6));\r
+ * "km" would be the logical base unit for all units of dinstance, thus, \r
+ * if the function fails to find a direct or reverse conversion in the table \r
+ * it is only logical to suspect that if there is a chance \r
+ * converting the value it only is via the "base" unit, and so \r
+ * there is not even a need for a recursive search keeping the perfomance \r
+ * acceptable and the ressource small...\r
+ *\r
+ * CVH check_key checks for a key in the Conversiontable and returns a value\r
+ */\r
+ function check_key( $key) {\r
+ if ( array_key_exists ($key,$this->conversion_table)) {\r
+ if (! empty($this->conversion_table[$key])) {\r
+ return $this->conversion_table[$key];\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Key function. Finds the conversion ratio and offset from one unit to another.\r
+ *\r
+ * @param string name of the source unit from which to convert\r
+ * @param string name of the target unit to which we are converting\r
+ * @param double conversion ratio found. Returned by reference.\r
+ * @param double offset which needs to be added (or subtracted, if negative)\r
+ * to the result to convert correctly. \r
+ * For temperature or some scientific conversions,\r
+ * i.e. Fahrenheit -> Celcius\r
+ * @return boolean true if ratio and offset are found for the supplied\r
+ * units, false otherwise\r
+ * @access private\r
+ */\r
+ function getConvSpecs($from_unit, $to_unit, $value, &$converted)\r
+ {\r
+ $key = $from_unit."_".$to_unit;\r
+ $revkey = $to_unit."_".$from_unit;\r
+ $found = false;\r
+ if ($ct_arr = $this->check_key($key)) {\r
+ // Conversion Specs found directly\r
+ $ratio = (double)$ct_arr['ratio'];\r
+ $offset = $ct_arr['offset']; \r
+ $converted = (double)(($value * $ratio)+ $offset);\r
+ \r
+ return true;\r
+ } // not found in direct order, try reverse order\r
+ elseif ($ct_arr = $this->check_key($revkey)) {\r
+ $ratio = (double)(1/$ct_arr['ratio']);\r
+ $offset = -$ct_arr['offset'];\r
+ $converted = (double)(($value + $offset) * $ratio);\r
+ \r
+ return true;\r
+ } // not found test for intermediary conversion\r
+ else {\r
+ // return ratio = 1 if keyparts match\r
+ if ($key == $revkey) {\r
+ $ratio = 1;\r
+ $offset = 0;\r
+ $converted = $value;\r
+ return true;\r
+ } \r
+ // otherwise search intermediary\r
+ reset($this->conversion_table);\r
+ while (list($convk, $i1_value) = each($this->conversion_table)) {\r
+ // split the key into parts\r
+ $keyparts = preg_split("/_/",$convk);\r
+ // return ratio = 1 if keyparts match\r
+ \r
+ // Now test if either part matches the from or to unit\r
+ if ($keyparts[1] == $to_unit && ($i2_value = $this->check_key($keyparts[0]."_".$from_unit))) {\r
+ // an intermediary $keyparts[0] was found\r
+ // now let us put things together intermediary 1 and 2\r
+ $converted = (double)(((($value - $i2_value['offset']) / $i2_value['ratio']) * $i1_value['ratio'])+ $i1_value['offset']);\r
+\r
+ $found = true;\r
+ \r
+ } elseif ($keyparts[1] == $from_unit && ($i2_value = $this->check_key($keyparts[0]."_".$to_unit))) {\r
+ // an intermediary $keyparts[0] was found\r
+ // now let us put things together intermediary 2 and 1\r
+ $converted = (double)(((($value - $i1_value['offset']) / $i1_value['ratio']) + $i2_value['offset']) * $i2_value['ratio']);\r
+\r
+ $found = true; \r
+ } \r
+ }\r
+ return $found; \r
+ }\r
+\r
+ } // end func getConvSpecs\r
+ \r
+} // end class UnitConvertor\r
+?>
\ No newline at end of file
--- /dev/null
+<?php\r
+\r
+\r
+function convert_install() {\r
+ register_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
+}\r
+\r
+function convert_uninstall() {\r
+ unregister_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
+}\r
+\r
+function convert_app_menu($a,&$b) {\r
+ $b['app_menu'] .= '<div class="app-title"><a href="convert">Units Conversion</a></div>'; \r
+}\r
+\r
+\r
+function convert_module() {}\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+function convert_content($app) {\r
+\r
+include("UnitConvertor.php");\r
+ \r
+ class TP_Converter extends UnitConvertor {\r
+ function TP_Converter($lang = "en")\r
+ {\r
+ if ($lang != 'en' ) {\r
+ $dec_point = '.'; $thousand_sep = "'";\r
+ } else {\r
+ $dec_point = '.'; $thousand_sep = ",";\r
+ }\r
+ \r
+ $this->UnitConvertor($dec_point , $thousand_sep );\r
+\r
+ } // end func UnitConvertor\r
+\r
+ function find_base_unit($from,$to) {\r
+ while (list($skey,$sval) = each($this->bases)) {\r
+ if ($skey == $from || $to == $skey || in_array($to,$sval) || in_array($from,$sval)) {\r
+ return $skey; \r
+ }\r
+ }\r
+ return false; \r
+ }\r
+\r
+ function getTable($value, $from_unit, $to_unit, $precision) {\r
+ \r
+ if ($base_unit = $this->find_base_unit($from_unit,$to_unit)) {\r
+ \r
+ // A baseunit was found now lets convert from -> $base_unit \r
+ \r
+ $cell ['value'] = $this->convert($value, $from_unit, $base_unit, $precision)." ".$base_unit; \r
+ $cell ['class'] = ($base_unit == $from_unit || $base_unit == $to_unit) ? "framedred": "";\r
+ $cells[] = $cell;\r
+ // We now have the base unit and value now lets produce the table;\r
+ while (list($key,$val) = each($this->bases[$base_unit])) {\r
+ $cell ['value'] = $this->convert($value, $from_unit, $val, $precision)." ".$val; \r
+ $cell ['class'] = ($val == $from_unit || $val == $to_unit) ? "framedred": "";\r
+ $cells[] = $cell;\r
+ }\r
+\r
+ $cc = count($cells);\r
+ $string = "<table class=\"framed grayish\" border=\"1\" cellpadding=\"5\" width=\"80%\" align=\"center\"><tr>";\r
+ $string .= "<td rowspan=\"$cc\" align=\"center\">$value $from_unit</td>";\r
+ $i=0;\r
+ foreach ($cells as $cell) {\r
+ if ($i==0) {\r
+ $string .= "<td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
+ $i++;\r
+ } else {\r
+ $string .= "</tr><tr><td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
+ }\r
+ }\r
+ $string .= "</tr></table>";\r
+ return $string;\r
+ } \r
+ \r
+ }\r
+}\r
+\r
+\r
+$conv = new TP_Converter('en');\r
+\r
+\r
+$conversions = array(\r
+ 'Temperature'=>array('base' =>'Celsius',\r
+ 'conv'=>array(\r
+ 'Fahrenheit'=>array('ratio'=>1.8, 'offset'=>32),\r
+ 'Kelvin'=>array('ratio'=>1, 'offset'=>273),\r
+ 'Reaumur'=>0.8\r
+ )\r
+ ),\r
+ 'Weight' => array('base' =>'kg',\r
+ 'conv'=>array(\r
+ 'g'=>1000,\r
+ 'mg'=>1000000,\r
+ 't'=>0.001,\r
+ 'grain'=>15432,\r
+ 'oz'=>35.274,\r
+ 'lb'=>2.2046,\r
+ 'cwt(UK)' => 0.019684,\r
+ 'cwt(US)' => 0.022046, \r
+ 'ton (US)' => 0.0011023,\r
+ 'ton (UK)' => 0.0009842\r
+ )\r
+ ),\r
+ 'Distance' => array('base' =>'km',\r
+ 'conv'=>array(\r
+ 'm'=>1000,\r
+ 'dm'=>10000,\r
+ 'cm'=>100000,\r
+ 'mm'=>1000000,\r
+ 'mile'=>0.62137,\r
+ 'naut.mile'=>0.53996,\r
+ 'inch(es)'=>39370,\r
+ 'ft'=>3280.8,\r
+ 'yd'=>1093.6,\r
+ 'furlong'=>4.970969537898672,\r
+ 'fathom'=>546.8066491688539\r
+ )\r
+ ),\r
+ 'Area' => array('base' =>'km 2',\r
+ 'conv'=>array( \r
+ 'ha'=>100,\r
+ 'acre'=>247.105,\r
+ 'm 2'=>pow(1000,2),\r
+ 'dm 2'=>pow(10000,2),\r
+ 'cm 2'=>pow(100000,2),\r
+ 'mm 2'=>pow(1000000,2), \r
+ 'mile 2'=>pow(0.62137,2),\r
+ 'naut.miles 2'=>pow(0.53996,2),\r
+ 'in 2'=>pow(39370,2),\r
+ 'ft 2'=>pow(3280.8,2),\r
+ 'yd 2'=>pow(1093.6,2),\r
+ )\r
+ ),\r
+ 'Volume' => array('base' =>'m 3',\r
+ 'conv'=>array(\r
+ 'in 3'=>61023.6,\r
+ 'ft 3'=>35.315,\r
+ 'cm 3'=>pow(10,6),\r
+ 'dm 3'=>1000,\r
+ 'litre'=>1000,\r
+ 'hl'=>10,\r
+ 'yd 3'=>1.30795,\r
+ 'gal(US)'=>264.172,\r
+ 'gal(UK)'=>219.969,\r
+ 'pint' => 2113.376,\r
+ 'quart' => 1056.688,\r
+ 'cup' => 4266.753,\r
+ 'fl oz' => 33814.02,\r
+ 'tablespoon' => 67628.04,\r
+ 'teaspoon' => 202884.1,\r
+ 'pt (UK)'=>1000/0.56826, \r
+ 'barrel petroleum'=>1000/158.99,\r
+ 'Register Tons'=>2.832, \r
+ 'Ocean Tons'=>1.1327\r
+ )\r
+ ),\r
+ 'Speed' =>array('base' =>'kmph',\r
+ 'conv'=>array(\r
+ 'mps'=>0.0001726031,\r
+ 'milesph'=>0.62137,\r
+ 'knots'=>0.53996,\r
+ 'mach STP'=>0.0008380431,\r
+ 'c (warp)'=>9.265669e-10\r
+ )\r
+ )\r
+);\r
+\r
+\r
+while (list($key,$val) = each($conversions)) {\r
+ $conv->addConversion($val['base'], $val['conv']);\r
+ $list[$key][] = $val['base'];\r
+ while (list($ukey,$uval) = each($val['conv'])) {\r
+ $list[$key][] = $ukey;\r
+ }\r
+}\r
+\r
+ $o .= '<h3>Unit Conversions</h3>';\r
+\r
+\r
+ if (isset($_POST['from_unit']) && isset($_POST['value'])) {\r
+ $_POST['value'] = $_POST['value'] + 0;\r
+\r
+\r
+ $o .= ($conv->getTable($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5))."</p>";\r
+ } else {\r
+ $o .= "<p>Select:</p>";\r
+ }\r
+\r
+ if(isset($_POST['value']))\r
+ $value = $_POST['value'];\r
+ else\r
+ $value = '';\r
+\r
+ $o .= '<form action="convert" method="post" name="conversion">';\r
+ $o .= '<input name="value" type="text" id="value" value="' . $value . '" size="10" maxlength="10" />';\r
+ $o .= '<select name="from_unit" size="12">';\r
+\r
+\r
+\r
+ reset($list);\r
+ while(list($key,$val) = each($list)) {\r
+ $o .= "\n\t<optgroup label=\"$key\">";\r
+ while(list($ukey,$uval) = each($val)) {\r
+ $selected = (($uval == $_POST['from_unit']) ? ' selected="selected" ' : '');\r
+ $o .= "\n\t\t<option value=\"$uval\" $selected >$uval</option>";\r
+ }\r
+ $o .= "\n\t</optgroup>";\r
+ }\r
+\r
+ $o .= '</select>';\r
+\r
+ $o .= '<input type="submit" name="Submit" value="Submit" /></form>';\r
+ \r
+ return $o;\r
+}\r