eZ Publish  [4.0]
ezfloattype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZFloatType class
00004 //
00005 // Created on: <26-Apr-2002 16:54:35 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 eZFloatType ezfloattype.php
00033   \ingroup eZDatatype
00034   \brief Stores a float value
00035 
00036 */
00037 
00038 //include_once( "kernel/classes/ezdatatype.php" );
00039 //include_once( "lib/ezutils/classes/ezfloatvalidator.php" );
00040 
00041 class eZFloatType extends eZDataType
00042 {
00043     const DATA_TYPE_STRING = "ezfloat";
00044     const MIN_FIELD = "data_float1";
00045     const MIN_VARIABLE = "_ezfloat_min_float_value_";
00046     const MAX_FIELD = "data_float2";
00047     const MAX_VARIABLE = "_ezfloat_max_float_value_";
00048     const DEFAULT_FIELD = "data_float3";
00049     const DEFAULT_VARIABLE = "_ezfloat_default_value_";
00050     const INPUT_STATE_FIELD = "data_float4";
00051     const NO_MIN_MAX_VALUE = 0;
00052     const HAS_MIN_VALUE = 1;
00053     const HAS_MAX_VALUE = 2;
00054     const HAS_MIN_MAX_VALUE = 3;
00055 
00056     function eZFloatType()
00057     {
00058         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Float", 'Datatype name' ),
00059                            array( 'serialize_supported' => true,
00060                                   'object_serialize_map' => array( 'data_float' => 'value' ) ) );
00061         $this->FloatValidator = new eZFloatValidator();
00062     }
00063 
00064     /*!
00065      Sets the default value.
00066     */
00067     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00068     {
00069         if ( $currentVersion != false )
00070         {
00071 //             $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00072 //             $currentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeID,
00073 //                                                                         $currentVersion );
00074             $dataFloat = $originalContentObjectAttribute->attribute( "data_float" );
00075             $contentObjectAttribute->setAttribute( "data_float", $dataFloat );
00076         }
00077         else
00078         {
00079             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00080             $default = $contentClassAttribute->attribute( "data_float3" );
00081             if ( $default !== 0 )
00082             {
00083                 $contentObjectAttribute->setAttribute( "data_float", $default );
00084             }
00085         }
00086     }
00087 
00088     /*!
00089      Fetches the http post var float input and stores it in the data instance.
00090     */
00091     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00092     {
00093         if ( $http->hasPostVariable( $base . "_data_float_" . $contentObjectAttribute->attribute( "id" ) ) )
00094         {
00095             $data = $http->postVariable( $base . "_data_float_" . $contentObjectAttribute->attribute( "id" ) );
00096             $contentObjectAttribute->setHTTPValue( $data );
00097 
00098             //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00099             $locale = eZLocale::instance();
00100             $data = $locale->internalNumber( $data );
00101 
00102             $data = str_replace(" ", "", $data);
00103 
00104             $contentObjectAttribute->setAttribute( "data_float", $data );
00105             return true;
00106         }
00107         return false;
00108     }
00109 
00110     /*!
00111      Validates the input and returns true if the input was
00112      valid for this datatype.
00113     */
00114     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00115     {
00116         if ( $http->hasPostVariable( $base . "_data_float_" . $contentObjectAttribute->attribute( "id" ) ) )
00117         {
00118             $data = $http->postVariable( $base . "_data_float_" . $contentObjectAttribute->attribute( "id" ) );
00119             $data = str_replace(" ", "", $data );
00120             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00121             $min = $classAttribute->attribute( self::MIN_FIELD );
00122             $max = $classAttribute->attribute( self::MAX_FIELD );
00123             $input_state = $classAttribute->attribute( self::INPUT_STATE_FIELD );
00124 
00125             if ( !$contentObjectAttribute->validateIsRequired() &&  ( $data == "" ) )
00126             {
00127                 return eZInputValidator::STATE_ACCEPTED;
00128             }
00129 
00130             //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00131             $locale = eZLocale::instance();
00132             $data = $locale->internalNumber( $data );
00133 
00134             switch( $input_state )
00135             {
00136                 case self::NO_MIN_MAX_VALUE:
00137                 {
00138                     $state = $this->FloatValidator->validate( $data );
00139                     if( $state===1 )
00140                         return eZInputValidator::STATE_ACCEPTED;
00141                     else
00142                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00143                                                                              'The given input is not a floating point number.' ) );
00144                 } break;
00145                 case self::HAS_MIN_VALUE:
00146                 {
00147                     $this->FloatValidator->setRange( $min, false );
00148                     $state = $this->FloatValidator->validate( $data );
00149                     if( $state===1 )
00150                         return eZInputValidator::STATE_ACCEPTED;
00151                     else
00152                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00153                                                                              'The input must be greater than %1' ),
00154                                                                      $min );
00155                 } break;
00156                 case self::HAS_MAX_VALUE:
00157                 {
00158                     $this->FloatValidator->setRange( false, $max );
00159                     $state = $this->FloatValidator->validate( $data );
00160                     if( $state===1 )
00161                         return eZInputValidator::STATE_ACCEPTED;
00162                     else
00163                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00164                                                                              'The input must be less than %1' ),
00165                                                                      $max );
00166                 } break;
00167                 case self::HAS_MIN_MAX_VALUE:
00168                 {
00169                     $this->FloatValidator->setRange( $min, $max );
00170                     $state = $this->FloatValidator->validate( $data );
00171                     if( $state===1 )
00172                         return eZInputValidator::STATE_ACCEPTED;
00173                     else
00174                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00175                                                                              'The input is not in defined range %1 - %2' ),
00176                                                                      $min, $max );
00177                 } break;
00178             }
00179         }
00180         return eZInputValidator::STATE_INVALID;
00181     }
00182 
00183     function fixupObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00184     {
00185     }
00186 
00187     function storeObjectAttribute( $attribute )
00188     {
00189     }
00190 
00191     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00192     {
00193         $minValueName = $base . self::MIN_VARIABLE . $classAttribute->attribute( "id" );
00194         $maxValueName = $base . self::MAX_VARIABLE . $classAttribute->attribute( "id" );
00195         $defaultValueName =  $base . self::DEFAULT_VARIABLE . $classAttribute->attribute( "id" );
00196 
00197         if ( $http->hasPostVariable( $minValueName ) and
00198              $http->hasPostVariable( $maxValueName ) and
00199              $http->hasPostVariable( $defaultValueName ) )
00200         {
00201             //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00202             $locale = eZLocale::instance();
00203 
00204             $minValueValue = $http->postVariable( $minValueName );
00205             $minValueValue = str_replace(" ", "", $minValueValue );
00206             $minValueValue = $locale->internalNumber( $minValueValue );
00207             $maxValueValue = $http->postVariable( $maxValueName );
00208             $maxValueValue = str_replace(" ", "", $maxValueValue );
00209             $maxValueValue = $locale->internalNumber( $maxValueValue );
00210             $defaultValueValue = $http->postVariable( $defaultValueName );
00211             $defaultValueValue = str_replace(" ", "", $defaultValueValue );
00212             $defaultValueValue = $locale->internalNumber( $defaultValueValue );
00213 
00214             $classAttribute->setAttribute( self::MIN_FIELD, $minValueValue );
00215             $classAttribute->setAttribute( self::MAX_FIELD, $maxValueValue );
00216             $classAttribute->setAttribute( self::DEFAULT_FIELD, $defaultValueValue );
00217 
00218             if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
00219                 $input_state = self::NO_MIN_MAX_VALUE;
00220                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00221             }
00222             else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
00223             {
00224                 $input_state = self::HAS_MAX_VALUE;
00225                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00226             }
00227             else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
00228             {
00229                 $input_state = self::HAS_MIN_VALUE;
00230                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00231             }
00232             else
00233             {
00234                 $input_state = self::HAS_MIN_MAX_VALUE;
00235                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00236             }
00237             return true;
00238         }
00239         return false;
00240     }
00241 
00242     function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
00243     {
00244         $minValueName = $base . self::MIN_VARIABLE . $classAttribute->attribute( "id" );
00245         $maxValueName = $base . self::MAX_VARIABLE . $classAttribute->attribute( "id" );
00246         $defaultValueName =  $base . self::DEFAULT_VARIABLE . $classAttribute->attribute( "id" );
00247 
00248         if ( $http->hasPostVariable( $minValueName ) and
00249              $http->hasPostVariable( $maxValueName ) and
00250              $http->hasPostVariable( $defaultValueName ) )
00251         {
00252             //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00253             $locale = eZLocale::instance();
00254 
00255             $minValueValue = $http->postVariable( $minValueName );
00256             $minValueValue = str_replace(" ", "", $minValueValue );
00257             $minValueValue = $locale->internalNumber( $minValueValue );
00258             $maxValueValue = $http->postVariable( $maxValueName );
00259             $maxValueValue = str_replace(" ", "", $maxValueValue );
00260             $maxValueValue = $locale->internalNumber( $maxValueValue );
00261             $defaultValueValue = $http->postVariable( $defaultValueName );
00262             $defaultValueValue = str_replace(" ", "", $defaultValueValue );
00263             $defaultValueValue = $locale->internalNumber( $defaultValueValue );
00264 
00265             if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
00266                 return  eZInputValidator::STATE_ACCEPTED;
00267             }
00268             else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
00269             {
00270                 $max_state = $this->FloatValidator->validate( $maxValueValue );
00271                 return  $max_state;
00272             }
00273             else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
00274             {
00275                 $min_state = $this->FloatValidator->validate( $minValueValue );
00276                 return  $min_state;
00277             }
00278             else
00279             {
00280                 $min_state = $this->FloatValidator->validate( $minValueValue );
00281                 $max_state = $this->FloatValidator->validate( $maxValueValue );
00282                 if ( ( $min_state == eZInputValidator::STATE_ACCEPTED ) and
00283                      ( $max_state == eZInputValidator::STATE_ACCEPTED ) )
00284                 {
00285                     if ($minValueValue <= $maxValueValue)
00286                         return eZInputValidator::STATE_ACCEPTED;
00287                     else
00288                     {
00289                         $state = eZInputValidator::STATE_INTERMEDIATE;
00290                         eZDebug::writeNotice( "Integer minimum value great than maximum value." );
00291                         return $state;
00292                     }
00293                 }
00294             }
00295 
00296             if ($defaultValueValue == ""){
00297                 $default_state =  eZInputValidator::STATE_ACCEPTED;
00298             }
00299             else
00300                 $default_state = $this->FloatValidator->validate( $defaultValueValue );
00301         }
00302         return eZInputValidator::STATE_INVALID;
00303     }
00304 
00305     function fixupClassAttributeHTTPInput( $http, $base, $classAttribute )
00306     {
00307         $minValueName = $base . self::MIN_VARIABLE . $classAttribute->attribute( "id" );
00308         $maxValueName = $base . self::MAX_VARIABLE . $classAttribute->attribute( "id" );
00309         if ( $http->hasPostVariable( $minValueName ) and $http->hasPostVariable( $maxValueName ) )
00310         {
00311             //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00312             $locale = eZLocale::instance();
00313 
00314             $minValueValue = $http->postVariable( $minValueName );
00315             $minValueValue = str_replace(" ", "", $minValueValue );
00316             $minValueValue = $locale->internalNumber( $minValueValue );
00317             $maxValueValue = $http->postVariable( $maxValueName );
00318             $maxValueValue = str_replace(" ", "", $maxValueValue );
00319             $maxValueValue = $locale->internalNumber( $maxValueValue );
00320 
00321             if ($minValueValue > $maxValueValue)
00322             {
00323                 $this->FloatValidator->setRange( $minValueValue, false );
00324                 $maxValueValue = $this->FloatValidator->fixup( $maxValueValue );
00325                 $http->setPostVariable( $maxValueName, $maxValueValue );
00326             }
00327         }
00328     }
00329 
00330     function storeClassAttribute( $attribute, $version )
00331     {
00332     }
00333 
00334     function metaData( $contentObjectAttribute )
00335     {
00336         return $contentObjectAttribute->attribute( "data_float" );
00337     }
00338 
00339     /*!
00340      Returns the content.
00341     */
00342     function objectAttributeContent( $contentObjectAttribute )
00343     {
00344         return $contentObjectAttribute->attribute( 'data_float' );
00345     }
00346 
00347     /*!
00348      Returns the float value.
00349     */
00350 
00351     function title( $contentObjectAttribute, $name = null )
00352     {
00353         return $contentObjectAttribute->attribute( "data_float" );
00354     }
00355 
00356     function hasObjectAttributeContent( $contentObjectAttribute )
00357     {
00358         return !is_null( $contentObjectAttribute->attribute( 'data_float' ) );
00359     }
00360     /*!
00361      \return string representation of an contentobjectattribute data for simplified export
00362 
00363     */
00364     function toString( $contentObjectAttribute )
00365     {
00366         return $contentObjectAttribute->attribute( 'data_float' );
00367     }
00368 
00369     function fromString( $contentObjectAttribute, $string )
00370     {
00371         return $contentObjectAttribute->setAttribute( 'data_float', $string );
00372     }
00373 
00374     /*!
00375      \reimp
00376     */
00377     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00378     {
00379         $defaultValue = $classAttribute->attribute( self::DEFAULT_FIELD );
00380         $minValue = $classAttribute->attribute( self::MIN_FIELD );
00381         $maxValue = $classAttribute->attribute( self::MAX_FIELD );
00382         $minMaxState = $classAttribute->attribute( self::INPUT_STATE_FIELD );
00383 
00384         $dom = $attributeParametersNode->ownerDocument;
00385         $defaultValueNode = $dom->createElement( 'default-value' );
00386         $defaultValueNode->appendChild( $dom->createTextNode( $defaultValue ) );
00387         $attributeParametersNode->appendChild( $defaultValueNode );
00388         if ( $minMaxState == self::HAS_MIN_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
00389         {
00390             $minValueNode = $dom->createElement( 'min-value' );
00391             $minValueNode->appendChild( $dom->createTextNode( $minValue ) );
00392             $attributeParametersNode->appendChild( $minValueNode );
00393         }
00394         if ( $minMaxState == self::HAS_MAX_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
00395         {
00396             $maxValueNode = $dom->createElement( 'max-value' );
00397             $maxValueNode->appendChild( $dom->createTextNode( $maxValue ) );
00398             $attributeParametersNode->appendChild( $maxValueNode );
00399         }
00400     }
00401 
00402     /*!
00403      \reimp
00404     */
00405     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00406     {
00407         $defaultValue = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 )->textContent;
00408         $minValue = $attributeParametersNode->getElementsByTagName( 'min-value' )->item( 0 )->textContent;
00409         $maxValue = $attributeParametersNode->getElementsByTagName( 'max-value' )->item( 0 )->textContent;
00410 
00411         if ( strlen( $minValue ) > 0 and strlen( $maxValue ) > 0 )
00412             $minMaxState = self::HAS_MIN_MAX_VALUE;
00413         else if ( strlen( $minValue ) > 0 )
00414             $minMaxState = self::HAS_MIN_VALUE;
00415         else if ( strlen( $maxValue ) > 0 )
00416             $minMaxState = self::HAS_MAX_VALUE;
00417         else
00418             $minMaxState = self::NO_MIN_MAX_VALUE;
00419 
00420         $classAttribute->setAttribute( self::DEFAULT_FIELD, $defaultValue );
00421         $classAttribute->setAttribute( self::MIN_FIELD, $minValue );
00422         $classAttribute->setAttribute( self::MAX_FIELD, $maxValue );
00423         $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $minMaxState );
00424     }
00425 
00426     /// \privatesection
00427     /// The float value validator
00428     public $FloatValidator;
00429 }
00430 
00431 eZDataType::register( eZFloatType::DATA_TYPE_STRING, "eZFloatType" );
00432 
00433 ?>