3 * Name: Converter App
\r
4 * Description: Unit converter application
\r
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
\r
9 function convert_install() {
\r
10 register_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');
\r
13 function convert_uninstall() {
\r
14 unregister_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');
\r
17 function convert_app_menu($a,&$b) {
\r
18 $b['app_menu'][] = '<div class="app-title"><a href="convert">Units Conversion</a></div>';
\r
22 function convert_module() {}
\r
30 function convert_content($app) {
\r
32 include("UnitConvertor.php");
\r
34 class TP_Converter extends UnitConvertor {
\r
35 function TP_Converter($lang = "en")
\r
37 if ($lang != 'en' ) {
\r
38 $dec_point = '.'; $thousand_sep = "'";
\r
40 $dec_point = '.'; $thousand_sep = ",";
\r
43 $this->UnitConvertor($dec_point , $thousand_sep );
\r
45 } // end func UnitConvertor
\r
47 function find_base_unit($from,$to) {
\r
48 while (list($skey,$sval) = each($this->bases)) {
\r
49 if ($skey == $from || $to == $skey || in_array($to,$sval) || in_array($from,$sval)) {
\r
56 function getTable($value, $from_unit, $to_unit, $precision) {
\r
58 if ($base_unit = $this->find_base_unit($from_unit,$to_unit)) {
\r
60 // A baseunit was found now lets convert from -> $base_unit
\r
62 $cell ['value'] = $this->convert($value, $from_unit, $base_unit, $precision)." ".$base_unit;
\r
63 $cell ['class'] = ($base_unit == $from_unit || $base_unit == $to_unit) ? "framedred": "";
\r
65 // We now have the base unit and value now lets produce the table;
\r
66 while (list($key,$val) = each($this->bases[$base_unit])) {
\r
67 $cell ['value'] = $this->convert($value, $from_unit, $val, $precision)." ".$val;
\r
68 $cell ['class'] = ($val == $from_unit || $val == $to_unit) ? "framedred": "";
\r
72 $cc = count($cells);
\r
73 $string = "<table class=\"framed grayish\" border=\"1\" cellpadding=\"5\" width=\"80%\" align=\"center\"><tr>";
\r
74 $string .= "<td rowspan=\"$cc\" align=\"center\">$value $from_unit</td>";
\r
76 foreach ($cells as $cell) {
\r
78 $string .= "<td class=\"".$cell['class']."\">".$cell['value']."</td>";
\r
81 $string .= "</tr><tr><td class=\"".$cell['class']."\">".$cell['value']."</td>";
\r
84 $string .= "</tr></table>";
\r
92 $conv = new TP_Converter('en');
\r
95 $conversions = array(
\r
96 'Temperature'=>array('base' =>'Celsius',
\r
98 'Fahrenheit'=>array('ratio'=>1.8, 'offset'=>32),
\r
99 'Kelvin'=>array('ratio'=>1, 'offset'=>273),
\r
103 'Weight' => array('base' =>'kg',
\r
111 'cwt(UK)' => 0.019684,
\r
112 'cwt(US)' => 0.022046,
\r
113 'ton (US)' => 0.0011023,
\r
114 'ton (UK)' => 0.0009842
\r
117 'Distance' => array('base' =>'km',
\r
124 'naut.mile'=>0.53996,
\r
128 'furlong'=>4.970969537898672,
\r
129 'fathom'=>546.8066491688539
\r
132 'Area' => array('base' =>'km 2',
\r
136 'm 2'=>pow(1000,2),
\r
137 'dm 2'=>pow(10000,2),
\r
138 'cm 2'=>pow(100000,2),
\r
139 'mm 2'=>pow(1000000,2),
\r
140 'mile 2'=>pow(0.62137,2),
\r
141 'naut.miles 2'=>pow(0.53996,2),
\r
142 'in 2'=>pow(39370,2),
\r
143 'ft 2'=>pow(3280.8,2),
\r
144 'yd 2'=>pow(1093.6,2),
\r
147 'Volume' => array('base' =>'m 3',
\r
156 'gal(US)'=>264.172,
\r
157 'gal(UK)'=>219.969,
\r
158 'pint' => 2113.376,
\r
159 'quart' => 1056.688,
\r
161 'fl oz' => 33814.02,
\r
162 'tablespoon' => 67628.04,
\r
163 'teaspoon' => 202884.1,
\r
164 'pt (UK)'=>1000/0.56826,
\r
165 'barrel petroleum'=>1000/158.99,
\r
166 'Register Tons'=>2.832,
\r
167 'Ocean Tons'=>1.1327
\r
170 'Speed' =>array('base' =>'kmph',
\r
172 'mps'=>0.0001726031,
\r
173 'milesph'=>0.62137,
\r
175 'mach STP'=>0.0008380431,
\r
176 'c (warp)'=>9.265669e-10
\r
182 while (list($key,$val) = each($conversions)) {
\r
183 $conv->addConversion($val['base'], $val['conv']);
\r
184 $list[$key][] = $val['base'];
\r
185 while (list($ukey,$uval) = each($val['conv'])) {
\r
186 $list[$key][] = $ukey;
\r
190 $o .= '<h3>Unit Conversions</h3>';
\r
193 if (isset($_POST['from_unit']) && isset($_POST['value'])) {
\r
194 $_POST['value'] = $_POST['value'] + 0;
\r
197 $o .= ($conv->getTable($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5))."</p>";
\r
199 $o .= "<p>Select:</p>";
\r
202 if(isset($_POST['value']))
\r
203 $value = $_POST['value'];
\r
207 $o .= '<form action="convert" method="post" name="conversion">';
\r
208 $o .= '<input name="value" type="text" id="value" value="' . $value . '" size="10" maxlength="10" />';
\r
209 $o .= '<select name="from_unit" size="12">';
\r
214 while(list($key,$val) = each($list)) {
\r
215 $o .= "\n\t<optgroup label=\"$key\">";
\r
216 while(list($ukey,$uval) = each($val)) {
\r
217 $selected = (($uval == $_POST['from_unit']) ? ' selected="selected" ' : '');
\r
218 $o .= "\n\t\t<option value=\"$uval\" $selected >$uval</option>";
\r
220 $o .= "\n\t</optgroup>";
\r
225 $o .= '<input type="submit" name="Submit" value="Submit" /></form>';
\r