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 include_once( "kernel/classes/ezdatatype.php" );
00038 include_once( "lib/ezlocale/classes/ezdatetime.php" );
00039
00040 define( 'EZ_DATATYPESTRING_DATETIME', 'ezdatetime' );
00041 define( 'EZ_DATATYPESTRING_DATETIME_DEFAULT', 'data_int1' );
00042 define( 'EZ_DATATYPESTRING_DATETIME_ADJUSTMENT_FIELD', 'data_text5' );
00043 define( 'EZ_DATATYPESTRING_DATETIME_DEFAULT_EMTPY', 0 );
00044 define( 'EZ_DATATYPESTRING_DATETIME_DEFAULT_CURRENT_DATE', 1 );
00045 define( 'EZ_DATATYPESTRING_DATETIME_DEFAULT_ADJUSTMENT', 2 );
00046
00047
00048 class eZDateTimeType extends eZDataType
00049 {
00050 function eZDateTimeType()
00051 {
00052 $this->eZDataType( EZ_DATATYPESTRING_DATETIME, ezi18n( 'kernel/classes/datatypes', "Date and time", 'Datatype name' ),
00053 array( 'serialize_supported' => true ) );
00054 }
00055
00056
00057
00058
00059 function validateDateTimeHTTPInput( $day, $month, $year, $hour, $minute, &$contentObjectAttribute )
00060 {
00061 include_once( 'lib/ezutils/classes/ezdatetimevalidator.php' );
00062
00063 $state = eZDateTimeValidator::validateDate( $day, $month, $year );
00064 if ( $state == EZ_INPUT_VALIDATOR_STATE_INVALID )
00065 {
00066 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00067 'Date is not valid.' ) );
00068 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00069 }
00070
00071 $state = eZDateTimeValidator::validateTime( $hour, $minute );
00072 if ( $state == EZ_INPUT_VALIDATOR_STATE_INVALID )
00073 {
00074 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00075 'Time is not valid.' ) );
00076 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00077 }
00078 return $state;
00079 }
00080
00081
00082
00083
00084
00085 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00086 {
00087 if ( $http->hasPostVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00088 $http->hasPostVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00089 $http->hasPostVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) ) and
00090 $http->hasPostVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
00091 $http->hasPostVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) ) )
00092 {
00093 $year = $http->postVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) );
00094 $month = $http->postVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) );
00095 $day = $http->postVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) );
00096 $hour = $http->postVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) );
00097 $minute = $http->postVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) );
00098 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00099
00100 if ( $year == '' or
00101 $month == '' or
00102 $day == '' or
00103 $hour == '' or
00104 $minute == '' )
00105 {
00106 if ( !( $year == '' and
00107 $month == '' and
00108 $day == '' and
00109 $hour == '' and
00110 $minute == '') or
00111 ( !$classAttribute->attribute( 'is_information_collector' ) and
00112 $contentObjectAttribute->validateIsRequired() ) )
00113 {
00114 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00115 'Missing datetime input.' ) );
00116 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00117 }
00118 else
00119 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00120 }
00121 else
00122 {
00123 return $this->validateDateTimeHTTPInput( $day, $month, $year, $hour, $minute, $contentObjectAttribute );
00124 }
00125 }
00126 else
00127 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00128 }
00129
00130
00131
00132
00133 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00134 {
00135 if ( $http->hasPostVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00136 $http->hasPostVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00137 $http->hasPostVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) ) and
00138 $http->hasPostVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
00139 $http->hasPostVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) ) )
00140 {
00141 $year = $http->postVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) );
00142 $month = $http->postVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) );
00143 $day = $http->postVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) );
00144 $hour = $http->postVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) );
00145 $minute = $http->postVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) );
00146
00147 $dateTime = new eZDateTime();
00148 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00149 if ( ( $year == '' and $month == ''and $day == '' and
00150 $hour == '' and $minute == '' ) or
00151 !checkdate( $month, $day, $year ) or $year < 1970 )
00152 {
00153 $dateTime->setTimeStamp( 0 );
00154 }
00155 else
00156 {
00157 $dateTime->setMDYHMS( $month, $day, $year, $hour, $minute, 0 );
00158 }
00159
00160 $contentObjectAttribute->setAttribute( 'data_int', $dateTime->timeStamp() );
00161 return true;
00162 }
00163 return false;
00164 }
00165
00166
00167
00168
00169 function validateCollectionAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00170 {
00171 if ( $http->hasPostVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00172 $http->hasPostVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00173 $http->hasPostVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) ) and
00174 $http->hasPostVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
00175 $http->hasPostVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) ) )
00176 {
00177 $year = $http->postVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) );
00178 $month = $http->postVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) );
00179 $day = $http->postVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) );
00180 $hour = $http->postVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) );
00181 $minute = $http->postVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) );
00182 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00183
00184 if ( $year == '' or
00185 $month == '' or
00186 $day == '' or
00187 $hour == '' or
00188 $minute == '' )
00189 {
00190 if ( !( $year == '' and
00191 $month == '' and
00192 $day == '' and
00193 $hour == '' and
00194 $minute == '') or
00195 $contentObjectAttribute->validateIsRequired() )
00196 {
00197 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00198 'Missing datetime input.' ) );
00199 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00200 }
00201 else
00202 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00203 }
00204 else
00205 {
00206 return $this->validateDateTimeHTTPInput( $day, $month, $year, $hour, $minute, $contentObjectAttribute );
00207 }
00208 }
00209 else
00210 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00211 }
00212
00213
00214
00215
00216
00217 function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
00218 {
00219 if ( $http->hasPostVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00220 $http->hasPostVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00221 $http->hasPostVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) ) and
00222 $http->hasPostVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) ) and
00223 $http->hasPostVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) ) )
00224 {
00225 $year = $http->postVariable( $base . '_datetime_year_' . $contentObjectAttribute->attribute( 'id' ) );
00226 $month = $http->postVariable( $base . '_datetime_month_' . $contentObjectAttribute->attribute( 'id' ) );
00227 $day = $http->postVariable( $base . '_datetime_day_' . $contentObjectAttribute->attribute( 'id' ) );
00228 $hour = $http->postVariable( $base . '_datetime_hour_' . $contentObjectAttribute->attribute( 'id' ) );
00229 $minute = $http->postVariable( $base . '_datetime_minute_' . $contentObjectAttribute->attribute( 'id' ) );
00230
00231 $dateTime = new eZDateTime();
00232 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00233 if ( ( $year == '' and $month == ''and $day == '' and
00234 $hour == '' and $minute == '' ) or
00235 !checkdate( $month, $day, $year ) or $year < 1970 )
00236 {
00237 $dateTime->setTimeStamp( 0 );
00238 }
00239 else
00240 {
00241 $dateTime->setMDYHMS( $month, $day, $year, $hour, $minute, 0 );
00242 }
00243
00244 $collectionAttribute->setAttribute( 'data_int', $dateTime->timeStamp() );
00245 return true;
00246 }
00247 return false;
00248 }
00249
00250
00251
00252
00253 function &objectAttributeContent( &$contentObjectAttribute )
00254 {
00255 $dateTime = new eZDateTime();
00256 $stamp = $contentObjectAttribute->attribute( 'data_int' );
00257 $dateTime->setTimeStamp( $stamp );
00258 return $dateTime;
00259 }
00260
00261
00262
00263
00264 function isIndexable()
00265 {
00266 return true;
00267 }
00268
00269
00270
00271
00272 function isInformationCollector()
00273 {
00274 return true;
00275 }
00276
00277
00278
00279
00280 function metaData( $contentObjectAttribute )
00281 {
00282 return $contentObjectAttribute->attribute( 'data_int' );
00283 }
00284
00285
00286
00287
00288 function toString( $contentObjectAttribute )
00289 {
00290 return $contentObjectAttribute->attribute( 'data_int' );
00291 }
00292
00293 function fromString( &$contentObjectAttribute, $string )
00294 {
00295 return $contentObjectAttribute->setAttribute( 'data_int', $string );
00296 }
00297
00298
00299
00300
00301 function initializeClassAttribute( &$classAttribute )
00302 {
00303 if ( $classAttribute->attribute( EZ_DATATYPESTRING_DATETIME_DEFAULT ) == null )
00304 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_DEFAULT, 0 );
00305 $classAttribute->store();
00306 }
00307
00308 function &parseXML( $xmlText )
00309 {
00310 include_once( 'lib/ezxml/classes/ezxml.php' );
00311 $xml = new eZXML();
00312 $dom =& $xml->domTree( $xmlText );
00313 return $dom;
00314 }
00315
00316 function &classAttributeContent( &$classAttribute )
00317 {
00318 $xmlText = $classAttribute->attribute( 'data_text5' );
00319 if ( trim( $xmlText ) == '' )
00320 {
00321 $classAttrContent = eZDateTimeType::defaultClassAttributeContent();
00322 return $classAttrContent;
00323 }
00324 $doc =& eZDateTimeType::parseXML( $xmlText );
00325 $root =& $doc->root();
00326 $type = $root->elementByName( 'year' );
00327 if ( $type )
00328 {
00329 $content['year'] = $type->attributeValue( 'value' );
00330 }
00331 $type = $root->elementByName( 'month' );
00332 if ( $type )
00333 {
00334 $content['month'] = $type->attributeValue( 'value' );
00335 }
00336 $type = $root->elementByName( 'day' );
00337 if ( $type )
00338 {
00339 $content['day'] = $type->attributeValue( 'value' );
00340 }
00341 $type = $root->elementByName( 'hour' );
00342 if ( $type )
00343 {
00344 $content['hour'] = $type->attributeValue( 'value' );
00345 }
00346 $type = $root->elementByName( 'minute' );
00347 if ( $type )
00348 {
00349 $content['minute'] = $type->attributeValue( 'value' );
00350 }
00351 return $content;
00352 }
00353
00354 function defaultClassAttributeContent()
00355 {
00356 return array( 'year' => '',
00357 'month' => '',
00358 'day' => '',
00359 'hour' => '',
00360 'minute' => '' );
00361 }
00362
00363
00364
00365
00366 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00367 {
00368 if ( $currentVersion != false )
00369 {
00370 $dataInt = $originalContentObjectAttribute->attribute( "data_int" );
00371 $contentObjectAttribute->setAttribute( "data_int", $dataInt );
00372 }
00373 else
00374 {
00375 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00376 $defaultType = $contentClassAttribute->attribute( EZ_DATATYPESTRING_DATETIME_DEFAULT );
00377 if ( $defaultType == EZ_DATATYPESTRING_DATETIME_DEFAULT_CURRENT_DATE )
00378 {
00379 $contentObjectAttribute->setAttribute( "data_int", mktime() );
00380 }
00381 else if ( $defaultType == EZ_DATATYPESTRING_DATETIME_DEFAULT_ADJUSTMENT )
00382 {
00383 $adjustments = eZDateTimeType::classAttributeContent( $contentClassAttribute );
00384 $value = new eZDateTime();
00385 $value->adjustDateTime( $adjustments['hour'], $adjustments['minute'], 0, $adjustments['month'], $adjustments['day'], $adjustments['year'] );
00386 $contentObjectAttribute->setAttribute( "data_int", $value->timeStamp() );
00387 }
00388 else
00389 $contentObjectAttribute->setAttribute( "data_int", 0 );
00390 }
00391 }
00392
00393 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00394 {
00395 $default = $base . "_ezdatetime_default_" . $classAttribute->attribute( 'id' );
00396 if ( $http->hasPostVariable( $default ) )
00397 {
00398 $defaultValue = $http->postVariable( $default );
00399 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_DEFAULT, $defaultValue );
00400 if ( $defaultValue == EZ_DATATYPESTRING_DATETIME_DEFAULT_ADJUSTMENT )
00401 {
00402 $doc = new eZDOMDocument( 'DateTimeAdjustments' );
00403 $root = $doc->createElementNode( 'adjustment' );
00404 $contentList = eZDateTimeType::contentObjectArrayXMLMap();
00405 foreach ( $contentList as $key => $value )
00406 {
00407 $postValue = $http->postVariable( $base . '_ezdatetime_' . $value . '_' . $classAttribute->attribute( 'id' ) );
00408 unset( $elementType );
00409 $elementType = $doc->createElementNode( $key, array( 'value' => $postValue ) );
00410 $root->appendChild( $elementType );
00411 }
00412 $doc->setRoot( $root );
00413 $docText = $doc->toString();
00414 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_ADJUSTMENT_FIELD , $docText );
00415 }
00416 }
00417 return true;
00418 }
00419
00420 function contentObjectArrayXMLMap()
00421 {
00422 return array( 'year' => 'year',
00423 'month' => 'month',
00424 'day' => 'day',
00425 'hour' => 'hour',
00426 'minute' => 'minute' );
00427 }
00428
00429
00430
00431
00432
00433 function title( &$contentObjectAttribute )
00434 {
00435 $locale =& eZLocale::instance();
00436 $retVal = $contentObjectAttribute->attribute( "data_int" ) == 0 ? '' : $locale->formatDateTime( $contentObjectAttribute->attribute( "data_int" ) );
00437 return $retVal;
00438 }
00439
00440 function hasObjectAttributeContent( &$contentObjectAttribute )
00441 {
00442 return $contentObjectAttribute->attribute( "data_int" ) != 0;
00443 }
00444
00445
00446
00447
00448 function sortKey( &$contentObjectAttribute )
00449 {
00450 return (int)$contentObjectAttribute->attribute( 'data_int' );
00451 }
00452
00453
00454
00455
00456 function sortKeyType()
00457 {
00458 return 'int';
00459 }
00460
00461
00462
00463
00464 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00465 {
00466 $defaultValue = $classAttribute->attribute( EZ_DATATYPESTRING_DATETIME_DEFAULT );
00467 switch ( $defaultValue )
00468 {
00469 case EZ_DATATYPESTRING_DATETIME_DEFAULT_CURRENT_DATE:
00470 {
00471 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
00472 array( 'type' => 'current-date' ) ) );
00473 } break;
00474 case EZ_DATATYPESTRING_DATETIME_DEFAULT_ADJUSTMENT:
00475 {
00476 $xml = new eZXML();
00477 $adjustValue = $classAttribute->attribute( EZ_DATATYPESTRING_DATETIME_ADJUSTMENT_FIELD );
00478 $adjustDOMValue =& $xml->domTree( $adjustValue );
00479 $defaultNode = eZDOMDocument::createElementNode( 'default-value', array( 'type' => 'adjustment' ) );
00480 if ( $adjustDOMValue )
00481 {
00482 $adjustmentNodeList =& $adjustDOMValue->elementsByName( 'adjustment' );
00483 if ( $adjustmentNodeList )
00484 {
00485 $defaultNode->appendChild( $adjustmentNodeList[0] );
00486 }
00487 }
00488 $attributeParametersNode->appendChild( $defaultNode );
00489 } break;
00490 case EZ_DATATYPESTRING_DATETIME_DEFAULT_EMTPY:
00491 {
00492 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
00493 array( 'type' => 'empty' ) ) );
00494 } break;
00495 default:
00496 {
00497 eZDebug::writeError( 'Unknown type of DateTime default value. Empty type used instead.',
00498 'eZDateTimeType::serializeContentClassAttribute()' );
00499 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
00500 array( 'type' => 'empty' ) ) );
00501 } break;
00502 }
00503 }
00504
00505
00506
00507
00508 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00509 {
00510 $defaultValue = '';
00511 $defaultNode =& $attributeParametersNode->elementByName( 'default-value' );
00512 if ( $defaultNode )
00513 {
00514 $defaultValue = strtolower( $defaultNode->attributeValue( 'type' ) );
00515 }
00516 switch ( $defaultValue )
00517 {
00518 case 'current-date':
00519 {
00520 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_DEFAULT, EZ_DATATYPESTRING_DATETIME_DEFAULT_CURRENT_DATE );
00521 } break;
00522 case 'adjustment':
00523 {
00524 $adjustmentValue = '';
00525 $adjustmentNode =& $defaultNode->elementByName( 'adjustment' );
00526 if ( $adjustmentNode )
00527 {
00528 $adjustmentDOMValue = new eZDOMDocument();
00529 $adjustmentDOMValue->setRoot( $adjustmentNode );
00530 $adjustmentValue = $adjustmentDOMValue->toString();
00531 }
00532
00533 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_DEFAULT, EZ_DATATYPESTRING_DATETIME_DEFAULT_ADJUSTMENT );
00534 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_ADJUSTMENT_FIELD, $adjustmentValue );
00535 } break;
00536 case 'empty':
00537 {
00538 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_DEFAULT, EZ_DATATYPESTRING_DATETIME_DEFAULT_EMTPY );
00539 } break;
00540 default:
00541 {
00542 eZDebug::writeError( 'Type of DateTime default value is not set. Empty type used as default.',
00543 'eZDateTimeType::unserializeContentClassAttribute()' );
00544 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATETIME_DEFAULT, EZ_DATATYPESTRING_DATETIME_DEFAULT_EMTPY );
00545 } break;
00546 }
00547 }
00548
00549
00550
00551
00552
00553 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00554 {
00555 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00556 $stamp = $objectAttribute->attribute( 'data_int' );
00557
00558 if ( $stamp )
00559 {
00560 include_once( 'lib/ezlocale/classes/ezdateutils.php' );
00561 $datetext = eZDateUtils::rfc1123Date( $stamp );
00562 $node->appendChild( eZDOMDocument::createElementTextNode( 'date_time', $datetext ) );
00563 }
00564
00565 return $node;
00566 }
00567
00568
00569
00570
00571 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00572 {
00573 $dateTimeNode = $attributeNode->elementByName( 'date_time' );
00574 if ( is_object( $dateTimeNode ) )
00575 {
00576 $timestampNode = $dateTimeNode->firstChild();
00577 if ( is_object( $timestampNode ) )
00578 {
00579 include_once( 'lib/ezlocale/classes/ezdateutils.php' );
00580 $timestamp = eZDateUtils::textToDate( $timestampNode->content() );
00581 $objectAttribute->setAttribute( 'data_int', $timestamp );
00582 }
00583 }
00584 }
00585 }
00586
00587 eZDataType::register( EZ_DATATYPESTRING_DATETIME, "ezdatetimetype" );
00588
00589 ?>