eZ Publish  [trunk]
ezshopfunctions.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZShopFunctions 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 eZShopFunctions
00012 {
00013     function eZShopFunctions()
00014     {
00015     }
00016 
00017     /*!
00018      \static
00019     */
00020     static function isProductClass( $contentClass )
00021     {
00022         $type = eZShopFunctions::productTypeByClass( $contentClass );
00023         return ( $type !== false );
00024     }
00025 
00026     /*!
00027      \static
00028     */
00029     static function isProductObject( $contentObject )
00030     {
00031         $type = eZShopFunctions::productTypeByObject( $contentObject );
00032         return ( $type !== false );
00033     }
00034 
00035     static function isSimplePriceClass( $contentClass )
00036     {
00037         $type = eZShopFunctions::productTypeByClass( $contentClass );
00038         return eZShopFunctions::isSimplePriceProductType( $type );
00039 
00040     }
00041 
00042     static function isSimplePriceProductType( $type )
00043     {
00044         return ( $type === 'ezprice' );
00045     }
00046 
00047     static function isMultiPriceClass( $contentClass )
00048     {
00049         $type = eZShopFunctions::productTypeByClass( $contentClass );
00050         return eZShopFunctions::isMultiPriceProductType( $type );
00051     }
00052 
00053     static function isMultiPriceProductType( $type )
00054     {
00055         return ( $type === 'ezmultiprice' );
00056     }
00057 
00058     /*!
00059      \static
00060     */
00061     static function productTypeByClass( $contentClass )
00062     {
00063         $type = false;
00064 
00065         if ( is_object( $contentClass ) )
00066         {
00067             $classAttributes = $contentClass->fetchAttributes();
00068             foreach ( $classAttributes as $classAttribute )
00069             {
00070                 $dataType = $classAttribute->attribute( 'data_type_string' );
00071                 if ( eZShopFunctions::isProductDatatype( $dataType ) )
00072                 {
00073                     return $dataType;
00074                 }
00075             }
00076         }
00077 
00078         return $type;
00079     }
00080 
00081     /*!
00082      \static
00083     */
00084     static function productTypeByObject( $contentObject )
00085     {
00086         if ( is_object( $contentObject ) )
00087         {
00088             $attributes = $contentObject->contentObjectAttributes();
00089             foreach ( $attributes as $attribute )
00090             {
00091                 $dataType = $attribute->dataType();
00092                 if ( eZShopFunctions::isProductDatatype( $dataType->isA() ) )
00093                 {
00094                     return $dataType->isA();
00095                 }
00096             }
00097         }
00098 
00099         return false;
00100     }
00101 
00102     /*!
00103      \static
00104     */
00105     static function isProductDatatype( $dataTypeString )
00106     {
00107         return in_array( $dataTypeString, eZShopFunctions::productDatatypeStringList() );
00108     }
00109 
00110     /*!
00111      \static
00112     */
00113     static function productDatatypeStringList()
00114     {
00115         return array( 'ezprice',
00116                       'ezmultiprice' );
00117     }
00118 
00119     static function productClassList()
00120     {
00121         $productClassList = array();
00122         $classList = eZContentClass::fetchList();
00123         foreach ( $classList as $class )
00124         {
00125             if ( eZShopFunctions::isProductClass( $class ) )
00126             {
00127                 $productClassList[] = $class;
00128             }
00129         }
00130 
00131         return $productClassList;
00132     }
00133 
00134     static function priceAttributeIdentifier( $productClass )
00135     {
00136         $identifier = '';
00137         $classAttribute = eZShopFunctions::priceAttribute( $productClass );
00138         if ( is_object( $classAttribute ) )
00139             $identifier = $classAttribute->attribute( 'identifier' );
00140 
00141         return $identifier;
00142     }
00143 
00144     static function priceAttribute( $productClass )
00145     {
00146         if ( is_object( $productClass ) )
00147         {
00148             foreach ( $productClass->fetchAttributes() as $classAttribute )
00149             {
00150                 $dataType = $classAttribute->attribute( 'data_type_string' );
00151                 if ( eZShopFunctions::isProductDatatype( $dataType ) )
00152                 {
00153                     return $classAttribute;
00154                 }
00155             }
00156         }
00157 
00158         return false;
00159     }
00160 
00161     /*!
00162      \static
00163     */
00164     static function preferredCurrencyCode()
00165     {
00166         if( !$currencyCode = eZPreferences::value( 'user_preferred_currency' ) )
00167         {
00168             $ini = eZINI::instance( 'shop.ini' );
00169             $currencyCode = $ini->variable( 'CurrencySettings', 'PreferredCurrency' );
00170         }
00171         return $currencyCode;
00172     }
00173 
00174     /*!
00175      \static
00176     */
00177     static function setPreferredCurrencyCode( $currencyCode )
00178     {
00179         $error = eZShopFunctions::isPreferredCurrencyValid( $currencyCode );
00180         if ( $error === eZError::SHOP_OK )
00181         {
00182             eZPreferences::setValue( 'user_preferred_currency', $currencyCode );
00183         }
00184 
00185         return $error;
00186     }
00187 
00188     /*!
00189      Get country stored in user object.
00190      \static
00191     */
00192     static function getUserCountry()
00193     {
00194         return eZVATManager::getUserCountry( false, false );
00195     }
00196 
00197 
00198     /*!
00199      Get country stored in user preferences.
00200      \static
00201     */
00202     static function getPreferredUserCountry()
00203     {
00204         return eZPreferences::value( 'user_preferred_country' );
00205     }
00206 
00207     /*!
00208      Store country to user preferences.
00209      \static
00210     */
00211     static function setPreferredUserCountry( $country )
00212     {
00213         eZPreferences::setValue( 'user_preferred_country', $country );
00214 
00215         return eZError::SHOP_OK;
00216     }
00217 
00218     /*!
00219      \static
00220     */
00221     static function isPreferredCurrencyValid( $currencyCode = false )
00222     {
00223         $error = eZError::SHOP_OK;
00224         if ( $currencyCode === false )
00225             $currencyCode = eZShopFunctions::preferredCurrencyCode();
00226 
00227         $currency = eZCurrencyData::fetch( $currencyCode );
00228         if ( $currency )
00229         {
00230             if ( !$currency->isActive() )
00231             {
00232                 $error = eZError::SHOP_PREFERRED_CURRENCY_INACTIVE;
00233                 eZDebug::writeWarning( "Currency '$currencyCode' is inactive.", __METHOD__ );
00234             }
00235         }
00236         else
00237         {
00238             $error = eZError::SHOP_PREFERRED_CURRENCY_DOESNOT_EXIST;
00239             eZDebug::writeWarning( "Currency '$currencyCode' doesn't exist", __METHOD__ );
00240         }
00241 
00242         return $error;
00243     }
00244 
00245     /*!
00246      \static
00247     */
00248     static function createCurrency( $currencyParams )
00249     {
00250         $currency = eZCurrencyData::create( $currencyParams['code'],
00251                                             $currencyParams['symbol'],
00252                                             $currencyParams['locale'],
00253                                             '0.0000',
00254                                             $currencyParams['custom_rate_value'],
00255                                             $currencyParams['rate_factor'] );
00256         if ( is_object( $currency ) )
00257         {
00258             $db = eZDB::instance();
00259             $db->begin();
00260 
00261             $currency->store();
00262             eZMultiPriceData::createPriceListForCurrency( $currencyParams['code'] );
00263 
00264             $db->commit();
00265         }
00266     }
00267 
00268     static function removeCurrency( $currencyCodeList )
00269     {
00270         $db = eZDB::instance();
00271         $db->begin();
00272 
00273         eZCurrencyData::removeCurrencyList( $currencyCodeList );
00274         eZMultiPriceData::removePriceListForCurrency( $currencyCodeList );
00275 
00276         $db->commit();
00277     }
00278 
00279     static function changeCurrency( $oldCurrencyCode, $newCurrencyCode )
00280     {
00281         $errCode = eZCurrencyData::ERROR_OK;
00282 
00283         if ( strcmp( $oldCurrencyCode, $newCurrencyCode ) !== 0 )
00284         {
00285             $errCode = eZCurrencyData::canCreate( $newCurrencyCode );
00286             if ( $errCode === eZCurrencyData::ERROR_OK )
00287             {
00288                 $currency = eZCurrencyData::fetch( $oldCurrencyCode );
00289                 if ( is_object( $currency ) )
00290                 {
00291                     $db = eZDB::instance();
00292                     $db->begin();
00293 
00294                     $currency->setAttribute( 'code', $newCurrencyCode );
00295                     $currency->sync();
00296 
00297                     eZMultiPriceData::changeCurrency( $oldCurrencyCode, $newCurrencyCode );
00298 
00299                     $db->commit();
00300                 }
00301                 else
00302                 {
00303                     $errCode = eZCurrencyData::ERROR_UNKNOWN;
00304                 }
00305             }
00306         }
00307 
00308         return $errCode;
00309     }
00310 
00311     static function updateAutoprices()
00312     {
00313         return eZMultiPriceData::updateAutoprices();
00314     }
00315 
00316     static function convertAdditionalPrice( $toCurrency, $value )
00317     {
00318         if ( $toCurrency == false )
00319             return $value;
00320 
00321         $converter = eZCurrencyConverter::instance();
00322         $converter->setRoundingType( eZCurrencyConverter::ROUNDING_TYPE_ROUND );
00323         $converter->setRoundingPrecision( 2 );
00324         $converter->setRoundingTarget( false );
00325 
00326         return $converter->convertFromLocaleCurrency( $toCurrency, $value, true );
00327     }
00328 
00329     static function updateAutoRates()
00330     {
00331         $error = array( 'code' => eZExchangeRatesUpdateHandler::OK,
00332                         'description' => '' );
00333 
00334         $handler = eZExchangeRatesUpdateHandler::create();
00335         if ( $handler )
00336         {
00337             $error = $handler->requestRates();
00338             if ( $error['code'] === eZExchangeRatesUpdateHandler::OK )
00339             {
00340                 $rateList = $handler->rateList();
00341                 if ( is_array( $rateList ) && count( $rateList ) > 0 )
00342                 {
00343                     $handlerBaseCurrency = $handler->baseCurrency();
00344                     if ( $handlerBaseCurrency )
00345                     {
00346                         $shopBaseCurrency = false;
00347                         $shopINI = eZINI::instance( 'shop.ini' );
00348                         if ( $shopINI->hasVariable( 'ExchangeRatesSettings', 'BaseCurrency' ) )
00349                             $shopBaseCurrency = $shopINI->variable( 'ExchangeRatesSettings', 'BaseCurrency' );
00350 
00351                         if ( !$shopBaseCurrency )
00352                             $shopBaseCurrency = $handlerBaseCurrency;
00353 
00354                         // update rates for existing currencies
00355                         //$baseCurrencyCode = $handler->baseCurrency();
00356                         if ( isset( $rateList[$shopBaseCurrency] ) || ( $shopBaseCurrency === $handlerBaseCurrency ) )
00357                         {
00358                             // to avoid unnecessary multiplication set $crossBaseRate to 'false';
00359                             $crossBaseRate = false;
00360                             if ( $shopBaseCurrency !== $handlerBaseCurrency )
00361                             {
00362                                 $crossBaseRate = 1.0 / (float)$rateList[$shopBaseCurrency];
00363                                 $rateList[$handlerBaseCurrency] = '1.0000';
00364                             }
00365 
00366                             $currencyList = eZCurrencyData::fetchList();
00367                             if ( is_array( $currencyList ) && count( $currencyList ) > 0 )
00368                             {
00369 
00370 
00371                                 foreach ( $currencyList as $currency )
00372                                 {
00373                                     $rateValue = false;
00374                                     $currencyCode = $currency->attribute( 'code' );
00375                                     if ( isset( $rateList[$currencyCode] ) )
00376                                     {
00377                                         $rateValue = $rateList[$currencyCode];
00378                                         if ( $crossBaseRate !== false )
00379                                             $rateValue *= $crossBaseRate;
00380                                     }
00381                                     else if ( $currencyCode === $shopBaseCurrency )
00382                                     {
00383                                         $rateValue = '1.0000';
00384                                     }
00385 
00386                                     $currency->setAttribute( 'auto_rate_value', $rateValue );
00387                                     $currency->sync();
00388                                 }
00389                             }
00390 
00391                             $error['code'] = eZExchangeRatesUpdateHandler::OK;
00392                             $error['description'] = ezpI18n::tr( 'kernel/shop', "'Auto' rates were updated successfully." );
00393                         }
00394                         else
00395                         {
00396                             $error['code'] = eZExchangeRatesUpdateHandler::INVALID_BASE_CROSS_RATE;
00397                             $error['description'] = ezpI18n::tr( 'kernel/shop', "Unable to calculate cross-rate for currency-pair '%1'/'%2'", null, array( $handlerBaseCurrency, $shopBaseCurrency ) );
00398                         }
00399                     }
00400                     else
00401                     {
00402                         $error['code'] = eZExchangeRatesUpdateHandler::UNKNOWN_BASE_CURRENCY;
00403                         $error['description'] = ezpI18n::tr( 'kernel/shop', 'Unable to determine currency for retrieved rates.' );
00404                     }
00405                 }
00406                 else
00407                 {
00408                     $error['code'] = eZExchangeRatesUpdateHandler::EMPTY_RATE_LIST;
00409                     $error['description'] = ezpI18n::tr( 'kernel/shop', 'Retrieved empty list of rates.' );
00410                 }
00411             }
00412         }
00413         else
00414         {
00415             $error['code'] = eZExchangeRatesUpdateHandler::CANT_CREATE_HANDLER;
00416             $error['description'] = ezpI18n::tr( 'kernel/shop', 'Unable to create handler to update auto rates.' );
00417 
00418         }
00419 
00420         if ( $error['code'] !== eZExchangeRatesUpdateHandler::OK )
00421         {
00422             eZDebug::writeError( $error['description'], __METHOD__ );
00423         }
00424 
00425         return $error;
00426     }
00427 }
00428 
00429 ?>