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