Openbiz 2.0 > Openbiz / Bin / TypeManager.php

Questions? Need Help? Want to share? » PHP Forum
Script Name:
Openbiz 2.0

Download:
openbiz-2.0.zip

Category:
Development Tools

Archive Content:

Openbiz > Bin

Openbiz > Bin > Service

Openbiz > Bin

Openbiz

Openbiz > Document

Openbiz > Document > Apidoc20 > BizController

Openbiz > Document > Apidoc20 > BizDataObj

Openbiz > Document > Apidoc20 > BizSystem

Openbiz > Document > Apidoc20 > BizView

Openbiz > Document > Apidoc20

Openbiz > Document > Apidoc20 > Media

Openbiz > Document > Apidoc20 > Media > Images

Openbiz > Document > Apidoc20 > Media > Lib

Openbiz > Document > Apidoc20 > Media

Openbiz > Document > Apidoc20

Openbiz > Document > Apidoc20 > PluginService

Openbiz > Document

Openbiz

Openbiz > Metadata

Openbiz > Metadata > Service

Openbiz > Others > Adodb

Openbiz > Others > Adodb > Contrib

Openbiz > Others > Adodb > Cute Icons For Site

Openbiz > Others > Adodb > Datadict

Openbiz > Others > Adodb > Docs

Openbiz > Others > Adodb > Drivers

Openbiz > Others > Adodb > Lang

Openbiz > Others > Adodb

Openbiz > Others > Adodb > Pear > Auth > Container

Openbiz > Others > Adodb > Pear

Openbiz > Others > Adodb > Perf

Openbiz > Others > Adodb

Openbiz > Others > Adodb > Session

Openbiz > Others > Adodb > Session > Old

Openbiz > Others > Adodb > Session

Openbiz > Others > Adodb > Tests

Openbiz > Others > Adodb

Openbiz > Others > Adodb > Xsl

Openbiz > Others > Dompdf

Openbiz > Others > Dompdf > Include

Openbiz > Others > Dompdf

Openbiz > Others > Dompdf > Lib

Openbiz > Others > Dompdf > Lib > Fonts

Openbiz > Others > Dompdf > Lib > Res

Openbiz > Others > Dompdf > Lib

Openbiz > Others > Dompdf

Openbiz > Others > Dompdf > Www

Openbiz > Others > Dompdf > Www > Images

Openbiz > Others > Dompdf > Www

Openbiz > Others > Dompdf > Www > Test

Openbiz > Others > Dompdf > Www

Openbiz > Others > Smarty

Openbiz > Others > Smarty > Demo > Configs

Openbiz > Others > Smarty > Demo

Openbiz > Others > Smarty > Demo > Templates

Openbiz > Others > Smarty

Openbiz > Others > Smarty > Libs

Openbiz > Others > Smarty > Libs > Internals

Openbiz > Others > Smarty > Libs > Plugins

Openbiz > Others > Smarty > Libs

Openbiz > Others > Smarty > Misc

Openbiz > Others > Smarty

Openbiz > Others > Smarty > Unit Test

Openbiz > Others > Smarty > Unit Test > Configs

Openbiz > Others > Smarty > Unit Test

Openbiz > Others > Smarty > Unit Test > Templates

Openbiz > Others > Smarty > Unit Test

Demoapp > Bin

Demoapp > Bin > Service

Demoapp > Bin > Shared

Demoapp

Demoapp > Css

Demoapp

Demoapp > Images

Demoapp > Metadata

Demoapp > Metadata > Demo

Demoapp > Metadata > Service

Demoapp > Metadata > Shared

Demoapp

Demoapp > Templates

TypeManager.php:


<?PHP
/**
 * TypeManager class - Type management class that has help methods to format data to UI and unformat UI input to data.
 * 
 * @package BizSystem
 * @author rocky swen 
 * @copyright Copyright (c) 2005
 * @access public 
 */
class TypeManager
{
   protected 
$m_LocaleInfo;
   
/**
    * TypeManager::__construct - Constructor of TypeManager,  set locale with localcode parameter
    * 
    * @param string $localeCode
    * @return void
    **/
   
public function __construct($localeCode="")
   {
      
setlocale(LC_ALL,  $localeCode);
      
$this->m_LocaleInfo localeconv();
      if (
$this->m_LocaleInfo['frac_digits']>10)
         
$this->m_LocaleInfo null;
   }
   
/**
    * TypeManager::FormattedStringToValue - Convert Formatted String To Value
    * 
    * @param string $type - field type
    * @param string $fmt - type format
    * @param string $fmtString - formatted string
    * @return mix value
    **/
   
public function FormattedStringToValue($type,  $fmt,  $fmtString)
   {
      if (
$type=="Number")       return $this->NumberToValue($fmt,  $fmtString);
      else if (
$type=="Text")    return $this->TextToValue($fmt,  $fmtString);
      else if (
$type=="Date")    return $this->DateToValue($fmt,  $fmtString);
      else if (
$type=="Datetime")return $this->DatetimeToValue($fmt,  $fmtString);
      else if (
$type=="Currency")return $this->CurrencyToValue($fmt,  $fmtString);
      else if (
$type=="Phone")   return $this->PhoneToValue($fmt,  $fmtString);
      else return 
$fmtString;
   }
   
/**
    * TypeManager::ValueToFormattedString - Convert Value To Formatted String
    * 
    * @param string $type - field type
    * @param string $fmt - type format
    * @param string $value - value
    * @return string formatted string
    **/
   
public function ValueToFormattedString($type,  $fmt,  $value)
   {
      if (
$type=="Number")       return $this->ValueToNumber($fmt,  $value);
      else if (
$type=="Text")    return $this->ValueToText($fmt,  $value);
      else if (
$type=="Date")    return $this->ValueToDate($fmt,  $value);
      else if (
$type=="Datetime")return $this->ValueToDatetime($fmt,  $value);
      else if (
$type=="Currency")return $this->ValueToCurrency($fmt,  $value);
      else if (
$type=="Phone")   return $this->ValueToPhone($fmt,  $value);
      else return 
$value;
   }
   
