/
ezintegertype.php
514 lines (461 loc) · 20.5 KB
/
ezintegertype.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/**
* File containing the eZIntegerType class.
*
* @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package kernel
*/
/*!
\class eZIntegerType ezintegertype.php
\ingroup eZDatatype
\brief A content datatype which handles integers
It provides the functionality to work as an integer and handles
class definition input, object definition input and object viewing.
It uses the spare field data_int in a content object attribute for storing
the attribute data.
*/
class eZIntegerType extends eZDataType
{
const DATA_TYPE_STRING = "ezinteger";
const MIN_VALUE_FIELD = "data_int1";
const MIN_VALUE_VARIABLE = "_ezinteger_min_integer_value_";
const MAX_VALUE_FIELD = "data_int2";
const MAX_VALUE_VARIABLE = "_ezinteger_max_integer_value_";
const DEFAULT_VALUE_FIELD = "data_int3";
const DEFAULT_VALUE_VARIABLE = "_ezinteger_default_value_";
const INPUT_STATE_FIELD = "data_int4";
const NO_MIN_MAX_VALUE = 0;
const HAS_MIN_VALUE = 1;
const HAS_MAX_VALUE = 2;
const HAS_MIN_MAX_VALUE = 3;
function eZIntegerType()
{
$this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Integer", 'Datatype name' ),
array( 'serialize_supported' => true,
'object_serialize_map' => array( 'data_int' => 'value' ) ) );
$this->IntegerValidator = new eZIntegerValidator();
}
/**
* Validates $data with the constraints defined on the class attribute
*
* @param $data
* @param eZContentObjectAttribute $contentObjectAttribute
* @param eZContentClassAttribute $classAttribute
*
* @return int
*/
function validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute )
{
$min = $classAttribute->attribute( self::MIN_VALUE_FIELD );
$max = $classAttribute->attribute( self::MAX_VALUE_FIELD );
$input_state = $classAttribute->attribute( self::INPUT_STATE_FIELD );
switch( $input_state )
{
case self::NO_MIN_MAX_VALUE:
{
$this->IntegerValidator->setRange( false, false );
$state = $this->IntegerValidator->validate( $data );
if( $state === eZInputValidator::STATE_INVALID || $state === eZInputValidator::STATE_INTERMEDIATE )
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
'The input is not a valid integer.' ) );
else
return $state;
} break;
case self::HAS_MIN_VALUE:
{
$this->IntegerValidator->setRange( $min, false );
$state = $this->IntegerValidator->validate( $data );
if( $state === eZInputValidator::STATE_ACCEPTED )
return eZInputValidator::STATE_ACCEPTED;
else
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
'The number must be greater than %1' ),
$min );
} break;
case self::HAS_MAX_VALUE:
{
$this->IntegerValidator->setRange( false, $max );
$state = $this->IntegerValidator->validate( $data );
if( $state===1 )
return eZInputValidator::STATE_ACCEPTED;
else
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
'The number must be less than %1' ),
$max );
} break;
case self::HAS_MIN_MAX_VALUE:
{
$this->IntegerValidator->setRange( $min, $max );
$state = $this->IntegerValidator->validate( $data );
if( $state===1 )
return eZInputValidator::STATE_ACCEPTED;
else
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
'The number is not within the required range %1 - %2' ),
$min, $max );
} break;
}
return eZInputValidator::STATE_INVALID;
}
/*!
Validates the input and returns true if the input was
valid for this datatype.
*/
function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
$classAttribute = $contentObjectAttribute->contentClassAttribute();
if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
{
$data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
$data = str_replace(" ", "", $data );
if ( $data == "" )
{
if ( !$classAttribute->attribute( 'is_information_collector' ) and
$contentObjectAttribute->validateIsRequired() )
{
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
'Input required.' ) );
return eZInputValidator::STATE_INVALID;
}
else
return eZInputValidator::STATE_ACCEPTED;
}
else
{
return $this->validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute );
}
}
else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
{
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Input required.' ) );
return eZInputValidator::STATE_INVALID;
}
else
return eZInputValidator::STATE_ACCEPTED;
}
function fixupObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
}
/*!
Sets the default value.
*/
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
{
if ( $currentVersion != false )
{
// $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
// $currentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeID,
// $currentVersion );
$dataInt = $originalContentObjectAttribute->attribute( "data_int" );
$contentObjectAttribute->setAttribute( "data_int", $dataInt );
}
else
{
$contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
$default = $contentClassAttribute->attribute( "data_int3" );
if ( $default !== 0 )
{
$contentObjectAttribute->setAttribute( "data_int", $default );
}
}
}
/*!
Fetches the http post var integer input and stores it in the data instance.
*/
function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
{
$data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
$data = trim( $data ) != '' ? $data : null;
$data = str_replace(" ", "", $data);
$contentObjectAttribute->setAttribute( "data_int", $data );
return true;
}
return false;
}
function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
{
$data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
$data = str_replace(" ", "", $data );
$classAttribute = $contentObjectAttribute->contentClassAttribute();
if ( $data == "" )
{
if ( $contentObjectAttribute->validateIsRequired() )
{
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
'Input required.' ) );
return eZInputValidator::STATE_INVALID;
}
else
return eZInputValidator::STATE_ACCEPTED;
}
else
{
return $this->validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute );
}
}
else
return eZInputValidator::STATE_INVALID;
}
/*!
Fetches the http post variables for collected information
*/
function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
{
if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
{
$data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
$data = trim( $data ) != '' ? $data : null;
$data = str_replace(" ", "", $data);
$collectionAttribute->setAttribute( "data_int", $data );
return true;
}
return false;
}
/*!
Does nothing, the data is already present in the attribute.
*/
function storeObjectAttribute( $object_attribute )
{
}
function storeClassAttribute( $attribute, $version )
{
}
function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
{
$minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
$maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
$defaultValueName = $base . self::DEFAULT_VALUE_VARIABLE . $classAttribute->attribute( "id" );
if ( $http->hasPostVariable( $minValueName ) and
$http->hasPostVariable( $maxValueName ) and
$http->hasPostVariable( $defaultValueName ) )
{
$minValueValue = $http->postVariable( $minValueName );
$minValueValue = str_replace(" ", "", $minValueValue );
$maxValueValue = $http->postVariable( $maxValueName );
$maxValueValue = str_replace(" ", "", $maxValueValue );
$defaultValueValue = $http->postVariable( $defaultValueName );
$defaultValueValue = str_replace(" ", "", $defaultValueValue );
if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
return eZInputValidator::STATE_ACCEPTED;
}
else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
{
$max_state = $this->IntegerValidator->validate( $maxValueValue );
return $max_state;
}
else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
{
$min_state = $this->IntegerValidator->validate( $minValueValue );
return $min_state;
}
else
{
$min_state = $this->IntegerValidator->validate( $minValueValue );
$max_state = $this->IntegerValidator->validate( $maxValueValue );
if ( ( $min_state == eZInputValidator::STATE_ACCEPTED ) and
( $max_state == eZInputValidator::STATE_ACCEPTED ) )
{
if ($minValueValue <= $maxValueValue)
return eZInputValidator::STATE_ACCEPTED;
else
{
$state = eZInputValidator::STATE_INTERMEDIATE;
eZDebug::writeNotice( "Integer minimum value great than maximum value." );
return $state;
}
}
}
if ($defaultValueValue == ""){
$default_state = eZInputValidator::STATE_ACCEPTED;
}
else
$default_state = $this->IntegerValidator->validate( $defaultValueValue );
}
return eZInputValidator::STATE_INVALID;
}
function fixupClassAttributeHTTPInput( $http, $base, $classAttribute )
{
$minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
$maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
if ( $http->hasPostVariable( $minValueName ) and $http->hasPostVariable( $maxValueName ) )
{
$minValueValue = $http->postVariable( $minValueName );
$minValueValue = $this->IntegerValidator->fixup( $minValueValue );
$http->setPostVariable( $minValueName, $minValueValue );
$maxValueValue = $http->postVariable( $maxValueName );
$maxValueValue = $this->IntegerValidator->fixup( $maxValueValue );
$http->setPostVariable( $maxValueName, $maxValueValue );
if ($minValueValue > $maxValueValue)
{
$this->IntegerValidator->setRange( $minValueValue, false );
$maxValueValue = $this->IntegerValidator->fixup( $maxValueValue );
$http->setPostVariable( $maxValueName, $maxValueValue );
}
}
}
function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
{
$minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
$maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
$defaultValueName = $base . self::DEFAULT_VALUE_VARIABLE . $classAttribute->attribute( "id" );
if ( $http->hasPostVariable( $minValueName ) and
$http->hasPostVariable( $maxValueName ) and
$http->hasPostVariable( $defaultValueName ) )
{
$minValueValue = $http->postVariable( $minValueName );
$minValueValue = str_replace(" ", "", $minValueValue );
$maxValueValue = $http->postVariable( $maxValueName );
$maxValueValue = str_replace(" ", "", $maxValueValue );
$defaultValueValue = $http->postVariable( $defaultValueName );
$defaultValueValue = str_replace(" ", "", $defaultValueValue );
$classAttribute->setAttribute( self::MIN_VALUE_FIELD, $minValueValue );
$classAttribute->setAttribute( self::MAX_VALUE_FIELD, $maxValueValue );
$classAttribute->setAttribute( self::DEFAULT_VALUE_FIELD, $defaultValueValue );
if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
$input_state = self::NO_MIN_MAX_VALUE;
$classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
}
else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
{
$input_state = self::HAS_MAX_VALUE;
$classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
}
else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
{
$input_state = self::HAS_MIN_VALUE;
$classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
}
else
{
$input_state = self::HAS_MIN_MAX_VALUE;
$classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
}
return true;
}
return false;
}
/*!
Returns the content.
*/
function objectAttributeContent( $contentObjectAttribute )
{
return $contentObjectAttribute->attribute( "data_int" );
}
/*!
Returns the meta data used for storing search indeces.
*/
function metaData( $contentObjectAttribute )
{
return (int)$contentObjectAttribute->attribute( "data_int" );
}
/*!
\return string representation of an contentobjectattribute data for simplified export
*/
function toString( $contentObjectAttribute )
{
return $contentObjectAttribute->attribute( 'data_int' );
}
function fromString( $contentObjectAttribute, $string )
{
return $contentObjectAttribute->setAttribute( 'data_int', $string );
}
/*!
Returns the integer value.
*/
function title( $contentObjectAttribute, $name = null )
{
return $contentObjectAttribute->attribute( "data_int" );
}
function hasObjectAttributeContent( $contentObjectAttribute )
{
return $contentObjectAttribute->attribute( 'data_int' ) !== null;
}
function isInformationCollector()
{
return true;
}
/*!
\return true if the datatype can be indexed
*/
function isIndexable()
{
return true;
}
function sortKey( $contentObjectAttribute )
{
return $contentObjectAttribute->attribute( 'data_int' );
}
function sortKeyType()
{
return 'int';
}
function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
{
$defaultValue = $classAttribute->attribute( self::DEFAULT_VALUE_FIELD );
$minValue = $classAttribute->attribute( self::MIN_VALUE_FIELD );
$maxValue = $classAttribute->attribute( self::MAX_VALUE_FIELD );
$minMaxState = $classAttribute->attribute( self::INPUT_STATE_FIELD );
$dom = $attributeParametersNode->ownerDocument;
$defaultValueNode = $dom->createElement( 'default-value' );
$defaultValueNode->appendChild( $dom->createTextNode( $defaultValue ) );
$attributeParametersNode->appendChild( $defaultValueNode );
if ( $minMaxState == self::HAS_MIN_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
{
$minValueNode = $dom->createElement( 'min-value' );
$minValueNode->appendChild( $dom->createTextNode( $minValue ) );
$attributeParametersNode->appendChild( $minValueNode );
}
if ( $minMaxState == self::HAS_MAX_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
{
$maxValueNode = $dom->createElement( 'max-value' );
$maxValueNode->appendChild( $dom->createTextNode( $maxValue ) );
$attributeParametersNode->appendChild( $maxValueNode );
}
}
function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
{
$defaultValue = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 )->textContent;
$minValue = $attributeParametersNode->getElementsByTagName( 'min-value' )->item( 0 )->textContent;
$maxValue = $attributeParametersNode->getElementsByTagName( 'max-value' )->item( 0 )->textContent;
if ( strlen( $minValue ) > 0 and strlen( $maxValue ) > 0 )
$minMaxState = self::HAS_MIN_MAX_VALUE;
else if ( strlen( $minValue ) > 0 )
$minMaxState = self::HAS_MIN_VALUE;
else if ( strlen( $maxValue ) > 0 )
$minMaxState = self::HAS_MAX_VALUE;
else
$minMaxState = self::NO_MIN_MAX_VALUE;
$classAttribute->setAttribute( self::DEFAULT_VALUE_FIELD, $defaultValue );
$classAttribute->setAttribute( self::MIN_VALUE_FIELD, $minValue );
$classAttribute->setAttribute( self::MAX_VALUE_FIELD, $maxValue );
$classAttribute->setAttribute( self::INPUT_STATE_FIELD, $minMaxState );
}
function batchInitializeObjectAttributeData( $classAttribute )
{
$default = $classAttribute->attribute( "data_int3" );
if ( $default === 0 )
{
return array();
}
else
{
return array( 'data_int' => $default,
'sort_key_int' => $default );
}
}
function supportsBatchInitializeObjectAttribute()
{
return true;
}
/// \privatesection
/// The integer value validator
public $IntegerValidator;
}
eZDataType::register( eZIntegerType::DATA_TYPE_STRING, "eZIntegerType" );
?>