Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezmultioptiontype.php
Go to the documentation of this file.
1 <?php
32 class eZMultiOptionType extends eZDataType
33 {
34 const DEFAULT_NAME_VARIABLE = "_ezmultioption_default_name_";
35 const DATA_TYPE_STRING = "ezmultioption";
36
37 public function __construct()
38 {
39 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Multi-option", 'Datatype name' ),
40 array( 'serialize_supported' => true ) );
41 }
42
47 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
48 {
49 $count = 0;
50 $classAttribute = $contentObjectAttribute->contentClassAttribute();
51 if ( $http->hasPostVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) ) )
52 {
53 $classAttribute = $contentObjectAttribute->contentClassAttribute();
54 $multioptionIDArray = $http->postVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) );
55
56 foreach ( $multioptionIDArray as $id )
57 {
58 $multioptionName = $http->postVariable( $base . "_data_multioption_name_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
59 $optionIDArray = $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
60 ? $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
61 : array();
62 $optionCountArray = $http->hasPostVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
63 ? $http->postVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
64 : array();
65 $optionValueArray = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
66 ? $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
67 : array();
68 $optionAdditionalPriceArray = $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
69 ? $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
70 : array();
71 for ( $i = 0; $i < count( $optionIDArray ); $i++ )
72 {
73 if ( $contentObjectAttribute->validateIsRequired() and !$classAttribute->attribute( 'is_information_collector' ) )
74 {
75 if ( trim( $optionValueArray[$i] ) == "" )
76 {
77 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
78 'The option value must be provided.' ) );
79 return eZInputValidator::STATE_INVALID;
80 }
81 else
82 ++$count;
83 }
84
85 if ( trim( $optionValueArray[$i] ) != "" )
86 {
87 if ( strlen( $optionAdditionalPriceArray[$i] ) && !preg_match( "#^[-|+]?[0-9]+(\.){0,1}[0-9]{0,2}$#", $optionAdditionalPriceArray[$i] ) )
88 {
89 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
90 'The additional price for the multioption value is not valid.' ) );
91 return eZInputValidator::STATE_INVALID;
92 }
93 }
94
95 }
96 }
97 }
98 if ( $contentObjectAttribute->validateIsRequired() and
99 !$classAttribute->attribute( 'is_information_collector' ) )
100 {
101 if ( $count == 0 )
102 {
103 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
104 'At least one option is required.' ) );
105 return eZInputValidator::STATE_INVALID;
106 }
107
108 $optionSetName = $http->hasPostVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) )
109 ? $http->postVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) )
110 : '';
111 if ( trim( $optionSetName ) == '' )
112 {
113 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
114 'Option set name is required.' ) );
115 return eZInputValidator::STATE_INVALID;
116 }
117 }
118
119
120 return eZInputValidator::STATE_ACCEPTED;
121 }
122
126 function storeObjectAttribute( $contentObjectAttribute )
127 {
128 $multioption = $contentObjectAttribute->content();
129 $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
130 }
131
135 function objectAttributeContent( $contentObjectAttribute )
136 {
137 $multioption = new eZMultiOption( "" );
138 $multioption->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
139 return $multioption;
140 }
141
142 function isIndexable()
143 {
144 return true;
145 }
146
150 function metaData( $contentObjectAttribute )
151 {
152 return $contentObjectAttribute->attribute( "data_text" );
153 }
154
158 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
159 {
160 $multioptionIDArray = $http->hasPostVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) )
161 ? $http->postVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) )
162 : array();
163 $optionSetName = $http->postVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) );
164 $multioption = new eZMultiOption( $optionSetName );
165 foreach ( $multioptionIDArray as $id )
166 {
167 $multioptionName = $http->postVariable( $base . "_data_multioption_name_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
168 $optionIDArray = $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
169 ? $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
170 : array();
171
172 $optionPriority = $http->postVariable( $base . "_data_multioption_priority_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
173 // check to prevent PHP warning if the default choice is specified (no radio button selected)
174 if ( $http->hasPostVariable( $base . "_data_radio_checked_" . $contentObjectAttribute->attribute("id") . '_' . $id ) )
175 $optionDefaultValue = $http->postVariable( $base . "_data_radio_checked_" . $contentObjectAttribute->attribute("id") . '_' . $id );
176 else
177 $optionDefaultValue = '';
178 $newID = $multioption->addMultiOption( $multioptionName,$optionPriority, $optionDefaultValue );
179
180 $optionCountArray = $http->hasPostVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
181 ? $http->postVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
182 : array();
183 $optionValueArray = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
184 ? $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
185 : array();
186 $optionAdditionalPriceArray = $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
187 ? $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
188 : array();
189
190 for ( $i = 0; $i < count( $optionIDArray ); $i++ )
191 $multioption->addOption( $newID, $optionCountArray[$i], $optionValueArray[$i], $optionAdditionalPriceArray[$i] );
192 }
193
194 $multioption->sortMultiOptions();
195 $multioption->resetOptionCounter();
196 $contentObjectAttribute->setContent( $multioption );
197 return true;
198 }
199
203 function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
204 {
205 $multioptionValue = $http->postVariable( $base . "_data_multioption_value_" . $contentObjectAttribute->attribute( "id" ) );
206 $collectionAttribute->setAttribute( 'data_int', $multioptionValue );
207 return true;
208 }
209
224 function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
225 {
226 $actionlist = explode( "_", $action );
227 if ( $actionlist[0] == "new-option" )
228 {
229 $multioption = $contentObjectAttribute->content();
230
231 $multioption->addOption( ( $actionlist[1] - 1 ), "", "", "");
232 $contentObjectAttribute->setContent( $multioption );
233 $contentObjectAttribute->store();
234 }
235 else if ( $actionlist[0] == "remove-selected-option" )
236 {
237 $multioption = $contentObjectAttribute->content();
238 $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" ) . "_" . $actionlist[1];
239 $array_remove = $http->hasPostVariable( $postvarname ) ? $http->postVariable( $postvarname ) : array();
240 $multioption->removeOptions( $array_remove, $actionlist[1] - 1 );
241 $contentObjectAttribute->setContent( $multioption );
242 $contentObjectAttribute->store();
243 }
244 else
245 {
246 switch ( $action )
247 {
248 case "new_multioption" :
249 {
250 $multioption = $contentObjectAttribute->content();
251 $newID = $multioption->addMultiOption( "" ,0,false );
252 $multioption->addOption( $newID, "", "", "" );
253 $multioption->addOption( $newID, "" ,"", "" );
254 $contentObjectAttribute->setContent( $multioption );
255 $contentObjectAttribute->store();
256 } break;
257
258 case "remove_selected_multioption":
259 {
260 $multioption = $contentObjectAttribute->content();
261 $postvarname = "ContentObjectAttribute" . "_data_multioption_remove_" . $contentObjectAttribute->attribute( "id" );
262 $array_remove = $http->hasPostVariable( $postvarname )? $http->postVariable( $postvarname ) : array();
263 $multioption->removeMultiOptions( $array_remove );
264 $contentObjectAttribute->setContent( $multioption );
265 $contentObjectAttribute->store();
266 } break;
267
268 default:
269 {
270 eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZMultiOptionType" );
271 } break;
272 }
273 }
274 }
275
281 function productOptionInformation( $objectAttribute, $optionID, $productItem )
282 {
283 $multioption = $objectAttribute->attribute( 'content' );
284
285 foreach ( $multioption->attribute( 'multioption_list' ) as $multioptionElement )
286 {
287 foreach ( $multioptionElement['optionlist'] as $option )
288 {
289 if ( $option['option_id'] != $optionID )
290 continue;
291
292 return array( 'id' => $option['option_id'],
293 'name' => $multioptionElement['name'],
294 'value' => $option['value'],
295 'additional_price' => $option['additional_price'] );
296 }
297 }
298 }
299
300 function title( $contentObjectAttribute, $name = "name" )
301 {
302 $multioption = $contentObjectAttribute->content();
303 return $multioption->attribute( $name );
304 }
305
309 function hasObjectAttributeContent( $contentObjectAttribute )
310 {
311 $multioption = $contentObjectAttribute->content();
312 $multioptions = $multioption->attribute( 'multioption_list' );
313 return count( $multioptions ) > 0;
314 }
315
319 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
320 {
321 if ( $currentVersion == false )
322 {
323 $multioption = $contentObjectAttribute->content();
324 if ( $multioption )
325 {
326 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
327 $multioption->setName( $contentClassAttribute->attribute( 'data_text1' ) );
328 $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
329 $contentObjectAttribute->setContent( $multioption );
330 }
331 }
332 else
333 {
334 $dataText = $originalContentObjectAttribute->attribute( "data_text" );
335 $contentObjectAttribute->setAttribute( "data_text", $dataText );
336 }
337 }
338
339 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
340 {
341 $defaultValueName = $base . self::DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
342 if ( $http->hasPostVariable( $defaultValueName ) )
343 {
344 $defaultValueValue = $http->postVariable( $defaultValueName );
345
346 if ( $defaultValueValue == "" )
347 {
348 $defaultValueValue = "";
349 }
350 $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
351 return true;
352 }
353 return false;
354 }
355
356 function toString( $contentObjectAttribute )
357 {
358
359 $content = $contentObjectAttribute->attribute( 'content' );
360
361 $multioptionArray = array();
362
363 $setName = $content->attribute( 'name' );
364 $multioptionArray[] = $setName;
365
366 $multioptionList = $content->attribute( 'multioption_list' );
367
368 foreach ( $multioptionList as $key => $option )
369 {
370 $optionArray = array();
371 $optionArray[] = $option['name'];
372 $optionArray[] = $option['default_option_id'];
373 foreach ( $option['optionlist'] as $key => $value )
374 {
375 $optionArray[] = $value['value'];
376 $optionArray[] = $value['additional_price'];
377 }
378 $multioptionArray[] = eZStringUtils::implodeStr( $optionArray, '|' );
379 }
380 return eZStringUtils::implodeStr( $multioptionArray, "&" );
381 }
382
383
384 function fromString( $contentObjectAttribute, $string )
385 {
386 if ( $string == '' )
387 return true;
388
389 $multioptionArray = eZStringUtils::explodeStr( $string, '&' );
390
391 $multioption = new eZMultiOption( "" );
392
393 $multioption->OptionCounter = 0;
394 $multioption->Options = array();
395 $multioption->Name = array_shift( $multioptionArray );
396 $priority = 1;
397 foreach ( $multioptionArray as $multioptionStr )
398 {
399 $optionArray = eZStringUtils::explodeStr( $multioptionStr, '|' );
400
401
402 $newID = $multioption->addMultiOption( array_shift( $optionArray ),
403 $priority,
404 array_shift( $optionArray ) );
405 $optionID = 0;
406 $count = count( $optionArray );
407 for ( $i = 0; $i < $count; $i +=2 )
408 {
409 $multioption->addOption( $newID, $optionID, array_shift( $optionArray ), array_shift( $optionArray ) );
410 $optionID++;
411 }
412 $priority++;
413 }
414
415 $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
416
417 return $multioption;
418
419 }
420
421 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
422 {
423 $defaultValue = $classAttribute->attribute( 'data_text1' );
424 $dom = $attributeParametersNode->ownerDocument;
425 $defaultValueNode = $dom->createElement( 'default-value' );
426 $defaultValueNode->appendChild( $dom->createTextNode( $defaultValue ) );
427 $attributeParametersNode->appendChild( $defaultValueNode );
428 }
429
430 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
431 {
432 $defaultValue = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 )->textContent;
433 $classAttribute->setAttribute( 'data_text1', $defaultValue );
434 }
435
436 function serializeContentObjectAttribute( $package, $objectAttribute )
437 {
438 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
439
440 $dom = new DOMDocument( '1.0', 'utf-8' );
441 $success = $dom->loadXML( $objectAttribute->attribute( 'data_text' ) );
442
443 $importedRoot = $node->ownerDocument->importNode( $dom->documentElement, true );
444 $node->appendChild( $importedRoot );
445
446 return $node;
447 }
448
449 function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
450 {
451 $rootNode = $attributeNode->getElementsByTagName( 'ezmultioption' )->item( 0 );
452 $xmlString = $rootNode ? $rootNode->ownerDocument->saveXML( $rootNode ) : '';
453 $objectAttribute->setAttribute( 'data_text', $xmlString );
454 }
455 }
456
457 eZDataType::register( eZMultiOptionType::DATA_TYPE_STRING, "eZMultiOptionType" );
458
459 ?>
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
eZMultiOptionType
A datatype which works with multiple options.
Definition: ezmultioptiontype.php:33
eZMultiOptionType\productOptionInformation
productOptionInformation( $objectAttribute, $optionID, $productItem)
Definition: ezmultioptiontype.php:281
eZMultiOptionType\customObjectAttributeHTTPAction
customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters)
Definition: ezmultioptiontype.php:224
eZMultiOptionType\toString
toString( $contentObjectAttribute)
Definition: ezmultioptiontype.php:356
eZMultiOptionType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezmultioptiontype.php:158
eZMultiOptionType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezmultioptiontype.php:47
eZMultiOptionType\isIndexable
isIndexable()
Definition: ezmultioptiontype.php:142
eZMultiOptionType\__construct
__construct()
Definition: ezmultioptiontype.php:37
eZMultiOptionType\title
title( $contentObjectAttribute, $name="name")
Definition: ezmultioptiontype.php:300
eZMultiOptionType\fromString
fromString( $contentObjectAttribute, $string)
Definition: ezmultioptiontype.php:384
eZMultiOptionType\storeObjectAttribute
storeObjectAttribute( $contentObjectAttribute)
Definition: ezmultioptiontype.php:126
eZMultiOptionType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezmultioptiontype.php:339
eZMultiOptionType\serializeContentObjectAttribute
serializeContentObjectAttribute( $package, $objectAttribute)
Definition: ezmultioptiontype.php:436
eZMultiOptionType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezmultioptiontype.php:35
eZMultiOptionType\serializeContentClassAttribute
serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezmultioptiontype.php:421
eZMultiOptionType\DEFAULT_NAME_VARIABLE
const DEFAULT_NAME_VARIABLE
Definition: ezmultioptiontype.php:34
eZMultiOptionType\unserializeContentObjectAttribute
unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode)
Definition: ezmultioptiontype.php:449
eZMultiOptionType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezmultioptiontype.php:309
eZMultiOptionType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezmultioptiontype.php:135
eZMultiOptionType\unserializeContentClassAttribute
unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezmultioptiontype.php:430
eZMultiOptionType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: ezmultioptiontype.php:319
eZMultiOptionType\fetchCollectionAttributeHTTPInput
fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute)
Definition: ezmultioptiontype.php:203
eZMultiOptionType\metaData
metaData( $contentObjectAttribute)
Definition: ezmultioptiontype.php:150
eZMultiOption
Encapsulates multiple options in one datatype.
Definition: ezmultioption.php:114
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+.