   protected function 
ValueToNumber($fmt,  $value)
   {
      if (
$fmt[0]=="%")
         return 
sprintf($fmt,  $value);
      if (!
$this->m_LocaleInfo)
         return 
$value;
      
$fmt_num $value;
      if (
$fmt=="Int")
         
$fmt_num number_format($value,  0,  $this->m_LocaleInfo['decimal_point'],  $this->m_LocaleInfo['thousands_sep']);
      else if (
$fmt=="Float")
         
$fmt_num number_format($value,  
            
$this->m_LocaleInfo['frac_digits'],  
            
$this->m_LocaleInfo['decimal_point'],  
            
$this->m_LocaleInfo['thousands_sep']);
      return 
$fmt_num;
   }
   protected function 
NumberToValue($fmt,  $fmt_value)
   {
      if (
$fmt[0]=="%")
         return 
sscanf($fmt_value,  $fmt);
      if (!
$this->m_LocaleInfo)
         return 
$fmt_value;
      
$tmp str_replace($this->m_LocaleInfo['thousands_sep'], null, $fmt_value);
      if (
$fmt=="Int")
         return (int)
$tmp;
      return (float)
$tmp;
   }
   
   protected function 
ValueToText($fmt,  $value)
   {
      return 
$value;
   }
   protected function 
TextToValue($fmt,  $fmt_value)
   {
      return 
$fmt_value;
   }
   
   protected function 
ValueToDate($fmt,  $value)
   {
      
// ISO format YYYY-MM-DD HH:MM:SS
      
if ($value == "0000-00-00") return "";
      if (!
$value)   return "";
      if (
strlen(trim($value))<1) return "";
      
$tt strtotime($value);
      if (
$tt != -1)
         return 
strftime($fmt,  strtotime($value));
      return 
"";
   }
   protected function 
DateToValue($fmt,  $fmt_value)
   {
      
// ISO format YYYY-MM-DD HH:MM:SS
      // use strtotime,  it only accept GNU date time syntax 
      /* http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html */
      
if (!$fmt_value) return '';
      return 
date("Y-m-d",  strtotime($fmt_value));
   }
   
   protected function 
ValueToDatetime($fmt,  $value)
   {
      
// ISO format YYYY-MM-DD HH:MM:SS
      
if ($value == "0000-00-00 00:00:00") return "";
      return 
$this->ValueToDate($fmt,  $value);
   }
   protected function 
DatetimeToValue($fmt,  $fmt_value)
   {
      
// ISO format YYYY-MM-DD HH:MM:SS
      // use strtotime,  it only accept GNU date time syntax 
      /* http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html */
      
if (!$fmt_value) return '';
      
$time strtotime($fmt_value);
      
$val date("Y-m-d H:i:s",  $time);
      return 
$val;
   }
   
   protected function 
ValueToCurrency($fmt,  $value)
   {
      if (!
$value)   return "";
      if (!
$this->m_LocaleInfo)
         return 
$value;
      
$fmt_num number_format($value,  
         
$this->m_LocaleInfo['frac_digits'],  
         
$this->m_LocaleInfo['mon_decimal_point'],  
         
$this->m_LocaleInfo['mon_thousands_sep']);
      return 
$this->m_LocaleInfo["currency_symbol"].$fmt_num;
   }
   protected function 
CurrencyToValue($fmt,  $fmt_value)
   {
      if (!
$this->m_LocaleInfo)
         return 
$fmt_value;
      
$tmp str_replace($this->m_LocaleInfo["currency_symbol"], null, $fmt_value);
      
$tmp str_replace($this->m_LocaleInfo['thousands_sep'], null, $tmp);
      return (float)
$tmp;
   }
   
   protected function 
ValueToPhone($mask,  $value)
   {
      if (
substr($value, 0, 1) == "*")   // if phone starts with "*",  it's an international number,  don't convert it
         
return $value;
      if (
trim($value)=="")
         return 
$value;
      
$mask_len strlen($mask);
      
$ph_len strlen($value);
      
$ph_fmt $mask;
      
$j=0;
      for(
$i=0$i<$mask_len$i++) {
         if (
$mask[$i] == "#") {
            
$ph_fmt[$i] = $value[$j];
            
$j++;
            if (
$j==$ph_len) break;
         }
      }
      return 
substr($ph_fmt, 0, $i+1);
   }
   protected function 
PhoneToValue($mask,  $fmt_value)
   {
      if (
$fmt_value[0] == "*")
         return 
$fmt_value;
      return 
ereg_replace("[^0-9]", null, $fmt_value);
   }
}

?>


Other Development Tools Scripts:

WebMaster Resources Home

©RingsWorld.com