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