3 * Name: Converter App
\r
4 * Description: Unit converter application
\r
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
\r
8 use Friendica\Core\Addon;
\r
10 function convert_install() {
\r
11 Addon::registerHook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');
\r
14 function convert_uninstall() {
\r
15 Addon::unregisterHook('app_menu', 'addon/convert/convert.php', 'convert_app_menu');
\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
23 function convert_module() {}
\r
31 function convert_content($app) {
\r
33 include("UnitConvertor.php");
\r
35 class TP_Converter extends UnitConvertor {
\r
36 function TP_Converter($lang = "en")
\r
38 if ($lang != 'en' ) {
\r
39 $dec_point = '.'; $thousand_sep = "'";
\r
41 $dec_point = '.'; $thousand_sep = ",";
\r
44 $this->UnitConvertor($dec_point , $thousand_sep );
\r
46 } // end func UnitConvertor
\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
57 function getTable($value, $from_unit, $to_unit, $precision) {
\r
59 if ($base_unit = $this->find_base_unit($from_unit,$to_unit)) {
\r
61 // A baseunit was found now lets convert from -> $base_unit
\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
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
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
77 foreach ($cells as $cell) {
\r
79 $string .= "<td class=\"".$cell['class']."\">".$cell['value']."</td>";
\r
82 $string .= "</tr><tr><td class=\"".$cell['class']."\">".$cell['value']."</td>";
\r
85 $string .= "</tr></table>";
\r
93 $conv = new TP_Converter('en');
\r
97 'Temperature'=>['base' =>'Celsius',
\r
99 'Fahrenheit'=>['ratio'=>1.8, 'offset'=>32],
\r
100 'Kelvin'=>['ratio'=>1, 'offset'=>273],
\r
104 'Weight' => ['base' =>'kg',
\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
118 'Distance' => ['base' =>'km',
\r
125 'naut.mile'=>0.53996,
\r
129 'furlong'=>4.970969537898672,
\r
130 'fathom'=>546.8066491688539
\r
133 'Area' => ['base' =>'km 2',
\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
148 'Volume' => ['base' =>'m 3',
\r
157 'gal(US)'=>264.172,
\r
158 'gal(UK)'=>219.969,
\r
159 'pint' => 2113.376,
\r
160 'quart' => 1056.688,
\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
171 'Speed' =>['base' =>'kmph',
\r
173 'mps'=>0.0001726031,
\r
174 'milesph'=>0.62137,
\r
176 'mach STP'=>0.0008380431,
\r
177 'c (warp)'=>9.265669e-10
\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
191 $o .= '<h3>Unit Conversions</h3>';
\r
194 if (isset($_POST['from_unit']) && isset($_POST['value'])) {
\r
195 $_POST['value'] = $_POST['value'] + 0;
\r
198 $o .= ($conv->getTable($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5))."</p>";
\r
200 $o .= "<p>Select:</p>";
\r
203 if(isset($_POST['value']))
\r
204 $value = $_POST['value'];
\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
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
221 $o .= "\n\t</optgroup>";
\r
226 $o .= '<input type="submit" name="Submit" value="Submit" /></form>';
\r