[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/shop/classes/ -> ezcurrencydata.php (source)

   1  <?php
   2  //
   3  // Definition of eZCurrencyData class
   4  //
   5  // Created on: <08-Nov-2005 13:06:15 dl>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezcurrencydata.php
  30  */
  31  
  32  define( 'EZ_CURRENCYDATA_DEFAULT_AUTO_RATE_VALUE', '0.0000' );
  33  define( 'EZ_CURRENCYDATA_DEFAULT_CUSTOM_RATE_VALUE', '0.0000' );
  34  define( 'EZ_CURRENCYDATA_DEFAULT_RATE_FACTOR_VALUE', '1.0000' );
  35  
  36  define( 'EZ_CURRENCYDATA_ERROR_OK', 0 );
  37  define( 'EZ_CURRENCYDATA_ERROR_UNKNOWN', 1 );
  38  define( 'EZ_CURRENCYDATA_ERROR_INVALID_CURRENCY_CODE', 2 );
  39  define( 'EZ_CURRENCYDATA_ERROR_CURRENCY_EXISTS', 3 );
  40  
  41  define( 'EZ_CURRENCYDATA_STATUS_ACTIVE', '1' );
  42  define( 'EZ_CURRENCYDATA_STATUS_INACTIVE', '2' );
  43  
  44  include_once ( "kernel/classes/ezpersistentobject.php" );
  45  
  46  class eZCurrencyData extends eZPersistentObject
  47  {
  48      function eZCurrencyData( $row )
  49      {
  50          $this->eZPersistentObject( $row );
  51          $this->RateValue = false;
  52      }
  53  
  54      function definition()
  55      {
  56          return array( 'fields' => array( 'id' => array( 'name' => 'ID',
  57                                                          'datatype' => 'integer',
  58                                                          'default' => 0,
  59                                                          'required' => true ),
  60                                           'code' => array( 'name' => 'Code',
  61                                                            'datatype' => 'string',
  62                                                            'default' => '',
  63                                                            'required' => true ),
  64                                           'symbol' => array( 'name' => 'Symbol',
  65                                                              'datatype' => 'string',
  66                                                              'default' => '',
  67                                                              'required' => false ),
  68                                           'locale' => array( 'name' => 'Locale',
  69                                                              'datatype' => 'string',
  70                                                              'default' => '',
  71                                                              'required' => false ),
  72                                           'status' => array( 'name' => 'Status',
  73                                                              'datatype' => 'integer',
  74                                                              'default' => 0,
  75                                                              'required' => true ),
  76                                           'auto_rate_value' => array( 'name' => 'AutoRateValue',
  77                                                                  'datatype' => 'string',
  78                                                                  'default' => EZ_CURRENCYDATA_DEFAULT_AUTO_RATE_VALUE,
  79                                                                  'required' => false ),
  80                                           'custom_rate_value' => array( 'name' => 'CustomRateValue',
  81                                                                    'datatype' => 'string',
  82                                                                    'default' => EZ_CURRENCYDATA_DEFAULT_CUSTOM_RATE_VALUE,
  83                                                                    'required' => false ),
  84                                           'rate_factor' => array( 'name' => 'RateFactor',
  85                                                                   'datatype' => 'string',
  86                                                                   'default' => EZ_CURRENCYDATA_DEFAULT_RATE_FACTOR_VALUE,
  87                                                                   'required' => false ) ),
  88                        'keys' => array( 'id' ),
  89                        'increment_key' => 'id',
  90                        'function_attributes' => array( 'rate_value' => 'rateValue' ),
  91                        'class_name' => "eZCurrencyData",
  92                        'sort' => array( 'code' => 'asc' ),
  93                        'name' => "ezcurrencydata" );
  94      }
  95  
  96      /*!
  97       \static
  98       \params codeList can be a single code like 'USD' or an array like array( 'USD', 'NOK' )
  99       or 'false' (means all currencies).
 100      */
 101      function fetchList( $conditions = null, $asObjects = true, $offset = false, $limit = false, $asHash = true )
 102      {
 103          $currencyList = array();
 104          $sort = null;
 105          $limitation = null;
 106          if ( $offset !== false or $limit !== false )
 107              $limitation = array( 'offset' => $offset, 'length' => $limit );
 108  
 109          $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
 110                                                       null,
 111                                                       $conditions,
 112                                                       $sort,
 113                                                       $limitation,
 114                                                       $asObjects );
 115  
 116          if ( count( $rows ) > 0 )
 117          {
 118              if ( $asHash )
 119              {
 120                  $keys = array_keys( $rows );
 121                  foreach ( $keys as $key )
 122                  {
 123                      if ( $asObjects )
 124                          $currencyList[$rows[$key]->attribute( 'code' )] =& $rows[$key];
 125                      else
 126                          $currencyList[$rows[$key]['code']] =& $rows[$key];
 127                  }
 128              }
 129              else
 130              {
 131                  $currencyList =& $rows;
 132              }
 133          }
 134  
 135          return $currencyList;
 136      }
 137  
 138      /*!
 139       \static
 140      */
 141      function fetchListCount( $conditions = null )
 142      {
 143          $rows = eZPersistentObject::fetchObjectList( eZCurrencyData::definition(),
 144                                                       array(),
 145                                                       $conditions,
 146                                                       null,
 147                                                       null,
 148                                                       false,
 149                                                       false,
 150                                                       array( array( 'operation' => 'count( * )',
 151                                                                     'name' => 'count' ) ) );
 152          return $rows[0]['count'];
 153      }
 154  
 155      /*!
 156       \static
 157      */
 158      function fetch( $currencyCode, $asObject = true )
 159      {
 160          if ( $currencyCode )
 161          {
 162              $currency = eZCurrencyData::fetchList( array( 'code' => $currencyCode ), $asObject );
 163              if ( is_array( $currency ) && count( $currency ) > 0 )
 164                  return $currency[$currencyCode];
 165          }
 166  
 167          return null;
 168      }
 169  
 170      /*!
 171       functional attribute
 172      */
 173      function &rateValue()
 174      {
 175          if ( $this->RateValue === false )
 176          {
 177              /*
 178              $rateValue = '0.00000';
 179              if ( $this->attribute( 'custom_rate_value' ) > 0 )
 180              {
 181                  $rateValue = $this->attribute( 'custom_rate_value' );
 182              }
 183              else
 184              {
 185                  $rateValue = $this->attribute( 'auto_rate_value' );
 186                  $rateValue = $rateValue * $this->attribute( 'rate_factor' );
 187                  $rateValue = sprintf( "%7.5f", $rateValue );
 188              }
 189              */
 190  
 191              $rateValue = '0.00000';
 192              if ( $this->attribute( 'custom_rate_value' ) > 0 )
 193                  $rateValue = $this->attribute( 'custom_rate_value' );
 194              else
 195                  $rateValue = $this->attribute( 'auto_rate_value' );
 196  
 197              if ( $rateValue > 0 )
 198                  $rateValue = $rateValue * $this->attribute( 'rate_factor' );
 199  
 200              $rateValue = sprintf( "%7.5f", $rateValue );
 201  
 202              $this->RateValue = $rateValue;
 203          }
 204  
 205          return $this->RateValue;
 206      }
 207  
 208      function invalidateRateValue()
 209      {
 210          $this->RateValue = false;
 211      }
 212  
 213      /*!
 214       \static
 215      */
 216      function create( $code, $symbol, $locale, $autoRateValue, $customRateValue, $rateFactor, $status = EZ_CURRENCYDATA_STATUS_ACTIVE )
 217      {
 218          $code = strtoupper( $code );
 219          $errCode = eZCurrencyData::canCreate( $code );
 220          if ( $errCode === EZ_CURRENCYDATA_ERROR_OK )
 221          {
 222              $currency = new eZCurrencyData( array( 'code' => $code,
 223                                                     'symbol' => $symbol,
 224                                                     'locale' => $locale,
 225                                                     'status' => $status,
 226                                                     'auto_rate_value' => $autoRateValue,
 227                                                     'custom_rate_value' => $customRateValue,
 228                                                     'rate_factor' => $rateFactor ) );
 229              $currency->setHasDirtyData( true );
 230              return $currency;
 231          }
 232  
 233          return $errCode;
 234      }
 235  
 236      /*!
 237       \static
 238     */
 239      function canCreate( $code )
 240      {
 241          $errCode = eZCurrencyData::validateCurrencyCode( $code );
 242          if ( $errCode === EZ_CURRENCYDATA_ERROR_OK && eZCurrencyData::currencyExists( $code ) )
 243              $errCode = EZ_CURRENCYDATA_ERROR_CURRENCY_EXISTS;
 244  
 245          return $errCode;
 246      }
 247  
 248      /*!
 249       \static
 250      */
 251      function validateCurrencyCode( $code )
 252      {
 253          if ( !preg_match( "/^[A-Z]{3}$/", $code ) )
 254              return EZ_CURRENCYDATA_ERROR_INVALID_CURRENCY_CODE;
 255  
 256          return EZ_CURRENCYDATA_ERROR_OK;
 257      }
 258  
 259      /*!
 260       \static
 261      */
 262      function currencyExists( $code )
 263      {
 264          return ( eZCurrencyData::fetch( $code ) !== null );
 265      }
 266  
 267      /*!
 268       \static
 269      */
 270      function removeCurrencyList( $currencyCodeList )
 271      {
 272          if ( is_array( $currencyCodeList ) && count( $currencyCodeList ) > 0 )
 273          {
 274              $db =& eZDB::instance();
 275              $db->begin();
 276                  eZPersistentObject::removeObject( eZCurrencyData::definition(),
 277                                                    array( 'code' => array( $currencyCodeList ) ) );
 278              $db->commit();
 279          }
 280      }
 281  
 282      function setStatus( $status )
 283      {
 284          $statusNumeric = eZCurrencyData::statusStringToNumeric( $status );
 285          if ( $statusNumeric !== false )
 286              $this->setAttribute( 'status', $statusNumeric );
 287          else
 288              eZDebug::writeError( "Unknow currency's status '$status'", 'eZCurrencyData::setStatus' );
 289      }
 290  
 291      function statusStringToNumeric( $statusString )
 292      {
 293          $status = false;
 294          if ( is_numeric( $statusString ) )
 295          {
 296              $status = $statusString;
 297          }
 298          if ( is_string( $statusString ) )
 299          {
 300              $statusString = strtoupper( $statusString );
 301              if ( defined( "EZ_CURRENCYDATA_STATUS_$statusString" ) )
 302                  $status = constant( "EZ_CURRENCYDATA_STATUS_$statusString" );
 303          }
 304  
 305          return $status;
 306      }
 307  
 308      /*!
 309       \static
 310      */
 311      function errorMessage( $errorCode )
 312      {
 313          switch ( $errorCode )
 314          {
 315              case EZ_CURRENCYDATA_ERROR_INVALID_CURRENCY_CODE:
 316                  return ezi18n( 'kernel/shop/classes/ezcurrencydata', 'Invalid characters in currency code.' );
 317  
 318              case EZ_CURRENCYDATA_ERROR_CURRENCY_EXISTS:
 319                  return ezi18n( 'kernel/shop/classes/ezcurrencydata', 'Currency already exists.' );
 320  
 321              case EZ_CURRENCYDATA_ERROR_UNKNOWN:
 322              default:
 323                  return ezi18n( 'kernel/shop/classes/ezcurrencydata', 'Unknown error.' );
 324          }
 325      }
 326  
 327      function store( $fieldFilters = null )
 328      {
 329          // data changed => reset RateValue
 330          $this->invalidateRateValue();
 331          eZPersistentObject::store( $fieldFilters );
 332      }
 333  
 334      function isActive()
 335      {
 336          return ( $this->attribute( 'status' ) == EZ_CURRENCYDATA_STATUS_ACTIVE );
 337      }
 338  
 339      var $RateValue;
 340  }
 341  
 342  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7