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