[ 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/ -> ezshopfunctions.php (source)

   1  <?php
   2  //
   3  // Definition of ezshopfunctions class
   4  //
   5  // Created on: <04-Nov-2005 12:26:52 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 ezshopfunctions.php
  30  */
  31  
  32  class eZShopFunctions
  33  {
  34      function eZShopFunctions()
  35      {
  36      }
  37  
  38      /*!
  39       \static
  40      */
  41      function isProductClass( &$contentClass )
  42      {
  43          $type = eZShopFunctions::productTypeByClass( $contentClass );
  44          return ( $type !== false );
  45      }
  46  
  47      /*!
  48       \static
  49      */
  50      function isProductObject( &$contentObject )
  51      {
  52          $type = eZShopFunctions::productTypeByObject( $contentObject );
  53          return ( $type !== false );
  54      }
  55  
  56      function isSimplePriceClass( &$contentClass )
  57      {
  58          $type = eZShopFunctions::productTypeByClass( $contentClass );
  59          return eZShopFunctions::isSimplePriceProductType( $type );
  60  
  61      }
  62  
  63      function isSimplePriceProductType( $type )
  64      {
  65          return ( $type === 'ezprice' );
  66      }
  67  
  68      function isMultiPriceClass( &$contentClass )
  69      {
  70          $type = eZShopFunctions::productTypeByClass( $contentClass );
  71          return eZShopFunctions::isMultiPriceProductType( $type );
  72      }
  73  
  74      function isMultiPriceProductType( $type )
  75      {
  76          return ( $type === 'ezmultiprice' );
  77      }
  78  
  79      /*!
  80       \static
  81      */
  82      function productTypeByClass( &$contentClass )
  83      {
  84          $type = false;
  85  
  86          if ( is_object( $contentClass ) )
  87          {
  88              $classAttributes =& $contentClass->fetchAttributes();
  89              $keys = array_keys( $classAttributes );
  90              foreach ( $keys as $key )
  91              {
  92                  $classAttribute =& $classAttributes[$key];
  93                  $dataType = $classAttribute->attribute( 'data_type_string' );
  94                  if ( eZShopFunctions::isProductDatatype( $dataType ) )
  95                  {
  96                      $type = $dataType;
  97                      break;
  98                  }
  99              }
 100          }
 101  
 102          return $type;
 103      }
 104  
 105      /*!
 106       \static
 107      */
 108      function productTypeByObject( &$contentObject )
 109      {
 110          $type = false;
 111  
 112          if ( is_object( $contentObject ) )
 113          {
 114              $attributes =& $contentObject->contentObjectAttributes();
 115              $keys = array_keys( $attributes );
 116              foreach ( $keys as $key )
 117              {
 118                  $attribute =& $attributes[$key];
 119                  $dataType = $attribute->dataType();
 120                  if ( eZShopFunctions::isProductDatatype( $dataType->isA() ) )
 121                  {
 122                      $type = $dataType->isA();
 123                      break;
 124                  }
 125              }
 126          }
 127  
 128          return $type;
 129      }
 130  
 131      /*!
 132       \static
 133      */
 134      function isProductDatatype( $dataTypeString )
 135      {
 136          return in_array( $dataTypeString, eZShopFunctions::productDatatypeStringList() );
 137      }
 138  
 139      /*!
 140       \static
 141      */
 142      function productDatatypeStringList()
 143      {
 144          return array( 'ezprice',
 145                        'ezmultiprice' );
 146      }
 147  
 148      function productClassList()
 149      {
 150          include_once ( 'kernel/classes/ezcontentclass.php' );
 151          $productClassList = array();
 152          $classList = eZContentClass::fetchList();
 153          $keys = array_keys( $classList );
 154          foreach ( $keys as $key )
 155          {
 156              $class =& $classList[$key];
 157              if ( eZShopFunctions::isProductClass( $class ) )
 158                  $productClassList[] = $class;
 159  
 160              unset( $class );
 161          }
 162  
 163          return $productClassList;
 164      }
 165  
 166      function priceAttributeIdentifier( &$productClass )
 167      {
 168          $identifier = '';
 169          $classAttribute = eZShopFunctions::priceAttribute( $productClass );
 170          if ( is_object( $classAttribute ) )
 171              $identifier = $classAttribute->attribute( 'identifier' );
 172  
 173          return $identifier;
 174      }
 175  
 176      function priceAttribute( &$productClass )
 177      {
 178          if ( is_object( $productClass ) )
 179          {
 180              $classAttributes =& $productClass->fetchAttributes();
 181              $keys = array_keys( $classAttributes );
 182              foreach ( $keys as $key )
 183              {
 184                  $classAttribute =& $classAttributes[$key];
 185                  $dataType = $classAttribute->attribute( 'data_type_string' );
 186                  if ( eZShopFunctions::isProductDatatype( $dataType ) )
 187                  {
 188                      return $classAttribute;
 189                  }
 190              }
 191          }
 192  
 193          return false;
 194      }
 195  
 196      /*!
 197       \static
 198      */
 199      function preferredCurrencyCode()
 200      {
 201          include_once ( 'kernel/classes/ezpreferences.php' );
 202          if( !$currencyCode = eZPreferences::value( 'user_preferred_currency' ) )
 203          {
 204              include_once ( 'lib/ezutils/classes/ezini.php' );
 205              $ini =& eZINI::instance( 'shop.ini' );
 206              $currencyCode = $ini->variable( 'CurrencySettings', 'PreferredCurrency' );
 207          }
 208          return $currencyCode;
 209      }
 210  
 211      /*!
 212       \static
 213      */
 214      function setPreferredCurrencyCode( $currencyCode )
 215      {
 216          include_once ( 'kernel/shop/errors.php' );
 217  
 218          $error = eZShopFunctions::isPreferredCurrencyValid( $currencyCode );
 219          if ( $error === EZ_ERROR_SHOP_OK )
 220          {
 221              include_once ( 'kernel/classes/ezpreferences.php' );
 222              eZPreferences::setValue( 'user_preferred_currency', $currencyCode );
 223          }
 224  
 225          return $error;
 226      }
 227  
 228      /*!
 229       Get country stored in user object.
 230       \static
 231      */
 232      function getUserCountry()
 233      {
 234          require_once ( 'kernel/classes/ezvatmanager.php' );
 235          return eZVATManager::getUserCountry( false, false );
 236      }
 237  
 238  
 239      /*!
 240       Get country stored in user preferences.
 241       \static
 242      */
 243      function getPreferredUserCountry()
 244      {
 245          include_once ( 'kernel/classes/ezpreferences.php' );
 246          return eZPreferences::value( 'user_preferred_country' );
 247      }
 248  
 249      /*!
 250       Store country to user preferences.
 251       \static
 252      */
 253      function setPreferredUserCountry( $country )
 254      {
 255          include_once ( 'kernel/classes/ezpreferences.php' );
 256          eZPreferences::setValue( 'user_preferred_country', $country );
 257  
 258          include_once ( 'kernel/shop/errors.php' );
 259          return EZ_ERROR_SHOP_OK;
 260      }
 261  
 262      /*!
 263       \static
 264      */
 265      function isPreferredCurrencyValid( $currencyCode = false )
 266      {
 267          include_once ( 'kernel/shop/errors.php' );
 268  
 269          $error = EZ_ERROR_SHOP_OK;
 270          if ( $currencyCode === false )
 271              $currencyCode = eZShopFunctions::preferredCurrencyCode();
 272  
 273          include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
 274          $currency = eZCurrencyData::fetch( $currencyCode );
 275          if ( $currency )
 276          {
 277              if ( !$currency->isActive() )
 278              {
 279                  $error = EZ_ERROR_SHOP_PREFERRED_CURRENCY_INACTIVE;
 280                  eZDebug::writeWarning( "Currency '$currencyCode' is inactive.", 'eZShopFunctions::isPreferredCurrencyValid' );
 281              }
 282          }
 283          else
 284          {
 285              $error = EZ_ERROR_SHOP_PREFERRED_CURRENCY_DOESNOT_EXIST;
 286              eZDebug::writeWarning( "Currency '$currencyCode' doesn't exist", 'eZShopFunctions::isPreferredCurrencyValid' );
 287          }
 288  
 289          return $error;
 290      }
 291  
 292      /*!
 293       \static
 294      */
 295      function createCurrency( $currencyParams )
 296      {
 297          include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
 298          include_once ( 'kernel/shop/classes/ezmultipricedata.php' );
 299  
 300          $currency = eZCurrencyData::create( $currencyParams['code'],
 301                                              $currencyParams['symbol'],
 302                                              $currencyParams['locale'],
 303                                              '0.0000',
 304                                              $currencyParams['custom_rate_value'],
 305                                              $currencyParams['rate_factor'] );
 306          if ( is_object( $currency ) )
 307          {
 308              $db =& eZDB::instance();
 309              $db->begin();
 310  
 311              $currency->store();
 312              eZMultiPriceData::createPriceListForCurrency( $currencyParams['code'] );
 313  
 314              $db->commit();
 315          }
 316      }
 317  
 318      function removeCurrency( $currencyCodeList )
 319      {
 320          include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
 321          include_once ( 'kernel/shop/classes/ezmultipricedata.php' );
 322  
 323          $db =& eZDB::instance();
 324          $db->begin();
 325  
 326          eZCurrencyData::removeCurrencyList( $currencyCodeList );
 327          eZMultiPriceData::removePriceListForCurrency( $currencyCodeList );
 328  
 329          $db->commit();
 330      }
 331  
 332      function changeCurrency( $oldCurrencyCode, $newCurrencyCode )
 333      {
 334          $errCode = EZ_CURRENCYDATA_ERROR_OK;
 335  
 336          if ( strcmp( $oldCurrencyCode, $newCurrencyCode ) !== 0 )
 337          {
 338              include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
 339              include_once ( 'kernel/shop/classes/ezmultipricedata.php' );
 340  
 341              $errCode = eZCurrencyData::canCreate( $newCurrencyCode );
 342              if ( $errCode === EZ_CURRENCYDATA_ERROR_OK )
 343              {
 344                  $currency = eZCurrencyData::fetch( $oldCurrencyCode );
 345                  if ( is_object( $currency ) )
 346                  {
 347                      $db =& eZDB::instance();
 348                      $db->begin();
 349  
 350                      $currency->setAttribute( 'code', $newCurrencyCode );
 351                      $currency->sync();
 352  
 353                      eZMultiPriceData::changeCurrency( $oldCurrencyCode, $newCurrencyCode );
 354  
 355                      $db->commit();
 356                  }
 357                  else
 358                  {
 359                      $errCode = EZ_CURRENCYDATA_ERROR_UNKNOWN;
 360                  }
 361              }
 362          }
 363  
 364          return $errCode;
 365      }
 366  
 367      function updateAutoprices()
 368      {
 369          include_once ( 'kernel/shop/classes/ezmultipricedata.php' );
 370          return eZMultiPriceData::updateAutoprices();
 371      }
 372  
 373      function convertAdditionalPrice( $toCurrency, $value )
 374      {
 375          if ( $toCurrency == false )
 376              return $value;
 377  
 378          include_once ( 'kernel/shop/classes/ezcurrencyconverter.php' );
 379          $converter =& eZCurrencyConverter::instance();
 380          $converter->setRoundingType( EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_ROUND );
 381          $converter->setRoundingPrecision( 2 );
 382          $converter->setRoundingTarget( false );
 383  
 384          return $converter->convertFromLocaleCurrency( $toCurrency, $value, true );
 385      }
 386  
 387      function updateAutoRates()
 388      {
 389          include_once ( 'kernel/shop/errors.php' );
 390          include_once ( 'kernel/shop/classes/exchangeratehandlers/ezexchangeratesupdatehandler.php' );
 391  
 392          $error = array( 'code' => EZ_EXCHANGE_RATES_HANDLER_OK,
 393                          'description' => '' );
 394  
 395          $handler = eZExchangeRatesUpdateHandler::create();
 396          if ( $handler )
 397          {
 398              $error = $handler->requestRates();
 399              if ( $error['code'] === EZ_EXCHANGE_RATES_HANDLER_OK )
 400              {
 401                  $rateList = $handler->rateList();
 402                  if ( is_array( $rateList ) && count( $rateList ) > 0 )
 403                  {
 404                      $handlerBaseCurrency = $handler->baseCurrency();
 405                      if ( $handlerBaseCurrency )
 406                      {
 407                          $shopBaseCurrency = false;
 408                          $shopINI =& eZINI::instance( 'shop.ini' );
 409                          if ( $shopINI->hasVariable( 'ExchangeRatesSettings', 'BaseCurrency' ) )
 410                              $shopBaseCurrency = $shopINI->variable( 'ExchangeRatesSettings', 'BaseCurrency' );
 411  
 412                          if ( !$shopBaseCurrency )
 413                              $shopBaseCurrency = $handlerBaseCurrency;
 414  
 415                          // update rates for existing currencies
 416                          //$baseCurrencyCode = $handler->baseCurrency();
 417                          if ( isset( $rateList[$shopBaseCurrency] ) || ( $shopBaseCurrency === $handlerBaseCurrency ) )
 418                          {
 419                              // to avoid unnecessary multiplication set $crossBaseRate to 'false';
 420                              $crossBaseRate = false;
 421                              if ( $shopBaseCurrency !== $handlerBaseCurrency )
 422                              {
 423                                  $crossBaseRate = 1.0 / (float)$rateList[$shopBaseCurrency];
 424                                  $rateList[$handlerBaseCurrency] = '1.0000';
 425                              }
 426  
 427                              include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
 428                              $currencyList = eZCurrencyData::fetchList();
 429                              if ( is_array( $currencyList ) && count( $currencyList ) > 0 )
 430                              {
 431  
 432  
 433                                  foreach ( $currencyList as $currency )
 434                                  {
 435                                      $rateValue = false;
 436                                      $currencyCode = $currency->attribute( 'code' );
 437                                      if ( isset( $rateList[$currencyCode] ) )
 438                                      {
 439                                          $rateValue = $rateList[$currencyCode];
 440                                          if ( $crossBaseRate !== false )
 441                                              $rateValue *= $crossBaseRate;
 442                                      }
 443                                      else if ( $currencyCode === $shopBaseCurrency )
 444                                      {
 445                                          $rateValue = '1.0000';
 446                                      }
 447  
 448                                      $currency->setAttribute( 'auto_rate_value', $rateValue );
 449                                      $currency->sync();
 450                                  }
 451                              }
 452  
 453                              $error['code'] = EZ_EXCHANGE_RATES_HANDLER_OK;
 454                              $error['description'] = ezi18n( 'kernel/shop', "'Auto' rates were updated successfully." );
 455                          }
 456                          else
 457                          {
 458                              $error['code'] = EZ_EXCHANGE_RATES_HANDLER_INVALID_BASE_CROSS_RATE;
 459                              $error['description'] = ezi18n( 'kernel/shop', "Unable to calculate cross-rate for currency-pair '%1'/'%2'", null, array( $handlerBaseCurrency, $shopBaseCurrency ) );
 460                          }
 461                      }
 462                      else
 463                      {
 464                          $error['code'] = EZ_EXCHANGE_RATES_HANDLER_UNKNOWN_BASE_CURRENCY;
 465                          $error['description'] = ezi18n( 'kernel/shop', 'Unable to determine currency for retrieved rates.' );
 466                      }
 467                  }
 468                  else
 469                  {
 470                      $error['code'] = EZ_EXCHANGE_RATES_HANDLER_EMPTY_RATE_LIST;
 471                      $error['description'] = ezi18n( 'kernel/shop', 'Retrieved empty list of rates.' );
 472                  }
 473              }
 474          }
 475          else
 476          {
 477              $error['code'] = EZ_EXCHANGE_RATES_HANDLER_CANT_CREATE_HANDLER;
 478              $error['description'] = ezi18n( 'kernel/shop', 'Unable to create handler to update auto rates.' );
 479  
 480          }
 481  
 482          if ( $error['code'] !== EZ_EXCHANGE_RATES_HANDLER_OK )
 483          {
 484              eZDebug::writeError( $error['description'],
 485                                   'eZShopFunctions::updateAutoRates' );
 486          }
 487  
 488          return $error;
 489      }
 490  }
 491  
 492  ?>


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