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