Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezmultioption.php
Go to the documentation of this file.
1 <?php
113 class eZMultiOption
114 {
123 public function __construct( $name )
124 {
125 $this->Name = $name;
126 $this->Options = array();
127 $this->MultiOptionCount = 0;
128 $this->OptionCounter = 0;
129 }
130
138 function addMultiOption( $name, $multiOptionPriority, $defaultValue )
139 {
140 $this->MultiOptionCount += 1;
141 $this->Options[$this->MultiOptionCount] = array( "id" => $this->MultiOptionCount,
142 "name" => $name,
143 'priority'=> $multiOptionPriority,
144 "default_option_id" => $defaultValue,
145 'optionlist' => array() );
146 return $this->MultiOptionCount;
147 }
148
155 function addOption( $newID, $OptionID, $optionValue, $optionAdditionalPrice )
156 {
157 $key = count( $this->Options[$newID]['optionlist'] ) + 1;
158 if ( strlen( $OptionID ) == 0 )
159 {
160 $this->OptionCounter += 1;
161 $OptionID = $this->OptionCounter;
162 }
163 $this->Options[$newID]['optionlist'][] = array( "id" => $key,
164 "option_id" => $OptionID,
165 "value" => $optionValue,
166 'additional_price' => $optionAdditionalPrice );
167 }
168
174 function sortMultiOptions()
175 {
176 usort( $this->Options, function( $a, $b ) {
177 if ( $a['priority'] == $b['priority'] )
178 {
179 return 0;
180 }
181
182 return ( $a['priority'] < $b['priority'] ) ? -1 : 1;
183 });
184 $this->changeMultiOptionID();
185 }
186
190 function resetOptionCounter()
191 {
192 $maxValue = 0;
193 foreach ( $this->Options as $optionList )
194 {
195 foreach ( $optionList['optionlist'] as $option )
196 {
197 if ( $maxValue < $option['option_id'] )
198 {
199 $maxValue = $option['option_id'];
200 }
201 }
202 }
203 $this->OptionCounter = $maxValue;
204 }
205
209 function changeMultiOptionId()
210 {
211 $i = 1 ;
212 foreach ( $this->Options as $key => $opt )
213 {
214 $this->Options[$key]['id'] = $i++;
215 }
216 $this->MultiOptionCount = $i - 1;
217 }
218
226 function removeMultiOptions( $array_remove )
227 {
228 foreach ( $array_remove as $id )
229 {
230 unset( $this->Options[ $id - 1 ] );
231 }
232 $this->Options = array_values( $this->Options );
233 $this->changeMultiOptionId();
234 }
235
243 function removeOptions( $arrayRemove, $optionId )
244 {
245 foreach ( $arrayRemove as $id )
246 {
247 unset( $this->Options[$optionId]['optionlist'][$id - 1] );
248 }
249 $this->Options = array_values( $this->Options );
250 $i = 1;
251 foreach ( $this->Options[$optionId]['optionlist'] as $key => $opt )
252 {
253 $this->Options[$optionId]['optionlist'][$key]['id'] = $i;
254 $i++;
255 }
256 }
257
261 function attributes()
262 {
263 return array( 'name',
264 'multioption_list' );
265 }
266
272 function hasAttribute( $name )
273 {
274 return in_array( $name, $this->attributes() );
275 }
276
282 function attribute( $name )
283 {
284 switch ( $name )
285 {
286 case "name" :
287 {
288 return $this->Name;
289 } break;
290
291 case "multioption_list" :
292 {
293 return $this->Options;
294 } break;
295 default:
296 {
297 eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
298 return null;
299 }break;
300 }
301 }
302
310 function decodeXML( $xmlString )
311 {
312 $this->OptionCounter = 0;
313 $this->Options = array();
314 if ( $xmlString != "" )
315 {
316 $dom = new DOMDocument( '1.0', 'utf-8' );
317 $success = $dom->loadXML( $xmlString );
318
319 $root = $dom->documentElement;
320 // set the name of the node
321 $this->Name = $root->getElementsByTagName( "name" )->item( 0 )->textContent;
322 $this->OptionCounter = $root->getAttribute( "option_counter" );
323 $multioptionsNode = $root->getElementsByTagName( "multioptions" )->item( 0 );
324 $multioptionsList = $multioptionsNode->getElementsByTagName( "multioption" );
325 //Loop for MultiOptions
326 foreach ( $multioptionsList as $multioption )
327 {
328 $newID = $this->addMultiOption( $multioption->getAttribute( "name" ),
329 $multioption->getAttribute( "priority" ),
330 $multioption->getAttribute( "default_option_id" ) );
331 $optionNode = $multioption->getElementsByTagName( "option" );
332 foreach ( $optionNode as $option )
333 {
334 $this->addOption( $newID, $option->getAttribute( "option_id" ), $option->getAttribute( "value" ), $option->getAttribute( "additional_price" ) );
335 }
336 }
337 }
338 }
339
344 function xmlString()
345 {
346 $doc = new DOMDocument( '1.0', 'utf-8' );
347 $root = $doc->createElement( "ezmultioption" );
348 $root->setAttribute( 'option_counter', $this->OptionCounter );
349 $doc->appendChild( $root );
350
351 $nameNode = $doc->createElement( 'name' );
352 $nameNode->appendChild( $doc->createTextNode( $this->Name ) );
353 $root->appendChild( $nameNode );
354
355 $multiOptionsNode = $doc->createElement( "multioptions" );
356 $root->appendChild( $multiOptionsNode );
357
358 foreach ( $this->Options as $multioption )
359 {
360 unset( $multioptionNode );
361 $multioptionNode = $doc->createElement( "multioption" );
362 $multioptionNode->setAttribute( "id", $multioption['id'] );
363 $multioptionNode->setAttribute( "name", $multioption['name'] );
364 $multioptionNode->setAttribute( "priority", $multioption['priority'] );
365 $multioptionNode->setAttribute( 'default_option_id', $multioption['default_option_id'] );
366 foreach ( $multioption['optionlist'] as $option )
367 {
368 unset( $optionNode );
369 $optionNode = $doc->createElement( "option" );
370 $optionNode->setAttribute( "id", $option['id'] );
371 $optionNode->setAttribute( "option_id", $option['option_id'] );
372 $optionNode->setAttribute( "value", $option['value'] );
373 $optionNode->setAttribute( 'additional_price', $option['additional_price'] );
374 $multioptionNode->appendChild( $optionNode );
375 }
376 $multiOptionsNode->appendChild( $multioptionNode );
377 }
378 $xml = $doc->saveXML();
379 return $xml;
380 }
381
384 public $Name;
386 public $Options;
388 public $MultiOptionCount;
390 public $OptionCounter;
391 }
392 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZMultiOption
Encapsulates multiple options in one datatype.
Definition: ezmultioption.php:114
eZMultiOption\attributes
attributes()
Definition: ezmultioption.php:261
eZMultiOption\decodeXML
decodeXML( $xmlString)
Definition: ezmultioption.php:310
eZMultiOption\attribute
attribute( $name)
Definition: ezmultioption.php:282
eZMultiOption\addMultiOption
addMultiOption( $name, $multiOptionPriority, $defaultValue)
Definition: ezmultioption.php:138
eZMultiOption\xmlString
xmlString()
Definition: ezmultioption.php:344
eZMultiOption\$OptionCounter
$OptionCounter
Contains the option counter value.
Definition: ezmultioption.php:390
eZMultiOption\removeOptions
removeOptions( $arrayRemove, $optionId)
Definition: ezmultioption.php:243
eZMultiOption\changeMultiOptionId
changeMultiOptionId()
Definition: ezmultioption.php:209
eZMultiOption\sortMultiOptions
sortMultiOptions()
Definition: ezmultioption.php:174
eZMultiOption\__construct
__construct( $name)
Definition: ezmultioption.php:123
eZMultiOption\$MultiOptionCount
$MultiOptionCount
Contains the multioption counter value.
Definition: ezmultioption.php:388
eZMultiOption\$Name
$Name
Definition: ezmultioption.php:384
eZMultiOption\addOption
addOption( $newID, $OptionID, $optionValue, $optionAdditionalPrice)
Definition: ezmultioption.php:155
eZMultiOption\$Options
$Options
Contains the Options.
Definition: ezmultioption.php:386
eZMultiOption\removeMultiOptions
removeMultiOptions( $array_remove)
Definition: ezmultioption.php:226
eZMultiOption\resetOptionCounter
resetOptionCounter()
Definition: ezmultioption.php:190
eZMultiOption\hasAttribute
hasAttribute( $name)
Definition: ezmultioption.php:272
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.