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 include_once( 'kernel/classes/ezdatatype.php' );
00042 include_once( 'kernel/classes/datatypes/ezenum/ezenum.php' );
00043 include_once( 'kernel/classes/ezcontentobjectattribute.php' );
00044 define( 'EZ_DATATYPESTRING_ENUM', 'ezenum' );
00045 define( 'EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD', 'data_int1' );
00046 define( 'EZ_DATATYPESTRING_ENUM_ISMULTIPLE_VARIABLE', '_ezenum_ismultiple_value_' );
00047 define( 'EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD', 'data_int2' );
00048 define( 'EZ_DATATYPESTRING_ENUM_ISOPTION_VARIABLE', '_ezenum_isoption_value_' );
00049
00050 class eZEnumType extends eZDataType
00051 {
00052
00053
00054
00055 function eZEnumType()
00056 {
00057 $this->eZDataType( EZ_DATATYPESTRING_ENUM, ezi18n( 'kernel/classes/datatypes', 'Enum', 'Datatype name' ),
00058 array( 'serialize_supported' => true ) );
00059 }
00060
00061
00062
00063
00064 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00065 {
00066 if ( $currentVersion != false )
00067 {
00068 $originalContentObjectAttributeID = $originalContentObjectAttribute->attribute( 'id' );
00069 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00070 $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00071
00072 $db =& eZDB::instance();
00073 $db->begin();
00074
00075
00076 if ( $originalContentObjectAttributeID != $contentObjectAttributeID )
00077 $this->deleteStoredObjectAttribute( $contentObjectAttribute, $currentVersion );
00078
00079 $newVersionEnumObject = eZEnumObjectValue::fetchAllElements( $originalContentObjectAttributeID, $currentVersion );
00080
00081 for ( $i = 0; $i < count( $newVersionEnumObject ); ++$i )
00082 {
00083 $enumobjectvalue = $newVersionEnumObject[$i];
00084 $enumobjectvalue->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) );
00085 $enumobjectvalue->setAttribute( 'contentobject_attribute_version', $contentObjectAttributeVersion );
00086 $enumobjectvalue->store();
00087 }
00088
00089 $db->commit();
00090 }
00091 }
00092
00093
00094
00095
00096 function cloneClassAttribute( &$oldClassAttribute, &$newClassAttribute )
00097 {
00098 $oldContentClassAttributeID = $oldClassAttribute->attribute( 'id' );
00099 $oldEnums = eZEnumValue::fetchAllElements( $oldContentClassAttributeID, 0 );
00100
00101 $db =& eZDB::instance();
00102 $db->begin();
00103
00104 foreach ( $oldEnums as $oldEnum )
00105 {
00106 $enum =& $oldEnum->clone();
00107 $enum->setAttribute( 'contentclass_attribute_id', $newClassAttribute->attribute( 'id' ) );
00108 $enum->setAttribute( 'contentclass_attribute_version', $newClassAttribute->attribute( 'version' ) );
00109 $enum->store();
00110 }
00111
00112 $db->commit();
00113 }
00114
00115
00116
00117
00118 function initializeClassAttribute( &$classAttribute )
00119 {
00120 $contentClassAttributeID = $classAttribute->attribute( 'id' );
00121 $enums = eZEnumValue::fetchAllElements( $contentClassAttributeID, 1 );
00122
00123 if ( count ( $enums ) == 0 )
00124 {
00125 $enums = eZEnumValue::fetchAllElements( $contentClassAttributeID, 0 );
00126 $db =& eZDB::instance();
00127 $db->begin();
00128 foreach ( $enums as $enum )
00129 {
00130 $enum->setAttribute( 'contentclass_attribute_version', 1 );
00131 $enum->store();
00132 }
00133 $db->commit();
00134 }
00135 }
00136
00137
00138
00139
00140 function deleteStoredObjectAttribute( &$contentObjectAttribute, $version = null )
00141 {
00142 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00143 eZEnumObjectValue::removeAllElements( $contentObjectAttributeID, $version );
00144
00145 }
00146
00147
00148
00149
00150 function deleteStoredClassAttribute( &$contentClassAttribute, $version = null )
00151 {
00152 $contentClassAttributeID = $contentClassAttribute->attribute( 'id' );
00153 eZEnumValue::removeAllElements( $contentClassAttributeID, $version );
00154
00155 }
00156
00157
00158
00159
00160 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00161 {
00162 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00163 $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00164 $enumID = $base . '_data_enumid_' . $contentObjectAttributeID;
00165 $enumElement = $base . '_data_enumelement_' . $contentObjectAttributeID;
00166 $enumValue = $base . '_data_enumvalue_' . $contentObjectAttributeID;
00167 $selectedEnumElement = $base . '_select_data_enumelement_' . $contentObjectAttributeID;
00168 if ( $http->hasPostVariable( $enumID ) &&
00169 $http->hasPostVariable( $enumElement ) &&
00170 $http->hasPostVariable( $enumValue ) )
00171 {
00172 $array_enumID = $http->postVariable( $enumID );
00173 $array_enumElement = $http->postVariable( $enumElement );
00174 $array_enumValue = $http->postVariable( $enumValue );
00175
00176 if ( $http->hasPostVariable( $selectedEnumElement ) )
00177 $array_selectedEnumElement = $http->postVariable( $selectedEnumElement );
00178 else
00179 $array_selectedEnumElement = null;
00180
00181 $db =& eZDB::instance();
00182 $db->begin();
00183
00184
00185 eZEnum::removeObjectEnumerations( $contentObjectAttributeID, $contentObjectAttributeVersion );
00186 for ( $i=0;$i<count( $array_enumElement );$i++ )
00187 {
00188 for ( $j=0;$j<count( $array_selectedEnumElement );$j++ )
00189 {
00190 if ( $array_enumElement[$i] === $array_selectedEnumElement[$j] )
00191 {
00192 $eID = $array_enumID[$i];
00193 $eElement = $array_enumElement[$i];
00194 $eValue = $array_enumValue[$i];
00195 eZEnum::storeObjectEnumeration( $contentObjectAttributeID,
00196 $contentObjectAttributeVersion,
00197 $eID,
00198 $eElement,
00199 $eValue );
00200 }
00201 }
00202 }
00203 $db->commit();
00204 return true;
00205 }
00206 return false;
00207 }
00208
00209
00210
00211
00212
00213 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00214 {
00215 if ( $http->hasPostVariable( $base . '_data_enumid_' . $contentObjectAttribute->attribute( 'id' ) ) )
00216 {
00217 $array_enumID = $http->postVariable( $base . '_data_enumid_' . $contentObjectAttribute->attribute( 'id' ) );
00218 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00219
00220 if ( $contentObjectAttribute->validateIsRequired() )
00221 {
00222 if ( !$http->hasPostVariable( $base . '_select_data_enumelement_' . $contentObjectAttribute->attribute( 'id' ) ) )
00223 {
00224 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00225 'At least one field should be chosen.' ) );
00226 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00227 }
00228 }
00229 }
00230 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00231 }
00232
00233
00234
00235
00236
00237 function storeObjectAttribute( &$contentObjectAttribute )
00238 {
00239 }
00240
00241
00242
00243
00244 function &objectAttributeContent( &$contentObjectAttribute )
00245 {
00246 $contentObjectAttributeID =& $contentObjectAttribute->attribute( 'id' );
00247 $contentObjectAttributeVersion =& $contentObjectAttribute->attribute( 'version' );
00248 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00249 $id = $contentClassAttribute->attribute( 'id' );
00250 $version = $contentClassAttribute->attribute( 'version' );
00251 $ismultiple = $contentClassAttribute->attribute( 'data_int1' );
00252 $isoption = $contentClassAttribute->attribute( 'data_int2' );
00253 $enum = new eZEnum( $id, $version );
00254 $enum->setIsmultipleValue( $ismultiple );
00255 $enum->setIsoptionValue( $isoption );
00256 $enum->setObjectEnumValue( $contentObjectAttributeID, $contentObjectAttributeVersion );
00257 return $enum;
00258 }
00259
00260
00261
00262
00263
00264 function validateClassAttributeHTTPInput( &$http, $base, &$contentClassAttribute )
00265 {
00266 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00267 }
00268
00269
00270
00271
00272 function fetchClassAttributeHTTPInput( &$http, $base, &$contentClassAttribute )
00273 {
00274 $ismultiple = $base . EZ_DATATYPESTRING_ENUM_ISMULTIPLE_VARIABLE . $contentClassAttribute->attribute( 'id' );
00275 $isoption = $base . EZ_DATATYPESTRING_ENUM_ISOPTION_VARIABLE . $contentClassAttribute->attribute( 'id' );
00276 $enumID = $base . '_data_enumid_' . $contentClassAttribute->attribute( 'id' );
00277 $enumElement = $base . '_data_enumelement_' . $contentClassAttribute->attribute( 'id' );
00278 $enumValue = $base . '_data_enumvalue_' . $contentClassAttribute->attribute( 'id' );
00279 $enumRemove = $base . '_data_enumremove_' . $contentClassAttribute->attribute( 'id' );
00280 $version = $contentClassAttribute->attribute( 'version' );
00281
00282 $ismultipleValue = $http->hasPostVariable( $ismultiple ) ? 1 : 0;
00283 $contentClassAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD, $ismultipleValue );
00284
00285 if ( $http->hasPostVariable( $isoption ) )
00286 {
00287 $optionValue = $http->postVariable( $isoption );
00288 $optionValueSet = $optionValue == 1 ? '1' : '0';
00289 $contentClassAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD, $optionValueSet );
00290 }
00291
00292 if ( $http->hasPostVariable( $enumID ) &&
00293 $http->hasPostVariable( $enumElement ) &&
00294 $http->hasPostVariable( $enumValue ) &&
00295 !($http->hasPostVariable( $enumRemove ) ) )
00296 {
00297 $array_enumID = $http->postVariable( $enumID );
00298 $array_enumElement = $http->postVariable( $enumElement );
00299 $array_enumValue = $http->postVariable( $enumValue );
00300 $enum =& $contentClassAttribute->content();
00301 $enum->setValue( $array_enumID, $array_enumElement, $array_enumValue, $version );
00302 $contentClassAttribute->setContent( $enum );
00303 }
00304 }
00305
00306 function storeClassAttribute( &$contentClassAttribute, $version )
00307 {
00308 $enum =& $contentClassAttribute->content();
00309 $enum->setVersion( $version );
00310 }
00311
00312 function storeDefinedClassAttribute( &$contentClassAttribute )
00313 {
00314 $enum =& $contentClassAttribute->content();
00315 $enum->setVersion( EZ_CLASS_VERSION_STATUS_DEFINED );
00316 }
00317
00318
00319
00320
00321 function &classAttributeContent( &$contentClassAttribute )
00322 {
00323 $id = $contentClassAttribute->attribute( 'id' );
00324 $version = $contentClassAttribute->attribute( 'version' );
00325 $enum = new eZEnum( $id, $version );
00326 return $enum;
00327 }
00328
00329
00330
00331 function customClassAttributeHTTPAction( $http, $action, &$contentClassAttribute )
00332 {
00333 $id = $contentClassAttribute->attribute( 'id' );
00334 switch ( $action )
00335 {
00336 case 'new_enumelement' :
00337 {
00338 $enum =& $contentClassAttribute->content( );
00339 $enum->addEnumeration('');
00340 $contentClassAttribute->setContent( $enum );
00341 }break;
00342 case 'remove_selected' :
00343 {
00344 $version = $contentClassAttribute->attribute( 'version' );
00345 $postvarname = 'ContentClass' . '_data_enumremove_' . $contentClassAttribute->attribute( 'id' );
00346 $array_remove = $http->hasPostVariable( $postvarname ) ? $http->postVariable( $postvarname ) : array();
00347 foreach( $array_remove as $enumid )
00348 {
00349 eZEnum::removeEnumeration( $id, $enumid, $version );
00350 }
00351 }break;
00352 default :
00353 {
00354 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZEnumType' );
00355 }break;
00356 }
00357 }
00358
00359
00360
00361
00362 function title( &$contentObjectAttribute, $name )
00363 {
00364 $enum = $this->objectAttributeContent( $contentObjectAttribute );
00365
00366 $enumObjectList = $enum->attribute( 'enumobject_list' );
00367
00368 $value = '';
00369
00370 foreach ( $enumObjectList as $count => $enumObjectValue )
00371 {
00372 if ( $count != 0 )
00373 $value .= ', ';
00374 $value .= $enumObjectValue->attribute( 'enumelement' );
00375 }
00376
00377 return $value;
00378 }
00379
00380
00381
00382
00383 function metaData( $contentObjectAttribute )
00384 {
00385 $contentObjectAttributeID =& $contentObjectAttribute->attribute( 'id' );
00386 $contentObjectAttributeVersion =& $contentObjectAttribute->attribute( 'version' );
00387 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00388 $id =& $contentClassAttribute->attribute( 'id' );
00389 $version =& $contentClassAttribute->attribute( 'version' );
00390 $ismultiple = $contentClassAttribute->attribute( 'data_int1' );
00391 $isoption = $contentClassAttribute->attribute( 'data_int2' );
00392
00393 $enum = new eZEnum( $id, $version );
00394 $enum->setIsmultipleValue( $ismultiple );
00395 $enum->setIsoptionValue( $isoption );
00396 $enum->setObjectEnumValue( $contentObjectAttributeID, $contentObjectAttributeVersion );
00397
00398 $return = '';
00399 foreach ( $enum->attribute( 'enumobject_list' ) as $enumElement )
00400 {
00401 $return .= $enumElement->attribute( 'enumvalue' ) . ' ';
00402 $return .= $enumElement->attribute( 'enumelement' ) . ' ';
00403 }
00404 return $return;
00405 }
00406
00407
00408
00409
00410
00411 function objectDisplayInformation( &$objectAttribute, $mergeInfo = false )
00412 {
00413 $classAttribute =& $objectAttribute->contentClassAttribute();
00414 $isOption = $classAttribute->attribute( 'data_int2' );
00415
00416 $editGrouped = ( $isOption == false );
00417 $info = array( 'edit' => array( 'grouped_input' => $editGrouped ),
00418 'collection' => array( 'grouped_input' => $editGrouped ) );
00419 return eZDataType::objectDisplayInformation( $objectAttribute, $info );
00420 }
00421
00422
00423
00424
00425 function isIndexable()
00426 {
00427 return true;
00428 }
00429
00430
00431
00432
00433
00434
00435 function serializeContentObjectAttribute( &$package, &$contentObjectAttribute )
00436 {
00437 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00438 $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00439
00440 $node = $this->createContentObjectAttributeDOMNode( $contentObjectAttribute );
00441
00442 $enumElements = eZEnumObjectValue::fetchAllElements( $contentObjectAttributeID, $contentObjectAttributeVersion );
00443
00444 foreach ( $enumElements as $enumElement )
00445 {
00446 unset( $elementNode );
00447 $elementNode = new eZDOMNode();
00448 $elementNode->setName( 'enum-element' );
00449
00450 $elementNode->appendAttribute( eZDOMDocument::createAttributeNode( 'id', $enumElement->attribute( 'enumid' ) ) );
00451 $elementNode->appendAttribute( eZDOMDocument::createAttributeNode( 'value', $enumElement->attribute( 'enumvalue' ) ) );
00452 $elementNode->appendAttribute( eZDOMDocument::createAttributeNode( 'element', $enumElement->attribute( 'enumelement' ) ) );
00453 $node->appendChild( $elementNode );
00454 }
00455
00456 return $node;
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00469 {
00470 if ( $attributeNode->hasChildren() )
00471 {
00472 $contentObjectAttributeID = $objectAttribute->attribute( 'id' );
00473 $contentObjectAttributeVersion = $objectAttribute->attribute( 'version' );
00474
00475 $enumNodes = $attributeNode->children();
00476 foreach ( array_keys( $enumNodes ) as $enumNodeKey )
00477 {
00478 $enumNode = $enumNodes[$enumNodeKey];
00479
00480 $eID = $enumNode->attributeValue( 'id' );
00481 $eValue = $enumNode->attributeValue( 'value' );
00482 $eElement = $enumNode->attributeValue( 'element' );
00483
00484 eZEnum::storeObjectEnumeration( $contentObjectAttributeID,
00485 $contentObjectAttributeVersion,
00486 $eID,
00487 $eElement,
00488 $eValue );
00489 }
00490 }
00491 else
00492 {
00493 eZDebug::writeError( "Can't find attributes for enumeration", 'eZEnumType::unserializeContentObjectAttribute' );
00494 }
00495 }
00496
00497
00498
00499
00500
00501 function hasObjectAttributeContent( &$contentObjectAttribute )
00502 {
00503 return true;
00504 }
00505
00506
00507
00508
00509 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00510 {
00511 $isOption = $classAttribute->attribute( EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD );
00512 $isMultiple = $classAttribute->attribute( EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD );
00513 $content =& $classAttribute->attribute( 'content' );
00514 $enumList =& $content->attribute( 'enum_list' );
00515 $attributeParametersNode->appendAttribute( eZDOMDocument::createAttributeNode( 'is-option', $isOption ? 'true' : 'false' ) );
00516 $attributeParametersNode->appendAttribute( eZDOMDocument::createAttributeNode( 'is-multiple', $isMultiple ? 'true' : 'false' ) );
00517 $elementListNode = eZDOMDocument::createElementNode( 'elements' );
00518 $attributeParametersNode->appendChild( $elementListNode );
00519 for ( $i = 0; $i < count( $enumList ); ++$i )
00520 {
00521 $enumElement =& $enumList[$i];
00522 unset( $elementNode );
00523 $elementNode = eZDOMDocument::createElementNode( 'element',
00524 array( 'id' => $enumElement->attribute( 'id' ),
00525 'name' => $enumElement->attribute( 'enumelement' ),
00526 'value' => $enumElement->attribute( 'enumvalue' ) ) );
00527 $elementListNode->appendChild( $elementNode );
00528 }
00529 }
00530
00531
00532
00533
00534 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00535 {
00536 $isOption = strtolower( $attributeParametersNode->attributeValue( 'is-option' ) ) == 'true';
00537 $isMultiple = strtolower( $attributeParametersNode->attributeValue( 'is-multiple' ) ) == 'true';
00538 $classAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD, $isOption );
00539 $classAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD, $isMultiple );
00540
00541 $enum = new eZEnum( $classAttribute->attribute( 'id' ), $classAttribute->attribute( 'version' ) );
00542 $elementListNode =& $attributeParametersNode->elementByName( 'elements' );
00543 $elementList = $elementListNode->children();
00544 foreach ( array_keys( $elementList ) as $elementKey )
00545 {
00546 $element =& $elementList[$elementKey];
00547 $elementID = $element->attributeValue( 'id' );
00548 $elementName = $element->attributeValue( 'name' );
00549 $elementValue = $element->attributeValue( 'value' );
00550 $value = eZEnumValue::create( $classAttribute->attribute( 'id' ),
00551 $classAttribute->attribute( 'version' ),
00552 $elementName );
00553 $value->setAttribute( 'enumvalue', $elementValue );
00554 $value->store();
00555 $enum->addEnumerationValue( $value );
00556 }
00557 }
00558
00559
00560
00561
00562 function diff( $old, $new, $options = false )
00563 {
00564 return null;
00565 }
00566 }
00567 eZDataType::register( EZ_DATATYPESTRING_ENUM, 'ezenumtype' );
00568
00569 ?>