eZ Publish  [4.0]
ezdatetype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZDateType class
00004 //
00005 // Created on: <26-Apr-2002 16:53:04 bf>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZDateType ezdatetype.php
00033   \ingroup eZDatatype
00034   \brief Stores a date value
00035 
00036 */
00037 
00038 //include_once( "kernel/classes/ezdatatype.php" );
00039 
00040 //include_once( "lib/ezlocale/classes/ezdate.php" );
00041 
00042 class eZDateType extends eZDataType
00043 {
00044     const DATA_TYPE_STRING = "ezdate";
00045 
00046     const DEFAULT_FIELD = 'data_int1';
00047 
00048     const DEFAULT_EMTPY = 0;
00049 
00050     const DEFAULT_CURRENT_DATE = 1;
00051 
00052     function eZDateType()
00053     {
00054         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Date", 'Datatype name' ),
00055                            array( 'serialize_supported' => true ) );
00056     }
00057 
00058 
00059     function validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute )
00060     {
00061         //include_once( 'lib/ezutils/classes/ezdatetimevalidator.php' );
00062         $state = eZDateTimeValidator::validateDate( $day, $month, $year );
00063         if ( $state == eZInputValidator::STATE_INVALID )
00064         {
00065             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00066                                                                  'Date is not valid.' ) );
00067             return eZInputValidator::STATE_INVALID;
00068         }
00069         return $state;
00070     }
00071     /*!
00072      Validates the input and returns true if the input was
00073      valid for this datatype.
00074     */
00075     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00076     {
00077         if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00078              $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00079              $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00080         {
00081             $year  = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00082             $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00083             $day   = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00084             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00085 
00086             if ( $year == '' or $month == '' or $day == '' )
00087             {
00088                 if ( !( $year == '' and $month == '' and $day == '' ) or
00089                      ( !$classAttribute->attribute( 'is_information_collector' ) and
00090                        $contentObjectAttribute->validateIsRequired() ) )
00091                 {
00092                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00093                                                                          'Missing date input.' ) );
00094                     return eZInputValidator::STATE_INVALID;
00095                 }
00096                 else
00097                     return eZInputValidator::STATE_ACCEPTED;
00098             }
00099             else
00100             {
00101                 return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
00102             }
00103         }
00104         else
00105             return eZInputValidator::STATE_ACCEPTED;
00106     }
00107 
00108     /*!
00109      Fetches the http post var integer input and stores it in the data instance.
00110     */
00111     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00112     {
00113         if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00114              $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00115              $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00116         {
00117 
00118             $year  = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00119             $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00120             $day   = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00121             $date = new eZDate();
00122             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00123 
00124             if ( ( $year == '' and $month == '' and $day == '' ) or
00125                  !checkdate( $month, $day, $year ) or
00126                  $year < 1970 )
00127             {
00128                 $date->setTimeStamp( 0 );
00129             }
00130             else
00131             {
00132                 $date->setMDY( $month, $day, $year );
00133             }
00134 
00135             $contentObjectAttribute->setAttribute( 'data_int', $date->timeStamp() );
00136             return true;
00137         }
00138         return false;
00139     }
00140 
00141     /*!
00142      \reimp
00143     */
00144     function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00145     {
00146         if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00147              $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00148              $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00149         {
00150             $year  = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00151             $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00152             $day   = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00153             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00154 
00155             if ( $year == '' or $month == '' or $day == '' )
00156             {
00157                 if ( !( $year == '' and $month == '' and $day == '' ) or
00158                      $contentObjectAttribute->validateIsRequired() )
00159                 {
00160                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00161                                                                          'Missing date input.' ) );
00162                     return eZInputValidator::STATE_INVALID;
00163                 }
00164                 else
00165                     return eZInputValidator::STATE_ACCEPTED;
00166             }
00167             else
00168             {
00169                 return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
00170             }
00171         }
00172         else
00173             return eZInputValidator::STATE_INVALID;
00174     }
00175 
00176     /*!
00177      Fetches the http post variables for collected information
00178     */
00179     function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
00180     {
00181         if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
00182              $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
00183              $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
00184         {
00185 
00186             $year  = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
00187             $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
00188             $day   = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
00189             $date = new eZDate();
00190             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00191 
00192             if ( ( $year == '' and $month == '' and $day == '' ) or
00193                  !checkdate( $month, $day, $year ) or
00194                  $year < 1970 )
00195             {
00196                 $date->setTimeStamp( 0 );
00197             }
00198             else
00199             {
00200                 $date->setMDY( $month, $day, $year );
00201             }
00202 
00203             $collectionAttribute->setAttribute( 'data_int', $date->timeStamp() );
00204             return true;
00205         }
00206         return false;
00207     }
00208 
00209     /*!
00210      Returns the content.
00211     */
00212     function objectAttributeContent( $contentObjectAttribute )
00213     {
00214         $date = new eZDate( );
00215         $stamp = $contentObjectAttribute->attribute( 'data_int' );
00216         $date->setTimeStamp( $stamp );
00217         return $date;
00218     }
00219 
00220     /*!
00221      Set class attribute value for template version
00222     */
00223     function initializeClassAttribute( $classAttribute )
00224     {
00225         if ( $classAttribute->attribute( self::DEFAULT_FIELD ) == null )
00226             $classAttribute->setAttribute( self::DEFAULT_FIELD, 0 );
00227         $classAttribute->store();
00228     }
00229 
00230     /*!
00231      Sets the default value.
00232     */
00233     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00234     {
00235         if ( $currentVersion != false )
00236         {
00237             $dataInt = $originalContentObjectAttribute->attribute( "data_int" );
00238             $contentObjectAttribute->setAttribute( "data_int", $dataInt );
00239         }
00240         else
00241         {
00242             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00243             $defaultType = $contentClassAttribute->attribute( self::DEFAULT_FIELD );
00244             if ( $defaultType == 1 )
00245                 $contentObjectAttribute->setAttribute( "data_int", time() );
00246         }
00247     }
00248 
00249     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00250     {
00251         $default = $base . "_ezdate_default_" . $classAttribute->attribute( 'id' );
00252         if ( $http->hasPostVariable( $default ) )
00253         {
00254             $defaultValue = $http->postVariable( $default );
00255             $classAttribute->setAttribute( self::DEFAULT_FIELD,  $defaultValue );
00256         }
00257         return true;
00258     }
00259 
00260     /*!
00261      \reimp
00262     */
00263     function isIndexable()
00264     {
00265         return true;
00266     }
00267 
00268     /*!
00269      \reimp
00270     */
00271     function isInformationCollector()
00272     {
00273         return true;
00274     }
00275 
00276     /*!
00277      Returns the meta data used for storing search indeces.
00278     */
00279     function metaData( $contentObjectAttribute )
00280     {
00281         return $contentObjectAttribute->attribute( 'data_int' );
00282     }
00283 
00284     /*!
00285      \return string representation of an contentobjectattribute data for simplified export
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      Returns the date.
00300     */
00301     function title( $contentObjectAttribute, $name = null )
00302     {
00303         $locale = eZLocale::instance();
00304         $retVal = $contentObjectAttribute->attribute( "data_int" ) == 0 ? '' : $locale->formatDate( $contentObjectAttribute->attribute( "data_int" ) );
00305         return $retVal;
00306     }
00307 
00308     function hasObjectAttributeContent( $contentObjectAttribute )
00309     {
00310         return $contentObjectAttribute->attribute( "data_int" ) != 0;
00311     }
00312 
00313     /*!
00314      \reimp
00315     */
00316     function sortKey( $contentObjectAttribute )
00317     {
00318         return (int)$contentObjectAttribute->attribute( 'data_int' );
00319     }
00320 
00321     /*!
00322      \reimp
00323     */
00324     function sortKeyType()
00325     {
00326         return 'int';
00327     }
00328 
00329     /*!
00330      \reimp
00331     */
00332     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00333     {
00334         $dom = $attributeParametersNode->ownerDocument;
00335 
00336         $defaultValue = $classAttribute->attribute( self::DEFAULT_FIELD );
00337         $defaultValueNode = $dom->createElement( 'default-value' );
00338         switch ( $defaultValue )
00339         {
00340             case self::DEFAULT_EMTPY:
00341                 $defaultValueNode->setAttribute( 'type', 'empty' );
00342                 break;
00343 
00344             case self::DEFAULT_CURRENT_DATE:
00345                 $defaultValueNode->setAttribute( 'type', 'current-date' );
00346                 break;
00347         }
00348         $attributeParametersNode->appendChild( $defaultValueNode );
00349     }
00350 
00351     /*!
00352      \reimp
00353     */
00354     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00355     {
00356         $defaultNode = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 );
00357         $defaultValue = strtolower( $defaultNode->getAttribute( 'type' ) );
00358         switch ( $defaultValue )
00359         {
00360             case 'empty':
00361             {
00362                 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_EMTPY );
00363             } break;
00364             case 'current-date':
00365             {
00366                 $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_CURRENT_DATE );
00367             } break;
00368         }
00369     }
00370 
00371     /*!
00372      \param package
00373      \param content attribute
00374 
00375      \return a DOM representation of the content object attribute
00376     */
00377     function serializeContentObjectAttribute( $package, $objectAttribute )
00378     {
00379         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00380 
00381         $stamp = $objectAttribute->attribute( 'data_int' );
00382 
00383         if ( !is_null( $stamp ) )
00384         {
00385             $dom = $node->ownerDocument;
00386             $dateNode = $dom->createElement( 'date' );
00387             $dateNode->appendChild( $dom->createTextNode( eZDateUtils::rfc1123Date( $stamp ) ) );
00388             $node->appendChild( $dateNode );
00389         }
00390         return $node;
00391     }
00392 
00393     /*!
00394      \reimp
00395      \param package
00396      \param contentobject attribute object
00397      \param domnode object
00398     */
00399     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00400     {
00401         $dateNode = $attributeNode->getElementsByTagName( 'date' )->item( 0 );
00402         if ( is_object( $dateNode ) )
00403         {
00404             //include_once( 'lib/ezlocale/classes/ezdateutils.php' );
00405             $timestamp = eZDateUtils::textToDate( $dateNode->textContent );
00406             $objectAttribute->setAttribute( 'data_int', $timestamp );
00407         }
00408     }
00409 }
00410 
00411 eZDataType::register( eZDateType::DATA_TYPE_STRING, "eZDateType" );
00412 
00413 ?>