eZ Publish  [4.0]
ezintegertype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZIntegerType class
00004 //
00005 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.0.x
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00027 //
00028 
00029 /*!
00030   \class eZIntegerType ezintegertype.php
00031   \ingroup eZDatatype
00032   \brief A content datatype which handles integers
00033 
00034   It provides the functionality to work as an integer and handles
00035   class definition input, object definition input and object viewing.
00036 
00037   It uses the spare field data_int in a content object attribute for storing
00038   the attribute data.
00039 */
00040 
00041 //include_once( "kernel/classes/ezdatatype.php" );
00042 //include_once( "lib/ezutils/classes/ezintegervalidator.php" );
00043 
00044 class eZIntegerType extends eZDataType
00045 {
00046     const DATA_TYPE_STRING = "ezinteger";
00047     const MIN_VALUE_FIELD = "data_int1";
00048     const MIN_VALUE_VARIABLE = "_ezinteger_min_integer_value_";
00049     const MAX_VALUE_FIELD = "data_int2";
00050     const MAX_VALUE_VARIABLE = "_ezinteger_max_integer_value_";
00051     const DEFAULT_VALUE_FIELD = "data_int3";
00052     const DEFAULT_VALUE_VARIABLE = "_ezinteger_default_value_";
00053     const INPUT_STATE_FIELD = "data_int4";
00054     const NO_MIN_MAX_VALUE = 0;
00055     const HAS_MIN_VALUE = 1;
00056     const HAS_MAX_VALUE = 2;
00057     const HAS_MIN_MAX_VALUE = 3;
00058 
00059     function eZIntegerType()
00060     {
00061         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Integer", 'Datatype name' ),
00062                            array( 'serialize_supported' => true,
00063                                   'object_serialize_map' => array( 'data_int' => 'value' ) ) );
00064         $this->IntegerValidator = new eZIntegerValidator();
00065     }
00066 
00067     /*!
00068      Private method, only for using inside this class.
00069     */
00070     function validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute )
00071     {
00072         $min = $classAttribute->attribute( self::MIN_VALUE_FIELD );
00073         $max = $classAttribute->attribute( self::MAX_VALUE_FIELD );
00074         $input_state = $classAttribute->attribute( self::INPUT_STATE_FIELD );
00075 
00076         switch( $input_state )
00077         {
00078             case self::NO_MIN_MAX_VALUE:
00079             {
00080                 $this->IntegerValidator->setRange( false, false );
00081                 $state = $this->IntegerValidator->validate( $data );
00082                 if( $state === eZInputValidator::STATE_INVALID || $state === eZInputValidator::STATE_INTERMEDIATE )
00083                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00084                                                                          'The input is not a valid integer.' ) );
00085                 else
00086                     return $state;
00087             } break;
00088             case self::HAS_MIN_VALUE:
00089             {
00090                 $this->IntegerValidator->setRange( $min, false );
00091                 $state = $this->IntegerValidator->validate( $data );
00092                 if( $state === eZInputValidator::STATE_ACCEPTED )
00093                     return eZInputValidator::STATE_ACCEPTED;
00094                 else
00095                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00096                                                                          'The number must be greater than %1' ),
00097                                                                  $min );
00098             } break;
00099             case self::HAS_MAX_VALUE:
00100             {
00101                 $this->IntegerValidator->setRange( false, $max );
00102                 $state = $this->IntegerValidator->validate( $data );
00103                 if( $state===1 )
00104                     return eZInputValidator::STATE_ACCEPTED;
00105                 else
00106                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00107                                                                          'The number must be less than %1' ),
00108                                                                  $max );
00109             } break;
00110             case self::HAS_MIN_MAX_VALUE:
00111             {
00112                 $this->IntegerValidator->setRange( $min, $max );
00113                 $state = $this->IntegerValidator->validate( $data );
00114                 if( $state===1 )
00115                     return eZInputValidator::STATE_ACCEPTED;
00116                 else
00117                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00118                                                                          'The number is not within the required range %1 - %2' ),
00119                                                                  $min, $max );
00120             } break;
00121         }
00122 
00123         return eZInputValidator::STATE_INVALID;
00124 
00125     }
00126 
00127     /*!
00128      Validates the input and returns true if the input was
00129      valid for this datatype.
00130     */
00131     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00132     {
00133         if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
00134         {
00135             $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
00136             $data = str_replace(" ", "", $data );
00137             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00138 
00139             if ( $data == "" )
00140             {
00141                 if ( !$classAttribute->attribute( 'is_information_collector' ) and
00142                      $contentObjectAttribute->validateIsRequired() )
00143                 {
00144                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00145                                                                          'Input required.' ) );
00146                     return eZInputValidator::STATE_INVALID;
00147                 }
00148                 else
00149                     return eZInputValidator::STATE_ACCEPTED;
00150             }
00151             else
00152             {
00153                 return $this->validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute );
00154             }
00155         }
00156         else
00157             return eZInputValidator::STATE_ACCEPTED;
00158     }
00159 
00160     function fixupObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00161     {
00162     }
00163 
00164     /*!
00165      Sets the default value.
00166     */
00167     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00168     {
00169         if ( $currentVersion != false )
00170         {
00171 //             $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00172 //             $currentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeID,
00173 //                                                                         $currentVersion );
00174             $dataInt = $originalContentObjectAttribute->attribute( "data_int" );
00175             $contentObjectAttribute->setAttribute( "data_int", $dataInt );
00176         }
00177         else
00178         {
00179             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00180             $default = $contentClassAttribute->attribute( "data_int3" );
00181             if ( $default !== 0 )
00182             {
00183                 $contentObjectAttribute->setAttribute( "data_int", $default );
00184             }
00185         }
00186     }
00187 
00188     /*!
00189      Fetches the http post var integer input and stores it in the data instance.
00190     */
00191     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00192     {
00193         if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
00194         {
00195             $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
00196             $data = trim( $data ) != '' ? $data : null;
00197             $data = str_replace(" ", "", $data);
00198             $contentObjectAttribute->setAttribute( "data_int", $data );
00199             return true;
00200         }
00201         return false;
00202     }
00203 
00204     /*!
00205      \reimp
00206     */
00207     function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00208     {
00209         if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
00210         {
00211             $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
00212             $data = str_replace(" ", "", $data );
00213             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00214 
00215             if ( $data == "" )
00216             {
00217                 if ( $contentObjectAttribute->validateIsRequired() )
00218                 {
00219                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00220                                                                          'Input required.' ) );
00221                     return eZInputValidator::STATE_INVALID;
00222                 }
00223                 else
00224                     return eZInputValidator::STATE_ACCEPTED;
00225             }
00226             else
00227             {
00228                 return $this->validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute );
00229             }
00230         }
00231         else
00232             return eZInputValidator::STATE_INVALID;
00233     }
00234 
00235     /*!
00236      Fetches the http post variables for collected information
00237     */
00238     function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
00239     {
00240         if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
00241         {
00242             $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
00243             $data = trim( $data ) != '' ? $data : null;
00244             $data = str_replace(" ", "", $data);
00245             $collectionAttribute->setAttribute( "data_int", $data );
00246             return true;
00247         }
00248         return false;
00249     }
00250 
00251     /*!
00252      Does nothing, the data is already present in the attribute.
00253     */
00254     function storeObjectAttribute( $object_attribute )
00255     {
00256     }
00257 
00258     function storeClassAttribute( $attribute, $version )
00259     {
00260     }
00261 
00262     /*!
00263      \reimp
00264     */
00265     function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
00266     {
00267         $minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00268         $maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00269         $defaultValueName = $base . self::DEFAULT_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00270 
00271         if ( $http->hasPostVariable( $minValueName ) and
00272              $http->hasPostVariable( $maxValueName ) and
00273              $http->hasPostVariable( $defaultValueName ) )
00274         {
00275             $minValueValue = $http->postVariable( $minValueName );
00276             $minValueValue = str_replace(" ", "", $minValueValue );
00277             $maxValueValue = $http->postVariable( $maxValueName );
00278             $maxValueValue = str_replace(" ", "", $maxValueValue );
00279             $defaultValueValue = $http->postVariable( $defaultValueName );
00280             $defaultValueValue = str_replace(" ", "", $defaultValueValue );
00281 
00282             if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
00283                 return  eZInputValidator::STATE_ACCEPTED;
00284             }
00285             else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
00286             {
00287                 $max_state = $this->IntegerValidator->validate( $maxValueValue );
00288                 return  $max_state;
00289             }
00290             else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
00291             {
00292                 $min_state = $this->IntegerValidator->validate( $minValueValue );
00293                 return  $min_state;
00294             }
00295             else
00296             {
00297                 $min_state = $this->IntegerValidator->validate( $minValueValue );
00298                 $max_state = $this->IntegerValidator->validate( $maxValueValue );
00299                 if ( ( $min_state == eZInputValidator::STATE_ACCEPTED ) and
00300                      ( $max_state == eZInputValidator::STATE_ACCEPTED ) )
00301                 {
00302                     if ($minValueValue <= $maxValueValue)
00303                         return eZInputValidator::STATE_ACCEPTED;
00304                     else
00305                     {
00306                         $state = eZInputValidator::STATE_INTERMEDIATE;
00307                         eZDebug::writeNotice( "Integer minimum value great than maximum value." );
00308                         return $state;
00309                     }
00310                 }
00311             }
00312 
00313             if ($defaultValueValue == ""){
00314                 $default_state =  eZInputValidator::STATE_ACCEPTED;
00315             }
00316             else
00317                 $default_state = $this->IntegerValidator->validate( $defaultValueValue );
00318         }
00319 
00320         return eZInputValidator::STATE_INVALID;
00321     }
00322 
00323     /*!
00324      \reimp
00325     */
00326     function fixupClassAttributeHTTPInput( $http, $base, $classAttribute )
00327     {
00328         $minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00329         $maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00330         if ( $http->hasPostVariable( $minValueName ) and $http->hasPostVariable( $maxValueName ) )
00331         {
00332             $minValueValue = $http->postVariable( $minValueName );
00333             $minValueValue = $this->IntegerValidator->fixup( $minValueValue );
00334             $http->setPostVariable( $minValueName, $minValueValue );
00335 
00336             $maxValueValue = $http->postVariable( $maxValueName );
00337             $maxValueValue = $this->IntegerValidator->fixup( $maxValueValue );
00338             $http->setPostVariable( $maxValueName, $maxValueValue );
00339 
00340             if ($minValueValue > $maxValueValue)
00341             {
00342                 $this->IntegerValidator->setRange( $minValueValue, false );
00343                 $maxValueValue = $this->IntegerValidator->fixup( $maxValueValue );
00344                 $http->setPostVariable( $maxValueName, $maxValueValue );
00345             }
00346         }
00347     }
00348 
00349     /*!
00350      \reimp
00351     */
00352     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00353     {
00354         $minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00355         $maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00356         $defaultValueName = $base . self::DEFAULT_VALUE_VARIABLE . $classAttribute->attribute( "id" );
00357 
00358         if ( $http->hasPostVariable( $minValueName ) and
00359              $http->hasPostVariable( $maxValueName ) and
00360              $http->hasPostVariable( $defaultValueName ) )
00361         {
00362             $minValueValue = $http->postVariable( $minValueName );
00363             $minValueValue = str_replace(" ", "", $minValueValue );
00364             $maxValueValue = $http->postVariable( $maxValueName );
00365             $maxValueValue = str_replace(" ", "", $maxValueValue );
00366             $defaultValueValue = $http->postVariable( $defaultValueName );
00367             $defaultValueValue = str_replace(" ", "", $defaultValueValue );
00368 
00369             $classAttribute->setAttribute( self::MIN_VALUE_FIELD, $minValueValue );
00370             $classAttribute->setAttribute( self::MAX_VALUE_FIELD, $maxValueValue );
00371             $classAttribute->setAttribute( self::DEFAULT_VALUE_FIELD, $defaultValueValue );
00372 
00373             if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
00374                 $input_state = self::NO_MIN_MAX_VALUE;
00375                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00376             }
00377             else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
00378             {
00379                 $input_state = self::HAS_MAX_VALUE;
00380                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00381             }
00382             else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
00383             {
00384                 $input_state = self::HAS_MIN_VALUE;
00385                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00386             }
00387             else
00388             {
00389                 $input_state = self::HAS_MIN_MAX_VALUE;
00390                 $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
00391             }
00392             return true;
00393         }
00394         return false;
00395     }
00396 
00397     /*!
00398      Returns the content.
00399     */
00400     function objectAttributeContent( $contentObjectAttribute )
00401     {
00402         return $contentObjectAttribute->attribute( "data_int" );
00403     }
00404 
00405 
00406     /*!
00407      Returns the meta data used for storing search indeces.
00408     */
00409     function metaData( $contentObjectAttribute )
00410     {
00411         return $contentObjectAttribute->attribute( "data_int" );
00412     }
00413     /*!
00414      \return string representation of an contentobjectattribute data for simplified export
00415 
00416     */
00417     function toString( $contentObjectAttribute )
00418     {
00419         return $contentObjectAttribute->attribute( 'data_int' );
00420     }
00421 
00422     function fromString( $contentObjectAttribute, $string )
00423     {
00424         return $contentObjectAttribute->setAttribute( 'data_int', $string );
00425     }
00426 
00427     /*!
00428      Returns the integer value.
00429     */
00430     function title( $contentObjectAttribute, $name = null )
00431     {
00432         return $contentObjectAttribute->attribute( "data_int" );
00433     }
00434 
00435     function hasObjectAttributeContent( $contentObjectAttribute )
00436     {
00437         return !is_null( $contentObjectAttribute->attribute( 'data_int' ) );
00438     }
00439 
00440     /*!
00441      \reimp
00442     */
00443     function isInformationCollector()
00444     {
00445         return true;
00446     }
00447 
00448     /*!
00449      \return true if the datatype can be indexed
00450     */
00451     function isIndexable()
00452     {
00453         return true;
00454     }
00455 
00456     /*!
00457      \reimp
00458     */
00459     function sortKey( $contentObjectAttribute )
00460     {
00461         return $contentObjectAttribute->attribute( 'data_int' );
00462     }
00463 
00464     /*!
00465      \reimp
00466     */
00467     function sortKeyType()
00468     {
00469         return 'int';
00470     }
00471 
00472     /*!
00473      \reimp
00474     */
00475     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00476     {
00477         $defaultValue = $classAttribute->attribute( self::DEFAULT_VALUE_FIELD );
00478         $minValue = $classAttribute->attribute( self::MIN_VALUE_FIELD );
00479         $maxValue = $classAttribute->attribute( self::MAX_VALUE_FIELD );
00480         $minMaxState = $classAttribute->attribute( self::INPUT_STATE_FIELD );
00481 
00482         $dom = $attributeParametersNode->ownerDocument;
00483         $defaultValueNode = $dom->createElement( 'default-value' );
00484         $defaultValueNode->appendChild( $dom->createTextNode( $defaultValue ) );
00485         $attributeParametersNode->appendChild( $defaultValueNode );
00486         if ( $minMaxState == self::HAS_MIN_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
00487         {
00488             $minValueNode = $dom->createElement( 'min-value' );
00489             $minValueNode->appendChild( $dom->createTextNode( $minValue ) );
00490             $attributeParametersNode->appendChild( $minValueNode );
00491         }
00492         if ( $minMaxState == self::HAS_MAX_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
00493         {
00494             $maxValueNode = $dom->createElement( 'max-value' );
00495             $maxValueNode->appendChild( $dom->createTextNode( $maxValue ) );
00496             $attributeParametersNode->appendChild( $maxValueNode );
00497         }
00498     }
00499 
00500     /*!
00501      \reimp
00502     */
00503     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00504     {
00505         $defaultValue = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 )->textContent;
00506         $minValue = $attributeParametersNode->getElementsByTagName( 'min-value' )->item( 0 )->textContent;
00507         $maxValue = $attributeParametersNode->getElementsByTagName( 'max-value' )->item( 0 )->textContent;
00508 
00509         if ( strlen( $minValue ) > 0 and strlen( $maxValue ) > 0 )
00510             $minMaxState = self::HAS_MIN_MAX_VALUE;
00511         else if ( strlen( $minValue ) > 0 )
00512             $minMaxState = self::HAS_MIN_VALUE;
00513         else if ( strlen( $maxValue ) > 0 )
00514             $minMaxState = self::HAS_MAX_VALUE;
00515         else
00516             $minMaxState = self::NO_MIN_MAX_VALUE;
00517 
00518         $classAttribute->setAttribute( self::DEFAULT_VALUE_FIELD, $defaultValue );
00519         $classAttribute->setAttribute( self::MIN_VALUE_FIELD, $minValue );
00520         $classAttribute->setAttribute( self::MAX_VALUE_FIELD, $maxValue );
00521         $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $minMaxState );
00522     }
00523 
00524     /// \privatesection
00525     /// The integer value validator
00526     public $IntegerValidator;
00527 }
00528 
00529 eZDataType::register( eZIntegerType::DATA_TYPE_STRING, "eZIntegerType" );
00530 
00531 ?>