partstock/includes/other_functions.php

154 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2018-12-17 17:50:23 +01:00
<?php
function OtherGetAvailableTemplates ()
{
$ReturnValue=array();
$DirList=scandir('./templates/');
foreach ($DirList as $i)
{
if (!is_dir('./templates/'.$i) || (substr($i,0,1)=="."))
continue;
$ReturnValue[count($ReturnValue)]=$i;
}
return $ReturnValue;
}
function OtherFormatPrice ($PriceString)
{
global $GlobalDecimalPoint;
return str_replace(".",$GlobalDecimalPoint,sprintf("%.3f",OtherConvertToFloat($PriceString)));
}
function OtherConvertToFloat ($Value)
{
global $GlobalDecimalPoint;
$Value=trim($Value);
for ($i=0;$i<(strlen($Value)-1);$i++)
{
//replace first non-decimal position with decimal-point
if ( ! ($Value[$i]=="0"
|| $Value[$i]=="1"
|| $Value[$i]=="2"
|| $Value[$i]=="3"
|| $Value[$i]=="4"
|| $Value[$i]=="5"
|| $Value[$i]=="6"
|| $Value[$i]=="7"
|| $Value[$i]=="8"
|| $Value[$i]=="9"
))
{
$Value[$i]=$GlobalDecimalPoint;
break;
}
}
return (float) $Value;
}
function OtherSiPrefixToFloat ($String)
{
$String=trim($String);
if (substr($String,strlen($String)-1,1)=="E")
return OtherConvertToFloat($String)*1000000000000000000;
if (substr($String,strlen($String)-1,1)=="P")
return OtherConvertToFloat($String)*1000000000000000;
if (substr($String,strlen($String)-1,1)=="T")
return OtherConvertToFloat($String)*1000000000000;
if (substr($String,strlen($String)-1,1)=="G")
return OtherConvertToFloat($String)*1000000000;
if (substr($String,strlen($String)-1,1)=="M")
return OtherConvertToFloat($String)*1000000;
if (substr($String,strlen($String)-1,1)=="k")
return OtherConvertToFloat($String)*1000;
if (substr($String,strlen($String)-1,1)=="m")
return OtherConvertToFloat($String)/1000;
if (substr($String,strlen($String)-1,1)=="µ")
return OtherConvertToFloat($String)/1000000;
if (substr($String,strlen($String)-1,1)=="u")
return OtherConvertToFloat($String)/1000000;
if (substr($String,strlen($String)-1,1)=="n")
return OtherConvertToFloat($String)/1000000000;
if (substr($String,strlen($String)-1,1)=="p")
return OtherConvertToFloat($String)/1000000000000;
if (substr($String,strlen($String)-1,1)=="f")
return OtherConvertToFloat($String)/1000000000000000;
if (substr($String,strlen($String)-1,1)=="a")
return OtherConvertToFloat($String)/1000000000000000000;
else
return OtherConvertToFloat($String);
}
function OtherFloatToSiPrefix ($Integer)
{
$Integer = trim(sprintf("%E",$Integer));
$Exponent=0;
for ($i=(strlen($Integer)-1); $i>=0; $i--)
{
if ($Integer[$i]=="E") break;
$Exponent = $Integer[$i] . $Exponent;
}
$Exponent = substr($Exponent,0,strlen($Exponent)-1);
//set si prefix
if ($Exponent>=18) return ($Integer/1000000000000000000)." E";
if ($Exponent>=15) return ($Integer/1000000000000000)." P";
if ($Exponent>=12) return ($Integer/1000000000000)." T";
if ($Exponent>=9) return ($Integer/1000000000)." G";
if ($Exponent>=6) return ($Integer/1000000)." M";
if ($Exponent>=3) return ($Integer/1000)." k";
if ($Exponent<3 && $Exponent>=0) return ($Integer/1)." ";
if ($Exponent<-15) return ($Integer*1000000000000000)." a";
if ($Exponent<-12) return ($Integer*1000000000000000)." f";
if ($Exponent<-9) return ($Integer*1000000000000)." p";
if ($Exponent<-6) return ($Integer*1000000000)." n";
if ($Exponent<-3) return ($Integer*1000000)." µ";
if ($Exponent<0) return ($Integer*1000)." m";
}
function OtherGetIcon ($IconName,$CssClass)
{
global $GlobalTemplate;
$IconDirectory='./templates/'.$GlobalTemplate.'/icons/';
$Error = 0;
$RetVal = "";
2018-12-17 17:50:23 +01:00
if (file_exists($IconDirectory.$IconName.".png")) $IconFile=$IconName.".png";
elseif (file_exists($IconDirectory.$IconName.".jpg")) $IconFile=$IconName.".jpg";
elseif (file_exists($IconDirectory.$IconName.".gif")) $IconFile=$IconName.".gif";
else
{
$Error = 1;
//log if template directory is existing - if not, anyone else will report this
if (file_exists("./templates/".$GlobalTemplate)) ErrorLog("Icon '$IconName' +[.png|.jpg|.gif] not found!");
}
2018-12-17 17:50:23 +01:00
if (!$Error)
{
$RetVal = "<img src=\"$IconDirectory$IconFile\"".(($CssClass)? " class=\"$CssClass\"":"").">";
}
2018-12-17 17:50:23 +01:00
return $RetVal;
}
// Returns a html <img> tag
function OtherGetPictureImg($part_id, $max_width=0, $max_height=0) {
// create anchor tag
$tag = "<a href=\"part_picture.php?PartId=$part_id\">";
// create img tag
$tag .= "<img src=\"part_picture.php?PartId=$part_id";
if ($max_width > 0) $tag .= "&MaxWidth=$max_width";
if ($max_height > 0) $tag .= "&MaxHeight=$max_height";
$tag .= "\""; // finish src attribute
$tag .= " alt=\"picture of part $part_id\"";
$tag .= ">"; // finish img tag
// close link
$tag .= '</a>';
return $tag;
}
?>