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
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 include_once( "kernel/classes/ezdatatype.php" );
00053 include_once( "kernel/classes/datatypes/ezmultioption/ezmultioption.php" );
00054 include_once( 'lib/ezutils/classes/ezstringutils.php' );
00055 define( "EZ_MULTIOPTION_DEFAULT_NAME_VARIABLE", "_ezmultioption_default_name_" );
00056 define( "EZ_DATATYPESTRING_MULTIOPTION", "ezmultioption" );
00057
00058 class eZMultiOptionType extends eZDataType
00059 {
00060
00061
00062
00063 function eZMultiOptionType()
00064 {
00065 $this->eZDataType( EZ_DATATYPESTRING_MULTIOPTION, ezi18n( 'kernel/classes/datatypes', "Multi-option", 'Datatype name' ),
00066 array( 'serialize_supported' => true ) );
00067 }
00068
00069
00070
00071
00072
00073 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00074 {
00075 $count = 0;
00076 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00077 if ( $http->hasPostVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) ) )
00078 {
00079 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00080 $multioptionIDArray = $http->postVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) );
00081
00082 foreach ( $multioptionIDArray as $id )
00083 {
00084 $multioptionName = $http->postVariable( $base . "_data_multioption_name_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
00085 $optionIDArray = $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00086 ? $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00087 : array();
00088 $optionCountArray = $http->hasPostVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00089 ? $http->postVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00090 : array();
00091 $optionValueArray = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00092 ? $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00093 : array();
00094 $optionAdditionalPriceArray = $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00095 ? $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00096 : array();
00097 for ( $i = 0; $i < count( $optionIDArray ); $i++ )
00098 {
00099 if ( $contentObjectAttribute->validateIsRequired() and !$classAttribute->attribute( 'is_information_collector' ) )
00100 {
00101 if ( trim( $optionValueArray[$i] ) == "" )
00102 {
00103 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00104 'The option value must be provided.' ) );
00105 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00106 }
00107 else
00108 ++$count;
00109 }
00110
00111 if ( trim( $optionValueArray[$i] ) != "" )
00112 {
00113 if ( strlen( $optionAdditionalPriceArray[$i] ) && !preg_match( "#^[-|+]?[0-9]+(\.){0,1}[0-9]{0,2}$#", $optionAdditionalPriceArray[$i] ) )
00114 {
00115 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00116 'The additional price for the multioption value is not valid.' ) );
00117 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00118 }
00119 }
00120
00121 }
00122 }
00123 }
00124 if ( $contentObjectAttribute->validateIsRequired() and
00125 !$classAttribute->attribute( 'is_information_collector' ) )
00126 {
00127 if ( $count == 0 )
00128 {
00129 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00130 'At least one option is required.' ) );
00131 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00132 }
00133
00134 $optionSetName = $http->hasPostVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) )
00135 ? $http->postVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) )
00136 : '';
00137 if ( trim( $optionSetName ) == '' )
00138 {
00139 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00140 'Option set name is required.' ) );
00141 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00142 }
00143 }
00144
00145
00146 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00147 }
00148
00149
00150
00151
00152 function storeObjectAttribute( &$contentObjectAttribute )
00153 {
00154 $multioption =& $contentObjectAttribute->content();
00155 $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
00156 }
00157
00158
00159
00160
00161 function &objectAttributeContent( &$contentObjectAttribute )
00162 {
00163 $multioption = new eZMultiOption( "" );
00164 $multioption->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
00165 return $multioption;
00166 }
00167
00168
00169
00170
00171 function isIndexable()
00172 {
00173 return true;
00174 }
00175
00176
00177
00178
00179 function metaData( $contentObjectAttribute )
00180 {
00181 return $contentObjectAttribute->attribute( "data_text" );
00182 }
00183
00184
00185
00186
00187 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00188 {
00189 $multioptionIDArray = $http->hasPostVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) )
00190 ? $http->postVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) )
00191 : array();
00192 $optionSetName = $http->postVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) );
00193 $multioption = new eZMultiOption( $optionSetName );
00194 foreach ( $multioptionIDArray as $id )
00195 {
00196 $multioptionName = $http->postVariable( $base . "_data_multioption_name_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
00197 $optionIDArray = $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00198 ? $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00199 : array();
00200
00201 $optionPriority = $http->postVariable( $base . "_data_multioption_priority_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
00202
00203 if ( $http->hasPostVariable( $base . "_data_radio_checked_" . $contentObjectAttribute->attribute("id") . '_' . $id ) )
00204 $optionDefaultValue = $http->postVariable( $base . "_data_radio_checked_" . $contentObjectAttribute->attribute("id") . '_' . $id );
00205 else
00206 $optionDefaultValue = '';
00207 $newID = $multioption->addMultiOption( $multioptionName,$optionPriority, $optionDefaultValue );
00208
00209 $optionCountArray = $http->hasPostVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00210 ? $http->postVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00211 : array();
00212 $optionValueArray = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00213 ? $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00214 : array();
00215 $optionAdditionalPriceArray = $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00216 ? $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
00217 : array();
00218
00219 for ( $i = 0; $i < count( $optionIDArray ); $i++ )
00220 $multioption->addOption( $newID, $optionCountArray[$i], $optionValueArray[$i], $optionAdditionalPriceArray[$i] );
00221 }
00222
00223 $multioption->sortMultiOptions();
00224 $multioption->resetOptionCounter();
00225 $contentObjectAttribute->setContent( $multioption );
00226 return true;
00227 }
00228
00229
00230
00231
00232 function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
00233 {
00234 $multioptionValue = $http->postVariable( $base . "_data_multioption_value_" . $contentObjectAttribute->attribute( "id" ) );
00235 $collectionAttribute->setAttribute( 'data_int', $multioptionValue );
00236 return true;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
00254 {
00255 $actionlist = explode( "_", $action );
00256 if ( $actionlist[0] == "new-option" )
00257 {
00258 $multioption =& $contentObjectAttribute->content();
00259
00260 $multioption->addOption( ( $actionlist[1] - 1 ), "", "", "");
00261 $contentObjectAttribute->setContent( $multioption );
00262 $contentObjectAttribute->store();
00263 }
00264 else if ( $actionlist[0] == "remove-selected-option" )
00265 {
00266 $multioption =& $contentObjectAttribute->content();
00267 $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" ) . "_" . $actionlist[1];
00268 $array_remove = $http->hasPostVariable( $postvarname ) ? $http->postVariable( $postvarname ) : array();
00269 $multioption->removeOptions( $array_remove, $actionlist[1] - 1 );
00270 $contentObjectAttribute->setContent( $multioption );
00271 $contentObjectAttribute->store();
00272 }
00273 else
00274 {
00275 switch ( $action )
00276 {
00277 case "new_multioption" :
00278 {
00279 $multioption =& $contentObjectAttribute->content();
00280 $newID = $multioption->addMultiOption( "" ,0,false );
00281 $multioption->addOption( $newID, "", "", "" );
00282 $multioption->addOption( $newID, "" ,"", "" );
00283 $contentObjectAttribute->setContent( $multioption );
00284 $contentObjectAttribute->store();
00285 } break;
00286
00287 case "remove_selected_multioption":
00288 {
00289 $multioption =& $contentObjectAttribute->content();
00290 $postvarname = "ContentObjectAttribute" . "_data_multioption_remove_" . $contentObjectAttribute->attribute( "id" );
00291 $array_remove = $http->hasPostVariable( $postvarname )? $http->postVariable( $postvarname ) : array();
00292 $multioption->removeMultiOptions( $array_remove );
00293 $contentObjectAttribute->setContent( $multioption );
00294 $contentObjectAttribute->store();
00295 } break;
00296
00297 default:
00298 {
00299 eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZMultiOptionType" );
00300 } break;
00301 }
00302 }
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 function productOptionInformation( &$objectAttribute, $optionID, &$productItem )
00312 {
00313 $multioption =& $objectAttribute->attribute( 'content' );
00314
00315 foreach ( $multioption->attribute( 'multioption_list' ) as $multioptionElement )
00316 {
00317 foreach ( $multioptionElement['optionlist'] as $option )
00318 {
00319 if ( $option['option_id'] != $optionID )
00320 continue;
00321
00322 return array( 'id' => $option['option_id'],
00323 'name' => $multioptionElement['name'],
00324 'value' => $option['value'],
00325 'additional_price' => $option['additional_price'] );
00326 }
00327 }
00328 }
00329
00330
00331
00332
00333 function title( &$contentObjectAttribute, $name = "name" )
00334 {
00335 $multioption =& $contentObjectAttribute->content();
00336 $value = $multioption->attribute( $name );
00337 return $value;
00338 }
00339
00340
00341
00342
00343
00344 function hasObjectAttributeContent( &$contentObjectAttribute )
00345 {
00346 $multioption =& $contentObjectAttribute->content();
00347 $multioptions = $multioption->attribute( 'multioption_list' );
00348 return count( $multioptions ) > 0;
00349 }
00350
00351
00352
00353
00354 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00355 {
00356 if ( $currentVersion == false )
00357 {
00358 $multioption =& $contentObjectAttribute->content();
00359 if ( $multioption )
00360 {
00361 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00362 $multioption->setName( $contentClassAttribute->attribute( 'data_text1' ) );
00363 $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
00364 $contentObjectAttribute->setContent( $multioption );
00365 }
00366 }
00367 else
00368 {
00369 $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00370 $contentObjectAttribute->setAttribute( "data_text", $dataText );
00371 }
00372 }
00373
00374
00375
00376
00377 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00378 {
00379 $defaultValueName = $base . EZ_MULTIOPTION_DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
00380 if ( $http->hasPostVariable( $defaultValueName ) )
00381 {
00382 $defaultValueValue = $http->postVariable( $defaultValueName );
00383
00384 if ( $defaultValueValue == "" )
00385 {
00386 $defaultValueValue = "";
00387 }
00388 $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
00389 return true;
00390 }
00391 return false;
00392 }
00393
00394 function toString( $contentObjectAttribute )
00395 {
00396
00397 $content = $contentObjectAttribute->attribute( 'content' );
00398
00399 $multioptionArray = array();
00400
00401 $setName = $content->attribute( 'name' );
00402 $multioptionArray[] = $setName;
00403
00404 $multioptionList = $content->attribute( 'multioption_list' );
00405
00406 foreach ( $multioptionList as $key => $option )
00407 {
00408 $optionArray = array();
00409 $optionArray[] = $option['name'];
00410 $optionArray[] = $option['default_option_id'];
00411 foreach ( $option['optionlist'] as $key => $value )
00412 {
00413 $optionArray[] = $value['value'];
00414 $optionArray[] = $value['additional_price'];
00415 }
00416 $multioptionArray[] = eZStringUtils::implodeStr( $optionArray, '|' );
00417 }
00418 return eZStringUtils::implodeStr( $multioptionArray, "&" );
00419 }
00420
00421
00422 function fromString( &$contentObjectAttribute, $string )
00423 {
00424 if ( $string == '' )
00425 return true;
00426
00427 $multioptionArray = eZStringUtils::explodeStr( $string, '&' );
00428
00429 $multioption = new eZMultiOption( "" );
00430
00431 $multioption->OptionCounter = 0;
00432 $multioption->Options = array();
00433 $multioption->Name = array_shift( $multioptionArray );
00434 $priority = 1;
00435 foreach ( $multioptionArray as $multioptionStr )
00436 {
00437 $optionArray = eZStringUtils::explodeStr( $multioptionStr, '|' );
00438
00439
00440 $newID = $multioption->addMultiOption( array_shift( $optionArray ),
00441 $priority,
00442 array_shift( $optionArray ) );
00443 $optionID = 0;
00444 $count = count( $optionArray );
00445 for ( $i = 0; $i < $count; $i +=2 )
00446 {
00447 $multioption->addOption( $newID, $optionID, array_shift( $optionArray ), array_shift( $optionArray ) );
00448 $optionID++;
00449 }
00450 $priority++;
00451 }
00452
00453 $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
00454
00455 return $multioption;
00456
00457 }
00458
00459
00460
00461
00462 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00463 {
00464 $defaultValue = $classAttribute->attribute( 'data_text1' );
00465 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'default-value', $defaultValue ) );
00466 }
00467
00468
00469
00470
00471 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00472 {
00473 $defaultValue = $attributeParametersNode->elementTextContentByName( 'default-value' );
00474 $classAttribute->setAttribute( 'data_text1', $defaultValue );
00475 }
00476
00477
00478
00479
00480 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00481 {
00482 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00483
00484 $xml = new eZXML();
00485 $domDocument = $xml->domTree( $objectAttribute->attribute( 'data_text' ) );
00486 $node->appendChild( $domDocument->root() );
00487
00488 return $node;
00489 }
00490
00491
00492
00493
00494 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00495 {
00496 $rootNode = $attributeNode->firstChild();
00497 $xmlString = $rootNode->attributeValue( 'local_name' ) == 'data-text' ? '' : $rootNode->toString( 0 );
00498 $objectAttribute->setAttribute( 'data_text', $xmlString );
00499 }
00500 }
00501
00502 eZDataType::register( EZ_DATATYPESTRING_MULTIOPTION, "ezmultioptiontype" );
00503
00504 ?>