Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezpricetype.php
Go to the documentation of this file.
1 <?php
18 class eZPriceType extends eZDataType
19 {
20 const DATA_TYPE_STRING = "ezprice";
21 const INCLUDE_VAT_FIELD = 'data_int1';
22 const INCLUDE_VAT_VARIABLE = '_ezprice_include_vat_';
23 const VAT_ID_FIELD = 'data_float1';
24 const VAT_ID_VARIABLE = '_ezprice_vat_id_';
25 const INCLUDED_VAT = 1;
26 const EXCLUDED_VAT = 2;
27
28 public function __construct()
29 {
30 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Price", 'Datatype name' ),
31 array( 'serialize_supported' => true,
32 'object_serialize_map' => array( 'data_float' => 'price' ) ) );
33 }
34
39 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
40 {
41 // Check "price inc/ex VAT" and "VAT type" fields.
42 $vatTypeID = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
43 $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
44 if ( $vatExInc == 1 && $vatTypeID == -1 )
45 {
46 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
47 'Dynamic VAT cannot be included.' ) );
48 return eZInputValidator::STATE_INVALID;
49 }
50
51 // Check price.
52 if ( $http->hasPostVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) ) )
53 {
54 $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
55
56 $locale = eZLocale::instance();
57 $data = $locale->internalCurrency( $data );
58 $classAttribute = $contentObjectAttribute->contentClassAttribute();
59 if( !$contentObjectAttribute->validateIsRequired() && ( $data == "" ) )
60 {
61 return eZInputValidator::STATE_ACCEPTED;
62 }
63 if ( preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $data ) )
64 return eZInputValidator::STATE_ACCEPTED;
65
66 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
67 'Invalid price.' ) );
68 return eZInputValidator::STATE_INVALID;
69 }
70 else if ( $contentObjectAttribute->validateIsRequired() )
71 {
72 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Input required.' ) );
73 return eZInputValidator::STATE_INVALID;
74 }
75 else
76 {
77 return eZInputValidator::STATE_ACCEPTED;
78 }
79 }
80
81 function storeObjectAttribute( $attribute )
82 {
83 }
84
85 function metaData( $contentObjectAttribute )
86 {
87 return $contentObjectAttribute->attribute( "data_float" );
88 }
89
93 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
94 {
95 if ( $currentVersion != false )
96 {
97 $dataText = $originalContentObjectAttribute->attribute( "data_text" );
98 $dataFloat = $originalContentObjectAttribute->attribute( "data_float" );
99 $contentObjectAttribute->setAttribute( "data_float", $dataFloat );
100 $contentObjectAttribute->setAttribute( "data_text", $dataText );
101 }
102 }
103
107 function initializeClassAttribute( $classAttribute )
108 {
109 if ( $classAttribute->attribute( self::INCLUDE_VAT_FIELD ) == 0 )
110 $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, self::INCLUDED_VAT );
111 $classAttribute->store();
112 }
113 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
114 {
115 $isVatIncludedVariable = $base . self::INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' );
116 if ( $http->hasPostVariable( $isVatIncludedVariable ) )
117 {
118 $isVatIncluded = $http->postVariable( $isVatIncludedVariable );
119 $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $isVatIncluded );
120 }
121 $vatIDVariable = $base . self::VAT_ID_VARIABLE . $classAttribute->attribute( 'id' );
122 if ( $http->hasPostVariable( $vatIDVariable ) )
123 {
124 $vatID = $http->postVariable( $vatIDVariable );
125 $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID );
126 }
127 return true;
128 }
129
133 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
134 {
135 $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
136 $vatType = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
137 $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
138
139 $locale = eZLocale::instance();
140 $data = $locale->internalCurrency( $data );
141
142 $data_text = $vatType . ',' . $vatExInc;
143
144 $contentObjectAttribute->setAttribute( "data_float", $data );
145 $contentObjectAttribute->setAttribute( 'data_text', $data_text );
146
147 return true;
148 }
149
153 function objectAttributeContent( $contentObjectAttribute )
154 {
155 $classAttribute = $contentObjectAttribute->contentClassAttribute();
156 $storedPrice = $contentObjectAttribute->attribute( "data_float" );
157 $price = new eZPrice( $classAttribute, $contentObjectAttribute, $storedPrice );
158
159 if ( $contentObjectAttribute->attribute( 'data_text' ) != '' )
160 {
161 list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( "data_text" ), 2 );
162
163 $price->setAttribute( 'selected_vat_type', $vatType );
164 $price->setAttribute( 'is_vat_included', $vatExInc );
165 }
166
167 return $price;
168 }
169
173 function classAttributeContent( $classAttribute )
174 {
175 $contentObjectAttribute = false;
176 $price = new eZPrice( $classAttribute, $contentObjectAttribute );
177 return $price;
178 }
179
188 function contentActionList( $classAttribute )
189 {
190 $actionList = parent::contentActionList( $classAttribute );
191 $actionList[] = array( 'name' => ezpI18n::tr( 'kernel/classes/datatypes', 'Add to basket' ),
192 'action' => 'ActionAddToBasket'
193 );
194 $actionList[] = array( 'name' => ezpI18n::tr( 'kernel/classes/datatypes', 'Add to wish list' ),
195 'action' => 'ActionAddToWishList'
196 );
197 return $actionList;
198 }
199
200 function title( $contentObjectAttribute, $name = null )
201 {
202 return $contentObjectAttribute->attribute( "data_float" );
203 }
204
205 function sortKey( $contentObjectAttribute )
206 {
207 $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
208 return $intPrice;
209 }
210
211 function sortKeyType()
212 {
213 return 'int';
214 }
215
216 function hasObjectAttributeContent( $contentObjectAttribute )
217 {
218 return true;
219 }
220
221 function toString( $contentObjectAttribute )
222 {
223
224 $price = $contentObjectAttribute->attribute( 'content' );
225 $vatType =$price->attribute( 'selected_vat_type' );
226
227 $priceStr = implode( '|', array( $price->attribute( 'price' ), $vatType->attribute( 'id' ) , ($price->attribute( 'is_vat_included' ) )? 1:0 ) );
228 return $priceStr;
229 }
230
231
232 function fromString( $contentObjectAttribute, $string )
233 {
234 if ( $string == '' )
235 return true;
236
237 $priceData = explode( '|', $string );
238 if ( count( $priceData ) != 3 )
239 return false;
240
241 $dataText = $priceData[1] . ',' . $priceData[2];
242 $price = $priceData[0];
243
244 $contentObjectAttribute->setAttribute( "data_float", $price );
245 $contentObjectAttribute->setAttribute( 'data_text', $dataText );
246
247 return true;
248 }
249
250 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
251 {
252 $price = $classAttribute->content();
253 if ( $price )
254 {
255 $vatIncluded = $price->attribute( 'is_vat_included' );
256 $vatTypes = $price->attribute( 'vat_type' );
257
258 $dom = $attributeParametersNode->ownerDocument;
259 $vatIncludedNode = $dom->createElement( 'vat-included' );
260 $vatIncludedNode->setAttribute( 'is-set', $vatIncluded ? 'true' : 'false' );
261 $attributeParametersNode->appendChild( $vatIncludedNode );
262 $vatTypeNode = $dom->createElement( 'vat-type' );
263 $chosenVatType = $classAttribute->attribute( 'data_float1' );
264 $gotVat = false;
265 foreach ( $vatTypes as $vatType )
266 {
267 $id = $vatType->attribute( 'id' );
268 if ( $id == $chosenVatType )
269 {
270 $vatTypeNode->setAttribute( 'name', $vatType->attribute( 'name' ) );
271 $vatTypeNode->setAttribute( 'percentage', $vatType->attribute( 'percentage' ) );
272 $gotVat = true;
273 break;
274 }
275 }
276 if ( $gotVat )
277 $attributeParametersNode->appendChild( $vatTypeNode );
278 }
279 }
280
281 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
282 {
283 $vatNode = $attributeParametersNode->getElementsByTagName( 'vat-included' )->item( 0 );
284 $vatIncluded = strtolower( $vatNode->getAttribute( 'is-set' ) ) == 'true';
285 if ( $vatIncluded )
286 $vatIncluded = self::INCLUDED_VAT;
287 else
288 $vatIncluded = self::EXCLUDED_VAT;
289
290 $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $vatIncluded );
291 $vatTypeNode = $attributeParametersNode->getElementsByTagName( 'vat-type' )->item( 0 );
292 $vatName = $vatTypeNode->getAttribute( 'name' );
293 $vatPercentage = $vatTypeNode->getAttribute( 'percentage' );
294 $vatID = false;
295 $vatTypes = eZVatType::fetchList();
296 foreach ( $vatTypes as $vatType )
297 {
298 if ( $vatType->attribute( 'name' ) == $vatName and
299 $vatType->attribute( 'percentage' ) == $vatPercentage )
300 {
301 $vatID = $vatType->attribute( 'id' );
302 break;
303 }
304 }
305 if ( !$vatID )
306 {
307 $vatType = eZVatType::create();
308 $vatType->setAttribute( 'name', $vatName );
309 $vatType->setAttribute( 'percentage', $vatPercentage );
310 $vatType->store();
311 $vatID = $vatType->attribute( 'id' );
312 }
313 $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID );
314 }
315
316 function supportsBatchInitializeObjectAttribute()
317 {
318 return true;
319 }
320 }
321
322 eZDataType::register( eZPriceType::DATA_TYPE_STRING, "eZPriceType" );
323
324 ?>
eZDataType
Base class for content datatypes.
Definition: ezdatatype.php:46
eZDataType\register
static register( $dataTypeString, $className)
Definition: ezdatatype.php:203
eZInputValidator\STATE_ACCEPTED
const STATE_ACCEPTED
Definition: ezinputvalidator.php:79
eZInputValidator\STATE_INVALID
const STATE_INVALID
Definition: ezinputvalidator.php:81
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
eZPriceType
Stores a price (float)
Definition: ezpricetype.php:19
eZPriceType\storeObjectAttribute
storeObjectAttribute( $attribute)
Definition: ezpricetype.php:81
eZPriceType\initializeClassAttribute
initializeClassAttribute( $classAttribute)
Definition: ezpricetype.php:107
eZPriceType\INCLUDED_VAT
const INCLUDED_VAT
Definition: ezpricetype.php:25
eZPriceType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezpricetype.php:39
eZPriceType\toString
toString( $contentObjectAttribute)
Definition: ezpricetype.php:221
eZPriceType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezpricetype.php:20
eZPriceType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezpricetype.php:216
eZPriceType\INCLUDE_VAT_FIELD
const INCLUDE_VAT_FIELD
Definition: ezpricetype.php:21
eZPriceType\VAT_ID_VARIABLE
const VAT_ID_VARIABLE
Definition: ezpricetype.php:24
eZPriceType\classAttributeContent
classAttributeContent( $classAttribute)
Definition: ezpricetype.php:173
eZPriceType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezpricetype.php:113
eZPriceType\title
title( $contentObjectAttribute, $name=null)
Definition: ezpricetype.php:200
eZPriceType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: ezpricetype.php:316
eZPriceType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezpricetype.php:133
eZPriceType\__construct
__construct()
Definition: ezpricetype.php:28
eZPriceType\unserializeContentClassAttribute
unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezpricetype.php:281
eZPriceType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: ezpricetype.php:93
eZPriceType\metaData
metaData( $contentObjectAttribute)
Definition: ezpricetype.php:85
eZPriceType\EXCLUDED_VAT
const EXCLUDED_VAT
Definition: ezpricetype.php:26
eZPriceType\serializeContentClassAttribute
serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezpricetype.php:250
eZPriceType\fromString
fromString( $contentObjectAttribute, $string)
Definition: ezpricetype.php:232
eZPriceType\INCLUDE_VAT_VARIABLE
const INCLUDE_VAT_VARIABLE
Definition: ezpricetype.php:22
eZPriceType\sortKeyType
sortKeyType()
Definition: ezpricetype.php:211
eZPriceType\sortKey
sortKey( $contentObjectAttribute)
Definition: ezpricetype.php:205
eZPriceType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezpricetype.php:153
eZPriceType\VAT_ID_FIELD
const VAT_ID_FIELD
Definition: ezpricetype.php:23
eZPriceType\contentActionList
contentActionList( $classAttribute)
Definition: ezpricetype.php:188
eZPrice
Definition: ezprice.php:19
eZVatType\fetchList
static fetchList( $asObject=true, $skipDynamic=false)
Definition: ezvattype.php:105
eZVatType\create
static create()
Definition: ezvattype.php:173
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.