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