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( "lib/ezdb/classes/ezdb.php" );
00039 include_once( "kernel/classes/ezpersistentobject.php" );
00040 include_once( 'kernel/classes/ezcontentclassattributenamelist.php' );
00041
00042 class eZContentClassAttribute extends eZPersistentObject
00043 {
00044 function eZContentClassAttribute( $row )
00045 {
00046 $this->eZPersistentObject( $row );
00047
00048 $this->Content = null;
00049 $this->DisplayInfo = null;
00050 $this->Module = null;
00051
00052 $this->NameList = new eZContentClassNameList();
00053 if ( isset( $row['serialized_name_list'] ) )
00054 $this->NameList = new eZContentClassAttributeNameList( $row['serialized_name_list'] );
00055 else
00056 $this->NameList->initDefault();
00057 }
00058
00059 function definition()
00060 {
00061 return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00062 'datatype' => 'integer',
00063 'default' => 0,
00064 'required' => true ),
00065 'serialized_name_list' => array( 'name' => 'SerializedNameList',
00066 'datatype' => 'string',
00067 'default' => '',
00068 'required' => true ),
00069 'version' => array( 'name' => 'Version',
00070 'datatype' => 'integer',
00071 'default' => 0,
00072 'required' => true ),
00073 'contentclass_id' => array( 'name' => 'ContentClassID',
00074 'datatype' => 'integer',
00075 'default' => 0,
00076 'required' => true,
00077 'foreign_class' => 'eZContentClass',
00078 'foreign_attribute' => 'id',
00079 'multiplicity' => '1..*' ),
00080 'identifier' => array( 'name' => 'Identifier',
00081 'datatype' => 'string',
00082 'default' => '',
00083 'required' => true ),
00084 'placement' => array( 'name' => 'Position',
00085 'datatype' => 'integer',
00086 'default' => 0,
00087 'required' => true ),
00088 'is_searchable' => array( 'name' => 'IsSearchable',
00089 'datatype' => 'integer',
00090 'default' => 0
00091 ),
00092 'is_required' => array( 'name' => 'IsRequired',
00093 'datatype' => 'integer',
00094 'default' => 0,
00095 'required' => true ),
00096 'can_translate' => array( 'name' => 'CanTranslate',
00097 'datatype' => 'integer',
00098 'default' => 0
00099 ),
00100 'is_information_collector' => array( 'name' => 'IsInformationCollector',
00101 'datatype' => 'integer',
00102 'default' => 0,
00103 'required' => true ),
00104 'data_type_string' => array( 'name' => 'DataTypeString',
00105 'datatype' => 'string',
00106 'default' => '',
00107 'required' => true ),
00108 'data_int1' => array( 'name' => 'DataInt1',
00109 'datatype' => 'integer',
00110 'default' => 0,
00111 'required' => true ),
00112 'data_int2' => array( 'name' => 'DataInt2',
00113 'datatype' => 'integer',
00114 'default' => 0,
00115 'required' => true ),
00116 'data_int3' => array( 'name' => 'DataInt3',
00117 'datatype' => 'integer',
00118 'default' => 0,
00119 'required' => true ),
00120 'data_int4' => array( 'name' => 'DataInt4',
00121 'datatype' => 'integer',
00122 'default' => 0,
00123 'required' => true ),
00124 'data_float1' => array( 'name' => 'DataFloat1',
00125 'datatype' => 'float',
00126 'default' => 0,
00127 'required' => true ),
00128 'data_float2' => array( 'name' => 'DataFloat2',
00129 'datatype' => 'float',
00130 'default' => 0,
00131 'required' => true ),
00132 'data_float3' => array( 'name' => 'DataFloat3',
00133 'datatype' => 'float',
00134 'default' => 0,
00135 'required' => true ),
00136 'data_float4' => array( 'name' => 'DataFloat4',
00137 'datatype' => 'float',
00138 'default' => 0,
00139 'required' => true ),
00140 'data_text1' => array( 'name' => 'DataText1',
00141 'datatype' => 'text',
00142 'default' => '',
00143 'required' => true ),
00144 'data_text2' => array( 'name' => 'DataText2',
00145 'datatype' => 'text',
00146 'default' => '',
00147 'required' => true ),
00148 'data_text3' => array( 'name' => 'DataText3',
00149 'datatype' => 'text',
00150 'default' => '',
00151 'required' => true ),
00152 'data_text4' => array( 'name' => 'DataText4',
00153 'datatype' => 'text',
00154 'default' => '',
00155 'required' => true ),
00156 'data_text5' => array( 'name' => 'DataText5',
00157 'datatype' => 'text',
00158 'default' => '',
00159 'required' => true ) ),
00160 'keys' => array( 'id', 'version' ),
00161 "function_attributes" => array( "content" => "content",
00162 'temporary_object_attribute' => 'instantiateTemporary',
00163 'data_type' => 'dataType',
00164 'display_info' => 'displayInfo',
00165 'name' => 'name',
00166 'nameList' => 'nameList' ),
00167 'set_functions' => array( 'name' => 'setName' ),
00168 'increment_key' => 'id',
00169 'sort' => array( 'placement' => 'asc' ),
00170 'class_name' => 'eZContentClassAttribute',
00171 'name' => 'ezcontentclass_attribute' );
00172 }
00173
00174 function clone()
00175 {
00176 $row = array(
00177 'id' => null,
00178 'version' => $this->attribute( 'version' ),
00179 'contentclass_id' => $this->attribute( 'contentclass_id' ),
00180 'identifier' => $this->attribute( 'identifier' ),
00181 'serialized_name_list' => $this->attribute( 'serialized_name_list' ),
00182 'is_searchable' => $this->attribute( 'is_searchable' ),
00183 'is_required' => $this->attribute( 'is_required' ),
00184 'can_translate' => $this->attribute( 'can_translate' ),
00185 'is_information_collector' => $this->attribute( 'is_information_collector' ),
00186 'data_type_string' => $this->attribute( 'data_type_string' ),
00187 'placement' => $this->attribute( 'placement' ),
00188 'data_int1' => $this->attribute( 'data_int1' ),
00189 'data_int2' => $this->attribute( 'data_int2' ),
00190 'data_int3' => $this->attribute( 'data_int3' ),
00191 'data_int4' => $this->attribute( 'data_int4' ),
00192 'data_float1' => $this->attribute( 'data_float1' ),
00193 'data_float2' => $this->attribute( 'data_float2' ),
00194 'data_float3' => $this->attribute( 'data_float3' ),
00195 'data_float4' => $this->attribute( 'data_float4' ),
00196 'data_text1' => $this->attribute( 'data_text1' ),
00197 'data_text2' => $this->attribute( 'data_text2' ),
00198 'data_text3' => $this->attribute( 'data_text3' ),
00199 'data_text4' => $this->attribute( 'data_text4' ),
00200 'data_text5' => $this->attribute( 'data_text5' ) );
00201 return new eZContentClassAttribute( $row );
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 function create( $class_id, $data_type_string, $optionalValues = array(), $languageLocale = false )
00224 {
00225 $nameList = new eZContentClassAttributeNameList();
00226 if ( isset( $optionalValues['serialized_name_list'] ) )
00227 $nameList->initFromSerializedList( $optionalValues['serialized_name_list'] );
00228 else if ( isset( $optionalValues['name'] ) )
00229 $nameList->initFromString( $optionalValues['name'], $languageLocale );
00230 else
00231 $nameList->initFromString( '', $languageLocale );
00232
00233 $row = array(
00234 'id' => null,
00235 'version' => EZ_CLASS_VERSION_STATUS_TEMPORARY,
00236 'contentclass_id' => $class_id,
00237 'identifier' => '',
00238 'serialized_name_list' => $nameList->serializeNames(),
00239 'is_searchable' => 1,
00240 'is_required' => 0,
00241 'can_translate' => 1,
00242 'is_information_collector' => 0,
00243 'data_type_string' => $data_type_string,
00244 'placement' => eZPersistentObject::newObjectOrder( eZContentClassAttribute::definition(),
00245 'placement',
00246 array( 'version' => 1,
00247 'contentclass_id' => $class_id ) ) );
00248 $row = array_merge( $row, $optionalValues );
00249 $attribute = new eZContentClassAttribute( $row );
00250
00251 return $attribute;
00252 }
00253
00254 function instantiate( $contentobjectID, $languageCode = false, $version = 1 )
00255 {
00256 $attribute = eZContentObjectAttribute::create( $this->attribute( 'id' ), $contentobjectID, $version, $languageCode );
00257 $attribute->initialize();
00258 $attribute->store();
00259 $attribute->postInitialize();
00260 }
00261
00262
00263
00264
00265
00266 function &instantiateTemporary( $contentobjectID = false )
00267 {
00268 $attribute = eZContentObjectAttribute::create( $this->attribute( 'id' ), $contentobjectID );
00269 return $attribute;
00270 }
00271
00272 function store()
00273 {
00274 $dataType = $this->dataType();
00275 if ( !$dataType )
00276 {
00277 return false;
00278 }
00279
00280 global $eZContentClassAttributeCacheListFull;
00281 unset( $eZContentClassAttributeCacheListFull );
00282 global $eZContentClassAttributeCacheList;
00283 unset( $eZContentClassAttributeCacheList[$this->attribute( 'contentclass_id' )] );
00284 global $eZContentClassAttributeCache;
00285 unset( $eZContentClassAttributeCache[$this->ID] );
00286
00287 $dataType->preStoreClassAttribute( $this, $this->attribute( 'version' ) );
00288
00289 $this->setAttribute( 'serialized_name_list', $this->NameList->serializeNames() );
00290
00291 $stored = eZPersistentObject::store();
00292
00293
00294 $info = $dataType->attribute( "information" );
00295 $dataType->storeClassAttribute( $this, $this->attribute( 'version' ) );
00296
00297 return $stored;
00298 }
00299
00300
00301
00302
00303
00304 function storeDefined()
00305 {
00306 $dataType = $this->dataType();
00307 if ( !$dataType )
00308 {
00309 return false;
00310 }
00311
00312 global $eZContentClassAttributeCacheListFull;
00313 unset( $eZContentClassAttributeCacheListFull );
00314 global $eZContentClassAttributeCacheList;
00315 unset( $eZContentClassAttributeCacheList[$this->attribute( 'contentclass_id' )] );
00316 global $eZContentClassAttributeCache;
00317 unset( $eZContentClassAttributeCache[$this->ID] );
00318
00319 $db =& eZDB::instance();
00320 $db->begin();
00321 $dataType->preStoreDefinedClassAttribute( $this );
00322
00323 $this->setAttribute( 'serialized_name_list', $this->NameList->serializeNames() );
00324
00325 $stored = eZPersistentObject::store();
00326
00327
00328 $info = $dataType->attribute( "information" );
00329 $dataType->storeDefinedClassAttribute( $this );
00330 $db->commit();
00331
00332 return $stored;
00333 }
00334
00335
00336
00337
00338
00339 function remove( $quiet = false )
00340 {
00341 $dataType = $this->dataType();
00342 if ( $dataType->isClassAttributeRemovable( $this ) )
00343 {
00344 global $eZContentClassAttributeCacheListFull;
00345 unset( $eZContentClassAttributeCacheListFull );
00346 global $eZContentClassAttributeCacheList;
00347 unset( $eZContentClassAttributeCacheList[$this->attribute( 'contentclass_id' )] );
00348 global $eZContentClassAttributeCache;
00349 unset( $eZContentClassAttributeCache[$this->ID] );
00350
00351 $db =& eZDB::instance();
00352 $db->begin();
00353 $dataType->deleteStoredClassAttribute( $this, $this->Version );
00354 eZPersistentObject::remove();
00355 $db->commit();
00356 return true;
00357 }
00358 else
00359 {
00360 if ( !$quiet )
00361 eZDebug::writeError( 'Datatype [' . $dataType->attribute( 'name' ) . '] can not be deleted to avoid system crash' );
00362 return false;
00363 }
00364 }
00365
00366 function &fetch( $id, $asObject = true, $version = EZ_CLASS_VERSION_STATUS_DEFINED, $field_filters = null )
00367 {
00368 $object = null;
00369 if ( $field_filters === null and $asObject and
00370 isset( $GLOBALS['eZContentClassAttributeCache'][$id][$version] ) )
00371 {
00372 $object =& $GLOBALS['eZContentClassAttributeCache'][$id][$version];
00373 }
00374 if ( $object === null )
00375 {
00376 $object = eZPersistentObject::fetchObject( eZContentClassAttribute::definition(),
00377 $field_filters,
00378 array( 'id' => $id,
00379 'version' => $version ),
00380 $asObject );
00381 if ( $field_filters === null and $asObject )
00382 {
00383 $GLOBALS['eZContentClassAttributeCache'][$id][$version] =& $object;
00384 }
00385 }
00386 return $object;
00387 }
00388
00389 function &fetchList( $asObject = true, $parameters = array() )
00390 {
00391 $parameters = array_merge( array( 'data_type' => false,
00392 'version' => false ),
00393 $parameters );
00394 $dataType = $parameters['data_type'];
00395 $version = $parameters['version'];
00396 $objects = null;
00397 if ( $asObject and $dataType === false and $version === false )
00398 {
00399 $objects =& $GLOBALS['eZContentClassAttributeCacheListFull'];
00400 }
00401 if ( !isset( $objects ) or
00402 $objects === null )
00403 {
00404 $conditions = null;
00405 if ( $dataType !== false or
00406 $version !== false )
00407 {
00408 $conditions = array();
00409 if ( $dataType !== false )
00410 $conditions['data_type_string'] = $dataType;
00411 if ( $version !== false )
00412 $conditions['version'] = $version;
00413 }
00414 $objects = eZPersistentObject::fetchObjectList( eZContentClassAttribute::definition(),
00415 null, $conditions, null, null,
00416 $asObject );
00417 if ( $asObject )
00418 {
00419 foreach ( array_keys( $objects ) as $objectKey )
00420 {
00421 $objectItem =& $objects[$objectKey];
00422 $objectID = $objectItem->ID;
00423 $objectVersion = $objectItem->Version;
00424 $GLOBALS['eZContentClassAttributeCache'][$objectID][$objectVersion] =& $objectItem;
00425 }
00426 if ( $dataType === false && $version === false )
00427 {
00428 $GLOBALS['eZContentClassAttributeCacheListFull'] =& $objects;
00429 }
00430 }
00431 }
00432 return $objects;
00433 }
00434
00435 function &fetchListByClassID( $classID, $version = EZ_CLASS_VERSION_STATUS_DEFINED, $asObject = true )
00436 {
00437 $objects = null;
00438 if ( $asObject )
00439 {
00440 $objects =& $GLOBALS['eZContentClassAttributeCacheList'][$classID][$version];
00441 }
00442 if ( !isset( $objects ) or
00443 $objects === null )
00444 {
00445 $cond = array( 'contentclass_id' => $classID,
00446 'version' => $version );
00447 $objects = eZPersistentObject::fetchObjectList( eZContentClassAttribute::definition(),
00448 null, $cond, null, null,
00449 $asObject );
00450 if ( $asObject )
00451 {
00452 foreach ( array_keys( $objects ) as $objectKey )
00453 {
00454 $objectItem =& $objects[$objectKey];
00455 $objectID = $objectItem->ID;
00456 $objectVersion = $objectItem->Version;
00457 if ( !isset( $GLOBALS['eZContentClassAttributeCache'][$objectID][$objectVersion] ) )
00458 $GLOBALS['eZContentClassAttributeCache'][$objectID][$objectVersion] =& $objectItem;
00459 }
00460 $GLOBALS['eZContentClassAttributeCacheList'][$classID][$version] =& $objects;
00461 }
00462 }
00463 return $objects;
00464 }
00465
00466 function &fetchFilteredList( $cond, $asObject = true )
00467 {
00468 $objectList = eZPersistentObject::fetchObjectList( eZContentClassAttribute::definition(),
00469 null, $cond, null, null,
00470 $asObject );
00471 if ( $asObject )
00472 {
00473 foreach ( array_keys( $objectList ) as $objectKey )
00474 {
00475 $objectItem =& $objectList[$objectKey];
00476 $objectID = $objectItem->ID;
00477 $objectVersion = $objectItem->Version;
00478 if ( !isset( $GLOBALS['eZContentClassAttributeCache'][$objectID][$objectVersion] ) )
00479 $GLOBALS['eZContentClassAttributeCache'][$objectID][$objectVersion] =& $objectItem;
00480 }
00481 }
00482 return $objectList;
00483 }
00484
00485
00486
00487
00488
00489
00490
00491 function move( $down, $params = null )
00492 {
00493 if ( is_array( $params ) )
00494 {
00495 $pos = $params['placement'];
00496 $cid = $params['contentclass_id'];
00497 $version = $params['version'];
00498 }
00499 else
00500 {
00501 $pos = $this->Position;
00502 $cid = $this->ContentClassID;
00503 $version = $this->Version;
00504 }
00505 eZPersistentObject::reorderObject( eZContentClassAttribute::definition(),
00506 array( 'placement' => $pos ),
00507 array( 'contentclass_id' => $cid,
00508 'version' => $version ),
00509 $down );
00510 }
00511
00512 function &dataType()
00513 {
00514 include_once( 'kernel/classes/ezdatatype.php' );
00515 $datatype = eZDataType::create( $this->DataTypeString );
00516 return $datatype;
00517 }
00518
00519
00520
00521
00522
00523
00524 function &content()
00525 {
00526 if ( $this->Content === null )
00527 {
00528 $dataType = $this->dataType();
00529 $this->Content =& $dataType->classAttributeContent( $this );
00530 }
00531
00532 return $this->Content;
00533 }
00534
00535
00536
00537
00538 function setContent( $content )
00539 {
00540 $this->Content =& $content;
00541 }
00542
00543
00544
00545
00546
00547 function &displayInfo()
00548 {
00549 if ( !$this->DisplayInfo )
00550 {
00551 $dataType = $this->dataType();
00552 if ( is_object( $dataType ) )
00553 {
00554 $this->DisplayInfo =& $dataType->classDisplayInformation( $this, false );
00555 }
00556 }
00557 return $this->DisplayInfo;
00558 }
00559
00560
00561
00562
00563 function customHTTPAction( &$module, &$http, $action )
00564 {
00565 $dataType = $this->dataType();
00566 $this->Module =& $module;
00567 $dataType->customClassAttributeHTTPAction( $http, $action, $this );
00568 unset( $this->Module );
00569 $this->Module = null;
00570 }
00571
00572
00573
00574
00575
00576 function ¤tModule()
00577 {
00578 return $this->Module;
00579 }
00580
00581 function cachedInfo()
00582 {
00583 include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00584 include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00585
00586 $info = array();
00587 $db =& eZDB::instance();
00588 $dbName = $db->DB;
00589
00590 $cacheDir = eZSys::cacheDirectory();
00591 $phpCache = new eZPHPCreator( "$cacheDir", "sortkey_$dbName.php", '', array( 'clustering' => 'sortkey' ) );
00592 $handler =& eZExpiryHandler::instance();
00593 $expiryTime = 0;
00594
00595 if ( $handler->hasTimestamp( 'sort-key-cache' ) )
00596 {
00597 $expiryTime = $handler->timestamp( 'sort-key-cache' );
00598 }
00599
00600 if ( $phpCache->canRestore( $expiryTime ) )
00601 {
00602 $info = $phpCache->restore( array( 'sortkey_type_array' => 'sortKeyTypeArray',
00603 'attribute_type_array' => 'attributeTypeArray' ) );
00604 }
00605 else
00606 {
00607
00608 $query = "SELECT id, data_type_string FROM ezcontentclass_attribute";
00609 $attributeArray = $db->arrayQuery( $query );
00610
00611 $attributeTypeArray = array();
00612 $sortKeyTypeArray = array();
00613 foreach ( $attributeArray as $attribute )
00614 {
00615 $attributeTypeArray[$attribute['id']] = $attribute['data_type_string'];
00616 $sortKeyTypeArray[$attribute['data_type_string']] = 0;
00617 }
00618
00619 include_once( 'kernel/classes/ezdatatype.php' );
00620
00621
00622 foreach ( array_keys( $sortKeyTypeArray ) as $key )
00623 {
00624 unset( $dataType );
00625 $dataType = eZDataType::create( $key );
00626 if( is_object( $dataType ) )
00627 $sortKeyTypeArray[$key] = $dataType->sortKeyType();
00628 }
00629 unset( $dataType );
00630
00631
00632 $phpCache->addVariable( 'sortKeyTypeArray', $sortKeyTypeArray );
00633 $phpCache->addVariable( 'attributeTypeArray', $attributeTypeArray );
00634 $phpCache->store();
00635
00636 $info['sortkey_type_array'] =& $sortKeyTypeArray;
00637 $info['attribute_type_array'] =& $attributeTypeArray;
00638 }
00639
00640 return $info;
00641 }
00642
00643
00644
00645
00646 function sortKeyTypeByID( $classAttributeID )
00647 {
00648 $sortKeyType = false;
00649
00650 $info = eZContentClassAttribute::cachedInfo();
00651 if ( isset( $info['attribute_type_array'][$classAttributeID] ) )
00652 {
00653 $classAttributeType = $info['attribute_type_array'][$classAttributeID];
00654 $sortKeyType = $info['sortkey_type_array'][$classAttributeType];
00655 }
00656
00657 return $sortKeyType;
00658 }
00659
00660
00661
00662
00663 function dataTypeByID( $classAttributeID )
00664 {
00665 $dataTypeString = false;
00666 $info = eZContentClassAttribute::cachedInfo();
00667
00668 if ( isset( $info['attribute_type_array'][$classAttributeID] ) )
00669 $dataTypeString = $info['attribute_type_array'][$classAttributeID];
00670
00671 return $dataTypeString;
00672 }
00673
00674
00675
00676
00677 function diff( $old, $new )
00678 {
00679 $datatype = $this->dataType();
00680 $result = $datatype->diff( $old, $new );
00681 return $result;
00682 }
00683
00684
00685
00686
00687 function nameFromSerializedString( $serailizedNameList, $languageLocale = false )
00688 {
00689 return eZContentClassAttributeNameList::nameFromSerializedString( $serailizedNameList, $languageLocale );
00690 }
00691
00692 function &name( $languageLocale = false )
00693 {
00694 $name = $this->NameList->name( $languageLocale );
00695 return $name;
00696 }
00697
00698 function setName( $name, $languageLocale = false )
00699 {
00700 $this->NameList->setName( $name, $languageLocale );
00701 }
00702
00703 function &nameList()
00704 {
00705 $nameList = $this->NameList->nameList();
00706 return $nameList;
00707 }
00708
00709 function setAlwaysAvailableLanguage( $languageLocale )
00710 {
00711 if ( $languageLocale )
00712 {
00713 $this->NameList->setAlwaysAvailableLanguage( $languageLocale );
00714 }
00715 else
00716 {
00717 $this->NameList->setAlwaysAvailableLanguage( false );
00718 }
00719 }
00720
00721 function removeTranslation( $languageLocale )
00722 {
00723 $this->NameList->removeName( $languageLocale );
00724 }
00725
00726
00727
00728
00729 var $Content;
00730
00731 var $DisplayInfo;
00732 var $ID;
00733 var $Version;
00734 var $ContentClassID;
00735 var $Identifier;
00736
00737 var $SerializedNameList;
00738
00739 var $NameList;
00740 var $DataTypeString;
00741 var $Position;
00742 var $IsSearchable;
00743 var $IsRequired;
00744 var $IsInformationCollector;
00745 var $Module;
00746 }
00747
00748 ?>