• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/TypeManager.php

00001 <?PHP
00025 class TypeManager
00026 {
00031     protected $_localeInfo;
00032 
00039     public function __construct ($localeCode = "")
00040     {
00041         //try to set correct locale for current language as defined in app.inc section I18n
00042         $currentLanguage = I18n::getCurrentLangCode();
00043         $localeCode = $GLOBALS["local"][$currentLanguage];
00044         setlocale(LC_ALL, $localeCode);
00045         $this->_localeInfo = localeconv();
00046         if ($this->_localeInfo['frac_digits'] > 10)
00047             $this->_localeInfo = null;
00048     }
00049 
00058     public function formattedStringToValue($type, $format, $formattedString)
00059     {
00060         if ($formattedString === null || $formattedString === "")
00061             return null;
00062         switch ($type)
00063         {
00064             case "Number": return $this->numberToValue($format, $formattedString);
00065             case "Text": return $this->textToValue($format, $formattedString);
00066             case "Date": return $this->dateToValue($format, $formattedString);
00067             case "Datetime": return $this->datetimeToValue($format, $formattedString);
00068             case "Currency": return $this->currencyToValue($format, $formattedString);
00069             case "Phone": return $this->phoneToValue($format, $formattedString);
00070             default: return $formattedString;
00071         }
00072     }
00073 
00082     public function valueToFormattedString($type, $format, $value)
00083     {
00084         switch ($type)
00085         {
00086             case "Number": return $this->valueToNumber($format, $value);
00087             case "Text": return $this->valueToText($format, $value);
00088             case "Date": return $this->valueToDate($format, $value);
00089             case "Datetime": return $this->valueToDatetime($format, $value);
00090             case "Currency": return $this->valueToCurrency($format, $value);
00091             case "Phone": return $this->valueToPhone($format, $value);
00092             default: return $value;
00093         }
00094     }
00095 
00103     protected function valueToNumber($format, $value)
00104     {
00105         if ($format[0] == "%")
00106             return sprintf($format, $value);
00107         if (! $this->_localeInfo)
00108             return $value;
00109         $formattedNumber = $value;
00110         if ($format == "Int")
00111             $formattedNumber = number_format($value, 0, $this->_localeInfo['decimal_point'], $this->_localeInfo['thousands_sep']);
00112         else
00113         if ($format == "Float")
00114             $formattedNumber = number_format($value, $this->_localeInfo['frac_digits'], $this->_localeInfo['decimal_point'], $this->_localeInfo['thousands_sep']);
00115         return $formattedNumber;
00116     }
00117 
00125     protected function numberToValue($format, $formattedValue)
00126     {
00127         if ($formattedValue === false || $formattedValue === true)
00128             return null;
00129         if ($format[0] == "%")
00130             return sscanf($formattedValue, $format);
00131         if (! $this->_localeInfo)
00132             return $formattedValue;
00133         $tmp = str_replace($this->_localeInfo['thousands_sep'], null, $formattedValue);
00134         $tmp = str_replace($this->_localeInfo['decimal_point'], ".", $tmp);
00135         if ($format == "Int")
00136             return (int) $tmp;
00137         return $tmp;
00138     }
00139 
00147     protected function valueToText($format, $value)
00148     {
00149         return $value;
00150     }
00151 
00159     protected function textToValue($format, $formattedValue)
00160     {
00161         if ($formattedValue === false)
00162             $formattedValue = "";
00163         return $formattedValue;
00164     }
00165 
00173     protected function valueToDate($format, $value)
00174     {
00175         // ISO format YYYY-MM-DD as input
00176         if ($value == "0000-00-00")
00177             return "";
00178         if (! $value)
00179             return "";
00180         if (strlen(trim($value)) < 1)
00181             return "";
00182         $tt = strtotime($value);
00183         if ($tt != - 1)
00184             return strftime($format, strtotime($value));
00185         return "";
00186     }
00187 
00195     protected function dateToValue($format, $formattedValue)
00196     {
00197         if (! $formattedValue)
00198             return '';
00199         $stdFormat = $this->convertDatetimeFormat($formattedValue, $format, '%Y-%m-%d');
00200         return $stdFormat;
00201     }
00202 
00210     protected function valueToDatetime ($fmt, $value)
00211     {
00212         // ISO format YYYY-MM-DD HH:MM:SS as input
00213         if ($value == "0000-00-00 00:00:00")
00214             return "";
00215         if ($fmt == null)
00216             $fmt = DATETIME_FORMAT;
00217         return $this->valueToDate($fmt, $value);
00218     }
00219 
00227     protected function datetimeToValue($format, $formattedValue)
00228     {
00229         if (! $formattedValue)
00230             return '';
00231         $stdFormat = $this->convertDatetimeFormat($formattedValue, $format, '%Y-%m-%d %H:%M:%S');
00232         return $stdFormat;
00233     }
00234 
00242     protected function valueToCurrency($format, $value)
00243     {
00244         if (! $value)
00245             return "";
00246         if (! $this->_localeInfo)
00247             return $value;
00248         $fmtNum = number_format($value, $this->_localeInfo['frac_digits'], $this->_localeInfo['mon_decimal_point'], $this->_localeInfo['mon_thousands_sep']);
00249         return $this->_localeInfo["currency_symbol"] . $fmtNum;
00250     }
00251 
00259     protected function currencyToValue($format, $formattedValue)
00260     {
00261         if (! $this->_localeInfo)
00262             return $formattedValue;
00263         $tmp = str_replace($this->_localeInfo["currency_symbol"], null, $formattedValue);
00264         $tmp = str_replace($this->_localeInfo['thousands_sep'], null, $tmp);
00265         return (float) $tmp;
00266     }
00267 
00275     protected function valueToPhone($mask, $value)
00276     {
00277         if (substr($value, 0, 1) == "*") // if phone starts with "*", it's an international number, don't convert it
00278             return $value;
00279         if (trim($value) == "")
00280             return $value;
00281         $maskLen = strlen($mask);
00282         $ph_len = strlen($value);
00283         $ph_fmt = $mask;
00284         $j = 0;
00285         for ($i = 0; $i < $maskLen; $i ++)
00286         {
00287             if ($mask[$i] == "#")
00288             {
00289                 $ph_fmt[$i] = $value[$j];
00290                 $j ++;
00291                 if ($j == $ph_len)
00292                     break;
00293             }
00294         }
00295         return substr($ph_fmt, 0, $i + 1);
00296     }
00297 
00305     protected function phoneToValue($mask, $formattedValue)
00306     {
00307         if ($formattedValue[0] == "*")
00308             return $formattedValue;
00309         return ereg_replace("[^0-9]", null, $formattedValue);
00310     }
00311 
00320     public function convertDatetimeFormat($oldFormattedValue, $oldFormat, $newFormat)
00321     {
00322         if ($oldFormat == $newFormat)
00323             return $oldFormattedValue;
00324         $timeStamp = $this->_parseDate($oldFormat, $oldFormattedValue);
00325         return strftime($newFormat, $timeStamp);
00326     }
00327 
00335     private function _parseDate ($fmt, $fmtValue)
00336     {
00337         $y = 0;
00338         $m = 0;
00339         $d = 0;
00340         $hr = 0;
00341         $min = 0;
00342         $sec = 0;
00343         $a = preg_split("/\W+/", $fmtValue);
00344         preg_match_all("/%./", $fmt, $b);
00345         for ($i = 0; $i < count($a); ++ $i)
00346         {
00347             if (! $a[$i])
00348                 continue;
00349             switch ($b[0][$i])
00350             {
00351                 case "%d": // the day of the month ( 00 .. 31 )
00352                 case "%e": // the day of the month ( 0 .. 31 )
00353                     $d = intval($a[$i], 10);
00354                     break;
00355                 case "%m": // month ( 01 .. 12 )
00356                     $m = intval($a[$i], 10);
00357                     break;
00358                 case "%Y": // year including the century ( ex. 1979 )
00359                 case "%y": // year without the century ( 00 .. 99 )
00360                     $y = intval($a[$i], 10);
00361                     if ($y < 100)
00362                         $y += ($y > 29) ? 1900 : 2000;
00363                     break;
00364                 case "%H": // hour ( 00 .. 23 )
00365                 case "%I": // hour ( 01 .. 12 )
00366                 case "%k": // hour ( 00 .. 23 )
00367                 case "%l": // hour ( 01 .. 12 )
00368                     $hr = intval($a[$i], 10);
00369                     break;
00370                 case "%P": // PM or AM
00371                 case "%p": // pm or am
00372                     if ($a[$i] == 'pm' && $hr < 12)
00373                         $hr += 12;
00374                     else
00375                     if ($a[$i] == 'am' && $hr >= 12)
00376                         $hr -= 12;
00377                     break;
00378                 case "%M": // minute ( 00 .. 59 )
00379                     $min = intval($a[$i], 10);
00380                     break;
00381                 case "%S": // second ( 00 .. 59 )
00382                     $sec = intval($a[$i], 10);
00383                     break;
00384                 //default:
00385             }
00386         }
00387         $timeStamp = mktime($hr, $min, $sec, $m, $d, $y);
00388         return $timeStamp;
00389     }
00390     
00391     public function setLocaleInfo($localeInfo)
00392     {
00393         $this->_localeInfo = $localeInfo;
00394     }    
00395 }

Generated on Thu Apr 19 2012 17:09:14 for openbiz by  doxygen 1.7.2