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