eZ Publish  [4.0]
eztimetype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTimeType class
00004 //
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 /*!
00031   \class eZTimeType eztimetype.php
00032   \ingroup eZDatatype
00033   \brief Stores a time value
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 class eZTimeType extends eZDataType
00042 {
00043     const DATA_TYPE_STRING = "eztime";
00044     const DEFAULT_FIELD = 'data_int1';
00045     const DEFAULT_EMTPY = 0;
00046     const DEFAULT_CURRENT_DATE = 1;
00047 
00048     function eZTimeType()
00049     {
00050         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Time", 'Datatype name' ),
00051                            array( 'serialize_supported' => true ) );
00052     }
00053 
00054     /*!
00055      Private method only for use inside this class
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 == eZInputValidator::STATE_INVALID )
00062         {
00063             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00064                                                                  'Invalid time.' ) );
00065             return eZInputValidator::STATE_INVALID;
00066         }
00067         return $state;
00068     }
00069 
00070     /*!
00071      \reimp
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 eZInputValidator::STATE_INVALID;
00091                 }
00092                 else
00093                     return eZInputValidator::STATE_ACCEPTED;
00094             }
00095             else
00096             {
00097                 return $this->validateTimeHTTPInput( $hours, $minute, $contentObjectAttribute );
00098             }
00099         }
00100         else
00101             return eZInputValidator::STATE_ACCEPTED;
00102     }
00103 
00104     /*!
00105      \reimp
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      \reimp
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 eZInputValidator::STATE_INVALID;
00148                 }
00149                 else
00150                     return eZInputValidator::STATE_ACCEPTED;
00151             }
00152             else
00153             {
00154                 return $this->validateTimeHTTPInput( $hours, $minute, $contentObjectAttribute );
00155             }
00156         }
00157         else
00158             return eZInputValidator::STATE_INVALID;
00159     }
00160 
00161    /*!
00162     \reimp
00163     Fetches the http post variables for collected information
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      Returns the content.
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      \reimp
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      \reimp
00224     */
00225     function sortKeyType()
00226     {
00227         return 'int';
00228     }
00229 
00230     /*!
00231      \return string representation of an contentobjectattribute data for simplified export
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      \reimp
00262     */
00263     function isInformationCollector()
00264     {
00265         return true;
00266     }
00267 
00268     /*!
00269      Set class attribute value for template version
00270     */
00271     function initializeClassAttribute( $classAttribute )
00272     {
00273         if ( $classAttribute->attribute( self::DEFAULT_FIELD ) == null )
00274             $classAttribute->setAttribute( self::DEFAULT_FIELD, 0 );
00275         $classAttribute->store();
00276     }
00277 
00278     /*!
00279      Sets the default value.
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( self::DEFAULT_FIELD );
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( self::DEFAULT_FIELD,  $defaultValue );
00308             return true;
00309         }
00310         return false;
00311     }
00312 
00313     /*!
00314      Returns the meta data used for storing search indeces.
00315     */
00316     function metaData( $contentObjectAttribute )
00317     {
00318         return $contentObjectAttribute->attribute( 'data_int' );
00319     }
00320 
00321     /*!
00322      Returns the date.
00323     */
00324     function title( $contentObjectAttribute, $name = null )
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      \reimp
00344     */
00345     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00346     {
00347         $dom = $attributeParametersNode->ownerDocument;
00348 
00349         $defaultValue = $classAttribute->attribute( self::DEFAULT_FIELD );
00350         $defaultValueNode = $dom->createElement( 'default-value' );
00351         switch ( $defaultValue )
00352         {
00353             case self::DEFAULT_EMTPY:
00354                 $defaultValueNode->setAttribute( 'type', 'empty' );
00355                 break;
00356 
00357             case self::DEFAULT_CURRENT_DATE:
00358                 $defaultValueNode->setAttribute( 'type', 'current-date' );
00359                 break;
00360         }
00361         $attributeParametersNode->appendChild( $defaultValueNode );
00362     }
00363 
00364     /*!
00365      \reimp
00366     */
00367     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00368     {
00369         $defaultNode = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 );
00370         $defaultValue = strtolower( $defaultNode->getAttribute( 'type' ) );
00371         switch ( $defaultValue )
00372         {
00373             case 'empty':
00374             {
00375                 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_EMTPY );
00376             } break;
00377             case 'current-date':
00378             {
00379                 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_CURRENT_DATE );
00380             } break;
00381         }
00382     }
00383 
00384     /*!
00385      \param package
00386      \param content attribute
00387 
00388      \return a DOM representation of the content object attribute
00389     */
00390     function serializeContentObjectAttribute( $package, $objectAttribute )
00391     {
00392         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00393 
00394         $stamp = $objectAttribute->attribute( 'data_int' );
00395 
00396         if ( !is_null( $stamp ) )
00397         {
00398             $dom = $node->ownerDocument;
00399             $dateNode = $dom->createElement( 'time' );
00400             $dateNode->appendChild( $dom->createTextNode( eZDateUtils::rfc1123Date( $stamp ) ) );
00401             $node->appendChild( $dateNode );
00402         }
00403         return $node;
00404     }
00405 
00406     /*!
00407      \reimp
00408      \param package
00409      \param contentobject attribute object
00410      \param ezdomnode object
00411     */
00412     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00413     {
00414         $timeNode = $attributeNode->getElementsByTagName( 'time' )->item( 0 );
00415         if ( is_object( $timeNode ) )
00416         {
00417             //include_once( 'lib/ezlocale/classes/ezdateutils.php' );
00418             $timestamp = eZDateUtils::textToDate( $timeNode->textContent );
00419             $timeOfDay = null;
00420             if ( $timestamp >= 0 )
00421             {
00422                 $time = new eZTime( $timestamp );
00423                 $timeOfDay = $time->timeOfDay();
00424             }
00425             $objectAttribute->setAttribute( 'data_int', $timeOfDay );
00426         }
00427     }
00428 }
00429 
00430 eZDataType::register( eZTimeType::DATA_TYPE_STRING, "eZTimeType" );
00431 
00432 ?>