]> git.mxchange.org Git - friendica-addons.git/blobdiff - convert/convert.php
invidious/invidious.php aktualisiert
[friendica-addons.git] / convert / convert.php
index e95637418454c552e89343f96cf3d03646d4cdda..57ce6ea83012420939c0d8734e4f753a5cc94afe 100644 (file)
-<?php\r
-/**\r
- * Name: Converter App\r
- * Description: Unit converter application\r
- * Version: 1.0\r
- * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>\r
- */\r
-use Friendica\Core\Hook;\r
-\r
-function convert_install() {\r
-       Hook::register('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
-}\r
-\r
-function convert_uninstall() {\r
-       Hook::unregister('app_menu', 'addon/convert/convert.php', 'convert_app_menu');\r
-}\r
-\r
-function convert_app_menu($a,&$b) {\r
-       $b['app_menu'][] = '<div class="app-title"><a href="convert">Units Conversion</a></div>';\r
-}\r
-\r
-\r
-function convert_module() {}\r
-\r
-\r
-\r
-\r
-\r
-\r
-\r
-function convert_content($app) {\r
-\r
-include("UnitConvertor.php");\r
-\r
- class TP_Converter extends UnitConvertor {\r
-       function TP_Converter($lang = "en")\r
-       {\r
-               if ($lang != 'en' ) {\r
-                       $dec_point = '.'; $thousand_sep = "'";\r
-               } else {\r
-                       $dec_point = '.'; $thousand_sep = ",";\r
-               }\r
-\r
-               $this->UnitConvertor($dec_point , $thousand_sep );\r
-\r
-       } // end func UnitConvertor\r
-\r
-       function find_base_unit($from,$to) {\r
-               while (list($skey,$sval) = each($this->bases)) {\r
-                               if ($skey == $from || $to == $skey || in_array($to,$sval) || in_array($from,$sval)) {\r
-                                       return $skey;\r
-                               }\r
-               }\r
-               return false;\r
-       }\r
-\r
-       function getTable($value, $from_unit, $to_unit, $precision) {\r
-\r
-               if ($base_unit = $this->find_base_unit($from_unit,$to_unit)) {\r
-\r
-                       // A baseunit was found now lets convert from -> $base_unit\r
-\r
-                               $cell ['value'] = $this->convert($value, $from_unit, $base_unit, $precision)." ".$base_unit;\r
-                               $cell ['class'] = ($base_unit == $from_unit || $base_unit == $to_unit) ? "framedred": "";\r
-                               $cells[] = $cell;\r
-                       // We now have the base unit and value now lets produce the table;\r
-                       while (list($key,$val) = each($this->bases[$base_unit])) {\r
-                               $cell ['value'] = $this->convert($value, $from_unit, $val, $precision)." ".$val;\r
-                               $cell ['class'] = ($val == $from_unit || $val == $to_unit) ? "framedred": "";\r
-                               $cells[] = $cell;\r
-                       }\r
-\r
-                       $cc = count($cells);\r
-                       $string = "<table class=\"framed grayish\" border=\"1\" cellpadding=\"5\" width=\"80%\" align=\"center\"><tr>";\r
-                       $string .= "<td rowspan=\"$cc\" align=\"center\">$value $from_unit</td>";\r
-                       $i=0;\r
-                       foreach ($cells as $cell) {\r
-                               if ($i==0) {\r
-                                       $string .= "<td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
-                                       $i++;\r
-                               } else {\r
-                                       $string .= "</tr><tr><td class=\"".$cell['class']."\">".$cell['value']."</td>";\r
-                               }\r
-                       }\r
-                       $string .= "</tr></table>";\r
-                       return $string;\r
-               }\r
-\r
-       }\r
-}\r
-\r
-\r
-$conv = new TP_Converter('en');\r
-\r
-\r
-$conversions = [\r
-       'Temperature'=>['base' =>'Celsius',\r
-               'conv'=>[\r
-                       'Fahrenheit'=>['ratio'=>1.8, 'offset'=>32],\r
-                       'Kelvin'=>['ratio'=>1, 'offset'=>273],\r
-                       'Reaumur'=>0.8\r
-               ]\r
-       ],\r
-       'Weight' => ['base' =>'kg',\r
-               'conv'=>[\r
-                       'g'=>1000,\r
-                       'mg'=>1000000,\r
-                       't'=>0.001,\r
-                       'grain'=>15432,\r
-                       'oz'=>35.274,\r
-                       'lb'=>2.2046,\r
-                       'cwt(UK)'       => 0.019684,\r
-                       'cwt(US)'       => 0.022046,\r
-                       'ton (US)'      => 0.0011023,\r
-                       'ton (UK)'      => 0.0009842\r
-               ]\r
-       ],\r
-       'Distance' => ['base' =>'km',\r
-               'conv'=>[\r
-                       'm'=>1000,\r
-                       'dm'=>10000,\r
-                       'cm'=>100000,\r
-                       'mm'=>1000000,\r
-                       'mile'=>0.62137,\r
-                       'naut.mile'=>0.53996,\r
-                       'inch(es)'=>39370,\r
-                       'ft'=>3280.8,\r
-                       'yd'=>1093.6,\r
-                       'furlong'=>4.970969537898672,\r
-                       'fathom'=>546.8066491688539\r
-               ]\r
-       ],\r
-       'Area' => ['base' =>'km 2',\r
-               'conv'=>[\r
-                       'ha'=>100,\r
-                       'acre'=>247.105,\r
-                       'm 2'=>pow(1000,2),\r
-                       'dm 2'=>pow(10000,2),\r
-                       'cm 2'=>pow(100000,2),\r
-                       'mm 2'=>pow(1000000,2),\r
-                       'mile 2'=>pow(0.62137,2),\r
-                       'naut.miles 2'=>pow(0.53996,2),\r
-                       'in 2'=>pow(39370,2),\r
-                       'ft 2'=>pow(3280.8,2),\r
-                       'yd 2'=>pow(1093.6,2),\r
-               ]\r
-       ],\r
-       'Volume' => ['base' =>'m 3',\r
-               'conv'=>[\r
-                       'in 3'=>61023.6,\r
-                       'ft 3'=>35.315,\r
-                       'cm 3'=>pow(10,6),\r
-                       'dm 3'=>1000,\r
-                       'litre'=>1000,\r
-                       'hl'=>10,\r
-                       'yd 3'=>1.30795,\r
-                       'gal(US)'=>264.172,\r
-                       'gal(UK)'=>219.969,\r
-                       'pint' => 2113.376,\r
-                       'quart' => 1056.688,\r
-                       'cup' => 4266.753,\r
-                       'fl oz' => 33814.02,\r
-                       'tablespoon' => 67628.04,\r
-                       'teaspoon' => 202884.1,\r
-                       'pt (UK)'=>1000/0.56826,\r
-                       'barrel petroleum'=>1000/158.99,\r
-                       'Register Tons'=>2.832,\r
-                       'Ocean Tons'=>1.1327\r
-               ]\r
-       ],\r
-       'Speed' =>['base' =>'kmph',\r
-               'conv'=>[\r
-                       'mps'=>0.0001726031,\r
-                       'milesph'=>0.62137,\r
-                       'knots'=>0.53996,\r
-                       'mach STP'=>0.0008380431,\r
-                       'c (warp)'=>9.265669e-10\r
-               ]\r
-       ]\r
-];\r
-\r
-\r
-while (list($key,$val) = each($conversions)) {\r
-       $conv->addConversion($val['base'], $val['conv']);\r
-       $list[$key][] = $val['base'];\r
-       while (list($ukey,$uval) = each($val['conv'])) {\r
-               $list[$key][] = $ukey;\r
-       }\r
-}\r
-\r
-  $o .= '<h3>Unit Conversions</h3>';\r
-\r
-\r
-       if (isset($_POST['from_unit']) && isset($_POST['value'])) {\r
-       $_POST['value'] = $_POST['value'] + 0;\r
-\r
-\r
-               $o .= ($conv->getTable($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5))."</p>";\r
-       } else {\r
-               $o .= "<p>Select:</p>";\r
-       }\r
-\r
-       if(isset($_POST['value']))\r
-               $value = $_POST['value'];\r
-       else\r
-               $value = '';\r
-\r
-       $o .= '<form action="convert" method="post" name="conversion">';\r
-    $o .= '<input name="value" type="text" id="value" value="' . $value . '" size="10" maxlength="10" />';\r
-    $o .= '<select name="from_unit" size="12">';\r
-\r
-\r
-\r
-       reset($list);\r
-       while(list($key,$val) = each($list)) {\r
-               $o .=  "\n\t<optgroup label=\"$key\">";\r
-               while(list($ukey,$uval) = each($val)) {\r
-                       $selected = (($uval == $_POST['from_unit']) ? ' selected="selected" ' : '');\r
-                       $o .=  "\n\t\t<option value=\"$uval\" $selected >$uval</option>";\r
-               }\r
-               $o .= "\n\t</optgroup>";\r
-       }\r
-\r
-       $o .= '</select>';\r
-\r
-    $o .= '<input type="submit" name="Submit" value="Submit" /></form>';\r
-\r
-       return $o;\r
-}\r
+<?php
+/**
+ * Name: Converter App
+ * Description: Unit converter application
+ * Version: 1.0
+ * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
+ */
+
+use Friendica\App;
+use Friendica\Core\Hook;
+
+function convert_install() {
+       Hook::register('app_menu', 'addon/convert/convert.php', 'convert_app_menu');
+}
+
+function convert_app_menu(array &$b)
+{
+       $b['app_menu'][] = '<div class="app-title"><a href="convert">Units Conversion</a></div>';
+}
+
+
+function convert_module() {}
+
+function convert_content() {
+       // @TODO UnitConverter uses a deprecated constructor with the class' name
+       // @TODO Let's one day rewrite this to a modern composer package
+       include 'UnitConvertor.php';
+
+       class TP_Converter extends UnitConvertor
+       {
+               public function __construct(string $lang = 'en')
+               {
+                       if ($lang == 'en' ) {
+                               $dec_point = '.';
+                               $thousand_sep = ',';
+                       } else {
+                               $dec_point = '.';
+                               $thousand_sep = "'";
+                       }
+
+                       parent::UnitConvertor($dec_point, $thousand_sep );
+               }
+
+               private function findBaseUnit($from, $to)
+               {
+                       while (list($skey, $sval) = each($this->bases)) {
+                               if ($skey == $from || $to == $skey || in_array($to, $sval) || in_array($from, $sval)) {
+                                       return $skey;
+                               }
+                       }
+
+                       return false;
+               }
+
+               public function getTable(int $value, $from_unit, $to_unit, $precision): string
+               {
+                       $string = '';
+
+                       if ($base_unit = $this->findBaseUnit($from_unit, $to_unit)) {
+                               // A baseunit was found now lets convert from -> $base_unit
+                               $cell ['value'] = $this->convert($value, $from_unit, $base_unit, $precision) . ' ' . $base_unit;
+                               $cell ['class'] = ($base_unit == $from_unit || $base_unit == $to_unit) ? 'framedred' : '';
+                               $cells[] = $cell;
+
+                               // We now have the base unit and value now lets produce the table;
+                               while (list($key, $val) = each($this->bases[$base_unit])) {
+                                       $cell ['value'] = $this->convert($value, $from_unit, $val, $precision) . ' ' . $val;
+                                       $cell ['class'] = ($val == $from_unit || $val == $to_unit) ? 'framedred' : '';
+                                       $cells[] = $cell;
+                               }
+
+                               $cc = count($cells);
+                               $string = "<table class=\"framed grayish\" border=\"1\" cellpadding=\"5\" width=\"80%\" align=\"center\"><tr>";
+                               $string .= "<td rowspan=\"$cc\" align=\"center\">$value $from_unit</td>";
+                               $i = 0;
+                               foreach ($cells as $cell) {
+                                       if ($i == 0) {
+                                               $string .= "<td class=\"" . $cell['class'] . "\">" . $cell['value'] . "</td>";
+                                               $i++;
+                                       } else {
+                                               $string .= "</tr><tr><td class=\"" . $cell['class'] . "\">" . $cell['value'] . "</td>";
+                                       }
+                               }
+                               $string .= "</tr></table>";
+                       }
+
+                       return $string;
+               }
+       }
+
+       $conv = new TP_Converter('en');
+
+       $conversions = [
+               'Temperature' => ['base'  => 'Celsius',
+                       'conv' => [
+                               'Fahrenheit' => ['ratio' => 1.8, 'offset' => 32],
+                               'Kelvin' => ['ratio' => 1, 'offset' => 273],
+                               'Reaumur' => 0.8
+                       ]
+               ],
+               'Weight'  =>  ['base'  => 'kg',
+                       'conv' => [
+                               'g' => 1000,
+                               'mg' => 1000000,
+                               't' => 0.001,
+                               'grain' => 15432,
+                               'oz' => 35.274,
+                               'lb' => 2.2046,
+                               'cwt(UK)'        =>  0.019684,
+                               'cwt(US)'        =>  0.022046,
+                               'ton (US)'       =>  0.0011023,
+                               'ton (UK)'       =>  0.0009842
+                       ]
+               ],
+               'Distance'  =>  ['base'  => 'km',
+                       'conv' => [
+                               'm' => 1000,
+                               'dm' => 10000,
+                               'cm' => 100000,
+                               'mm' => 1000000,
+                               'mile' => 0.62137,
+                               'naut.mile' => 0.53996,
+                               'inch(es)' => 39370,
+                               'ft' => 3280.8,
+                               'yd' => 1093.6,
+                               'furlong' => 4.970969537898672,
+                               'fathom' => 546.8066491688539
+                       ]
+               ],
+               'Area'  =>  ['base'  => 'km 2',
+                       'conv' => [
+                               'ha' => 100,
+                               'acre' => 247.105,
+                               'm 2' => pow(1000,2),
+                               'dm 2' => pow(10000,2),
+                               'cm 2' => pow(100000,2),
+                               'mm 2' => pow(1000000,2),
+                               'mile 2' => pow(0.62137,2),
+                               'naut.miles 2' => pow(0.53996,2),
+                               'in 2' => pow(39370,2),
+                               'ft 2' => pow(3280.8,2),
+                               'yd 2' => pow(1093.6,2),
+                       ]
+               ],
+               'Volume'  =>  ['base'  => 'm 3',
+                       'conv' => [
+                               'in 3' => 61023.6,
+                               'ft 3' => 35.315,
+                               'cm 3' => pow(10,6),
+                               'dm 3' => 1000,
+                               'litre' => 1000,
+                               'hl' => 10,
+                               'yd 3' => 1.30795,
+                               'gal(US)' => 264.172,
+                               'gal(UK)' => 219.969,
+                               'pint'  =>  2113.376,
+                               'quart'  =>  1056.688,
+                               'cup'  =>  4266.753,
+                               'fl oz'  =>  33814.02,
+                               'tablespoon'  =>  67628.04,
+                               'teaspoon'  =>  202884.1,
+                               'pt (UK)' => 1000/0.56826,
+                               'barrel petroleum' => 1000/158.99,
+                               'Register Tons' => 2.832,
+                               'Ocean Tons' => 1.1327
+                       ]
+               ],
+               'Speed'  => ['base'  => 'kmph',
+                       'conv' => [
+                               'mps' => 0.0001726031,
+                               'milesph' => 0.62137,
+                               'knots' => 0.53996,
+                               'mach STP' => 0.0008380431,
+                               'c (warp)' => 9.265669e-10
+                       ]
+               ]
+       ];
+
+       while (list($key, $val) = each($conversions)) {
+               $conv->addConversion($val['base'], $val['conv']);
+               $list[$key][] = $val['base'];
+               while (list($ukey, $uval) = each($val['conv'])) {
+                       $list[$key][] = $ukey;
+               }
+       }
+
+       $o .= '<h3>Unit Conversions</h3>';
+
+       if (isset($_POST['from_unit']) && isset($_POST['value'])) {
+               $o .= ($conv->getTable(intval($_POST['value']), $_POST['from_unit'], $_POST['to_unit'], 5)) . '</p>';
+       } else {
+               $o .= '<p>Select:</p>';
+       }
+
+       if (isset($_POST['value'])) {
+               $value = $_POST['value'];
+       } else {
+               $value = '';
+       }
+
+       $o .= '<form action="convert" method="post" name="conversion">';
+       $o .= '<input name="value" type="text" id="value" value="' . $value . '" size="10" maxlength="10" />';
+       $o .= '<select name="from_unit" size="12">';
+
+       reset($list);
+       while(list($key, $val) = each($list)) {
+               $o .=  "\n\t<optgroup label=\"$key\">";
+               while(list($ukey, $uval) = each($val)) {
+                       $selected = (($uval == $_POST['from_unit']) ? ' selected="selected" ' : '');
+                       $o .=  "\n\t\t<option value=\"$uval\" $selected >$uval</option>";
+               }
+               $o .= "\n\t</optgroup>";
+       }
+
+       $o .= '</select>';
+       $o .= '<input type="submit" name="Submit" value="Submit" /></form>';
+
+       return $o;
+}