eZ Publish  [4.0]
ezstringtype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZStringType class
00004 //
00005 // Created on: <16-Apr-2002 11:08:14 amos>
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 eZStringType ezstringtype.php
00033   \ingroup eZDatatype
00034   \brief A content datatype which handles text lines
00035 
00036   It provides the functionality to work as a text line and handles
00037   class definition input, object definition input and object viewing.
00038 
00039   It uses the spare field data_text in a content object attribute for storing
00040   the attribute data.
00041 
00042 */
00043 
00044 //include_once( 'kernel/classes/ezdatatype.php' );
00045 //include_once( 'lib/ezutils/classes/ezintegervalidator.php' );
00046 require_once( 'kernel/common/i18n.php' );
00047 
00048 class eZStringType extends eZDataType
00049 {
00050     const DATA_TYPE_STRING = 'ezstring';
00051     const MAX_LEN_FIELD = 'data_int1';
00052     const MAX_LEN_VARIABLE = '_ezstring_max_string_length_';
00053     const DEFAULT_STRING_FIELD = "data_text1";
00054     const DEFAULT_STRING_VARIABLE = "_ezstring_default_value_";
00055 
00056     /*!
00057      Initializes with a string id and a description.
00058     */
00059     function eZStringType()
00060     {
00061         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', 'Text line', 'Datatype name' ),
00062                            array( 'serialize_supported' => true,
00063                                   'object_serialize_map' => array( 'data_text' => 'text' ) ) );
00064         $this->MaxLenValidator = new eZIntegerValidator();
00065     }
00066 
00067     /*!
00068      Sets the default value.
00069     */
00070     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00071     {
00072         if ( $currentVersion != false )
00073         {
00074 //             $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00075 //             $currentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeID,
00076 //                                                                         $currentVersion );
00077             $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00078             $contentObjectAttribute->setAttribute( "data_text", $dataText );
00079         }
00080         else
00081         {
00082             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00083             $default = $contentClassAttribute->attribute( 'data_text1' );
00084             if ( $default !== '' && $default !== NULL )
00085             {
00086                 $contentObjectAttribute->setAttribute( 'data_text', $default );
00087             }
00088         }
00089     }
00090 
00091     /*
00092      Private method, only for using inside this class.
00093     */
00094     function validateStringHTTPInput( $data, $contentObjectAttribute, $classAttribute )
00095     {
00096         $maxLen = $classAttribute->attribute( self::MAX_LEN_FIELD );
00097         $textCodec = eZTextCodec::instance( false );
00098         if ( $textCodec->strlen( $data ) > $maxLen and
00099              $maxLen > 0 )
00100         {
00101             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00102                                                                  'The input text is too long. The maximum number of characters allowed is %1.' ),
00103                                                          $maxLen );
00104             return eZInputValidator::STATE_INVALID;
00105         }
00106         return eZInputValidator::STATE_ACCEPTED;
00107     }
00108 
00109 
00110     /*!
00111      \reimp
00112     */
00113     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00114     {
00115         if ( $http->hasPostVariable( $base . '_ezstring_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
00116         {
00117             $data = $http->postVariable( $base . '_ezstring_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
00118             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00119 
00120             if ( $data == "" )
00121             {
00122                 if ( !$classAttribute->attribute( 'is_information_collector' ) and
00123                      $contentObjectAttribute->validateIsRequired() )
00124                 {
00125                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00126                                                                          'Input required.' ) );
00127                     return eZInputValidator::STATE_INVALID;
00128                 }
00129             }
00130             else
00131             {
00132                 return $this->validateStringHTTPInput( $data, $contentObjectAttribute, $classAttribute );
00133             }
00134         }
00135         return eZInputValidator::STATE_ACCEPTED;
00136     }
00137 
00138     /*!
00139      \reimp
00140     */
00141     function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00142     {
00143         if ( $http->hasPostVariable( $base . '_ezstring_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
00144         {
00145             $data = $http->postVariable( $base . '_ezstring_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
00146             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00147 
00148             if ( $data == "" )
00149             {
00150                 if ( $contentObjectAttribute->validateIsRequired() )
00151                 {
00152                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00153                                                                          'Input required.' ) );
00154                     return eZInputValidator::STATE_INVALID;
00155                 }
00156                 else
00157                     return eZInputValidator::STATE_ACCEPTED;
00158             }
00159             else
00160             {
00161                 return $this->validateStringHTTPInput( $data, $contentObjectAttribute, $classAttribute );
00162             }
00163         }
00164         else
00165             return eZInputValidator::STATE_INVALID;
00166     }
00167 
00168     /*!
00169      Fetches the http post var string input and stores it in the data instance.
00170     */
00171     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00172     {
00173         if ( $http->hasPostVariable( $base . '_ezstring_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
00174         {
00175             $data = $http->postVariable( $base . '_ezstring_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
00176             $contentObjectAttribute->setAttribute( 'data_text', $data );
00177             return true;
00178         }
00179         return false;
00180     }
00181 
00182     /*!
00183      Fetches the http post variables for collected information
00184     */
00185     function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
00186     {
00187         if ( $http->hasPostVariable( $base . "_ezstring_data_text_" . $contentObjectAttribute->attribute( "id" ) ) )
00188         {
00189             $dataText = $http->postVariable( $base . "_ezstring_data_text_" . $contentObjectAttribute->attribute( "id" ) );
00190             $collectionAttribute->setAttribute( 'data_text', $dataText );
00191             return true;
00192         }
00193         return false;
00194     }
00195 
00196     /*!
00197      Does nothing since it uses the data_text field in the content object attribute.
00198      See fetchObjectAttributeHTTPInput for the actual storing.
00199     */
00200     function storeObjectAttribute( $attribute )
00201     {
00202     }
00203 
00204     /*!
00205      \reimp
00206      Simple string insertion is supported.
00207     */
00208     function isSimpleStringInsertionSupported()
00209     {
00210         return true;
00211     }
00212 
00213     /*!
00214      \reimp
00215      Inserts the string \a $string in the \c 'data_text' database field.
00216     */
00217     function insertSimpleString( $object, $objectVersion, $objectLanguage,
00218                                  $objectAttribute, $string,
00219                                  &$result )
00220     {
00221         $result = array( 'errors' => array(),
00222                          'require_storage' => true );
00223         $objectAttribute->setContent( $string );
00224         $objectAttribute->setAttribute( 'data_text', $string );
00225         return true;
00226     }
00227 
00228     function storeClassAttribute( $attribute, $version )
00229     {
00230     }
00231 
00232     function storeDefinedClassAttribute( $attribute )
00233     {
00234     }
00235 
00236     /*!
00237      \reimp
00238     */
00239     function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
00240     {
00241         $maxLenName = $base . self::MAX_LEN_VARIABLE . $classAttribute->attribute( 'id' );
00242         if ( $http->hasPostVariable( $maxLenName ) )
00243         {
00244             $maxLenValue = $http->postVariable( $maxLenName );
00245             $maxLenValue = str_replace(" ", "", $maxLenValue );
00246             if( ( $maxLenValue == "" ) ||  ( $maxLenValue == 0 ) )
00247             {
00248                 $maxLenValue = 0;
00249                 $http->setPostVariable( $maxLenName, $maxLenValue );
00250                 return eZInputValidator::STATE_ACCEPTED;
00251             }
00252             else
00253             {
00254                 $this->MaxLenValidator->setRange( 1, false );
00255                 return $this->MaxLenValidator->validate( $maxLenValue );
00256             }
00257         }
00258         return eZInputValidator::STATE_INVALID;
00259     }
00260 
00261     /*!
00262      \reimp
00263     */
00264     function fixupClassAttributeHTTPInput( $http, $base, $classAttribute )
00265     {
00266         $maxLenName = $base . self::MAX_LEN_VARIABLE . $classAttribute->attribute( 'id' );
00267         if ( $http->hasPostVariable( $maxLenName ) )
00268         {
00269             $maxLenValue = $http->postVariable( $maxLenName );
00270             $this->MaxLenValidator->setRange( 1, false );
00271             $maxLenValue = $this->MaxLenValidator->fixup( $maxLenValue );
00272             $http->setPostVariable( $maxLenName, $maxLenValue );
00273         }
00274     }
00275 
00276     /*!
00277      \reimp
00278     */
00279     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00280     {
00281         $maxLenName = $base . self::MAX_LEN_VARIABLE . $classAttribute->attribute( 'id' );
00282         $defaultValueName = $base . self::DEFAULT_STRING_VARIABLE . $classAttribute->attribute( 'id' );
00283         if ( $http->hasPostVariable( $maxLenName ) )
00284         {
00285             $maxLenValue = $http->postVariable( $maxLenName );
00286             $classAttribute->setAttribute( self::MAX_LEN_FIELD, $maxLenValue );
00287         }
00288         if ( $http->hasPostVariable( $defaultValueName ) )
00289         {
00290             $defaultValueValue = $http->postVariable( $defaultValueName );
00291 
00292             $classAttribute->setAttribute( self::DEFAULT_STRING_FIELD, $defaultValueValue );
00293         }
00294         return true;
00295     }
00296 
00297     /*!
00298      Returns the content.
00299     */
00300     function objectAttributeContent( $contentObjectAttribute )
00301     {
00302         return $contentObjectAttribute->attribute( 'data_text' );
00303     }
00304 
00305     /*!
00306      Returns the meta data used for storing search indeces.
00307     */
00308     function metaData( $contentObjectAttribute )
00309     {
00310         return $contentObjectAttribute->attribute( 'data_text' );
00311     }
00312     /*!
00313      \return string representation of an contentobjectattribute data for simplified export
00314 
00315     */
00316     function toString( $contentObjectAttribute )
00317     {
00318         return $contentObjectAttribute->attribute( 'data_text' );
00319     }
00320 
00321     function fromString( $contentObjectAttribute, $string )
00322     {
00323         return $contentObjectAttribute->setAttribute( 'data_text', $string );
00324     }
00325 
00326 
00327     /*!
00328      Returns the content of the string for use as a title
00329     */
00330     function title( $contentObjectAttribute, $name = null )
00331     {
00332         return $contentObjectAttribute->attribute( 'data_text' );
00333     }
00334 
00335     function hasObjectAttributeContent( $contentObjectAttribute )
00336     {
00337         return trim( $contentObjectAttribute->attribute( 'data_text' ) ) != '';
00338     }
00339 
00340     /*!
00341      \reimp
00342     */
00343     function isIndexable()
00344     {
00345         return true;
00346     }
00347 
00348     /*!
00349      \reimp
00350     */
00351     function isInformationCollector()
00352     {
00353         return true;
00354     }
00355 
00356     /*!
00357      \reimp
00358     */
00359     function sortKey( $contentObjectAttribute )
00360     {
00361         //include_once( 'lib/ezi18n/classes/ezchartransform.php' );
00362         $trans = eZCharTransform::instance();
00363         return $trans->transformByGroup( $contentObjectAttribute->attribute( 'data_text' ), 'lowercase' );
00364     }
00365 
00366     /*!
00367      \reimp
00368     */
00369     function sortKeyType()
00370     {
00371         return 'string';
00372     }
00373 
00374     /*!
00375      \reimp
00376     */
00377     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00378     {
00379         $maxLength = $classAttribute->attribute( self::MAX_LEN_FIELD );
00380         $defaultString = $classAttribute->attribute( self::DEFAULT_STRING_FIELD );
00381         $dom = $attributeParametersNode->ownerDocument;
00382         $maxLengthNode = $dom->createElement( 'max-length' );
00383         $maxLengthNode->appendChild( $dom->createTextNode( $maxLength ) );
00384         $attributeParametersNode->appendChild( $maxLengthNode );
00385         $defaultStringNode = $dom->createElement( 'default-string' );
00386         if ( $defaultString )
00387         {
00388             $defaultStringNode->appendChild( $dom->createTextNode( $defaultString ) );
00389         }
00390         $attributeParametersNode->appendChild( $defaultStringNode );
00391     }
00392 
00393     /*!
00394      \reimp
00395     */
00396     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00397     {
00398         $maxLength = $attributeParametersNode->getElementsByTagName( 'max-length' )->item( 0 )->textContent;
00399         $defaultString = $attributeParametersNode->getElementsByTagName( 'default-string' )->item( 0 )->textContent;
00400         $classAttribute->setAttribute( self::MAX_LEN_FIELD, $maxLength );
00401         $classAttribute->setAttribute( self::DEFAULT_STRING_FIELD, $defaultString );
00402     }
00403 
00404     /*!
00405       \reimp
00406     */
00407     function diff( $old, $new, $options = false )
00408     {
00409         //include_once( 'lib/ezdiff/classes/ezdiff.php' );
00410         $diff = new eZDiff();
00411         $diff->setDiffEngineType( $diff->engineType( 'text' ) );
00412         $diff->initDiffEngine();
00413         $diffObject = $diff->diff( $old->content(), $new->content() );
00414         return $diffObject;
00415     }
00416 
00417     /// \privatesection
00418     /// The max len validator
00419     public $MaxLenValidator;
00420 }
00421 
00422 eZDataType::register( eZStringType::DATA_TYPE_STRING, 'eZStringType' );
00423 
00424 ?>