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