eZ Publish  [trunk]
ezcurrencydata.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZCurrencyData class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 class eZCurrencyData extends eZPersistentObject
00012 {
00013     const DEFAULT_AUTO_RATE_VALUE = '0.0000';
00014     const DEFAULT_CUSTOM_RATE_VALUE = '0.0000';
00015     const DEFAULT_RATE_FACTOR_VALUE = '1.0000';
00016 
00017     const ERROR_OK = 0;
00018     const ERROR_UNKNOWN = 1;
00019     const ERROR_INVALID_CURRENCY_CODE = 2;
00020     const ERROR_CURRENCY_EXISTS = 3;
00021 
00022     const STATUS_ACTIVE = '1';
00023     const STATUS_INACTIVE = '2';
00024 
00025     function eZCurrencyData( $row )
00026     {
00027         $this->eZPersistentObject( $row );
00028         $this->RateValue = false;
00029     }
00030 
00031     static function definition()
00032     {
00033         return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00034                                                         'datatype' => 'integer',
00035                                                         'default' => 0,
00036                                                         'required' => true ),
00037                                          'code' => array( 'name' => 'Code',
00038                                                           'datatype' => 'string',
00039                                                           'default' => '',
00040                                                           'required' => true ),
00041                                          'symbol' => array( 'name' => 'Symbol',
00042                                                             'datatype' => 'string',
00043                                                             'default' => '',
00044                                                             'required' => false ),
00045                                          'locale' => array( 'name' => 'Locale',
00046                                                             'datatype' => 'string',
00047                                                             'default' => '',
00048                                                             'required' => false ),
00049                                          'status' => array( 'name' => 'Status',
00050                                                             'datatype' => 'integer',
00051                                                             'default' => 0,
00052                                                             'required' => true ),
00053                                          'auto_rate_value' => array( 'name' => 'AutoRateValue',
00054                                                                 'datatype' => 'string',
00055                                                                 'default' => self::DEFAULT_AUTO_RATE_VALUE,
00056                                                                 'required' => false ),
00057                                          'custom_rate_value' => array( 'name' => 'CustomRateValue',
00058                                                                   'datatype' => 'string',
00059                                                                   'default' => self::DEFAULT_CUSTOM_RATE_VALUE,
00060                                                                   'required' => false ),
00061                                          'rate_factor' => array( 'name' => 'RateFactor',
00062                                                                  'datatype' => 'string',
00063                                                                  'default' => self::DEFAULT_RATE_FACTOR_VALUE,
00064                                                                  'required' => false ) ),
00065                       'keys' => array( 'id' ),
00066                       'increment_key' => 'id',
00067                       'function_attributes' => array( 'rate_value' => 'rateValue' ),
00068                       'class_name' => "eZCurrencyData",
00069                       'sort' => array( 'code' => 'asc' ),
00070                       'name' => "ezcurrencydata" );
00071     }
00072 
00073     /*!
00074      \static
00075      \params codeList can be a single code like 'USD' or an array like array( 'USD', 'NOK' )
00076      or 'false' (means all currencies).
00077     */
00078     static function fetchList( $conditions = null, $asObjects = true, $offset = false, $limit = false, $asHash = true )
00079     {
00080         $currencyList = array();
00081         $sort = null;
00082         $limitation = null;
00083         if ( $offset !== false or $limit !== false )
00084             $limitation = array( 'offset' => $offset, 'length' => $limit );
00085 
00086         $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
00087                                                      null,
00088                                                      $conditions,
00089                                                      $sort,
00090                                                      $limitation,
00091                                                      $asObjects );
00092 
00093         if ( count( $rows ) > 0 )
00094         {
00095             if ( $asHash )
00096             {
00097                 $keys = array_keys( $rows );
00098                 foreach ( $keys as $key )
00099                 {
00100                     if ( $asObjects )
00101                         $currencyList[$rows[$key]->attribute( 'code' )] = $rows[$key];
00102                     else
00103                         $currencyList[$rows[$key]['code']] = $rows[$key];
00104                 }
00105             }
00106             else
00107             {
00108                 $currencyList = $rows;
00109             }
00110         }
00111 
00112         return $currencyList;
00113     }
00114 
00115     /*!
00116      \static
00117     */
00118     static function fetchListCount( $conditions = null )
00119     {
00120         $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
00121                                                      array(),
00122                                                      $conditions,
00123                                                      false,
00124                                                      null,
00125                                                      false,
00126                                                      false,
00127                                                      array( array( 'operation' => 'count( * )',
00128                                                                    'name' => 'count' ) ) );
00129         return $rows[0]['count'];
00130     }
00131 
00132     /*!
00133      \static
00134     */
00135     static function fetch( $currencyCode, $asObject = true )
00136     {
00137         if ( $currencyCode )
00138         {
00139             $currency = eZCurrencyData::fetchList( array( 'code' => $currencyCode ), $asObject );
00140             if ( is_array( $currency ) && count( $currency ) > 0 )
00141                 return $currency[$currencyCode];
00142         }
00143 
00144         return null;
00145     }
00146 
00147     /*!
00148      functional attribute
00149     */
00150     function rateValue()
00151     {
00152         if ( $this->RateValue === false )
00153         {
00154             /*
00155             $rateValue = '0.00000';
00156             if ( $this->attribute( 'custom_rate_value' ) > 0 )
00157             {
00158                 $rateValue = $this->attribute( 'custom_rate_value' );
00159             }
00160             else
00161             {
00162                 $rateValue = $this->attribute( 'auto_rate_value' );
00163                 $rateValue = $rateValue * $this->attribute( 'rate_factor' );
00164                 $rateValue = sprintf( "%7.5f", $rateValue );
00165             }
00166             */
00167 
00168             $rateValue = '0.00000';
00169             if ( $this->attribute( 'custom_rate_value' ) > 0 )
00170                 $rateValue = $this->attribute( 'custom_rate_value' );
00171             else
00172                 $rateValue = $this->attribute( 'auto_rate_value' );
00173 
00174             if ( $rateValue > 0 )
00175                 $rateValue = $rateValue * $this->attribute( 'rate_factor' );
00176 
00177             $rateValue = sprintf( "%7.5f", $rateValue );
00178 
00179             $this->RateValue = $rateValue;
00180         }
00181 
00182         return $this->RateValue;
00183     }
00184 
00185     function invalidateRateValue()
00186     {
00187         $this->RateValue = false;
00188     }
00189 
00190     /*!
00191      \static
00192     */
00193     static function create( $code, $symbol, $locale, $autoRateValue, $customRateValue, $rateFactor, $status = self::STATUS_ACTIVE )
00194     {
00195         $code = strtoupper( $code );
00196         $errCode = eZCurrencyData::canCreate( $code );
00197         if ( $errCode === self::ERROR_OK )
00198         {
00199             $currency = new eZCurrencyData( array( 'code' => $code,
00200                                                    'symbol' => $symbol,
00201                                                    'locale' => $locale,
00202                                                    'status' => $status,
00203                                                    'auto_rate_value' => $autoRateValue,
00204                                                    'custom_rate_value' => $customRateValue,
00205                                                    'rate_factor' => $rateFactor ) );
00206             $currency->setHasDirtyData( true );
00207             return $currency;
00208         }
00209 
00210         return $errCode;
00211     }
00212 
00213     /*!
00214      \static
00215    */
00216     static function canCreate( $code )
00217     {
00218         $errCode = eZCurrencyData::validateCurrencyCode( $code );
00219         if ( $errCode === self::ERROR_OK && eZCurrencyData::currencyExists( $code ) )
00220             $errCode = self::ERROR_CURRENCY_EXISTS;
00221 
00222         return $errCode;
00223     }
00224 
00225     /*!
00226      \static
00227     */
00228     static function validateCurrencyCode( $code )
00229     {
00230         if ( !preg_match( "/^[A-Z]{3}$/", $code ) )
00231             return self::ERROR_INVALID_CURRENCY_CODE;
00232 
00233         return self::ERROR_OK;
00234     }
00235 
00236     /*!
00237      \static
00238     */
00239     static function currencyExists( $code )
00240     {
00241         return ( eZCurrencyData::fetch( $code ) !== null );
00242     }
00243 
00244     /*!
00245      \static
00246     */
00247     static function removeCurrencyList( $currencyCodeList )
00248     {
00249         if ( is_array( $currencyCodeList ) && count( $currencyCodeList ) > 0 )
00250         {
00251             $db = eZDB::instance();
00252             $db->begin();
00253                 eZPersistentObject::removeObject( eZCurrencyData::definition(),
00254                                                   array( 'code' => array( $currencyCodeList ) ) );
00255             $db->commit();
00256         }
00257     }
00258 
00259     function setStatus( $status )
00260     {
00261         $statusNumeric = eZCurrencyData::statusStringToNumeric( $status );
00262         if ( $statusNumeric !== false )
00263         {
00264             $this->setAttribute( 'status', $statusNumeric );
00265         }
00266         else
00267         {
00268             eZDebug::writeError( "Unknow currency's status '$status'", __METHOD__ );
00269         }
00270     }
00271 
00272     static function statusStringToNumeric( $statusString )
00273     {
00274         $status = false;
00275         if ( is_numeric( $statusString ) )
00276         {
00277             $status = $statusString;
00278         }
00279         if ( is_string( $statusString ) )
00280         {
00281             $statusString = strtoupper( $statusString );
00282             if ( defined( "self::STATUS_{$statusString}" ) )
00283                 $status = constant( "self::STATUS_{$statusString}" );
00284         }
00285 
00286         return $status;
00287     }
00288 
00289     /*!
00290      \static
00291     */
00292     static function errorMessage( $errorCode )
00293     {
00294         switch ( $errorCode )
00295         {
00296             case self::ERROR_INVALID_CURRENCY_CODE:
00297                 return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Invalid characters in currency code.' );
00298 
00299             case self::ERROR_CURRENCY_EXISTS:
00300                 return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Currency already exists.' );
00301 
00302             case self::ERROR_UNKNOWN:
00303             default:
00304                 return ezpI18n::tr( 'kernel/shop/classes/ezcurrencydata', 'Unknown error.' );
00305         }
00306     }
00307 
00308     function store( $fieldFilters = null )
00309     {
00310         // data changed => reset RateValue
00311         $this->invalidateRateValue();
00312         eZPersistentObject::store( $fieldFilters );
00313     }
00314 
00315     function isActive()
00316     {
00317         return ( $this->attribute( 'status' ) == self::STATUS_ACTIVE );
00318     }
00319 
00320     public $RateValue;
00321 }
00322 
00323 ?>