[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZEnumtype class 4 // 5 // Created on: <24-ßÂ-2002 14:33:53 wy> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 30 /*! \file ezenumtype.php 31 */ 32 33 /*! 34 \class eZEnumType ezenumtype.php 35 \ingroup eZDatatype 36 37 */ 38 39 include_once ( 'kernel/classes/ezdatatype.php' ); 40 include_once ( 'kernel/classes/datatypes/ezenum/ezenum.php' ); 41 include_once ( 'kernel/classes/ezcontentobjectattribute.php' ); 42 define( 'EZ_DATATYPESTRING_ENUM', 'ezenum' ); 43 define( 'EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD', 'data_int1' ); 44 define( 'EZ_DATATYPESTRING_ENUM_ISMULTIPLE_VARIABLE', '_ezenum_ismultiple_value_' ); 45 define( 'EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD', 'data_int2' ); 46 define( 'EZ_DATATYPESTRING_ENUM_ISOPTION_VARIABLE', '_ezenum_isoption_value_' ); 47 48 class eZEnumType extends eZDataType 49 { 50 /*! 51 Constructor 52 */ 53 function eZEnumType() 54 { 55 $this->eZDataType( EZ_DATATYPESTRING_ENUM, ezi18n( 'kernel/classes/datatypes', 'Enum', 'Datatype name' ), 56 array( 'serialize_supported' => true ) ); 57 } 58 59 /*! 60 Sets value according to current version 61 */ 62 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute ) 63 { 64 if ( $currentVersion != false ) 65 { 66 $originalContentObjectAttributeID = $originalContentObjectAttribute->attribute( 'id' ); 67 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' ); 68 $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' ); 69 70 $db =& eZDB::instance(); 71 $db->begin(); 72 73 // Delete stored object attributes when initialize translated attribute. 74 if ( $originalContentObjectAttributeID != $contentObjectAttributeID ) 75 $this->deleteStoredObjectAttribute( $contentObjectAttribute, $currentVersion ); 76 77 $newVersionEnumObject = eZEnumObjectValue::fetchAllElements( $originalContentObjectAttributeID, $currentVersion ); 78 79 for ( $i = 0; $i < count( $newVersionEnumObject ); ++$i ) 80 { 81 $enumobjectvalue = $newVersionEnumObject[$i]; 82 $enumobjectvalue->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) ); 83 $enumobjectvalue->setAttribute( 'contentobject_attribute_version', $contentObjectAttributeVersion ); 84 $enumobjectvalue->store(); 85 } 86 87 $db->commit(); 88 } 89 } 90 91 /*! 92 \reimp 93 */ 94 function cloneClassAttribute( &$oldClassAttribute, &$newClassAttribute ) 95 { 96 $oldContentClassAttributeID = $oldClassAttribute->attribute( 'id' ); 97 $oldEnums = eZEnumValue::fetchAllElements( $oldContentClassAttributeID, 0 ); 98 99 $db =& eZDB::instance(); 100 $db->begin(); 101 102 foreach ( $oldEnums as $oldEnum ) 103 { 104 $enum =& $oldEnum->clone(); 105 $enum->setAttribute( 'contentclass_attribute_id', $newClassAttribute->attribute( 'id' ) ); 106 $enum->setAttribute( 'contentclass_attribute_version', $newClassAttribute->attribute( 'version' ) ); 107 $enum->store(); 108 } 109 110 $db->commit(); 111 } 112 113 /*! 114 Set class attribute value for template version 115 */ 116 function initializeClassAttribute( &$classAttribute ) 117 { 118 $contentClassAttributeID = $classAttribute->attribute( 'id' ); 119 $enums = eZEnumValue::fetchAllElements( $contentClassAttributeID, 1 ); 120 121 if ( count ( $enums ) == 0 ) 122 { 123 $enums = eZEnumValue::fetchAllElements( $contentClassAttributeID, 0 ); 124 $db =& eZDB::instance(); 125 $db->begin(); 126 foreach ( $enums as $enum ) 127 { 128 $enum->setAttribute( 'contentclass_attribute_version', 1 ); 129 $enum->store(); 130 } 131 $db->commit(); 132 } 133 } 134 135 /*! 136 Delete stored object attribute 137 */ 138 function deleteStoredObjectAttribute( &$contentObjectAttribute, $version = null ) 139 { 140 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' ); 141 eZEnumObjectValue::removeAllElements( $contentObjectAttributeID, $version ); 142 143 } 144 145 /*! 146 Delete stored class attribute 147 */ 148 function deleteStoredClassAttribute( &$contentClassAttribute, $version = null ) 149 { 150 $contentClassAttributeID = $contentClassAttribute->attribute( 'id' ); 151 eZEnumValue::removeAllElements( $contentClassAttributeID, $version ); 152 153 } 154 155 /*! 156 Fetches the http post var integer input and stores it in the data instance. 157 */ 158 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 159 { 160 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' ); 161 $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' ); 162 $enumID = $base . '_data_enumid_' . $contentObjectAttributeID; 163 $enumElement = $base . '_data_enumelement_' . $contentObjectAttributeID; 164 $enumValue = $base . '_data_enumvalue_' . $contentObjectAttributeID; 165 $selectedEnumElement = $base . '_select_data_enumelement_' . $contentObjectAttributeID; 166 if ( $http->hasPostVariable( $enumID ) && 167 $http->hasPostVariable( $enumElement ) && 168 $http->hasPostVariable( $enumValue ) ) 169 { 170 $array_enumID = $http->postVariable( $enumID ); 171 $array_enumElement = $http->postVariable( $enumElement ); 172 $array_enumValue = $http->postVariable( $enumValue ); 173 174 if ( $http->hasPostVariable( $selectedEnumElement ) ) 175 $array_selectedEnumElement = $http->postVariable( $selectedEnumElement ); 176 else 177 $array_selectedEnumElement = null; 178 179 $db =& eZDB::instance(); 180 $db->begin(); 181 182 // Remove stored enumerations before we store new enumerations 183 eZEnum::removeObjectEnumerations( $contentObjectAttributeID, $contentObjectAttributeVersion ); 184 for ( $i=0;$i<count( $array_enumElement );$i++ ) 185 { 186 for ( $j=0;$j<count( $array_selectedEnumElement );$j++ ) 187 { 188 if ( $array_enumElement[$i] === $array_selectedEnumElement[$j] ) 189 { 190 $eID = $array_enumID[$i]; 191 $eElement = $array_enumElement[$i]; 192 $eValue = $array_enumValue[$i]; 193 eZEnum::storeObjectEnumeration( $contentObjectAttributeID, 194 $contentObjectAttributeVersion, 195 $eID, 196 $eElement, 197 $eValue ); 198 } 199 } 200 } 201 $db->commit(); 202 return true; 203 } 204 return false; 205 } 206 207 /*! 208 Validates the input and returns true if the input was 209 valid for this datatype. 210 */ 211 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 212 { 213 if ( $http->hasPostVariable( $base . '_data_enumid_' . $contentObjectAttribute->attribute( 'id' ) ) ) 214 { 215 $array_enumID = $http->postVariable( $base . '_data_enumid_' . $contentObjectAttribute->attribute( 'id' ) ); 216 $classAttribute =& $contentObjectAttribute->contentClassAttribute(); 217 218 if ( $contentObjectAttribute->validateIsRequired() ) 219 { 220 if ( !$http->hasPostVariable( $base . '_select_data_enumelement_' . $contentObjectAttribute->attribute( 'id' ) ) ) 221 { 222 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 223 'At least one field should be chosen.' ) ); 224 return EZ_INPUT_VALIDATOR_STATE_INVALID; 225 } 226 } 227 } 228 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED; 229 } 230 231 /*! 232 Does nothing since it has been stored. 233 See fetchObjectAttributeHTTPInput for the actual storing. 234 */ 235 function storeObjectAttribute( &$contentObjectAttribute ) 236 { 237 } 238 239 /*! 240 Returns actual the class attribute content. 241 */ 242 function &objectAttributeContent( &$contentObjectAttribute ) 243 { 244 $contentObjectAttributeID =& $contentObjectAttribute->attribute( 'id' ); 245 $contentObjectAttributeVersion =& $contentObjectAttribute->attribute( 'version' ); 246 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute(); 247 $id = $contentClassAttribute->attribute( 'id' ); 248 $version = $contentClassAttribute->attribute( 'version' ); 249 $ismultiple = $contentClassAttribute->attribute( 'data_int1' ); 250 $isoption = $contentClassAttribute->attribute( 'data_int2' ); 251 $enum = new eZEnum( $id, $version ); 252 $enum->setIsmultipleValue( $ismultiple ); 253 $enum->setIsoptionValue( $isoption ); 254 $enum->setObjectEnumValue( $contentObjectAttributeID, $contentObjectAttributeVersion ); 255 return $enum; 256 } 257 258 /*! 259 Validates the input and returns true if the input was 260 valid for this datatype. 261 */ 262 function validateClassAttributeHTTPInput( &$http, $base, &$contentClassAttribute ) 263 { 264 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED; 265 } 266 267 /*! 268 Fetches the http post var integer input and stores it in the data instance. 269 */ 270 function fetchClassAttributeHTTPInput( &$http, $base, &$contentClassAttribute ) 271 { 272 $ismultiple = $base . EZ_DATATYPESTRING_ENUM_ISMULTIPLE_VARIABLE . $contentClassAttribute->attribute( 'id' ); 273 $isoption = $base . EZ_DATATYPESTRING_ENUM_ISOPTION_VARIABLE . $contentClassAttribute->attribute( 'id' ); 274 $enumID = $base . '_data_enumid_' . $contentClassAttribute->attribute( 'id' ); 275 $enumElement = $base . '_data_enumelement_' . $contentClassAttribute->attribute( 'id' ); 276 $enumValue = $base . '_data_enumvalue_' . $contentClassAttribute->attribute( 'id' ); 277 $enumRemove = $base . '_data_enumremove_' . $contentClassAttribute->attribute( 'id' ); 278 $version = $contentClassAttribute->attribute( 'version' ); 279 280 $ismultipleValue = $http->hasPostVariable( $ismultiple ) ? 1 : 0; 281 $contentClassAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD, $ismultipleValue ); 282 283 if ( $http->hasPostVariable( $isoption ) ) 284 { 285 $optionValue = $http->postVariable( $isoption ); 286 $optionValueSet = $optionValue == 1 ? '1' : '0'; 287 $contentClassAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD, $optionValueSet ); 288 } 289 290 if ( $http->hasPostVariable( $enumID ) && 291 $http->hasPostVariable( $enumElement ) && 292 $http->hasPostVariable( $enumValue ) && 293 !($http->hasPostVariable( $enumRemove ) ) ) 294 { 295 $array_enumID = $http->postVariable( $enumID ); 296 $array_enumElement = $http->postVariable( $enumElement ); 297 $array_enumValue = $http->postVariable( $enumValue ); 298 $enum =& $contentClassAttribute->content(); 299 $enum->setValue( $array_enumID, $array_enumElement, $array_enumValue, $version ); 300 $contentClassAttribute->setContent( $enum ); 301 } 302 } 303 304 function storeClassAttribute( &$contentClassAttribute, $version ) 305 { 306 $enum =& $contentClassAttribute->content(); 307 $enum->setVersion( $version ); 308 } 309 310 function storeDefinedClassAttribute( &$contentClassAttribute ) 311 { 312 $id = $contentClassAttribute->attribute( 'id' ); 313 $version = 1; 314 $enumVersion1 = new eZEnum( $id, $version ); 315 $enumVersion1->setVersion( 0 ); 316 eZEnumValue::removeAllElements( $id, 1 ); 317 } 318 319 /*! 320 Returns the content. 321 */ 322 function &classAttributeContent( &$contentClassAttribute ) 323 { 324 $id = $contentClassAttribute->attribute( 'id' ); 325 $version = $contentClassAttribute->attribute( 'version' ); 326 $enum = new eZEnum( $id, $version ); 327 return $enum; 328 } 329 330 /*! 331 */ 332 function customClassAttributeHTTPAction( $http, $action, &$contentClassAttribute ) 333 { 334 $id = $contentClassAttribute->attribute( 'id' ); 335 switch ( $action ) 336 { 337 case 'new_enumelement' : 338 { 339 $enum =& $contentClassAttribute->content( ); 340 $enum->addEnumeration(''); 341 $contentClassAttribute->setContent( $enum ); 342 }break; 343 case 'remove_selected' : 344 { 345 $version = $contentClassAttribute->attribute( 'version' ); 346 $postvarname = 'ContentClass' . '_data_enumremove_' . $contentClassAttribute->attribute( 'id' ); 347 $array_remove = $http->hasPostVariable( $postvarname ) ? $http->postVariable( $postvarname ) : array(); 348 foreach( $array_remove as $enumid ) 349 { 350 eZEnum::removeEnumeration( $id, $enumid, $version ); 351 } 352 }break; 353 default : 354 { 355 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZEnumType' ); 356 }break; 357 } 358 } 359 360 /*! 361 Returns the object attribute title. 362 */ 363 function title( &$contentObjectAttribute, $name ) 364 { 365 $enum = $this->objectAttributeContent( $contentObjectAttribute ); 366 367 $enumObjectList = $enum->attribute( 'enumobject_list' ); 368 369 $value = ''; 370 371 foreach ( $enumObjectList as $count => $enumObjectValue ) 372 { 373 if ( $count != 0 ) 374 $value .= ', '; 375 $value .= $enumObjectValue->attribute( 'enumelement' ); 376 } 377 378 return $value; 379 } 380 381 /*! 382 Returns the meta data used for storing search indeces. 383 */ 384 function metaData( $contentObjectAttribute ) 385 { 386 $contentObjectAttributeID =& $contentObjectAttribute->attribute( 'id' ); 387 $contentObjectAttributeVersion =& $contentObjectAttribute->attribute( 'version' ); 388 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute(); 389 $id =& $contentClassAttribute->attribute( 'id' ); 390 $version =& $contentClassAttribute->attribute( 'version' ); 391 $ismultiple = $contentClassAttribute->attribute( 'data_int1' ); 392 $isoption = $contentClassAttribute->attribute( 'data_int2' ); 393 394 $enum = new eZEnum( $id, $version ); 395 $enum->setIsmultipleValue( $ismultiple ); 396 $enum->setIsoptionValue( $isoption ); 397 $enum->setObjectEnumValue( $contentObjectAttributeID, $contentObjectAttributeVersion ); 398 399 $return = ''; 400 foreach ( $enum->attribute( 'enumobject_list' ) as $enumElement ) 401 { 402 $return .= $enumElement->attribute( 'enumvalue' ) . ' '; 403 $return .= $enumElement->attribute( 'enumelement' ) . ' '; 404 } 405 return $return; 406 } 407 408 /*! 409 \reimp 410 Sets \c grouped_input to \c true when checkboxes or radiobuttons are used. 411 */ 412 function objectDisplayInformation( &$objectAttribute, $mergeInfo = false ) 413 { 414 $classAttribute =& $objectAttribute->contentClassAttribute(); 415 $isOption = $classAttribute->attribute( 'data_int2' ); 416 417 $editGrouped = ( $isOption == false ); 418 $info = array( 'edit' => array( 'grouped_input' => $editGrouped ), 419 'collection' => array( 'grouped_input' => $editGrouped ) ); 420 return eZDataType::objectDisplayInformation( $objectAttribute, $info ); 421 } 422 423 /*! 424 \reimp 425 */ 426 function isIndexable() 427 { 428 return true; 429 } 430 431 /*! 432 \reimp 433 \return a DOM representation of the content object attribute 434 */ 435 436 function serializeContentObjectAttribute( &$package, &$contentObjectAttribute ) 437 { 438 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' ); 439 $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' ); 440 441 $node = $this->createContentObjectAttributeDOMNode( $contentObjectAttribute ); 442 443 $enumElements = eZEnumObjectValue::fetchAllElements( $contentObjectAttributeID, $contentObjectAttributeVersion ); 444 445 foreach ( $enumElements as $enumElement ) 446 { 447 unset( $elementNode ); 448 $elementNode = new eZDOMNode(); 449 $elementNode->setName( 'enum-element' ); 450 451 $elementNode->appendAttribute( eZDOMDocument::createAttributeNode( 'id', $enumElement->attribute( 'enumid' ) ) ); 452 $elementNode->appendAttribute( eZDOMDocument::createAttributeNode( 'value', $enumElement->attribute( 'enumvalue' ) ) ); 453 $elementNode->appendAttribute( eZDOMDocument::createAttributeNode( 'element', $enumElement->attribute( 'enumelement' ) ) ); 454 $node->appendChild( $elementNode ); 455 } 456 457 return $node; 458 } 459 460 461 /*! 462 \reimp 463 Unserailize contentobject attribute 464 465 \param package 466 \param contentobject attribute object 467 \param ezdomnode object 468 */ 469 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode ) 470 { 471 if ( $attributeNode->hasChildren() ) 472 { 473 $contentObjectAttributeID = $objectAttribute->attribute( 'id' ); 474 $contentObjectAttributeVersion = $objectAttribute->attribute( 'version' ); 475 476 $enumNodes = $attributeNode->children(); 477 foreach ( array_keys( $enumNodes ) as $enumNodeKey ) 478 { 479 $enumNode = $enumNodes[$enumNodeKey]; 480 481 $eID = $enumNode->attributeValue( 'id' ); 482 $eValue = $enumNode->attributeValue( 'value' ); 483 $eElement = $enumNode->attributeValue( 'element' ); 484 485 eZEnum::storeObjectEnumeration( $contentObjectAttributeID, 486 $contentObjectAttributeVersion, 487 $eID, 488 $eElement, 489 $eValue ); 490 } 491 } 492 else 493 { 494 eZDebug::writeError( "Can't find attributes for enumeration", 'eZEnumType::unserializeContentObjectAttribute' ); 495 } 496 } 497 498 499 /*! 500 \reimp 501 */ 502 function hasObjectAttributeContent( &$contentObjectAttribute ) 503 { 504 return true; 505 } 506 507 /*! 508 \reimp 509 */ 510 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode ) 511 { 512 $isOption = $classAttribute->attribute( EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD ); 513 $isMultiple = $classAttribute->attribute( EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD ); 514 $content =& $classAttribute->attribute( 'content' ); 515 $enumList =& $content->attribute( 'enum_list' ); 516 $attributeParametersNode->appendAttribute( eZDOMDocument::createAttributeNode( 'is-option', $isOption ? 'true' : 'false' ) ); 517 $attributeParametersNode->appendAttribute( eZDOMDocument::createAttributeNode( 'is-multiple', $isMultiple ? 'true' : 'false' ) ); 518 $elementListNode = eZDOMDocument::createElementNode( 'elements' ); 519 $attributeParametersNode->appendChild( $elementListNode ); 520 for ( $i = 0; $i < count( $enumList ); ++$i ) 521 { 522 $enumElement =& $enumList[$i]; 523 unset( $elementNode ); 524 $elementNode = eZDOMDocument::createElementNode( 'element', 525 array( 'id' => $enumElement->attribute( 'id' ), 526 'name' => $enumElement->attribute( 'enumelement' ), 527 'value' => $enumElement->attribute( 'enumvalue' ) ) ); 528 $elementListNode->appendChild( $elementNode ); 529 } 530 } 531 532 /*! 533 \reimp 534 */ 535 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode ) 536 { 537 $isOption = strtolower( $attributeParametersNode->attributeValue( 'is-option' ) ) == 'true'; 538 $isMultiple = strtolower( $attributeParametersNode->attributeValue( 'is-multiple' ) ) == 'true'; 539 $classAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISOPTION_FIELD, $isOption ); 540 $classAttribute->setAttribute( EZ_DATATYPESTRING_ENUM_ISMULTIPLE_FIELD, $isMultiple ); 541 542 $enum = new eZEnum( $classAttribute->attribute( 'id' ), $classAttribute->attribute( 'version' ) ); 543 $elementListNode =& $attributeParametersNode->elementByName( 'elements' ); 544 $elementList = $elementListNode->children(); 545 foreach ( array_keys( $elementList ) as $elementKey ) 546 { 547 $element =& $elementList[$elementKey]; 548 $elementID = $element->attributeValue( 'id' ); 549 $elementName = $element->attributeValue( 'name' ); 550 $elementValue = $element->attributeValue( 'value' ); 551 $value = eZEnumValue::create( $classAttribute->attribute( 'id' ), 552 $classAttribute->attribute( 'version' ), 553 $elementName ); 554 $value->setAttribute( 'enumvalue', $elementValue ); 555 $value->store(); 556 $enum->addEnumerationValue( $value ); 557 } 558 } 559 560 /*! 561 \reimp 562 */ 563 function diff( $old, $new, $options = false ) 564 { 565 return null; 566 } 567 } 568 eZDataType::register( EZ_DATATYPESTRING_ENUM, 'ezenumtype' ); 569 570 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |