]> git.mxchange.org Git - friendica-addons.git/blob - convert/convert.php
751316503366b944c8498c4dbc532863fa8c400c
[friendica-addons.git] / convert / convert.php
1 <?php\r
2 /**\r
3  * Name: Converter App\r
4  * Description: Unit converter application\r
5  * Version: 1.0\r
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>\r
7  */\r
8 \r
9 function convert_install() {\r
10         register_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
11 }\r
12 \r
13 function convert_uninstall() {\r
14         unregister_hook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
15 }\r
16 \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
19 }\r
20 \r
21 \r
22 function convert_module() {}\r
23 \r
24 \r
25 \r
26 \r
27 \r
28 \r
29 \r
30 function convert_content($app) {\r
31 \r
32 include("UnitConvertor.php");\r
33  \r
34  class TP_Converter extends UnitConvertor {\r
35         function TP_Converter($lang = "en")\r
36         {\r
37                 if ($lang != 'en' ) {\r
38                         $dec_point = '.'; $thousand_sep = "'";\r
39                 } else {\r
40                         $dec_point = '.'; $thousand_sep = ",";\r
41                 }\r
42                 \r
43                 $this->UnitConvertor($dec_point , $thousand_sep );\r
44 \r
45         } // end func UnitConvertor\r
46 \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
50                                         return $skey;                           \r
51                                 }\r
52                 }\r
53                 return false;           \r
54         }\r
55 \r
56         function getTable($value, $from_unit, $to_unit, $precision) {\r
57         \r
58                 if ($base_unit = $this->find_base_unit($from_unit,$to_unit)) {\r
59                 \r
60                         // A baseunit was found now lets convert from -> $base_unit \r
61                         \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
64                                 $cells[] = $cell;\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
69                                 $cells[] = $cell;\r
70                         }\r
71 \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
75                         $i=0;\r
76                         foreach ($cells as $cell) {\r
77                                 if ($i==0) {\r
78                                         $string .= "<td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
79                                         $i++;\r
80                                 } else {\r
81                                         $string .= "</tr><tr><td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
82                                 }\r
83                         }\r
84                         $string .= "</tr></table>";\r
85                         return $string;\r
86                 }               \r
87                 \r
88         }\r
89 }\r
90 \r
91 \r
92 $conv = new TP_Converter('en');\r
93 \r
94 \r
95 $conversions = array(\r
96         'Temperature'=>array('base' =>'Celsius',\r
97                 'conv'=>array(\r
98                         'Fahrenheit'=>array('ratio'=>1.8, 'offset'=>32),\r
99                         'Kelvin'=>array('ratio'=>1, 'offset'=>273),\r
100                         'Reaumur'=>0.8\r
101                 )\r
102         ),\r
103         'Weight' => array('base' =>'kg',\r
104                 'conv'=>array(\r
105                         'g'=>1000,\r
106                         'mg'=>1000000,\r
107                         't'=>0.001,\r
108                         'grain'=>15432,\r
109                         'oz'=>35.274,\r
110                         'lb'=>2.2046,\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
115                 )\r
116         ),\r
117         'Distance' => array('base' =>'km',\r
118                 'conv'=>array(\r
119                         'm'=>1000,\r
120                         'dm'=>10000,\r
121                         'cm'=>100000,\r
122                         'mm'=>1000000,\r
123                         'mile'=>0.62137,\r
124                         'naut.mile'=>0.53996,\r
125                         'inch(es)'=>39370,\r
126                         'ft'=>3280.8,\r
127                         'yd'=>1093.6,\r
128                         'furlong'=>4.970969537898672,\r
129                         'fathom'=>546.8066491688539\r
130                 )\r
131         ),\r
132         'Area' => array('base' =>'km 2',\r
133                 'conv'=>array(  \r
134                         'ha'=>100,\r
135                         'acre'=>247.105,\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
145                 )\r
146         ),\r
147         'Volume' => array('base' =>'m 3',\r
148                 'conv'=>array(\r
149                         'in 3'=>61023.6,\r
150                         'ft 3'=>35.315,\r
151                         'cm 3'=>pow(10,6),\r
152                         'dm 3'=>1000,\r
153                         'litre'=>1000,\r
154                         'hl'=>10,\r
155                         'yd 3'=>1.30795,\r
156                         'gal(US)'=>264.172,\r
157                         'gal(UK)'=>219.969,\r
158                         'pint' => 2113.376,\r
159                         'quart' => 1056.688,\r
160                         'cup' => 4266.753,\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
168                 )\r
169         ),\r
170         'Speed' =>array('base' =>'kmph',\r
171                 'conv'=>array(\r
172                         'mps'=>0.0001726031,\r
173                         'milesph'=>0.62137,\r
174                         'knots'=>0.53996,\r
175                         'mach STP'=>0.0008380431,\r
176                         'c (warp)'=>9.265669e-10\r
177                 )\r
178         )\r
179 );\r
180 \r
181 \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
187         }\r
188 }\r
189 \r
190   $o .= '<h3>Unit Conversions</h3>';\r
191 \r
192 \r
193         if (isset($_POST['from_unit']) && isset($_POST['value'])) {\r
194         $_POST['value'] = $_POST['value'] + 0;\r
195 \r
196 \r
197                 $o .= ($conv->getTable($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5))."</p>";\r
198         } else {\r
199                 $o .= "<p>Select:</p>";\r
200         }\r
201 \r
202         if(isset($_POST['value']))\r
203                 $value = $_POST['value'];\r
204         else\r
205                 $value = '';\r
206 \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
210 \r
211 \r
212 \r
213         reset($list);\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
219                 }\r
220                 $o .= "\n\t</optgroup>";\r
221         }\r
222 \r
223         $o .= '</select>';\r
224 \r
225     $o .= '<input type="submit" name="Submit" value="Submit" /></form>';\r
226   \r
227         return $o;\r
228 }\r