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( "kernel/classes/ezdatatype.php" );
00039
00040 define( "EZ_DATATYPESTRING_DATE", "ezdate" );
00041 define( 'EZ_DATATYPESTRING_DATE_DEFAULT', 'data_int1' );
00042 define( 'EZ_DATATYPESTRING_DATE_DEFAULT_EMTPY', 0 );
00043 define( 'EZ_DATATYPESTRING_DATE_DEFAULT_CURRENT_DATE', 1 );
00044 include_once( "lib/ezlocale/classes/ezdate.php" );
00045
00046 class eZDateType extends eZDataType
00047 {
00048 function eZDateType()
00049 {
00050 $this->eZDataType( EZ_DATATYPESTRING_DATE, ezi18n( 'kernel/classes/datatypes', "Date", 'Datatype name' ),
00051 array( 'serialize_supported' => true ) );
00052 }
00053
00054
00055 function validateDateTimeHTTPInput( $day, $month, $year, &$contentObjectAttribute )
00056 {
00057 include_once( 'lib/ezutils/classes/ezdatetimevalidator.php' );
00058 $state = eZDateTimeValidator::validateDate( $day, $month, $year );
00059 if ( $state == EZ_INPUT_VALIDATOR_STATE_INVALID )
00060 {
00061 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00062 'Date is not valid.' ) );
00063 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00064 }
00065 return $state;
00066 }
00067
00068
00069
00070
00071 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00072 {
00073 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00074 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00075 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00076 {
00077 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00078 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00079 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00080 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00081
00082 if ( $year == '' or $month == '' or $day == '' )
00083 {
00084 if ( !( $year == '' and $month == '' and $day == '' ) or
00085 ( !$classAttribute->attribute( 'is_information_collector' ) and
00086 $contentObjectAttribute->validateIsRequired() ) )
00087 {
00088 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00089 'Missing date input.' ) );
00090 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00091 }
00092 else
00093 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00094 }
00095 else
00096 {
00097 return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
00098 }
00099 }
00100 else
00101 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00102 }
00103
00104
00105
00106
00107 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00108 {
00109 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00110 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00111 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00112 {
00113
00114 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00115 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00116 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00117 $date = new eZDate();
00118 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00119
00120 if ( ( $year == '' and $month == '' and $day == '' ) or
00121 !checkdate( $month, $day, $year ) or
00122 $year < 1970 )
00123 {
00124 $date->setTimeStamp( 0 );
00125 }
00126 else
00127 {
00128 $date->setMDY( $month, $day, $year );
00129 }
00130
00131 $contentObjectAttribute->setAttribute( 'data_int', $date->timeStamp() );
00132 return true;
00133 }
00134 return false;
00135 }
00136
00137
00138
00139
00140 function validateCollectionAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00141 {
00142 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00143 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00144 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00145 {
00146 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00147 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00148 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00149 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00150
00151 if ( $year == '' or $month == '' or $day == '' )
00152 {
00153 if ( !( $year == '' and $month == '' and $day == '' ) or
00154 $contentObjectAttribute->validateIsRequired() )
00155 {
00156 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00157 'Missing date input.' ) );
00158 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00159 }
00160 else
00161 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00162 }
00163 else
00164 {
00165 return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
00166 }
00167 }
00168 else
00169 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00170 }
00171
00172
00173
00174
00175 function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
00176 {
00177 if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00178 $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00179 $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00180 {
00181
00182 $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00183 $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00184 $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00185 $date = new eZDate();
00186 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00187
00188 if ( ( $year == '' and $month == '' and $day == '' ) or
00189 !checkdate( $month, $day, $year ) or
00190 $year < 1970 )
00191 {
00192 $date->setTimeStamp( 0 );
00193 }
00194 else
00195 {
00196 $date->setMDY( $month, $day, $year );
00197 }
00198
00199 $collectionAttribute->setAttribute( 'data_int', $date->timeStamp() );
00200 return true;
00201 }
00202 return false;
00203 }
00204
00205
00206
00207
00208 function &objectAttributeContent( &$contentObjectAttribute )
00209 {
00210 $date = new eZDate( );
00211 $stamp = $contentObjectAttribute->attribute( 'data_int' );
00212 $date->setTimeStamp( $stamp );
00213 return $date;
00214 }
00215
00216
00217
00218
00219 function initializeClassAttribute( &$classAttribute )
00220 {
00221 if ( $classAttribute->attribute( EZ_DATATYPESTRING_DATE_DEFAULT ) == null )
00222 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATE_DEFAULT, 0 );
00223 $classAttribute->store();
00224 }
00225
00226
00227
00228
00229 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00230 {
00231 if ( $currentVersion != false )
00232 {
00233 $dataInt = $originalContentObjectAttribute->attribute( "data_int" );
00234 $contentObjectAttribute->setAttribute( "data_int", $dataInt );
00235 }
00236 else
00237 {
00238 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00239 $defaultType = $contentClassAttribute->attribute( EZ_DATATYPESTRING_DATE_DEFAULT );
00240 if ( $defaultType == 1 )
00241 $contentObjectAttribute->setAttribute( "data_int", mktime() );
00242 }
00243 }
00244
00245 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00246 {
00247 $default = $base . "_ezdate_default_" . $classAttribute->attribute( 'id' );
00248 if ( $http->hasPostVariable( $default ) )
00249 {
00250 $defaultValue = $http->postVariable( $default );
00251 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATE_DEFAULT, $defaultValue );
00252 }
00253 return true;
00254 }
00255
00256
00257
00258
00259 function isIndexable()
00260 {
00261 return true;
00262 }
00263
00264
00265
00266
00267 function isInformationCollector()
00268 {
00269 return true;
00270 }
00271
00272
00273
00274
00275 function metaData( $contentObjectAttribute )
00276 {
00277 return $contentObjectAttribute->attribute( 'data_int' );
00278 }
00279
00280
00281
00282
00283
00284 function toString( $contentObjectAttribute )
00285 {
00286 return $contentObjectAttribute->attribute( 'data_int' );
00287 }
00288
00289 function fromString( &$contentObjectAttribute, $string )
00290 {
00291 return $contentObjectAttribute->setAttribute( 'data_int', $string );
00292 }
00293
00294
00295
00296
00297 function title( &$contentObjectAttribute )
00298 {
00299 $locale =& eZLocale::instance();
00300 $retVal = $contentObjectAttribute->attribute( "data_int" ) == 0 ? '' : $locale->formatDate( $contentObjectAttribute->attribute( "data_int" ) );
00301 return $retVal;
00302 }
00303
00304 function hasObjectAttributeContent( &$contentObjectAttribute )
00305 {
00306 return $contentObjectAttribute->attribute( "data_int" ) != 0;
00307 }
00308
00309
00310
00311
00312 function sortKey( &$contentObjectAttribute )
00313 {
00314 return (int)$contentObjectAttribute->attribute( 'data_int' );
00315 }
00316
00317
00318
00319
00320 function sortKeyType()
00321 {
00322 return 'int';
00323 }
00324
00325
00326
00327
00328 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00329 {
00330 $defaultValue = $classAttribute->attribute( EZ_DATATYPESTRING_DATE_DEFAULT );
00331 switch ( $defaultValue )
00332 {
00333 case EZ_DATATYPESTRING_DATE_DEFAULT_EMTPY:
00334 {
00335 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
00336 array( 'type' => 'empty' ) ) );
00337 } break;
00338 case EZ_DATATYPESTRING_DATE_DEFAULT_CURRENT_DATE:
00339 {
00340 $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
00341 array( 'type' => 'current-date' ) ) );
00342 } break;
00343 }
00344 }
00345
00346
00347
00348
00349 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00350 {
00351 $defaultNode =& $attributeParametersNode->elementByName( 'default-value' );
00352 $defaultValue = strtolower( $defaultNode->attributeValue( 'type' ) );
00353 switch ( $defaultValue )
00354 {
00355 case 'empty':
00356 {
00357 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATE_DEFAULT, EZ_DATATYPESTRING_DATE_DEFAULT_EMTPY );
00358 } break;
00359 case 'current-date':
00360 {
00361 $classAttribute->setAttribute( EZ_DATATYPESTRING_DATE_DEFAULT, EZ_DATATYPESTRING_DATE_DEFAULT_CURRENT_DATE );
00362 } break;
00363 }
00364 }
00365
00366
00367
00368
00369
00370
00371
00372 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00373 {
00374 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00375
00376 $stamp = $objectAttribute->attribute( 'data_int' );
00377
00378 if ( !is_null( $stamp ) )
00379 {
00380 include_once( 'lib/ezlocale/classes/ezdateutils.php' );
00381 $node->appendChild( eZDOMDocument::createElementTextNode( 'date', eZDateUtils::rfc1123Date( $stamp ) ) );
00382 }
00383 return $node;
00384 }
00385
00386
00387
00388
00389
00390
00391
00392 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00393 {
00394 $timeNode = $attributeNode->elementByName( 'date' );
00395 if ( is_object( $timeNode ) )
00396 $timestampNode = $timeNode->firstChild();
00397 if ( is_object( $timestampNode ) )
00398 {
00399 include_once( 'lib/ezlocale/classes/ezdateutils.php' );
00400 $objectAttribute->setAttribute( 'data_int', eZDateUtils::textToDate( $timestampNode->content() ) );
00401 }
00402 }
00403 }
00404
00405 eZDataType::register( EZ_DATATYPESTRING_DATE, "ezdatetype" );
00406
00407 ?>