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 include_once( "kernel/classes/ezdatatype.php" );
00041 include_once( "lib/ezxml/classes/ezxml.php" );
00042 include_once( 'lib/ezutils/classes/ezstringutils.php' );
00043
00044 define( "EZ_DATATYPESTRING_EZ_SELECTION", "ezselection" );
00045
00046 class eZSelectionType extends eZDataType
00047 {
00048
00049
00050
00051 function eZSelectionType()
00052 {
00053 $this->eZDataType( EZ_DATATYPESTRING_EZ_SELECTION, ezi18n( 'kernel/classes/datatypes', "Selection", 'Datatype name' ),
00054 array( 'serialize_supported' => true ) );
00055 }
00056
00057
00058
00059
00060
00061
00062 function validateClassAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00063 {
00064 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00065 }
00066
00067
00068
00069
00070
00071 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00072 {
00073 $attributeContent =& $this->classAttributeContent( $classAttribute );
00074 $classAttributeID = $classAttribute->attribute( 'id' );
00075 $isMultipleSelection = false;
00076
00077 if ( $http->hasPostVariable( $base . "_ezselection_ismultiple_value_" . $classAttributeID ) )
00078 {
00079 if( $http->postVariable( $base . "_ezselection_ismultiple_value_" . $classAttributeID ) != 0 )
00080 {
00081 $isMultipleSelection = true;
00082 }
00083 }
00084
00085 $currentOptions = $attributeContent['options'];
00086 $hasPostData = false;
00087
00088 if ( $http->hasPostVariable( $base . "_ezselection_option_name_array_" . $classAttributeID ) )
00089 {
00090 $nameArray = $http->postVariable( $base . "_ezselection_option_name_array_" . $classAttributeID );
00091
00092
00093 foreach ( array_keys( $currentOptions ) as $key )
00094 {
00095 $currentOptions[$key]['name'] = $nameArray[$currentOptions[$key]['id']];
00096 }
00097 $hasPostData = true;
00098
00099 }
00100
00101 if ( $http->hasPostVariable( $base . "_ezselection_newoption_button_" . $classAttributeID ) )
00102 {
00103 $currentCount = 0;
00104 foreach ( $currentOptions as $option )
00105 {
00106 $currentCount = max( $currentCount, $option['id'] );
00107 }
00108 $currentCount += 1;
00109 $currentOptions[] = array( 'id' => $currentCount,
00110 'name' => '' );
00111 $hasPostData = true;
00112
00113 }
00114
00115 if ( $http->hasPostVariable( $base . "_ezselection_removeoption_button_" . $classAttributeID ) )
00116 {
00117 if ( $http->hasPostVariable( $base . "_ezselection_option_remove_array_". $classAttributeID ) )
00118 {
00119 $removeArray = $http->postVariable( $base . "_ezselection_option_remove_array_". $classAttributeID );
00120
00121 foreach ( array_keys( $currentOptions ) as $key )
00122 {
00123 if ( isset( $removeArray[$currentOptions[$key]['id']] ) and
00124 $removeArray[$currentOptions[$key]['id']] )
00125 unset( $currentOptions[$key] );
00126 }
00127 $hasPostData = true;
00128 }
00129 }
00130
00131 if ( $hasPostData )
00132 {
00133
00134
00135 $doc = new eZDOMDocument( "selection" );
00136 $root = $doc->createElementNode( "ezselection" );
00137 $doc->setRoot( $root );
00138
00139 $options = $doc->createElementNode( "options" );
00140
00141 $root->appendChild( $options );
00142 foreach ( $currentOptions as $optionArray )
00143 {
00144 unset( $optionNode );
00145 $optionNode = $doc->createElementNode( "option" );
00146 $optionNode->appendAttribute( $doc->createAttributeNode( "id", $optionArray['id'] ) );
00147 $optionNode->appendAttribute( $doc->createAttributeNode( 'name', $optionArray['name'] ) );
00148
00149 $options->appendChild( $optionNode );
00150 }
00151
00152 $xml = $doc->toString();
00153
00154 $classAttribute->setAttribute( "data_text5", $xml );
00155
00156 if ( $isMultipleSelection == true )
00157 $classAttribute->setAttribute( "data_int1", 1 );
00158 else
00159 $classAttribute->setAttribute( "data_int1", 0 );
00160 }
00161 return true;
00162 }
00163
00164
00165
00166
00167
00168 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00169 {
00170 if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00171 {
00172 $data = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00173 $classAttribute = $contentObjectAttribute->contentClassAttribute();
00174
00175 if ( $data == "" )
00176 {
00177 if ( !$classAttribute->attribute( 'is_information_collector' ) &&
00178 $contentObjectAttribute->validateIsRequired() )
00179 {
00180 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00181 'Input required.' ) );
00182 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00183 }
00184 }
00185 }
00186 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00187 }
00188
00189
00190
00191
00192
00193 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00194 {
00195 if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00196 {
00197 $selectOptions = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00198 $idString = ( is_array( $selectOptions ) ? implode( '-', $selectOptions ) : "" );
00199 $contentObjectAttribute->setAttribute( 'data_text', $idString );
00200 return true;
00201 }
00202 return false;
00203 }
00204
00205
00206
00207
00208 function validateCollectionAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00209 {
00210 if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00211 {
00212 $data = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00213
00214 if ( $data == "" && $contentObjectAttribute->validateIsRequired() )
00215 {
00216 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 'Input required.' ) );
00217 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00218 }
00219 else
00220 {
00221 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00222 }
00223 }
00224 else
00225 {
00226 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00227 }
00228 }
00229
00230
00231
00232
00233
00234 function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
00235 {
00236 if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00237 {
00238 $selectOptions = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00239 $idString = ( is_array( $selectOptions ) ? implode( '-', $selectOptions ) : "" );
00240 $collectionAttribute->setAttribute( 'data_text', $idString );
00241 return true;
00242 }
00243 return false;
00244 }
00245
00246
00247
00248
00249 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00250 {
00251 if ( $currentVersion != false )
00252 {
00253 $idString = $originalContentObjectAttribute->attribute( "data_text" );
00254 $contentObjectAttribute->setAttribute( "data_text", $idString );
00255 $contentObjectAttribute->store();
00256 }
00257 }
00258
00259
00260
00261
00262 function &objectAttributeContent( &$contentObjectAttribute )
00263 {
00264 $idString = explode( '-', $contentObjectAttribute->attribute( 'data_text' ) );
00265 return $idString;
00266 }
00267
00268
00269
00270
00271 function &classAttributeContent( &$classAttribute )
00272 {
00273 $xml = new eZXML();
00274 $xmlString =& $classAttribute->attribute( 'data_text5' );
00275 $dom =& $xml->domTree( $xmlString );
00276 if ( $dom )
00277 {
00278 $options =& $dom->elementsByName( 'option' );
00279 $optionArray = array();
00280 foreach ( $options as $optionNode )
00281 {
00282 $optionArray[] = array( 'id' => $optionNode->attributeValue( 'id' ),
00283 'name' => $optionNode->attributeValue( 'name' ) );
00284 }
00285 }
00286 else
00287 {
00288 $optionArray[] = array( 'id' => 0,
00289 'name' => '' );
00290 }
00291 $attrValue = array( 'options' => $optionArray,
00292 'is_multiselect' => $classAttribute->attribute( 'data_int1' ) );
00293 return $attrValue;
00294 }
00295
00296
00297
00298
00299 function metaData( $contentObjectAttribute )
00300 {
00301 $selected = $this->objectAttributeContent( $contentObjectAttribute );
00302 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00303 $return = '';
00304 if ( count( $selected ) == 0)
00305 {
00306 return '';
00307 }
00308
00309 $count = 0;
00310 $optionArray = $classContent['options'];
00311 foreach ( $selected as $id )
00312 {
00313 if ( $count++ != 0 )
00314 $return .= ' ';
00315 foreach ( $optionArray as $option )
00316 {
00317 $optionID = $option['id'];
00318 if ( $optionID == $id )
00319 $return .= $option['name'];
00320 }
00321 }
00322 return $return;
00323 }
00324
00325 function toString( $contentObjectAttribute )
00326 {
00327 $selected = $this->objectAttributeContent( $contentObjectAttribute );
00328 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00329
00330 if ( count( $selected ) )
00331 {
00332 $optionArray = $classContent['options'];
00333 foreach ( $selected as $id )
00334 {
00335 foreach ( $optionArray as $option )
00336 {
00337 $optionID = $option['id'];
00338 if ( $optionID == $id )
00339 $returnData[] = $option['name'];
00340 }
00341 }
00342 return eZStringUtils::implodeStr( $returnData, '|' );
00343 }
00344 return '';
00345 }
00346
00347
00348 function fromString( &$contentObjectAttribute, $string )
00349 {
00350 if ( $string == '' )
00351 return true;
00352 $selectedNames = eZStringUtils::explodeStr( $string, '|' );
00353 $selectedIDList = array();
00354 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00355 $optionArray = $classContent['options'];
00356 foreach ( $selectedNames as $name )
00357 {
00358 foreach ( $optionArray as $option )
00359 {
00360 $optionName = $option['name'];
00361 if ( $optionName == $name )
00362 $selectedIDList[] = $option['id'];
00363 }
00364 }
00365 $idString = ( is_array( $selectedIDList ) ? implode( '-', $selectedIDList ) : "" );
00366 $contentObjectAttribute->setAttribute( 'data_text', $idString );
00367 return true;
00368 }
00369
00370
00371
00372
00373 function title( &$contentObjectAttribute )
00374 {
00375 $selected = $this->objectAttributeContent( $contentObjectAttribute );
00376 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00377 $return = '';
00378 if ( count( $selected ) )
00379 {
00380 $selectedNames = array();
00381 foreach ( $classContent['options'] as $option )
00382 {
00383 if ( in_array( $option['id'], $selected ) )
00384 $selectedNames[] = $option['name'];
00385 }
00386 $return = implode( ', ', $selectedNames );
00387 }
00388 return $return;
00389 }
00390
00391 function hasObjectAttributeContent( &$contentObjectAttribute )
00392 {
00393 return true;
00394 }
00395
00396
00397
00398
00399 function sortKey( &$contentObjectAttribute )
00400 {
00401 return strtolower( $contentObjectAttribute->attribute( 'data_text' ) );
00402 }
00403
00404
00405
00406
00407 function sortKeyType()
00408 {
00409 return 'string';
00410 }
00411
00412
00413
00414
00415 function isIndexable()
00416 {
00417 return true;
00418 }
00419
00420
00421
00422
00423 function isInformationCollector()
00424 {
00425 return true;
00426 }
00427
00428
00429
00430
00431 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00432 {
00433 $xml = new eZXML();
00434
00435 $isMultipleSelection =& $classAttribute->attribute( 'data_int1' );
00436 $xmlString =& $classAttribute->attribute( 'data_text5' );
00437
00438 $dom =& $xml->domTree( $xmlString );
00439 $domRoot =& $dom->root();
00440 $options =& $domRoot->elementByName( 'options' );
00441
00442 $attributeParametersNode->appendChild( $options );
00443 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'is-multiselect', $isMultipleSelection ) );
00444 }
00445
00446
00447
00448
00449 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00450 {
00451 $options =& $attributeParametersNode->elementByName( 'options' );
00452
00453 $doc = new eZDOMDocument( "selection" );
00454 $root = $doc->createElementNode( "ezselection" );
00455
00456 $doc->setRoot( $root );
00457 $root->appendChild( $options );
00458
00459 $xml = $doc->toString();
00460 $classAttribute->setAttribute( "data_text5", $xml );
00461
00462 if ( $attributeParametersNode->elementTextContentByName( 'is-multiselect' ) == 0 )
00463 $classAttribute->setAttribute( "data_int1", 0 );
00464 else
00465 $classAttribute->setAttribute( "data_int1", 1 );
00466 }
00467 }
00468
00469
00470
00471 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00472 {
00473 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00474 $idString = $objectAttribute->attribute( 'data_text' );
00475
00476 $node->appendChild( eZDOMDocument::createElementTextNode( 'idstring', $idString ) );
00477 return $node;
00478 }
00479
00480
00481
00482
00483 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00484 {
00485 $idString = $attributeNode->elementTextContentByName( 'idstring' );
00486
00487 if ( $idString === false )
00488 $idString = '';
00489
00490 $objectAttribute->setAttribute( 'data_text', $idString );
00491 }
00492
00493 eZDataType::register( EZ_DATATYPESTRING_EZ_SELECTION, "ezselectiontype" );
00494 ?>