]> git.mxchange.org Git - friendica.git/blob - addon/convert/convert.php
units conversion app - needs some styling and code cleanup
[friendica.git] / addon / convert / convert.php
1 <?php\r
2 \r
3 \r
4 function convert_install() {\r
5         register_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
6 }\r
7 \r
8 function convert_uninstall() {\r
9         unregister_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
10 }\r
11 \r
12 function convert_app_menu($a,&$b) {\r
13         $b['app_menu'] .= '<div class="app-title"><a href="convert">Units Conversion</a></div>'; \r
14 }\r
15 \r
16 \r
17 function convert_module() {}\r
18 \r
19 \r
20 \r
21 \r
22 \r
23 \r
24 \r
25 function convert_content($app) {\r
26 \r
27 include("UnitConvertor.php");\r
28  \r
29  class TP_Converter extends UnitConvertor {\r
30         function TP_Converter($lang = "en")\r
31         {\r
32                 if ($lang != 'en' ) {\r
33                         $dec_point = '.'; $thousand_sep = "'";\r
34                 } else {\r
35                         $dec_point = '.'; $thousand_sep = ",";\r
36                 }\r
37                 \r
38                 $this->UnitConvertor($dec_point , $thousand_sep );\r
39 \r
40         } // end func UnitConvertor\r
41 \r
42         function find_base_unit($from,$to) {\r
43                 while (list($skey,$sval) = each($this->bases)) {\r
44                                 if ($skey == $from || $to == $skey || in_array($to,$sval) || in_array($from,$sval)) {\r
45                                         return $skey;                           \r
46                                 }\r
47                 }\r
48                 return false;           \r
49         }\r
50 \r
51         function getTable($value, $from_unit, $to_unit, $precision) {\r
52         \r
53                 if ($base_unit = $this->find_base_unit($from_unit,$to_unit)) {\r
54                 \r
55                         // A baseunit was found now lets convert from -> $base_unit \r
56                         \r
57                                 $cell ['value'] = $this->convert($value, $from_unit, $base_unit, $precision)." ".$base_unit;    \r
58                                 $cell ['class'] = ($base_unit == $from_unit || $base_unit == $to_unit) ? "framedred": "";\r
59                                 $cells[] = $cell;\r
60                         // We now have the base unit and value now lets produce the table;\r
61                         while (list($key,$val) = each($this->bases[$base_unit])) {\r
62                                 $cell ['value'] = $this->convert($value, $from_unit, $val, $precision)." ".$val;        \r
63                                 $cell ['class'] = ($val == $from_unit || $val == $to_unit) ? "framedred": "";\r
64                                 $cells[] = $cell;\r
65                         }\r
66 \r
67                         $cc = count($cells);\r
68                         $string = "<table class=\"framed grayish\" border=\"1\" cellpadding=\"5\" width=\"80%\" align=\"center\"><tr>";\r
69                         $string .= "<td rowspan=\"$cc\" align=\"center\">$value $from_unit</td>";\r
70                         $i=0;\r
71                         foreach ($cells as $cell) {\r
72                                 if ($i==0) {\r
73                                         $string .= "<td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
74                                         $i++;\r
75                                 } else {\r
76                                         $string .= "</tr><tr><td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
77                                 }\r
78                         }\r
79                         $string .= "</tr></table>";\r
80                         return $string;\r
81                 }               \r
82                 \r
83         }\r
84 }\r
85 \r
86 \r
87 $conv = new TP_Converter('en');\r
88 \r
89 \r
90 $conversions = array(\r
91         'Temperature'=>array('base' =>'Celsius',\r
92                 'conv'=>array(\r
93                         'Fahrenheit'=>array('ratio'=>1.8, 'offset'=>32),\r
94                         'Kelvin'=>array('ratio'=>1, 'offset'=>273),\r
95                         'Reaumur'=>0.8\r
96                 )\r
97         ),\r
98         'Weight' => array('base' =>'kg',\r
99                 'conv'=>array(\r
100                         'g'=>1000,\r
101                         'mg'=>1000000,\r
102                         't'=>0.001,\r
103                         'grain'=>15432,\r
104                         'oz'=>35.274,\r
105                         'lb'=>2.2046,\r
106                         'cwt(UK)'       => 0.019684,\r
107                         'cwt(US)'       => 0.022046, \r
108                         'ton (US)'      => 0.0011023,\r
109                         'ton (UK)'      => 0.0009842\r
110                 )\r
111         ),\r
112         'Distance' => array('base' =>'km',\r
113                 'conv'=>array(\r
114                         'm'=>1000,\r
115                         'dm'=>10000,\r
116                         'cm'=>100000,\r
117                         'mm'=>1000000,\r
118                         'mile'=>0.62137,\r
119                         'naut.mile'=>0.53996,\r
120                         'inch(es)'=>39370,\r
121                         'ft'=>3280.8,\r
122                         'yd'=>1093.6,\r
123                         'furlong'=>4.970969537898672,\r
124                         'fathom'=>546.8066491688539\r
125                 )\r
126         ),\r
127         'Area' => array('base' =>'km 2',\r
128                 'conv'=>array(  \r
129                         'ha'=>100,\r
130                         'acre'=>247.105,\r
131                         'm 2'=>pow(1000,2),\r
132                         'dm 2'=>pow(10000,2),\r
133                         'cm 2'=>pow(100000,2),\r
134                         'mm 2'=>pow(1000000,2), \r
135                         'mile 2'=>pow(0.62137,2),\r
136                         'naut.miles 2'=>pow(0.53996,2),\r
137                         'in 2'=>pow(39370,2),\r
138                         'ft 2'=>pow(3280.8,2),\r
139                         'yd 2'=>pow(1093.6,2),\r
140                 )\r
141         ),\r
142         'Volume' => array('base' =>'m 3',\r
143                 'conv'=>array(\r
144                         'in 3'=>61023.6,\r
145                         'ft 3'=>35.315,\r
146                         'cm 3'=>pow(10,6),\r
147                         'dm 3'=>1000,\r
148                         'litre'=>1000,\r
149                         'hl'=>10,\r
150                         'yd 3'=>1.30795,\r
151                         'gal(US)'=>264.172,\r
152                         'gal(UK)'=>219.969,\r
153                         'pint' => 2113.376,\r
154                         'quart' => 1056.688,\r
155                         'cup' => 4266.753,\r
156                         'fl oz' => 33814.02,\r
157                         'tablespoon' => 67628.04,\r
158                         'teaspoon' => 202884.1,\r
159                         'pt (UK)'=>1000/0.56826, \r
160                         'barrel petroleum'=>1000/158.99,\r
161                         'Register Tons'=>2.832, \r
162                         'Ocean Tons'=>1.1327\r
163                 )\r
164         ),\r
165         'Speed' =>array('base' =>'kmph',\r
166                 'conv'=>array(\r
167                         'mps'=>0.0001726031,\r
168                         'milesph'=>0.62137,\r
169                         'knots'=>0.53996,\r
170                         'mach STP'=>0.0008380431,\r
171                         'c (warp)'=>9.265669e-10\r
172                 )\r
173         )\r
174 );\r
175 \r
176 \r
177 while (list($key,$val) = each($conversions)) {\r
178         $conv->addConversion($val['base'], $val['conv']);\r
179         $list[$key][] = $val['base'];\r
180         while (list($ukey,$uval) = each($val['conv'])) {\r
181                 $list[$key][] = $ukey;\r
182         }\r
183 }\r
184 \r
185   $o .= '<h3>Unit Conversions</h3>';\r
186 \r
187 \r
188         if (isset($_POST['from_unit']) && isset($_POST['value'])) {\r
189         $_POST['value'] = $_POST['value'] + 0;\r
190 \r
191 \r
192                 $o .= ($conv->getTable($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5))."</p>";\r
193         } else {\r
194                 $o .= "<p>Select:</p>";\r
195         }\r
196 \r
197         if(isset($_POST['value']))\r
198                 $value = $_POST['value'];\r
199         else\r
200                 $value = '';\r
201 \r
202         $o .= '<form action="convert" method="post" name="conversion">';\r
203     $o .= '<input name="value" type="text" id="value" value="' . $value . '" size="10" maxlength="10" />';\r
204     $o .= '<select name="from_unit" size="12">';\r
205 \r
206 \r
207 \r
208         reset($list);\r
209         while(list($key,$val) = each($list)) {\r
210                 $o .=  "\n\t<optgroup label=\"$key\">";\r
211                 while(list($ukey,$uval) = each($val)) {\r
212                         $selected = (($uval == $_POST['from_unit']) ? ' selected="selected" ' : '');\r
213                         $o .=  "\n\t\t<option value=\"$uval\" $selected >$uval</option>";\r
214                 }\r
215                 $o .= "\n\t</optgroup>";\r
216         }\r
217 \r
218         $o .= '</select>';\r
219 \r
220     $o .= '<input type="submit" name="Submit" value="Submit" /></form>';\r
221   \r
222         return $o;\r
223 }\r