| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZSelectionType class 4 // 5 // Created on: <23-Jul-2003 12:51:27 bf> 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 \class ezselectiontype ezselectiontype.php 31 \ingroup eZDatatype 32 \brief Handles the single and multiple selections. 33 \date Wednesday 23 July 2003 12:48:45 pm 34 \author Bård Farstad 35 36 */ 37 38 include_once ( "kernel/classes/ezdatatype.php" ); 39 include_once ( "lib/ezxml/classes/ezxml.php" ); 40 include_once ( 'lib/ezutils/classes/ezstringutils.php' ); 41 42 define( "EZ_DATATYPESTRING_EZ_SELECTION", "ezselection" ); 43 44 class eZSelectionType extends eZDataType 45 { 46 /*! 47 Constructor 48 */ 49 function eZSelectionType() 50 { 51 $this->eZDataType( EZ_DATATYPESTRING_EZ_SELECTION, ezi18n( 'kernel/classes/datatypes', "Selection", 'Datatype name' ), 52 array( 'serialize_supported' => true ) ); 53 } 54 55 /*! 56 Validates all variables given on content class level 57 \return EZ_INPUT_VALIDATOR_STATE_ACCEPTED or EZ_INPUT_VALIDATOR_STATE_INVALID if 58 the values are accepted or not 59 */ 60 function validateClassAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 61 { 62 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED; 63 } 64 65 /*! 66 Fetches all variables inputed on content class level 67 \return true if fetching of class attributes are successfull, false if not 68 */ 69 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute ) 70 { 71 $attributeContent =& $this->classAttributeContent( $classAttribute ); 72 $classAttributeID = $classAttribute->attribute( 'id' ); 73 $isMultipleSelection = false; 74 75 if ( $http->hasPostVariable( $base . "_ezselection_ismultiple_value_" . $classAttributeID ) ) 76 { 77 if( $http->postVariable( $base . "_ezselection_ismultiple_value_" . $classAttributeID ) != 0 ) 78 { 79 $isMultipleSelection = true; 80 } 81 } 82 83 $currentOptions = $attributeContent['options']; 84 $hasPostData = false; 85 86 if ( $http->hasPostVariable( $base . "_ezselection_option_name_array_" . $classAttributeID ) ) 87 { 88 $nameArray = $http->postVariable( $base . "_ezselection_option_name_array_" . $classAttributeID ); 89 90 // Fill in new names for options 91 foreach ( array_keys( $currentOptions ) as $key ) 92 { 93 $currentOptions[$key]['name'] = $nameArray[$currentOptions[$key]['id']]; 94 } 95 $hasPostData = true; 96 97 } 98 99 if ( $http->hasPostVariable( $base . "_ezselection_newoption_button_" . $classAttributeID ) ) 100 { 101 $currentCount = 0; 102 foreach ( $currentOptions as $option ) 103 { 104 $currentCount = max( $currentCount, $option['id'] ); 105 } 106 $currentCount += 1; 107 108 $currentOptions[] = array( 'id' => $currentCount, 109 'name' => '' ); 110 $hasPostData = true; 111 112 } 113 114 if ( $http->hasPostVariable( $base . "_ezselection_removeoption_button_" . $classAttributeID ) ) 115 { 116 if ( $http->hasPostVariable( $base . "_ezselection_option_remove_array_". $classAttributeID ) ) 117 { 118 $removeArray = $http->postVariable( $base . "_ezselection_option_remove_array_". $classAttributeID ); 119 120 foreach ( array_keys( $currentOptions ) as $key ) 121 { 122 if ( $removeArray[$currentOptions[$key]['id']] ) 123 unset( $currentOptions[$key] ); 124 } 125 $hasPostData = true; 126 } 127 } 128 129 if ( $hasPostData ) 130 { 131 132 // Serialize XML 133 $doc = new eZDOMDocument( "selection" ); 134 $root = $doc->createElementNode( "ezselection" ); 135 $doc->setRoot( $root ); 136 137 $options = $doc->createElementNode( "options" ); 138 139 $root->appendChild( $options ); 140 foreach ( $currentOptions as $optionArray ) 141 { 142 unset( $optionNode ); 143 $optionNode = $doc->createElementNode( "option" ); 144 $optionNode->appendAttribute( $doc->createAttributeNode( "id", $optionArray['id'] ) ); 145 $optionNode->appendAttribute( $doc->createAttributeNode( 'name', $optionArray['name'] ) ); 146 147 $options->appendChild( $optionNode ); 148 } 149 150 $xml = $doc->toString(); 151 152 $classAttribute->setAttribute( "data_text5", $xml ); 153 154 if ( $isMultipleSelection == true ) 155 $classAttribute->setAttribute( "data_int1", 1 ); 156 else 157 $classAttribute->setAttribute( "data_int1", 0 ); 158 } 159 return true; 160 } 161 /*! 162 Validates input on content object level 163 \return EZ_INPUT_VALIDATOR_STATE_ACCEPTED or EZ_INPUT_VALIDATOR_STATE_INVALID if 164 the values are accepted or not 165 */ 166 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 167 { 168 if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) ) 169 { 170 $data = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ); 171 172 if ( $data == "" ) 173 { 174 if ( $contentObjectAttribute->validateIsRequired() ) 175 { 176 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 177 'Input required.' ) ); 178 return EZ_INPUT_VALIDATOR_STATE_INVALID; 179 } 180 } 181 } 182 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED; 183 } 184 185 /*! 186 Fetches all variables from the object 187 \return true if fetching of class attributes are successfull, false if not 188 */ 189 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 190 { 191 if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) ) 192 { 193 $selectOptions = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ); 194 $idString = ( is_array( $selectOptions ) ? implode( '-', $selectOptions ) : "" ); 195 $contentObjectAttribute->setAttribute( 'data_text', $idString ); 196 return true; 197 } 198 return false; 199 } 200 201 /*! 202 \reimp 203 */ 204 function validateCollectionAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 205 { 206 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED; 207 } 208 209 /*! 210 \reimp 211 Fetches the http post variables for collected information 212 */ 213 function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute ) 214 { 215 if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) ) 216 { 217 $selectOptions = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ); 218 $idString = ( is_array( $selectOptions ) ? implode( '-', $selectOptions ) : "" ); 219 $collectionAttribute->setAttribute( 'data_text', $idString ); 220 return true; 221 } 222 return false; 223 } 224 225 /*! 226 Sets the default value. 227 */ 228 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute ) 229 { 230 if ( $currentVersion != false ) 231 { 232 $idString = $originalContentObjectAttribute->attribute( "data_text" ); 233 $contentObjectAttribute->setAttribute( "data_text", $idString ); 234 $contentObjectAttribute->store(); 235 } 236 } 237 238 /*! 239 Returns the selected options by id. 240 */ 241 function &objectAttributeContent( &$contentObjectAttribute ) 242 { 243 $idString = explode( '-', $contentObjectAttribute->attribute( 'data_text' ) ); 244 return $idString; 245 } 246 247 /*! 248 Returns the content data for the given content class attribute. 249 */ 250 function &classAttributeContent( &$classAttribute ) 251 { 252 $xml = new eZXML(); 253 $xmlString =& $classAttribute->attribute( 'data_text5' ); 254 $dom =& $xml->domTree( $xmlString ); 255 if ( $dom ) 256 { 257 $options =& $dom->elementsByName( 'option' ); 258 $optionArray = array(); 259 foreach ( $options as $optionNode ) 260 { 261 $optionArray[] = array( 'id' => $optionNode->attributeValue( 'id' ), 262 'name' => $optionNode->attributeValue( 'name' ) ); 263 } 264 } 265 else 266 { 267 $optionArray[] = array( 'id' => 0, 268 'name' => '' ); 269 } 270 $attrValue = array( 'options' => $optionArray, 271 'is_multiselect' => $classAttribute->attribute( 'data_int1' ) ); 272 return $attrValue; 273 } 274 275 /*! 276 Returns the meta data used for storing search indeces. 277 */ 278 function metaData( $contentObjectAttribute ) 279 { 280 $selected = $this->objectAttributeContent( $contentObjectAttribute ); 281 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) ); 282 $return = ''; 283 if ( count( $selected ) == 0) 284 { 285 return ''; 286 } 287 288 $count = 0; 289 $optionArray = $classContent['options']; 290 foreach ( $selected as $id ) 291 { 292 if ( $count++ != 0 ) 293 $return .= ' '; 294 foreach ( $optionArray as $option ) 295 { 296 $optionID = $option['id']; 297 if ( $optionID == $id ) 298 $return .= $option['name']; 299 } 300 } 301 return $return; 302 } 303 304 function toString( $contentObjectAttribute ) 305 { 306 307 $selected = $this->objectAttributeContent( $contentObjectAttribute ); 308 309 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) ); 310 $return = ''; 311 if ( count( $selected ) == 0) 312 { 313 return ''; 314 } 315 316 317 $optionArray = $classContent['options']; 318 foreach ( $selected as $id ) 319 { 320 foreach ( $optionArray as $option ) 321 { 322 $optionID = $option['id']; 323 if ( $optionID == $id ) 324 $returnData[] = $option['name']; 325 } 326 } 327 return eZStringUtils::implodeStr( $returnData, '|' ); 328 } 329 330 331 function fromString( &$contentObjectAttribute, $string ) 332 { 333 if ( $string == '' ) 334 return true; 335 $selectedNames = eZStringUtils::explodeStr( $string, '|' ); 336 $selectedIDList = array(); 337 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) ); 338 $optionArray = $classContent['options']; 339 foreach ( $selectedNames as $name ) 340 { 341 foreach ( $optionArray as $option ) 342 { 343 $optionName = $option['name']; 344 if ( $optionName == $name ) 345 $selectedIDList[] = $option['id']; 346 } 347 } 348 $idString = ( is_array( $selectedIDList ) ? implode( '-', $selectedIDList ) : "" ); 349 $contentObjectAttribute->setAttribute( 'data_text', $idString ); 350 return true; 351 } 352 353 /*! 354 Returns the value as it will be shown if this attribute is used in the object name pattern. 355 */ 356 function title( &$contentObjectAttribute ) 357 { 358 $selected = $this->objectAttributeContent( $contentObjectAttribute ); 359 $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) ); 360 $return = ""; 361 if ( count( $selected ) == 0) 362 { 363 return ""; 364 } 365 366 $count = 0; 367 foreach ( $selected as $id ) 368 { 369 /*if ( $id == 0 ) // first object gets id==0, while rest of objects get id with offset from 1 370 $id++; 371 if ( $count++ != 0 ) 372 $return .= ', '; 373 $return .= $classContent['options'][$id-1]['name'];*/ 374 if ( $count != 0 ) 375 $return .= ', '; 376 $return .= $classContent['options'][$id]['name']; 377 $count++; 378 } 379 return $return; 380 } 381 382 function hasObjectAttributeContent( &$contentObjectAttribute ) 383 { 384 return true; 385 } 386 387 /*! 388 \reimp 389 */ 390 function sortKey( &$contentObjectAttribute ) 391 { 392 return strtolower( $contentObjectAttribute->attribute( 'data_text' ) ); 393 } 394 395 /*! 396 \reimp 397 */ 398 function sortKeyType() 399 { 400 return 'string'; 401 } 402 403 /*! 404 \return true if the datatype can be indexed 405 */ 406 function isIndexable() 407 { 408 return true; 409 } 410 411 /*! 412 \reimp 413 */ 414 function isInformationCollector() 415 { 416 return true; 417 } 418 419 /*! 420 \reimp 421 */ 422 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode ) 423 { 424 $xml = new eZXML(); 425 426 $isMultipleSelection =& $classAttribute->attribute( 'data_int1' ); 427 $xmlString =& $classAttribute->attribute( 'data_text5' ); 428 429 $dom =& $xml->domTree( $xmlString ); 430 $domRoot =& $dom->root(); 431 $options =& $domRoot->elementByName( 'options' ); 432 433 $attributeParametersNode->appendChild( $options ); 434 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'is-multiselect', $isMultipleSelection ) ); 435 } 436 437 /*! 438 \reimp 439 */ 440 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode ) 441 { 442 $options =& $attributeParametersNode->elementByName( 'options' ); 443 444 $doc = new eZDOMDocument( "selection" ); 445 $root = $doc->createElementNode( "ezselection" ); 446 447 $doc->setRoot( $root ); 448 $root->appendChild( $options ); 449 450 $xml = $doc->toString(); 451 $classAttribute->setAttribute( "data_text5", $xml ); 452 453 if ( $attributeParametersNode->elementTextContentByName( 'is-multiselect' ) == 0 ) 454 $classAttribute->setAttribute( "data_int1", 0 ); 455 else 456 $classAttribute->setAttribute( "data_int1", 1 ); 457 } 458 } 459 /*! 460 \reimp 461 */ 462 function serializeContentObjectAttribute( &$package, &$objectAttribute ) 463 { 464 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute ); 465 $idString = $objectAttribute->attribute( 'data_text' ); 466 467 $node->appendChild( eZDOMDocument::createElementTextNode( 'idstring', $idString ) ); 468 return $node; 469 } 470 471 /*! 472 \reimp 473 */ 474 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode ) 475 { 476 $idString = $attributeNode->elementTextContentByName( 'idstring' ); 477 478 if ( $idString === false ) 479 $idString = ''; 480 481 $objectAttribute->setAttribute( 'data_text', $idString ); 482 } 483 484 eZDataType::register( EZ_DATATYPESTRING_EZ_SELECTION, "ezselectiontype" ); 485 ?>
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 |