|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZMultiPriceType class 00004 // 00005 // Created on: <04-Nov-2005 16:54:35 dl> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! 00032 \class eZMultiPriceType ezmultipricetype.php 00033 \ingroup eZMultiDatatype 00034 \brief Stores a price in multicurrency. 00035 00036 */ 00037 00038 //include_once( 'kernel/classes/ezdatatype.php' ); 00039 //include_once( 'kernel/classes/datatypes/ezmultiprice/ezmultiprice.php' ); 00040 //include_once( 'lib/ezutils/classes/ezstringutils.php' ); 00041 00042 class eZMultiPriceType extends eZDataType 00043 { 00044 const DATA_TYPE_STRING = 'ezmultiprice'; 00045 const DEFAULT_CURRENCY_CODE_FIELD = 'data_text1'; 00046 const DEFAULT_CURRENCY_CODE_VARIABLE = '_ezmultiprice_currency_code_'; 00047 const INCLUDE_VAT_FIELD = 'data_int1'; 00048 const INCLUDE_VAT_VARIABLE = '_ezmultiprice_include_vat_'; 00049 const VAT_ID_FIELD = 'data_float1'; 00050 const VAT_ID_VARIABLE = '_ezmultiprice_vat_id_'; 00051 const INCLUDED_VAT = 1; 00052 const EXCLUDED_VAT = 2; 00053 00054 function eZMultiPriceType() 00055 { 00056 $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', 'Multi-price', 'Datatype name' ), 00057 array( 'serialize_supported' => true ) ); 00058 } 00059 00060 /*! 00061 Validates the input and returns true if the input was 00062 valid for this datatype. 00063 */ 00064 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute ) 00065 { 00066 // Check "price inc/ex VAT" and "VAT type" fields. 00067 $vatTypeID = $http->postVariable( $base . '_ezmultiprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) ); 00068 $vatExInc = $http->postVariable( $base . '_ezmultiprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) ); 00069 00070 00071 if ( $vatExInc == 1 && $vatTypeID == -1 ) 00072 { 00073 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 00074 'Dynamic VAT cannot be included.' ) ); 00075 return eZInputValidator::STATE_INVALID; 00076 } 00077 00078 // Check price. 00079 if ( $http->hasPostVariable( $base . '_price_array_' . $contentObjectAttribute->attribute( "id" ) ) ) 00080 { 00081 $customPriceList = $http->postVariable( $base . '_price_array_' . $contentObjectAttribute->attribute( "id" ) ); 00082 foreach ( $customPriceList as $currencyCode => $value ) 00083 { 00084 if( $contentObjectAttribute->validateIsRequired() || ( $value != '' ) ) 00085 { 00086 if ( !preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $value ) ) 00087 { 00088 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 00089 "Invalid price for '%currencyCode' currency ", 00090 false, 00091 array( '%currencyCode' => $currencyCode ) ) ); 00092 return eZInputValidator::STATE_INVALID; 00093 } 00094 } 00095 } 00096 } 00097 00098 return eZInputValidator::STATE_ACCEPTED; 00099 } 00100 00101 function storeObjectAttribute( $attribute ) 00102 { 00103 $multiprice = $attribute->attribute( 'content' ); 00104 $multiprice->store(); 00105 } 00106 00107 /*! 00108 Set default class attribute value 00109 */ 00110 function initializeClassAttribute( $classAttribute ) 00111 { 00112 if ( $classAttribute->attribute( self::INCLUDE_VAT_FIELD ) == 0 ) 00113 $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, self::INCLUDED_VAT ); 00114 $classAttribute->store(); 00115 } 00116 00117 /*! 00118 Sets the default value. 00119 */ 00120 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute ) 00121 { 00122 if ( $currentVersion != false ) 00123 { 00124 $dataText = $originalContentObjectAttribute->attribute( 'data_text' ); 00125 $contentObjectAttribute->setAttribute( "data_text", $dataText ); 00126 } 00127 } 00128 00129 /*! 00130 Set default object attribute value. 00131 */ 00132 function postInitializeObjectAttribute( $objectAttribute, $currentVersion, $originalContentObjectAttribute ) 00133 { 00134 $contentClassAttribute = $objectAttribute->contentClassAttribute(); 00135 $multiprice = new eZMultiPrice( $contentClassAttribute, $objectAttribute ); 00136 00137 if ( $currentVersion == false ) 00138 { 00139 $defaultCurrency = $contentClassAttribute->attribute( self::DEFAULT_CURRENCY_CODE_FIELD ); 00140 $multiprice->setCustomPrice( $defaultCurrency, '0.00' ); 00141 $multiprice->updateAutoPriceList(); 00142 $multiprice->store(); 00143 } 00144 else 00145 { 00146 $originalMultiprice = $originalContentObjectAttribute->content(); 00147 $multiprice = new eZMultiPrice( $contentClassAttribute, $objectAttribute ); 00148 00149 foreach ( $originalMultiprice->priceList() as $price ) 00150 { 00151 $multiprice->setPriceByCurrency( $price->attribute( 'currency_code' ), $price->attribute( 'value' ), $price->attribute( 'type') ); 00152 } 00153 00154 $multiprice->store(); 00155 } 00156 } 00157 00158 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute ) 00159 { 00160 $currencyCodeVariable = $base . self::DEFAULT_CURRENCY_CODE_VARIABLE . $classAttribute->attribute( 'id' ); 00161 if ( $http->hasPostVariable( $currencyCodeVariable ) ) 00162 { 00163 $currencyCode = $http->postVariable( $currencyCodeVariable ); 00164 $classAttribute->setAttribute( self::DEFAULT_CURRENCY_CODE_FIELD, $currencyCode ); 00165 } 00166 00167 $isVatIncludedVariable = $base . self::INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' ); 00168 if ( $http->hasPostVariable( $isVatIncludedVariable ) ) 00169 { 00170 $isVatIncluded = $http->postVariable( $isVatIncludedVariable ); 00171 $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $isVatIncluded ); 00172 } 00173 $vatIDVariable = $base . self::VAT_ID_VARIABLE . $classAttribute->attribute( 'id' ); 00174 if ( $http->hasPostVariable( $vatIDVariable ) ) 00175 { 00176 $vatID = $http->postVariable( $vatIDVariable ); 00177 $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID ); 00178 } 00179 return true; 00180 } 00181 00182 /*! 00183 Fetches the http post var integer input and stores it in the data instance. 00184 */ 00185 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute ) 00186 { 00187 $multiprice = $contentObjectAttribute->attribute( 'content' ); 00188 00189 $priceArrayName = $base . "_price_array_" . $contentObjectAttribute->attribute( "id" ); 00190 if ( $http->hasPostVariable( $priceArrayName ) ) 00191 { 00192 $customPriceList = $http->postVariable( $priceArrayName ); 00193 00194 foreach ( $customPriceList as $currencyCode => $value ) 00195 $multiprice->setCustomPrice( $currencyCode, $value ); 00196 } 00197 00198 $multiprice->updateAutoPriceList(); 00199 00200 $vatType = $http->postVariable( $base . '_ezmultiprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) ); 00201 $vatExInc = $http->postVariable( $base . '_ezmultiprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) ); 00202 $multiprice->setAttribute( 'selected_vat_type', $vatType ); 00203 $multiprice->setAttribute( 'is_vat_included', $vatExInc ); 00204 00205 $data_text = $vatType . ',' . $vatExInc; 00206 $contentObjectAttribute->setAttribute( 'data_text', $data_text ); 00207 00208 return true; 00209 } 00210 00211 /*! 00212 Returns the content. 00213 */ 00214 function objectAttributeContent( $contentObjectAttribute ) 00215 { 00216 $classAttribute = $contentObjectAttribute->contentClassAttribute(); 00217 $multiprice = new eZMultiPrice( $classAttribute, $contentObjectAttribute ); 00218 00219 if ( $contentObjectAttribute->attribute( 'data_text' ) != '' ) 00220 { 00221 list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( 'data_text' ), 2 ); 00222 00223 $multiprice->setAttribute( 'selected_vat_type', $vatType ); 00224 $multiprice->setAttribute( 'is_vat_included', $vatExInc ); 00225 } 00226 00227 return $multiprice; 00228 } 00229 00230 /*! 00231 Returns class content. 00232 */ 00233 function classAttributeContent( $classAttribute ) 00234 { 00235 $contentObjectAttribute = false; 00236 $multiprice = new eZMultiPrice( $classAttribute, $contentObjectAttribute ); 00237 return $multiprice; 00238 } 00239 00240 function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters ) 00241 { 00242 switch ( $action ) 00243 { 00244 case 'set_custom_price' : 00245 { 00246 $selectedCurrencyName = 'ContentObjectAttribute' . '_selected_currency_' . $contentObjectAttribute->attribute( 'id' ); 00247 if ( $http->hasPostVariable( $selectedCurrencyName ) ) 00248 { 00249 $selectedCurrency = $http->postVariable( $selectedCurrencyName ); 00250 $multiprice = $contentObjectAttribute->content(); 00251 00252 // to keep right order of currency after adding we do 'remove' and 'add' 00253 // instead of just '$multiprice->setCustomPrice( $currencyCode, false )' 00254 $price = $multiprice->priceByCurrency( $selectedCurrency ); 00255 $multiprice->removePriceByCurrency( $selectedCurrency ); 00256 $multiprice->setCustomPrice( $selectedCurrency, $price->attribute( 'value' ) ); 00257 00258 $multiprice->store(); 00259 } 00260 }break; 00261 00262 case 'remove_prices' : 00263 { 00264 $removePriceArrayName = 'ContentObjectAttribute' . '_remove_price_array_' . $contentObjectAttribute->attribute( 'id' ); 00265 if ( $http->hasPostVariable( $removePriceArrayName ) ) 00266 { 00267 $removePriceArray = $http->postVariable( $removePriceArrayName ); 00268 $multiprice = $contentObjectAttribute->content(); 00269 00270 foreach( $removePriceArray as $currencyCode => $value ) 00271 $multiprice->setAutoPrice( $currencyCode, false ); 00272 00273 $multiprice->updateAutoPriceList(); 00274 $multiprice->store(); 00275 } 00276 }break; 00277 00278 default : 00279 { 00280 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZMultiPriceType' ); 00281 }break; 00282 } 00283 } 00284 00285 function contentActionList( $classAttribute ) 00286 { 00287 return array( array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to basket' ), 00288 'action' => 'ActionAddToBasket' 00289 ), 00290 array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to wish list' ), 00291 'action' => 'ActionAddToWishList' 00292 ) ); 00293 } 00294 00295 /*! 00296 Clean up stored object attribute 00297 */ 00298 function deleteStoredObjectAttribute( $objectAttribute, $version = null ) 00299 { 00300 eZMultiPrice::removeByID( $objectAttribute->attribute( 'id' ), $version ); 00301 } 00302 00303 function title( $contentObjectAttribute, $name = null ) 00304 { 00305 return ''; 00306 } 00307 00308 function hasObjectAttributeContent( $contentObjectAttribute ) 00309 { 00310 return true; 00311 } 00312 00313 function toString( $contentObjectAttribute ) 00314 { 00315 00316 $multiprice = $contentObjectAttribute->attribute( 'content' ); 00317 00318 $priceList = $multiprice->attribute( 'price_list' ); 00319 00320 $priceArray = explode( ',', $contentObjectAttribute->attribute( 'data_text' ) ); 00321 foreach ( $priceList as $priceData ) 00322 { 00323 $type = $priceData->attribute( 'type' ); 00324 if ( $type == 1 ) 00325 { 00326 $type = 'CUSTOM'; 00327 } 00328 else if ( $type == 2 ) 00329 { 00330 $type = 'AUTO'; 00331 } 00332 else 00333 $type = 'LIMIT'; 00334 $priceArray = array_merge( $priceArray, array( $priceData->attribute( 'currency_code'), $priceData->attribute( 'value' ), $type ) ); 00335 } 00336 return eZStringUtils::implodeStr( $priceArray, '|' ); 00337 } 00338 00339 00340 function fromString( $contentObjectAttribute, $string ) 00341 { 00342 if ( $string == '' ) 00343 return true; 00344 00345 $multiprice = $contentObjectAttribute->attribute( 'content' ); 00346 00347 $multipriceData = eZStringUtils::explodeSTR( $string, '|' ); 00348 00349 $vatType = array_shift( $multipriceData ); 00350 $vatExInc = array_shift( $multipriceData ); 00351 00352 $contentObjectAttribute->setAttribute( 'data_text', $vatType . ',' . $vatExInc ); 00353 00354 while ( $multipriceData ) 00355 { 00356 $currencyCode = array_shift( $multipriceData ); 00357 $value = array_shift( $multipriceData ); 00358 00359 $type = array_shift( $multipriceData ); 00360 if ( $type == 'CUSTOM' ) 00361 { 00362 $type = 1; 00363 } 00364 else if ( $type == 'AUTO' ) 00365 { 00366 $type = 2; 00367 } 00368 else 00369 $type = 5000; 00370 00371 $multiprice->setPriceByCurrency( $currencyCode, $value, $type ); 00372 00373 } 00374 $multiprice->store(); 00375 return $multiprice; 00376 00377 } 00378 00379 /*! 00380 \reimp 00381 */ 00382 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode ) 00383 { 00384 $price = $classAttribute->content(); 00385 if ( $price ) 00386 { 00387 $vatIncluded = $price->attribute( 'is_vat_included' ); 00388 $vatTypes = $price->attribute( 'vat_type' ); 00389 00390 $dom = $attributeParametersNode->ownerDocument; 00391 $vatIncludedNode = $dom->createElement( 'vat-included' ); 00392 $vatIncludedNode->setAttribute( 'is-set', $vatIncluded ? 'true' : 'false' ); 00393 $attributeParametersNode->appendChild( $vatIncludedNode ); 00394 $vatTypeNode = $dom->createElement( 'vat-type' ); 00395 $chosenVatType = $classAttribute->attribute( self::VAT_ID_FIELD ); 00396 $gotVat = false; 00397 foreach ( $vatTypes as $vatType ) 00398 { 00399 $id = $vatType->attribute( 'id' ); 00400 if ( $id == $chosenVatType ) 00401 { 00402 $vatTypeNode->setAttribute( 'name', $vatType->attribute( 'name' ) ); 00403 $vatTypeNode->setAttribute( 'percentage', $vatType->attribute( 'percentage' ) ); 00404 $gotVat = true; 00405 break; 00406 } 00407 } 00408 if ( $gotVat ) 00409 $attributeParametersNode->appendChild( $vatTypeNode ); 00410 00411 $defualtCurrency = $classAttribute->attribute( self::DEFAULT_CURRENCY_CODE_FIELD ); 00412 $defaultCurrencyNode = $dom->createElement( 'default-currency' ); 00413 $defaultCurrencyNode->setAttribute( 'code', $defualtCurrency ); 00414 $attributeParametersNode->appendChild( $defaultCurrencyNode ); 00415 } 00416 } 00417 00418 /*! 00419 \reimp 00420 */ 00421 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode ) 00422 { 00423 $vatNode = $attributeParametersNode->getElementsByTagName( 'vat-included' )->item( 0 ); 00424 $vatIncluded = strtolower( $vatNode->getAttribute( 'is-set' ) ) == 'true'; 00425 if ( $vatIncluded ) 00426 $vatIncluded = self::INCLUDED_VAT; 00427 else 00428 $vatIncluded = self::EXCLUDED_VAT; 00429 00430 $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $vatIncluded ); 00431 $vatTypeNode = $attributeParametersNode->getElementsByTagName( 'vat-type' )->item( 0 ); 00432 $vatName = $vatTypeNode->getAttribute( 'name' ); 00433 $vatPercentage = $vatTypeNode->getAttribute( 'percentage' ); 00434 $vatID = false; 00435 $vatTypes = eZVatType::fetchList(); 00436 foreach ( $vatTypes as $vatType ) 00437 { 00438 if ( $vatType->attribute( 'name' ) == $vatName and 00439 $vatType->attribute( 'percentage' ) == $vatPercentage ) 00440 { 00441 $vatID = $vatType->attribute( 'id' ); 00442 break; 00443 } 00444 } 00445 if ( !$vatID ) 00446 { 00447 $vatType = eZVatType::create(); 00448 $vatType->setAttribute( 'name', $vatName ); 00449 $vatType->setAttribute( 'percentage', $vatPercentage ); 00450 $vatType->store(); 00451 $vatID = $vatType->attribute( 'id' ); 00452 } 00453 $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID ); 00454 00455 $defaultCurrency = $attributeParametersNode->getElementsByTagName( 'default-currency' )->item( 0 ); 00456 $currencyCode = $defaultCurrency->getAttribute( 'code' ); 00457 $classAttribute->setAttribute( self::DEFAULT_CURRENCY_CODE_FIELD, $currencyCode ); 00458 } 00459 00460 00461 /*! 00462 \reimp 00463 */ 00464 function serializeContentObjectAttribute( $package, $objectAttribute ) 00465 { 00466 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute ); 00467 00468 $multiprice = $objectAttribute->content(); 00469 $domDocument = $multiprice->DOMDocument(); 00470 00471 $importedRoot = $node->ownerDocument->importNode( $domDocument->documentElement, true ); 00472 $node->appendChild( $importedRoot ); 00473 00474 return $node; 00475 } 00476 00477 /*! 00478 \reimp 00479 */ 00480 function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode ) 00481 { 00482 $rootNode = $attributeNode->getElementsByTagName( 'ezmultiprice' )->item( 0 ); 00483 00484 $multiprice = $objectAttribute->content(); 00485 $multiprice->decodeDOMTree( $rootNode ); 00486 } 00487 00488 function customSorting() 00489 { 00490 return true; 00491 } 00492 00493 function customSortingSQL( $params ) 00494 { 00495 $multipriceTableAlias = "mp"; 00496 00497 if ( isset( $params['table_alias_suffix'] ) ) 00498 $multipriceTableAlias .= $params['table_alias_suffix']; 00499 00500 $sql = array( 'from' => '', 00501 'where' => '', 00502 'sorting_field' => '' ); 00503 00504 $sql['from'] = "ezmultipricedata $multipriceTableAlias"; 00505 00506 $and = ''; 00507 if ( isset( $params['contentobject_attr_id'] ) ) 00508 { 00509 $sql['where'] = " 00510 $multipriceTableAlias.contentobject_attr_id = {$params['contentobject_attr_id']}"; 00511 $and = ' AND'; 00512 } 00513 00514 if ( isset( $params['contentobject_attr_version'] ) ) 00515 { 00516 $sql['where'] .= " 00517 $and $multipriceTableAlias.contentobject_attr_version = {$params['contentobject_attr_version']}"; 00518 $and = ' AND'; 00519 } 00520 00521 if ( !isset( $params['currency_code'] ) ) 00522 { 00523 //include_once( 'kernel/shop/classes/ezshopfunctions.php' ); 00524 $params['currency_code'] = eZShopFunctions::preferredCurrencyCode(); 00525 } 00526 00527 if ( $params['currency_code'] !== false ) 00528 { 00529 $sql['where'] .= " 00530 $and $multipriceTableAlias.currency_code = '{$params['currency_code']}'"; 00531 $and = ' AND'; 00532 } 00533 00534 $sql['sorting_field'] = "$multipriceTableAlias.value"; 00535 00536 return $sql; 00537 } 00538 00539 /*! 00540 \reimp 00541 */ 00542 function diff( $old, $new, $options = false ) 00543 { 00544 return null; 00545 } 00546 } 00547 00548 eZDataType::register( eZMultiPriceType::DATA_TYPE_STRING, "eZMultiPriceType" ); 00549 00550 ?>