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/ezprice/ezprice.php" );
00040
00041 define( "EZ_DATATYPESTRING_PRICE", "ezprice" );
00042 define( 'EZ_DATATYPESTRING_INCLUDE_VAT_FIELD', 'data_int1' );
00043 define( 'EZ_DATATYPESTRING_INCLUDE_VAT_VARIABLE', '_ezprice_include_vat_' );
00044 define( 'EZ_DATATYPESTRING_VAT_ID_FIELD', 'data_float1' );
00045 define( 'EZ_DATATYPESTRING_VAT_ID_VARIABLE', '_ezprice_vat_id_' );
00046 define( "EZ_PRICE_INCLUDED_VAT", 1 );
00047 define( "EZ_PRICE_EXCLUDED_VAT", 2 );
00048
00049 class eZPriceType extends eZDataType
00050 {
00051 function eZPriceType()
00052 {
00053 $this->eZDataType( EZ_DATATYPESTRING_PRICE, ezi18n( 'kernel/classes/datatypes', "Price", 'Datatype name' ),
00054 array( 'serialize_supported' => true,
00055 'object_serialize_map' => array( 'data_float' => 'price' ) ) );
00056 }
00057
00058
00059
00060
00061
00062 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00063 {
00064
00065 $vatTypeID = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
00066 $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
00067 if ( $vatExInc == 1 && $vatTypeID == -1 )
00068 {
00069 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00070 'Dynamic VAT cannot be included.' ) );
00071 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00072 }
00073
00074
00075 if ( $http->hasPostVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) ) )
00076 {
00077 $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
00078
00079 include_once( 'lib/ezlocale/classes/ezlocale.php' );
00080 $locale =& eZLocale::instance();
00081 $data = $locale->internalCurrency( $data );
00082 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00083 if( !$contentObjectAttribute->validateIsRequired() && ( $data == "" ) )
00084 {
00085 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00086 }
00087 if ( preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $data ) )
00088 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00089
00090 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00091 'Invalid price.' ) );
00092 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00093 }
00094 else
00095 {
00096 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00097 }
00098 }
00099
00100 function storeObjectAttribute( &$attribute )
00101 {
00102 }
00103
00104 function metaData( $contentObjectAttribute )
00105 {
00106 return $contentObjectAttribute->attribute( "data_float" );
00107 }
00108
00109
00110
00111
00112 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00113 {
00114 if ( $currentVersion != false )
00115 {
00116 $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00117 $dataFloat = $originalContentObjectAttribute->attribute( "data_float" );
00118 $contentObjectAttribute->setAttribute( "data_float", $dataFloat );
00119 $contentObjectAttribute->setAttribute( "data_text", $dataText );
00120 }
00121 }
00122
00123
00124
00125
00126 function initializeClassAttribute( &$classAttribute )
00127 {
00128 if ( $classAttribute->attribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD ) == 0 )
00129 $classAttribute->setAttribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD, EZ_PRICE_INCLUDED_VAT );
00130 $classAttribute->store();
00131 }
00132 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00133 {
00134 $isVatIncludedVariable = $base . EZ_DATATYPESTRING_INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' );
00135 if ( $http->hasPostVariable( $isVatIncludedVariable ) )
00136 {
00137 $isVatIncluded = $http->postVariable( $isVatIncludedVariable );
00138 $classAttribute->setAttribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD, $isVatIncluded );
00139 }
00140 $vatIDVariable = $base . EZ_DATATYPESTRING_VAT_ID_VARIABLE . $classAttribute->attribute( 'id' );
00141 if ( $http->hasPostVariable( $vatIDVariable ) )
00142 {
00143 $vatID = $http->postVariable( $vatIDVariable );
00144 $classAttribute->setAttribute( EZ_DATATYPESTRING_VAT_ID_FIELD, $vatID );
00145 }
00146 return true;
00147 }
00148
00149
00150
00151
00152 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00153 {
00154 $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
00155 $vatType = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
00156 $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
00157
00158 include_once( 'lib/ezlocale/classes/ezlocale.php' );
00159 $locale =& eZLocale::instance();
00160 $data = $locale->internalCurrency( $data );
00161
00162 $data_text = $vatType . ',' . $vatExInc;
00163
00164 $contentObjectAttribute->setAttribute( "data_float", $data );
00165 $contentObjectAttribute->setAttribute( 'data_text', $data_text );
00166
00167 return true;
00168 }
00169
00170
00171
00172
00173 function &objectAttributeContent( &$contentObjectAttribute )
00174 {
00175 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00176 $storedPrice = $contentObjectAttribute->attribute( "data_float" );
00177 $price = new eZPrice( $classAttribute, $contentObjectAttribute, $storedPrice );
00178
00179 if ( $contentObjectAttribute->attribute( 'data_text' ) != '' )
00180 {
00181 list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( "data_text" ), 2 );
00182
00183 $price->setAttribute( 'selected_vat_type', $vatType );
00184 $price->setAttribute( 'is_vat_included', $vatExInc );
00185 }
00186
00187 return $price;
00188 }
00189
00190
00191
00192
00193 function &classAttributeContent( &$classAttribute )
00194 {
00195 $contentObjectAttribute = false;
00196 $price = new eZPrice( $classAttribute, $contentObjectAttribute );
00197 return $price;
00198 }
00199
00200 function contentActionList( )
00201 {
00202 return array( array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to basket' ),
00203 'action' => 'ActionAddToBasket'
00204 ),
00205 array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to wish list' ),
00206 'action' => 'ActionAddToWishList'
00207 ) );
00208 }
00209
00210 function title( &$contentObjectAttribute )
00211 {
00212 return $contentObjectAttribute->attribute( "data_float" );
00213 }
00214
00215
00216
00217
00218 function sortKey( &$contentObjectAttribute )
00219 {
00220 $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
00221 return $intPrice;
00222 }
00223
00224
00225
00226
00227 function sortKeyType()
00228 {
00229 return 'int';
00230 }
00231
00232 function hasObjectAttributeContent( &$contentObjectAttribute )
00233 {
00234 return true;
00235 }
00236
00237 function toString( $contentObjectAttribute )
00238 {
00239
00240 $price = $contentObjectAttribute->attribute( 'content' );
00241 $vatType =$price->attribute( 'selected_vat_type' );
00242
00243 $priceStr = implode( '|', array( $price->attribute( 'price' ), $vatType->attribute( 'id' ) , ($price->attribute( 'is_vat_included' ) )? 1:0 ) );
00244 return $priceStr;
00245 }
00246
00247
00248 function fromString( &$contentObjectAttribute, $string )
00249 {
00250 if ( $string == '' )
00251 return true;
00252
00253 $priceData = explode( '|', $string );
00254 if ( count( $priceData ) != 3 )
00255 return false;
00256
00257 $dataText = $priceData[1] . ',' . $priceData[2];
00258 $price = $priceData[0];
00259
00260 $contentObjectAttribute->setAttribute( "data_float", $price );
00261 $contentObjectAttribute->setAttribute( 'data_text', $dataText );
00262
00263 return true;
00264 }
00265
00266
00267
00268
00269 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00270 {
00271 $price =& $classAttribute->content();
00272 if ( $price )
00273 {
00274 $vatIncluded = $price->attribute( 'is_vat_included' );
00275 $vatTypes = $price->attribute( 'vat_type' );
00276 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'vat-included',
00277 array( 'is-set' => $vatIncluded ? 'true' : 'false' ) ) );
00278 $vatTypeNode = eZDOMDocument::createElementNode( 'vat-type' );
00279 $chosenVatType = $classAttribute->attribute( 'data_float1' );
00280 $gotVat = false;
00281 foreach ( $vatTypes as $vatType )
00282 {
00283 $id = $vatType->attribute( 'id' );
00284 if ( $id == $chosenVatType )
00285 {
00286 $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'name', $vatType->attribute( 'name' ) ) );
00287 $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'percentage', $vatType->attribute( 'percentage' ) ) );
00288 $gotVat = true;
00289 break;
00290 }
00291 }
00292 if ( $gotVat )
00293 $attributeParametersNode->appendChild( $vatTypeNode );
00294 }
00295 }
00296
00297
00298
00299
00300 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00301 {
00302 $vatNode =& $attributeParametersNode->elementByName( 'vat-included' );
00303 $vatIncluded = strtolower( $vatNode->attributeValue( 'is-set' ) ) == 'true';
00304 if ( $vatIncluded )
00305 $vatIncluded = EZ_PRICE_INCLUDED_VAT;
00306 else
00307 $vatIncluded = EZ_PRICE_EXCLUDED_VAT;
00308
00309 $classAttribute->setAttribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD, $vatIncluded );
00310 $vatTypeNode =& $attributeParametersNode->elementByName( 'vat-type' );
00311 $vatName = $vatTypeNode->attributeValue( 'name' );
00312 $vatPercentage = $vatTypeNode->attributeValue( 'percentage' );
00313 $vatID = false;
00314 $vatTypes = eZVATType::fetchList();
00315 foreach ( array_keys( $vatTypes ) as $vatTypeKey )
00316 {
00317 $vatType =& $vatTypes[$vatTypeKey];
00318 if ( $vatType->attribute( 'name' ) == $vatName and
00319 $vatType->attribute( 'percentage' ) == $vatPercentage )
00320 {
00321 $vatID = $vatType->attribute( 'id' );
00322 break;
00323 }
00324 }
00325 if ( !$vatID )
00326 {
00327 $vatType = eZVATType::create();
00328 $vatType->setAttribute( 'name', $vatName );
00329 $vatType->setAttribute( 'percentage', $vatPercentage );
00330 $vatType->store();
00331 $vatID = $vatType->attribute( 'id' );
00332 }
00333 $classAttribute->setAttribute( EZ_DATATYPESTRING_VAT_ID_FIELD, $vatID );
00334 }
00335 }
00336
00337 eZDataType::register( EZ_DATATYPESTRING_PRICE, "ezpricetype" );
00338
00339 ?>