/
ezidentifiertype.php
425 lines (355 loc) · 16.8 KB
/
ezidentifiertype.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
<?php
/**
* File containing the eZIdentifierType class.
*
* @copyright Copyright (C) 1999-2013 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 eZIdentifierType ezidentifiertype.php
\ingroup eZDatatype
\brief The class eZIdentifierType does
*/
class eZIdentifierType extends eZDataType
{
const PRETEXT_FIELD = "data_text1";
const PRETEXT_VARIABLE = "_ezidentifier_pretext_value_";
const POSTTEXT_FIELD = "data_text2";
const POSTTEXT_VARIABLE = "_ezidentifier_posttext_value_";
const START_VALUE_FIELD = "data_int1";
const START_VALUE_VARIABLE = "_ezidentifier_start_integer_value_";
const DIGITS_FIELD = "data_int2";
const DIGITS_VARIABLE = "_ezidentifier_digits_integer_value_";
const IDENTIFIER_FIELD = "data_int3";
const IDENTIFIER_VARIABLE = "_ezidentifier_identifier_value_";
const DATA_TYPE_STRING = "ezidentifier";
/*!
Constructor
*/
function eZIdentifierType()
{
$this->eZDataType( self::DATA_TYPE_STRING,
ezpI18n::tr( 'kernel/classes/datatypes', "Identifier", 'Datatype name' ),
array( 'serialize_supported' => true,
'object_serialize_map' => array( 'data_text' => 'identifier',
'data_int' => 'number' ) ) );
$this->IntegerValidator = new eZIntegerValidator( 1 );
}
/*!
Validates the input and returns true if the input was
valid for this datatype.
*/
function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
}
function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
}
/*!
Store the content. Since the content has been stored in function fetchObjectAttributeHTTPInput(),
this function is with empty code.
*/
function storeObjectAttribute( $contentObjectattribute )
{
}
/*!
Returns the content.
*/
function objectAttributeContent( $contentObjectAttribute )
{
$content = $contentObjectAttribute->attribute( "data_text" );
if ( trim( $content ) == '' )
{
$contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
$content = eZIdentifierType::generateIdentifierString( $contentClassAttribute, false );
}
return $content;
}
function toString( $contentObjectAttribute )
{
return $contentObjectAttribute->attribute( 'data_text' );
}
function fromString( $contentObjectAttribute, $string )
{
if ( $string == '' )
return true;
$contentObjectAttribute->setAttribute( 'data_text', $string );
return true;
}
function hasObjectAttributeContent( $contentObjectAttribute )
{
$content = $contentObjectAttribute->attribute( "data_text" );
return ( trim( $content ) != '' );
}
function initializeClassAttribute( $classAttribute )
{
if ( $classAttribute->attribute( self::START_VALUE_FIELD ) == null
&& $classAttribute->attribute( self::DIGITS_FIELD ) == null
&& $classAttribute->attribute( self::IDENTIFIER_FIELD ) == null )
{
$classAttribute->setAttribute( self::START_VALUE_FIELD, 1 );
$classAttribute->setAttribute( self::IDENTIFIER_FIELD, 1 );
$classAttribute->setAttribute( self::DIGITS_FIELD, 1 );
}
}
/*!
Validates the input and returns true if the input was
valid for this datatype.
*/
function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
{
$startValueName = $base . self::START_VALUE_VARIABLE . $classAttribute->attribute( "id" );
$digitsName = $base . self::DIGITS_VARIABLE . $classAttribute->attribute( "id" );
if ( $http->hasPostVariable( $startValueName ) and
$http->hasPostVariable( $digitsName ) )
{
$startValueValue = str_replace( " ", "", $http->postVariable( $startValueName ) );
$digitsValue = str_replace( " ", "", $http->postVariable( $digitsName ) );
$startValueValueState = $this->IntegerValidator->validate( $startValueValue );
$digitsValueState = $this->IntegerValidator->validate( $digitsValue );
if ( ( $startValueValueState == eZInputValidator::STATE_ACCEPTED ) and
( $digitsValueState == eZInputValidator::STATE_ACCEPTED ) )
{
return eZInputValidator::STATE_ACCEPTED;
}
return eZInputValidator::STATE_INTERMEDIATE;
}
return eZInputValidator::STATE_INVALID;
}
function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
{
$startValueName = $base . self::START_VALUE_VARIABLE . $classAttribute->attribute( "id" );
$digitsName = $base . self::DIGITS_VARIABLE . $classAttribute->attribute( "id" );
$preTextName = $base . self::PRETEXT_VARIABLE . $classAttribute->attribute( "id" );
$postTextName = $base . self::POSTTEXT_VARIABLE . $classAttribute->attribute( "id" );
if ( $http->hasPostVariable( $startValueName ) and
$http->hasPostVariable( $digitsName ) and
$http->hasPostVariable( $preTextName ) and
$http->hasPostVariable( $postTextName ) )
{
$startValueValue = str_replace( " ", "", $http->postVariable( $startValueName ) );
$startValueValue = ( int ) $startValueValue;
if ( $startValueValue < 1 )
{
$startValueValue = 1;
}
$digitsValue = str_replace( " ", "", $http->postVariable( $digitsName ) );
$digitsValue = ( int ) $digitsValue;
if ( $digitsValue < 1 )
{
$digitsValue = 1;
}
$preTextValue = $http->postVariable( $preTextName );
$postTextValue = $http->postVariable( $postTextName );
$classAttribute->setAttribute( self::DIGITS_FIELD, $digitsValue );
$classAttribute->setAttribute( self::PRETEXT_FIELD, $preTextValue );
$classAttribute->setAttribute( self::POSTTEXT_FIELD, $postTextValue );
$classAttribute->setAttribute( self::START_VALUE_FIELD, $startValueValue );
$classAttribute->setAttribute( self::IDENTIFIER_FIELD,
$classAttribute->attribute( self::START_VALUE_FIELD ) );
$originalClassAttribute = eZContentClassAttribute::fetch( $classAttribute->attribute( 'id' ), true, 0 );
if ( $originalClassAttribute )
{
if ( $originalClassAttribute->attribute( self::DIGITS_FIELD ) == $digitsValue
&& $originalClassAttribute->attribute( self::PRETEXT_FIELD ) == $preTextValue
&& $originalClassAttribute->attribute( self::POSTTEXT_FIELD ) == $postTextValue
&& $originalClassAttribute->attribute( self::IDENTIFIER_FIELD ) >= $startValueValue )
{
$classAttribute->setAttribute( self::START_VALUE_FIELD, $originalClassAttribute->attribute( self::START_VALUE_FIELD ) );
$classAttribute->setAttribute( self::IDENTIFIER_FIELD, $originalClassAttribute->attribute( self::IDENTIFIER_FIELD ) );
}
}
}
return true;
}
/*!
Returns the meta data used for storing search indices.
*/
function metaData( $contentObjectAttribute )
{
return $contentObjectAttribute->attribute( "data_text" );
}
/*!
Returns the text.
*/
function title( $contentObjectAttribute, $name = null )
{
return $contentObjectAttribute->attribute( "data_text" );
}
function isIndexable()
{
return true;
}
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
{
$contentObjectAttributeID = $originalContentObjectAttribute->attribute( "id" );
$version = $contentObjectAttribute->attribute( "version" );
if ( $currentVersion == false )
{
// If this is not a copy we need to see if a unique ID must be
// assigned. This is handled in assignValue().
$contentClassAttribute = $contentObjectAttribute->attribute( 'contentclass_attribute' );
$ret = eZIdentifierType::assignValue( $contentClassAttribute, $contentObjectAttribute );
}
}
/*!
When published it will check if it needs to aquire a new unique identifier, if so
it updates all existing versions with this new identifier.
*/
function onPublish( $contentObjectAttribute, $contentObject, $publishedNodes )
{
$contentClassAttribute = $contentObjectAttribute->attribute( 'contentclass_attribute' );
$ret = eZIdentifierType::assignValue( $contentClassAttribute, $contentObjectAttribute );
return $ret;
}
/*!
\private
Assigns the identifiervalue for the first version of the current attribute.
*/
function assignValue( $contentClassAttribute, $contentObjectAttribute )
{
$retValue = false;
$ret = array();
$version = $contentObjectAttribute->attribute( 'version' );
$contentClassAttributeID = $contentClassAttribute->attribute( 'id' );
$objectID = (int)$contentObjectAttribute->attribute( 'contentobject_id' );
$classAttributeID = (int)$contentObjectAttribute->attribute( 'contentclassattribute_id' );
$db = eZDB::instance();
$existingIDs = $db->arrayQuery( "SELECT data_int " .
"FROM ezcontentobject_attribute " .
"WHERE contentobject_id = $objectID AND" .
" contentclassattribute_id = $classAttributeID AND" .
" data_type_string = 'ezidentifier' AND" .
" data_int != 0" );
if ( count( $existingIDs ) > 0 )
{
$identifierValue = $existingIDs[0]['data_int'];
$ret[] = eZIdentifierType::storeIdentifierValue( $contentClassAttribute, $contentObjectAttribute, $identifierValue );
}
else
{
$db->begin();
// Ensure that we don't get another identifier with the same id, so lock ezcontentclass_attribute
$db->lock( array( array( 'table' => 'ezcontentclass_attribute' ) ) );
$selectQuery = "SELECT data_int3 FROM ezcontentclass_attribute WHERE " .
"id=$contentClassAttributeID AND version=0";
$result = $db->arrayQuery( $selectQuery );
$identifierValue = $result[0]['data_int3'];
// should only increment when we don't have the first version
$updateQuery = "UPDATE ezcontentclass_attribute SET data_int3=data_int3 + 1 WHERE " .
"id=$contentClassAttributeID AND version=0";
$ret[] = $db->query( $updateQuery );
$db->unlock();
// unlock before we start to update the ezcontentobject_attribute table
$ret[] = eZIdentifierType::storeIdentifierValue( $contentClassAttribute, $contentObjectAttribute, $identifierValue );
if ( !in_array( false, $ret ) )
{
// Now make sure all existing versions (if any) gets the same identifier
$dataText = $db->escapeString( $contentObjectAttribute->attribute( 'data_text' ) );
$dataInt = (int)$contentObjectAttribute->attribute( 'data_int' );
$trans = eZCharTransform::instance();
$sortText = $db->escapeString( $trans->transformByGroup( $contentObjectAttribute->attribute( 'data_text' ),
'lowercase' ) );
$db->query( "UPDATE ezcontentobject_attribute " .
"SET data_text = '$dataText', data_int = $dataInt, sort_key_string = '$sortText' " .
"WHERE contentobject_id = $objectID AND" .
" contentclassattribute_id = $classAttributeID AND" .
" data_type_string = 'ezidentifier'" );
}
if ( !in_array( false, $ret ) )
$db->commit();
else
$db->rollback();
}
if ( !in_array( false, $ret ) )
$retValue = true;
return $retValue;
}
function sortKey( $contentObjectAttribute )
{
$trans = eZCharTransform::instance();
return $trans->transformByGroup( $contentObjectAttribute->attribute( 'data_text' ), 'lowercase' );
}
function sortKeyType()
{
return 'string';
}
/*!
\private
Store the new value to the attribute.
*/
function storeIdentifierValue( $contentClassAttribute, $contentObjectAttribute, $identifierValue )
{
$value = eZIdentifierType::generateIdentifierString( $contentClassAttribute, $identifierValue );
$contentObjectAttribute->setAttribute( 'data_text', $value );
$contentObjectAttribute->setAttribute( 'data_int', $identifierValue );
return true;
}
function generateIdentifierString( $contentClassAttribute, $identifierValue = false )
{
$preText = $contentClassAttribute->attribute( self::PRETEXT_FIELD );
$postText = $contentClassAttribute->attribute( self::POSTTEXT_FIELD );
$digits = $contentClassAttribute->attribute( self::DIGITS_FIELD );
if ( $identifierValue !== false )
$midText = str_pad( $identifierValue, $digits, '0', STR_PAD_LEFT );
else
$midText = str_repeat( 'x', $digits );
$value = $preText . $midText . $postText;
return $value;
}
function customClassAttributeHTTPAction( $http, $action, $contentClassAttribute )
{
}
function preStoreClassAttribute( $classAttribute, $version )
{
}
function preStoreDefinedClassAttribute( $classAttribute )
{
}
function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
{
$digits = $classAttribute->attribute( self::DIGITS_FIELD );
$preText = $classAttribute->attribute( self::PRETEXT_FIELD );
$postText = $classAttribute->attribute( self::POSTTEXT_FIELD );
$startValue = $classAttribute->attribute( self::START_VALUE_FIELD );
$identifier = $classAttribute->attribute( self::IDENTIFIER_FIELD );
$dom = $attributeParametersNode->ownerDocument;
$digitsNode = $dom->createElement( 'digits' );
$digitsNode->appendChild( $dom->createTextNode( $digits ) );
$attributeParametersNode->appendChild( $digitsNode );
$preTextNode = $dom->createElement( 'pre-text' );
$preTextNode->appendChild( $dom->createTextNode( $preText ) );
$attributeParametersNode->appendChild( $preTextNode );
$postTextNode = $dom->createElement( 'post-text' );
$postTextNode->appendChild( $dom->createTextNode( $postText ) );
$attributeParametersNode->appendChild( $postTextNode );
$startValueNode = $dom->createElement( 'start-value' );
$startValueNode->appendChild( $dom->createTextNode( $startValue ) );
$attributeParametersNode->appendChild( $startValueNode );
$identifierNode = $dom->createElement( 'identifier' );
$identifierNode->appendChild( $dom->createTextNode( $identifier ) );
$attributeParametersNode->appendChild( $identifierNode );
}
function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
{
$digits = $attributeParametersNode->getElementsByTagName( 'digits' )->item( 0 )->textContent;
$preText = $attributeParametersNode->getElementsByTagName( 'pre-text' )->item( 0 )->textContent;
$postText = $attributeParametersNode->getElementsByTagName( 'post-text' )->item( 0 )->textContent;
$startValue = $attributeParametersNode->getElementsByTagName( 'start-value' )->item( 0 )->textContent;
$identifier = $attributeParametersNode->getElementsByTagName( 'identifier' )->item( 0 )->textContent;
if ( $digits !== false )
$classAttribute->setAttribute( self::DIGITS_FIELD, $digits );
if ( $preText !== false )
$classAttribute->setAttribute( self::PRETEXT_FIELD, $preText );
if ( $postText !== false )
$classAttribute->setAttribute( self::POSTTEXT_FIELD, $postText );
if ( $startValue !== false )
$classAttribute->setAttribute( self::START_VALUE_FIELD, $startValue );
if ( $identifier !== false )
$classAttribute->setAttribute( self::IDENTIFIER_FIELD, $identifier );
}
public $IntegerValidator;
}
?>