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