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