Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezoptiontype.php
Go to the documentation of this file.
1 <?php
18 class eZOptionType extends eZDataType
19 {
20 const DEFAULT_NAME_VARIABLE = "_ezoption_default_name_";
21
22 const DATA_TYPE_STRING = "ezoption";
23
24 public function __construct()
25 {
26 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Option", 'Datatype name' ),
27 array( 'serialize_supported' => true ) );
28 }
29
30 function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
31 {
32 $classAttribute = $contentObjectAttribute->contentClassAttribute();
33 if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
34 {
35 $value = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
36
37 if ( $contentObjectAttribute->validateIsRequired() and !$value )
38 {
39 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
40 'Input required.' ) );
41 return eZInputValidator::STATE_INVALID;
42 }
43 }
44 else
45 {
46 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
47 'Input required.' ) );
48 return eZInputValidator::STATE_INVALID;
49 }
50 }
51
56 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
57 {
58 $count = 0;
59 $classAttribute = $contentObjectAttribute->contentClassAttribute();
60 if ( $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) ) )
61 {
62 $idList = $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) );
63 $valueList = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
64 $dataName = $http->postVariable( $base . "_data_option_name_" . $contentObjectAttribute->attribute( "id" ) );
65
66 if ( $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) ) )
67 $optionAdditionalPriceList = $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) );
68 else
69 $optionAdditionalPriceList = array();
70
71 for ( $i = 0; $i < count( $valueList ); ++$i )
72 if ( trim( $valueList[$i] ) <> '' )
73 {
74 ++$count;
75 break;
76 }
77 if ( $contentObjectAttribute->validateIsRequired() and trim( $dataName ) == '' )
78 {
79 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
80 'NAME is required.' ) );
81 return eZInputValidator::STATE_INVALID;
82 }
83 if ( $count != 0 )
84 {
85 for ( $i=0;$i<count( $idList );$i++ )
86 {
87 $value = $valueList[$i];
88 if ( trim( $value )== "" )
89 {
90 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
91 'The option value must be provided.' ) );
92 return eZInputValidator::STATE_INVALID;
93 }
94 if ( isset( $optionAdditionalPriceList[$i] ) &&
95 strlen( $optionAdditionalPriceList[$i] ) &&
96 !preg_match( "#^[-|+]?[0-9]+(\.){0,1}[0-9]{0,2}$#", $optionAdditionalPriceList[$i] ) )
97 {
98 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
99 'The Additional price value is not valid.' ) );
100 return eZInputValidator::STATE_INVALID;
101 }
102 }
103 }
104 }
105 if ( $contentObjectAttribute->validateIsRequired() and
106 !$classAttribute->attribute( 'is_information_collector' ) )
107 {
108 if ( $count == 0 )
109 {
110 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
111 'At least one option is required.' ) );
112 return eZInputValidator::STATE_INVALID;
113 }
114 }
115 return eZInputValidator::STATE_ACCEPTED;
116 }
117
121 function storeObjectAttribute( $contentObjectAttribute )
122 {
123 $option = $contentObjectAttribute->content();
124 $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
125 }
126
130 function objectAttributeContent( $contentObjectAttribute )
131 {
132 $option = new eZOption( "" );
133
134 $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
135
136 return $option;
137 }
138
142 function metaData( $contentObjectAttribute )
143 {
144 return $contentObjectAttribute->attribute( "data_text" );
145 }
146
150 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
151 {
152 $optionName = $http->postVariable( $base . "_data_option_name_" . $contentObjectAttribute->attribute( "id" ) );
153 if ( $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) ) )
154 $optionIDArray = $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) );
155 else
156 $optionIDArray = array();
157 if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
158 $optionValueArray = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
159 else
160 $optionValueArray = array();
161 if ( $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) ) )
162 $optionAdditionalPriceArray = $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) );
163 else
164 $optionAdditionalPriceArray = array();
165
166 $option = new eZOption( $optionName );
167
168 $i = 0;
169 foreach ( $optionIDArray as $id )
170 {
171 $option->addOption( array( 'value' => $optionValueArray[$i],
172 'additional_price' => ( isset( $optionAdditionalPriceArray[$i] ) ? $optionAdditionalPriceArray[$i] : 0 ) ) );
173 $i++;
174 }
175 $contentObjectAttribute->setContent( $option );
176 return true;
177 }
178
179
183 function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
184 {
185 if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
186 {
187 $optionValue = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
188
189 $collectionAttribute->setAttribute( 'data_int', $optionValue );
190 $attr = $contentObjectAttribute->attribute( 'contentclass_attribute' );
191
192 return true;
193 }
194 return false;
195 }
196
197 function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
198 {
199 switch ( $action )
200 {
201 case "new_option" :
202 {
203 $option = $contentObjectAttribute->content( );
204
205 $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" );
206 if ( $http->hasPostVariable( $postvarname ) )
207 {
208 $idArray = $http->postVariable( $postvarname );
209 $beforeID = array_shift( $idArray );
210 if ( $beforeID >= 0 )
211 {
212 $option->insertOption( array(), $beforeID );
213 $contentObjectAttribute->setContent( $option );
214 $contentObjectAttribute->store();
215 $option = new eZOption( "" );
216 $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
217 $contentObjectAttribute->setContent( $option );
218 return;
219 }
220 }
221 $option->addOption( "" );
222 $contentObjectAttribute->setContent( $option );
223 $contentObjectAttribute->store();
224 }break;
225 case "remove_selected" :
226 {
227 $option = $contentObjectAttribute->content( );
228 $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" );
229 $array_remove = $http->postVariable( $postvarname );
230 $option->removeOptions( $array_remove );
231 $contentObjectAttribute->setContent( $option );
232 $contentObjectAttribute->store();
233 $option = new eZOption( "" );
234 $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
235 $contentObjectAttribute->setContent( $option );
236 }break;
237 default :
238 {
239 eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZOptionType" );
240 }break;
241 }
242 }
243
248 function productOptionInformation( $objectAttribute, $optionID, $productItem )
249 {
250 $option = $objectAttribute->attribute( 'content' );
251 foreach( $option->attribute( 'option_list' ) as $optionArray )
252 {
253 if ( $optionArray['id'] == $optionID )
254 {
255 return array( 'id' => $optionArray['id'],
256 'name' => $option->attribute( 'name' ),
257 'value' => $optionArray['value'],
258 'additional_price' => $optionArray['additional_price'] );
259 }
260 }
261 return false;
262 }
263
267 function title( $contentObjectAttribute, $name = "name" )
268 {
269 $option = $contentObjectAttribute->content( );
270
271 $value = $option->attribute( $name );
272
273 return $value;
274 }
275
276 function hasObjectAttributeContent( $contentObjectAttribute )
277 {
278 $option = $contentObjectAttribute->content( );
279 $options = $option->attribute( 'option_list' );
280 return count( $options ) > 0;
281 }
282
286 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
287 {
288 if ( $currentVersion == false )
289 {
290 $option = $contentObjectAttribute->content();
291 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
292 if ( !$option )
293 {
294 $option = new eZOption( $contentClassAttribute->attribute( 'data_text1' ) );
295 }
296 else
297 {
298 $option->setName( $contentClassAttribute->attribute( 'data_text1' ) );
299 }
300 $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
301 $contentObjectAttribute->setContent( $option );
302 }
303 else
304 {
305 $dataText = $originalContentObjectAttribute->attribute( "data_text" );
306 $contentObjectAttribute->setAttribute( "data_text", $dataText );
307 }
308 }
309
310 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
311 {
312 $defaultValueName = $base . self::DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
313 if ( $http->hasPostVariable( $defaultValueName ) )
314 {
315 $defaultValueValue = $http->postVariable( $defaultValueName );
316
317 if ($defaultValueValue == "")
318 {
319 $defaultValueValue = "";
320 }
321 $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
322 return true;
323 }
324 return false;
325 }
326
327 function toString( $contentObjectAttribute )
328 {
329
330 $option = $contentObjectAttribute->attribute( 'content' );
331 $optionArray = array();
332 $optionArray[] = $option->attribute( 'name' );
333
334 $optionList = $option->attribute( 'option_list' );
335
336 foreach ( $optionList as $key => $value )
337 {
338 $optionArray[] = $value['value'];
339 $optionArray[] = $value['additional_price'];
340 }
341 return eZStringUtils::implodeStr( $optionArray, "|" );
342 }
343
344
345 function fromString( $contentObjectAttribute, $string )
346 {
347 if ( $string == '' )
348 return true;
349
350 $optionArray = eZStringUtils::explodeStr( $string, '|' );
351
352 $option = new eZOption( "" );
353
354 $option->OptionCount = 0;
355 $option->Options = array();
356 $option->Name = array_shift( $optionArray );
357 $count = count( $optionArray );
358 for ( $i = 0; $i < $count; $i +=2 )
359 {
360
361 $option->addOption( array( 'value' => array_shift( $optionArray ),
362 'additional_price' => array_shift( $optionArray ) ) );
363 }
364
365
366 $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
367
368 return $option;
369
370 }
371 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
372 {
373 $defaultValue = $classAttribute->attribute( 'data_text1' );
374 $dom = $attributeParametersNode->ownerDocument;
375 $defaultValueNode = $dom->createElement( 'default-value' );
376 $defaultValueNode->appendChild( $dom->createTextNode( $defaultValue ) );
377 $attributeParametersNode->appendChild( $defaultValueNode );
378 }
379
380 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
381 {
382 $defaultValue = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 )->textContent;
383 $classAttribute->setAttribute( 'data_text1', $defaultValue );
384 }
385
386 function serializeContentObjectAttribute( $package, $objectAttribute )
387 {
388 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
389
390 $domDocument = new DOMDocument( '1.0', 'utf-8' );
391 $success = $domDocument->loadXML( $objectAttribute->attribute( 'data_text' ) );
392
393 $importedRoot = $node->ownerDocument->importNode( $domDocument->documentElement, true );
394 $node->appendChild( $importedRoot );
395
396 return $node;
397 }
398
399 function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
400 {
401 $xmlString = '';
402 $optionNode = $attributeNode->getElementsByTagName( 'ezoption' )->item( 0 );
403
404 if ( $optionNode )
405 {
406 $xmlString = $optionNode->ownerDocument->saveXML( $optionNode );
407 }
408 else
409 {
410 // backward compatibility
411 $optionNode = $attributeNode->getElementsByTagName( 'data-text' )->item( 0 );
412 if ( $optionNode )
413 {
414 $xmlString = $optionNode->textContent;
415 }
416 else
417 {
418 // dl: unknown case. Probably should be removed at all.
419 $optionNode = $attributeNode->firstChild;
420 $xmlString = $optionNode->getAttribute( 'local_name' ) == 'data-text' ? '' : $optionNode->textContent;
421 }
422 }
423
424 $objectAttribute->setAttribute( 'data_text', $xmlString );
425 }
426
427 function isInformationCollector()
428 {
429 return true;
430 }
431
432 function supportsBatchInitializeObjectAttribute()
433 {
434 return true;
435 }
436
437 function batchInitializeObjectAttributeData( $classAttribute )
438 {
439 $option = new eZOption( $classAttribute->attribute( 'data_text1' ) );
440 $db = eZDB::instance();
441 return array( 'data_text' => "'" . $db->escapeString( $option->xmlString() ) . "'" );
442 }
443 }
444
445 eZDataType::register( eZOptionType::DATA_TYPE_STRING, "eZOptionType" );
446
447 ?>
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDataType
Base class for content datatypes.
Definition: ezdatatype.php:46
eZDataType\register
static register( $dataTypeString, $className)
Definition: ezdatatype.php:203
eZDataType\createContentObjectAttributeDOMNode
createContentObjectAttributeDOMNode( $objectAttribute)
Definition: ezdatatype.php:1371
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZInputValidator\STATE_ACCEPTED
const STATE_ACCEPTED
Definition: ezinputvalidator.php:79
eZInputValidator\STATE_INVALID
const STATE_INVALID
Definition: ezinputvalidator.php:81
eZOptionType
Stores option values.
Definition: ezoptiontype.php:19
eZOptionType\unserializeContentClassAttribute
unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezoptiontype.php:380
eZOptionType\serializeContentClassAttribute
serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezoptiontype.php:371
eZOptionType\fromString
fromString( $contentObjectAttribute, $string)
Definition: ezoptiontype.php:345
eZOptionType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezoptiontype.php:130
eZOptionType\unserializeContentObjectAttribute
unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode)
Definition: ezoptiontype.php:399
eZOptionType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: ezoptiontype.php:432
eZOptionType\fetchCollectionAttributeHTTPInput
fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute)
Definition: ezoptiontype.php:183
eZOptionType\toString
toString( $contentObjectAttribute)
Definition: ezoptiontype.php:327
eZOptionType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: ezoptiontype.php:286
eZOptionType\batchInitializeObjectAttributeData
batchInitializeObjectAttributeData( $classAttribute)
Definition: ezoptiontype.php:437
eZOptionType\isInformationCollector
isInformationCollector()
Definition: ezoptiontype.php:427
eZOptionType\__construct
__construct()
Definition: ezoptiontype.php:24
eZOptionType\serializeContentObjectAttribute
serializeContentObjectAttribute( $package, $objectAttribute)
Definition: ezoptiontype.php:386
eZOptionType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezoptiontype.php:276
eZOptionType\DEFAULT_NAME_VARIABLE
const DEFAULT_NAME_VARIABLE
Definition: ezoptiontype.php:20
eZOptionType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezoptiontype.php:150
eZOptionType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezoptiontype.php:56
eZOptionType\metaData
metaData( $contentObjectAttribute)
Definition: ezoptiontype.php:142
eZOptionType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezoptiontype.php:22
eZOptionType\customObjectAttributeHTTPAction
customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters)
Definition: ezoptiontype.php:197
eZOptionType\validateCollectionAttributeHTTPInput
validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezoptiontype.php:30
eZOptionType\title
title( $contentObjectAttribute, $name="name")
Definition: ezoptiontype.php:267
eZOptionType\productOptionInformation
productOptionInformation( $objectAttribute, $optionID, $productItem)
Definition: ezoptiontype.php:248
eZOptionType\storeObjectAttribute
storeObjectAttribute( $contentObjectAttribute)
Definition: ezoptiontype.php:121
eZOptionType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezoptiontype.php:310
eZOption
eZOption handles option set datatypes
Definition: ezoption.php:29
eZStringUtils\implodeStr
static implodeStr( $values, $delimiter='|')
Definition: ezstringutils.php:44
eZStringUtils\explodeStr
static explodeStr( $str, $delimiter='|')
Definition: ezstringutils.php:19
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+.