00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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 define( 'EZ_DATATYPESTRING_MULTIPRICE', 'ezmultiprice' );
00042 define( 'EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD', 'data_text1' );
00043 define( 'EZ_DATATYPESTRING_CURRENCY_CODE_VARIABLE', '_ezmultiprice_currency_code_' );
00044 define( 'EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD', 'data_int1' );
00045 define( 'EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_VARIABLE', '_ezmultiprice_include_vat_' );
00046 define( 'EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD', 'data_float1' );
00047 define( 'EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_VARIABLE', '_ezmultiprice_vat_id_' );
00048 define( 'EZ_MULTIPRICE_INCLUDED_VAT', 1 );
00049 define( 'EZ_MULTIPRICE_EXCLUDED_VAT', 2 );
00050
00051 class eZMultiPriceType extends eZDataType
00052 {
00053 function eZMultiPriceType()
00054 {
00055 $this->eZDataType( EZ_DATATYPESTRING_MULTIPRICE, ezi18n( 'kernel/classes/datatypes', 'Multi-price', 'Datatype name' ),
00056 array( 'serialize_supported' => true ) );
00057 }
00058
00059
00060
00061
00062
00063 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00064 {
00065
00066 $vatTypeID = $http->postVariable( $base . '_ezmultiprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
00067 $vatExInc = $http->postVariable( $base . '_ezmultiprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
00068
00069
00070 if ( $vatExInc == 1 && $vatTypeID == -1 )
00071 {
00072 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00073 'Dynamic VAT cannot be included.' ) );
00074 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00075 }
00076
00077
00078 if ( $http->hasPostVariable( $base . '_price_array_' . $contentObjectAttribute->attribute( "id" ) ) )
00079 {
00080 $customPriceList = $http->postVariable( $base . '_price_array_' . $contentObjectAttribute->attribute( "id" ) );
00081 foreach ( $customPriceList as $currencyCode => $value )
00082 {
00083 if( $contentObjectAttribute->validateIsRequired() || ( $value != '' ) )
00084 {
00085 if ( !preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $value ) )
00086 {
00087 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00088 "Invalid price for '%currencyCode' currency ",
00089 false,
00090 array( '%currencyCode' => $currencyCode ) ) );
00091 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00092 }
00093 }
00094 }
00095 }
00096
00097 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00098 }
00099
00100 function storeObjectAttribute( &$attribute )
00101 {
00102 $multiprice =& $attribute->attribute( 'content' );
00103 $multiprice->store();
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 function initializeClassAttribute( &$classAttribute )
00118 {
00119 if ( $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD ) == 0 )
00120 $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD, EZ_MULTIPRICE_INCLUDED_VAT );
00121 $classAttribute->store();
00122 }
00123
00124
00125
00126
00127 function postInitializeObjectAttribute( &$objectAttribute, $currentVersion, &$originalContentObjectAttribute )
00128 {
00129 $contentClassAttribute =& $objectAttribute->contentClassAttribute();
00130 $multiprice = new eZMultiPrice( $contentClassAttribute, $objectAttribute );
00131
00132 if ( $currentVersion == false )
00133 {
00134 $defaultCurrency = $contentClassAttribute->attribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD );
00135 $multiprice->setCustomPrice( $defaultCurrency, '0.00' );
00136 $multiprice->updateAutoPriceList();
00137 $multiprice->store();
00138 }
00139 else
00140 {
00141 $originalMultiprice =& $originalContentObjectAttribute->content();
00142 $multiprice = new eZMultiPrice( $contentClassAttribute, $objectAttribute );
00143
00144 $priceList =& $originalMultiprice->priceList();
00145 foreach ( $priceList as $price )
00146 $multiprice->setPriceByCurrency( $price->attribute( 'currency_code' ), $price->attribute( 'value' ), $price->attribute( 'type') );
00147
00148 $multiprice->store();
00149 }
00150 }
00151
00152 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00153 {
00154 $currencyCodeVariable = $base . EZ_DATATYPESTRING_CURRENCY_CODE_VARIABLE . $classAttribute->attribute( 'id' );
00155 if ( $http->hasPostVariable( $currencyCodeVariable ) )
00156 {
00157 $currencyCode = $http->postVariable( $currencyCodeVariable );
00158 $classAttribute->setAttribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD, $currencyCode );
00159 }
00160
00161 $isVatIncludedVariable = $base . EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' );
00162 if ( $http->hasPostVariable( $isVatIncludedVariable ) )
00163 {
00164 $isVatIncluded = $http->postVariable( $isVatIncludedVariable );
00165 $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD, $isVatIncluded );
00166 }
00167 $vatIDVariable = $base . EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_VARIABLE . $classAttribute->attribute( 'id' );
00168 if ( $http->hasPostVariable( $vatIDVariable ) )
00169 {
00170 $vatID = $http->postVariable( $vatIDVariable );
00171 $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD, $vatID );
00172 }
00173 return true;
00174 }
00175
00176
00177
00178
00179 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00180 {
00181 $multiprice =& $contentObjectAttribute->attribute( 'content' );
00182
00183 $priceArrayName = $base . "_price_array_" . $contentObjectAttribute->attribute( "id" );
00184 if ( $http->hasPostVariable( $priceArrayName ) )
00185 {
00186 $customPriceList = $http->postVariable( $priceArrayName );
00187
00188 foreach ( $customPriceList as $currencyCode => $value )
00189 $multiprice->setCustomPrice( $currencyCode, $value );
00190 }
00191
00192 $multiprice->updateAutoPriceList();
00193
00194 $vatType = $http->postVariable( $base . '_ezmultiprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
00195 $vatExInc = $http->postVariable( $base . '_ezmultiprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
00196 $multiprice->setAttribute( 'selected_vat_type', $vatType );
00197 $multiprice->setAttribute( 'is_vat_included', $vatExInc );
00198
00199 $data_text = $vatType . ',' . $vatExInc;
00200 $contentObjectAttribute->setAttribute( 'data_text', $data_text );
00201
00202 return true;
00203 }
00204
00205
00206
00207
00208 function &objectAttributeContent( &$contentObjectAttribute )
00209 {
00210 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00211 $multiprice = new eZMultiPrice( $classAttribute, $contentObjectAttribute );
00212
00213 if ( $contentObjectAttribute->attribute( 'data_text' ) != '' )
00214 {
00215 list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( 'data_text' ), 2 );
00216
00217 $multiprice->setAttribute( 'selected_vat_type', $vatType );
00218 $multiprice->setAttribute( 'is_vat_included', $vatExInc );
00219 }
00220
00221 return $multiprice;
00222 }
00223
00224
00225
00226
00227 function &classAttributeContent( &$classAttribute )
00228 {
00229 $contentObjectAttribute = false;
00230 $multiprice = new eZMultiPrice( $classAttribute, $contentObjectAttribute );
00231 return $multiprice;
00232 }
00233
00234 function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
00235 {
00236 switch ( $action )
00237 {
00238 case 'set_custom_price' :
00239 {
00240 $selectedCurrencyName = 'ContentObjectAttribute' . '_selected_currency_' . $contentObjectAttribute->attribute( 'id' );
00241 if ( $http->hasPostVariable( $selectedCurrencyName ) )
00242 {
00243 $selectedCurrency = $http->postVariable( $selectedCurrencyName );
00244 $multiprice =& $contentObjectAttribute->content();
00245
00246
00247
00248 $price =& $multiprice->priceByCurrency( $selectedCurrency );
00249 $multiprice->removePriceByCurrency( $selectedCurrency );
00250 $multiprice->setCustomPrice( $selectedCurrency, $price->attribute( 'value' ) );
00251
00252 $multiprice->store();
00253 }
00254 }break;
00255
00256 case 'remove_prices' :
00257 {
00258 $removePriceArrayName = 'ContentObjectAttribute' . '_remove_price_array_' . $contentObjectAttribute->attribute( 'id' );
00259 if ( $http->hasPostVariable( $removePriceArrayName ) )
00260 {
00261 $removePriceArray = $http->postVariable( $removePriceArrayName );
00262 $multiprice =& $contentObjectAttribute->content();
00263
00264 foreach( $removePriceArray as $currencyCode => $value )
00265 $multiprice->setAutoPrice( $currencyCode, false );
00266
00267 $multiprice->updateAutoPriceList();
00268 $multiprice->store();
00269 }
00270 }break;
00271
00272 default :
00273 {
00274 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZMultiPriceType' );
00275 }break;
00276 }
00277 }
00278
00279 function contentActionList( )
00280 {
00281 return array( array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to basket' ),
00282 'action' => 'ActionAddToBasket'
00283 ),
00284 array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to wish list' ),
00285 'action' => 'ActionAddToWishList'
00286 ) );
00287 }
00288
00289
00290
00291
00292 function deleteStoredObjectAttribute( &$objectAttribute, $version = null )
00293 {
00294 $multiprice =& $objectAttribute->content();
00295 $multiprice->remove( $objectAttribute->attribute( 'id' ), $version );
00296 }
00297
00298 function title( &$contentObjectAttribute )
00299 {
00300 return '';
00301 }
00302
00303 function hasObjectAttributeContent( &$contentObjectAttribute )
00304 {
00305 return true;
00306 }
00307
00308 function toString( $contentObjectAttribute )
00309 {
00310
00311 $multiprice = $contentObjectAttribute->attribute( 'content' );
00312
00313 $priceList = $multiprice->attribute( 'price_list' );
00314
00315 $priceArray = explode( ',', $contentObjectAttribute->attribute( 'data_text' ) );
00316 foreach ( $priceList as $priceData )
00317 {
00318 $type = $priceData->attribute( 'type' );
00319 if ( $type == 1 )
00320 {
00321 $type = 'CUSTOM';
00322 }
00323 else if ( $type == 2 )
00324 {
00325 $type = 'AUTO';
00326 }
00327 else
00328 $type = 'LIMIT';
00329 $priceArray = array_merge( $priceArray, array( $priceData->attribute( 'currency_code'), $priceData->attribute( 'value' ), $type ) );
00330 }
00331 return eZStringUtils::implodeStr( $priceArray, '|' );
00332 }
00333
00334
00335 function fromString( &$contentObjectAttribute, $string )
00336 {
00337 if ( $string == '' )
00338 return true;
00339
00340 $multiprice = $contentObjectAttribute->attribute( 'content' );
00341
00342 $multipriceData = eZStringUtils::explodeSTR( $string, '|' );
00343
00344 $vatType = array_shift( $multipriceData );
00345 $vatExInc = array_shift( $multipriceData );
00346
00347 $contentObjectAttribute->setAttribute( 'data_text', $vatType . ',' . $vatExInc );
00348
00349 while ( $multipriceData )
00350 {
00351 $currencyCode = array_shift( $multipriceData );
00352 $value = array_shift( $multipriceData );
00353
00354 $type = array_shift( $multipriceData );
00355 if ( $type == 'CUSTOM' )
00356 {
00357 $type = 1;
00358 }
00359 else if ( $type == 'AUTO' )
00360 {
00361 $type = 2;
00362 }
00363 else
00364 $type = 5000;
00365
00366 $multiprice->setPriceByCurrency( $currencyCode, $value, $type );
00367
00368 }
00369 $multiprice->store();
00370 return $multiprice;
00371
00372 }
00373
00374
00375
00376
00377 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00378 {
00379 $price =& $classAttribute->content();
00380 if ( $price )
00381 {
00382 $vatIncluded = $price->attribute( 'is_vat_included' );
00383 $vatTypes = $price->attribute( 'vat_type' );
00384 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'vat-included',
00385 array( 'is-set' => $vatIncluded ? 'true' : 'false' ) ) );
00386 $vatTypeNode = eZDOMDocument::createElementNode( 'vat-type' );
00387 $chosenVatType = $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD );
00388 $gotVat = false;
00389 foreach ( $vatTypes as $vatType )
00390 {
00391 $id = $vatType->attribute( 'id' );
00392 if ( $id == $chosenVatType )
00393 {
00394 $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'name', $vatType->attribute( 'name' ) ) );
00395 $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'percentage', $vatType->attribute( 'percentage' ) ) );
00396 $gotVat = true;
00397 break;
00398 }
00399 }
00400 if ( $gotVat )
00401 $attributeParametersNode->appendChild( $vatTypeNode );
00402
00403 $defualtCurrency = $classAttribute->attribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD );
00404 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-currency', array( 'code' => $defualtCurrency ) ) );
00405 }
00406 }
00407
00408
00409
00410
00411 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00412 {
00413 $vatNode =& $attributeParametersNode->elementByName( 'vat-included' );
00414 $vatIncluded = strtolower( $vatNode->attributeValue( 'is-set' ) ) == 'true';
00415 if ( $vatIncluded )
00416 $vatIncluded = EZ_MULTIPRICE_INCLUDED_VAT;
00417 else
00418 $vatIncluded = EZ_MULTIPRICE_EXCLUDED_VAT;
00419
00420 $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD, $vatIncluded );
00421 $vatTypeNode =& $attributeParametersNode->elementByName( 'vat-type' );
00422 $vatName = $vatTypeNode->attributeValue( 'name' );
00423 $vatPercentage = $vatTypeNode->attributeValue( 'percentage' );
00424 $vatID = false;
00425 $vatTypes = eZVATType::fetchList();
00426 foreach ( array_keys( $vatTypes ) as $vatTypeKey )
00427 {
00428 $vatType =& $vatTypes[$vatTypeKey];
00429 if ( $vatType->attribute( 'name' ) == $vatName and
00430 $vatType->attribute( 'percentage' ) == $vatPercentage )
00431 {
00432 $vatID = $vatType->attribute( 'id' );
00433 break;
00434 }
00435 }
00436 if ( !$vatID )
00437 {
00438 $vatType = eZVATType::create();
00439 $vatType->setAttribute( 'name', $vatName );
00440 $vatType->setAttribute( 'percentage', $vatPercentage );
00441 $vatType->store();
00442 $vatID = $vatType->attribute( 'id' );
00443 }
00444 $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD, $vatID );
00445
00446 $defaultCurrency =& $attributeParametersNode->elementByName( 'default-currency' );
00447 $currencyCode = $defaultCurrency->attributeValue( 'code' );
00448 $classAttribute->setAttribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD, $currencyCode );
00449 }
00450
00451
00452
00453
00454
00455 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00456 {
00457 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00458
00459 $multiprice =& $objectAttribute->content();
00460 $domDocument = $multiprice->DOMDocument();
00461
00462 $node->appendChild( $domDocument->root() );
00463
00464 return $node;
00465 }
00466
00467
00468
00469
00470 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00471 {
00472 $rootNode = $attributeNode->firstChild();
00473
00474 $multiprice =& $objectAttribute->content();
00475 $multiprice->decodeDOMTree( $rootNode );
00476 }
00477
00478 function customSorting()
00479 {
00480 return true;
00481 }
00482
00483 function customSortingSQL( $params )
00484 {
00485 $multipriceTableAlias = "mp";
00486
00487 if ( isset( $params['table_alias_suffix'] ) )
00488 $multipriceTableAlias .= $params['table_alias_suffix'];
00489
00490 $sql = array( 'from' => '',
00491 'where' => '',
00492 'sorting_field' => '' );
00493
00494 $sql['from'] = "ezmultipricedata $multipriceTableAlias";
00495
00496 $and = '';
00497 if ( isset( $params['contentobject_attr_id'] ) )
00498 {
00499 $sql['where'] = "
00500 $multipriceTableAlias.contentobject_attr_id = {$params['contentobject_attr_id']}";
00501 $and = ' AND';
00502 }
00503
00504 if ( isset( $params['contentobject_attr_version'] ) )
00505 {
00506 $sql['where'] .= "
00507 $and $multipriceTableAlias.contentobject_attr_version = {$params['contentobject_attr_version']}";
00508 $and = ' AND';
00509 }
00510
00511 if ( !isset( $params['currency_code'] ) )
00512 {
00513 include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00514 $params['currency_code'] = eZShopFunctions::preferredCurrencyCode();
00515 }
00516
00517 if ( $params['currency_code'] !== false )
00518 {
00519 $sql['where'] .= "
00520 $and $multipriceTableAlias.currency_code = '{$params['currency_code']}'";
00521 $and = ' AND';
00522 }
00523
00524 $sql['sorting_field'] = "$multipriceTableAlias.value";
00525
00526 return $sql;
00527 }
00528
00529
00530
00531
00532 function diff( $old, $new, $options = false )
00533 {
00534 return null;
00535 }
00536 }
00537
00538 eZDataType::register( EZ_DATATYPESTRING_MULTIPRICE, "ezmultipricetype" );
00539
00540 ?>