Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezisbntype.php
Go to the documentation of this file.
1 <?php
18 class eZISBNType extends eZDataType
19 {
20 const DATA_TYPE_STRING = "ezisbn";
21 const CLASS_IS_ISBN13 = 'data_int1';
22 const CONTENT_VALUE = 'data_text';
23
24 public function __construct()
25 {
26 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "ISBN", 'Datatype name' ),
27 array( 'serialize_supported' => true,
28 'object_serialize_map' => array( self::CONTENT_VALUE => 'isbn' ) ) );
29 }
30
35 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
36 {
37 $classAttribute = $contentObjectAttribute->contentClassAttribute();
38 $classContent = $classAttribute->content();
39 if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
40 {
41 $number13 = $http->hasPostVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
42 ? $http->postVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
43 : false;
44
45 if ( $contentObjectAttribute->validateIsRequired() and ( !$number13 or $number13 == '' ) )
46 {
47 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
48 'Input required.' ) );
49
50 return eZInputValidator::STATE_INVALID;
51 }
52 else if ( !$contentObjectAttribute->validateIsRequired() and ( !$number13 or $number13 == '' ) )
53 {
54 return eZInputValidator::STATE_ACCEPTED;
55 }
56
57 // Should also accept ISBN-10 values, which should be automatically converted to ISBN-13 later.
58 $isbn10TestNumber = preg_replace( "/[\s|\-]/", "", trim( $number13 ) );
59 if ( strlen( $isbn10TestNumber ) == 10 )
60 {
61 $status = $this->validateISBNChecksum( $isbn10TestNumber );
62 if ( $status === true )
63 {
64 return eZInputValidator::STATE_ACCEPTED;
65 }
66 else
67 {
68 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
69 'The ISBN number should be ISBN13, but seems to be ISBN10.' ) );
70 return eZInputValidator::STATE_INVALID;
71 }
72 }
73
74 $isbn13 = new eZISBN13();
75 $valid = $isbn13->validate( $number13, $error );
76
77 if ( $valid )
78 {
79 return eZInputValidator::STATE_ACCEPTED;
80 }
81 else
82 {
83 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
84 'The ISBN number is not correct. ' ) . $error );
85 return eZInputValidator::STATE_INVALID;
86 }
87
88 return eZInputValidator::STATE_INVALID;
89 }
90
91 $field1 = $http->postVariable( $base . "_isbn_field1_" . $contentObjectAttribute->attribute( "id" ) );
92 $field2 = $http->postVariable( $base . "_isbn_field2_" . $contentObjectAttribute->attribute( "id" ) );
93 $field3 = $http->postVariable( $base . "_isbn_field3_" . $contentObjectAttribute->attribute( "id" ) );
94 $field4 = $http->postVariable( $base . "_isbn_field4_" . $contentObjectAttribute->attribute( "id" ) );
95 $isbn = $field1 . '-' . $field2 . '-' . $field3 . '-' . $field4;
96
97 $isbn = strtoupper( $isbn );
98 if ( !$contentObjectAttribute->validateIsRequired() and $isbn == "---" )
99 {
100 return eZInputValidator::STATE_ACCEPTED;
101 }
102
103 if ( preg_match( "#^[0-9]{1,2}\-[0-9]+\-[0-9]+\-[0-9X]{1}$#", $isbn ) )
104 {
105 $digits = str_replace( "-", "", $isbn );
106 if ( strlen( $digits ) == 10 )
107 {
108 $valid = $this->validateISBNChecksum ( $digits );
109 }
110 else
111 {
112 $valid = false;
113 }
114
115 if ( $valid )
116 {
117 return eZInputValidator::STATE_ACCEPTED;
118 }
119 else
120 {
121 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
122 'The ISBN number is not correct. Please check the input for mistakes.' ) );
123 return eZInputValidator::STATE_INVALID;
124 }
125 }
126 else
127 {
128 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
129 'The ISBN number is not correct. Please check the input for mistakes.' ) );
130 return eZInputValidator::STATE_INVALID;
131 }
132 return eZInputValidator::STATE_INVALID;
133 }
134
135
144 function validateISBNChecksum ( $isbnNr )
145 {
146 $result = 0;
147 $isbnNr = strtoupper( $isbnNr );
148 for ( $i = 10; $i > 0; $i-- )
149 {
150 if ( is_numeric( $isbnNr[$i-1] ) or ( $i == 10 and $isbnNr[$i-1] == 'X' ) )
151 {
152 if ( ( $i == 1 ) and ( $isbnNr[9] == 'X' ) )
153 {
154 $result += 10 * $i;
155 }
156 else
157 {
158 $result += $isbnNr[10-$i] * $i;
159 }
160 }
161 else
162 {
163 return false;
164 }
165 }
166 return ( $result % 11 == 0 );
167 }
168
176 function validateISBN13Checksum ( $isbnNr, &$error )
177 {
178 $isbn13 = new eZISBN13();
179 $status = $isbn13->validateISBN13Checksum( $isbnNr, $error );
180
181 return $status;
182 }
183
189 static function convertISBN10toISBN13( $isbnNr )
190 {
191 $isbnNr = 978 . substr( $isbnNr, 0, 9 );
192
193 $weight13 = 1;
194 $checksum13 = 0;
195 $val = 0;
196
197 for ( $i = 0; $i < 12; $i++ )
198 {
199 $val = $isbnNr[$i];
200 $checksum13 = $checksum13 + $weight13 * $val;
201 $weight13 = ( $weight13 + 2 ) % 4;
202 }
203
204 $checkDigit = ( 10 - ( $checksum13 % 10 ) ) % 10;
205 $isbnNr .= $checkDigit;
206
207 return $isbnNr;
208 }
209
213 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
214 {
215 $classAttribute = $contentObjectAttribute->contentClassAttribute();
216 $classContent = $classAttribute->content();
217 if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
218 {
219 $number13 = $http->hasPostVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
220 ? $http->postVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
221 : false;
222 if ( $number13 === false )
223 return true;
224
225 if ( !$contentObjectAttribute->validateIsRequired() and ( !$number13 or $number13 == '' ) )
226 {
227 $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, '' );
228 return true;
229 }
230
231 // Test if we have an ISBN-10 number. This should be automatically converted to ISBN-13 if found.
232 $isbn10TestNumber = preg_replace( "/[\s|\-]/", "", trim( $number13 ) );
233 if ( strlen( $isbn10TestNumber ) == 10 )
234 {
235 if ( $contentObjectAttribute->IsValid == eZInputValidator::STATE_ACCEPTED )
236 {
237 // Convert the ISBN-10 number to ISBN-13.
238 $number13 = $this->convertISBN10toISBN13( $isbn10TestNumber );
239 }
240 else
241 {
242 // Add the value so the added value will be shown back to the user with an error message.
243 $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $number13 );
244 return true;
245 }
246 }
247
248 // Extract the different parts and set the hyphens correctly.
249 $isbn13 = new eZISBN13();
250 $isbn13Value = $isbn13->formatedISBNValue( $number13, $error );
251 $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $isbn13Value );
252 return true;
253 }
254
255 $field1 = $http->postVariable( $base . "_isbn_field1_" . $contentObjectAttribute->attribute( "id" ) );
256 $field2 = $http->postVariable( $base . "_isbn_field2_" . $contentObjectAttribute->attribute( "id" ) );
257 $field3 = $http->postVariable( $base . "_isbn_field3_" . $contentObjectAttribute->attribute( "id" ) );
258 $field4 = $http->postVariable( $base . "_isbn_field4_" . $contentObjectAttribute->attribute( "id" ) );
259 // If $fields are empty if should not store empty content to db.
260 if ( !$field1 and !$field2 and !$field3 and !$field4 )
261 return true;
262
263 $isbn = $field1 . '-' . $field2 . '-' . $field3 . '-' . $field4;
264 $isbn = strtoupper( $isbn );
265 $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $isbn );
266 return true;
267 }
268
269 function customClassAttributeHTTPAction( $http, $action, $classAttribute )
270 {
271 switch ( $action )
272 {
273 case 'ImportISBN13Data':
274 {
275 $this->importDBDataFromDBAFile();
276 }break;
277 }
278 }
279
280 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
281 {
282 $classAttributeID = $classAttribute->attribute( 'id' );
283 $content = $classAttribute->content();
284
285 if ( $http->hasPostVariable( $base . '_ezisbn_13_value_' . $classAttributeID . '_exists' ) )
286 {
287 $content['ISBN13'] = $http->hasPostVariable( $base . '_ezisbn_13_value_' . $classAttributeID ) ? 1 : 0;
288 }
289
290 $classAttribute->setContent( $content );
291 $classAttribute->store();
292 return true;
293 }
294
298 function storeObjectAttribute( $attribute )
299 {
300 }
301
302 function preStoreClassAttribute( $classAttribute, $version )
303 {
304 return eZISBNType::storeClassAttributeContent( $classAttribute, $classAttribute->content() );
305 }
306
307 function storeClassAttributeContent( $classAttribute, $content )
308 {
309 if ( is_array( $content ) )
310 {
311 $ISBN_13 = $content['ISBN13'];
312 $classAttribute->setAttribute( self::CLASS_IS_ISBN13, $ISBN_13 );
313 }
314 return false;
315 }
316
320 function objectAttributeContent( $contentObjectAttribute )
321 {
322 $data = $contentObjectAttribute->attribute( self::CONTENT_VALUE );
323 $classAttribute = $contentObjectAttribute->contentClassAttribute();
324 $classContent = $classAttribute->content();
325 if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
326 {
327 list ( $prefix, $field1, $field2, $field3, $field4 ) = array_merge( preg_split( '#-#', $data ),
328 array( 0 => '', 1 => '', 2 => '', 3 => '', 4 => '' ) );
329 $dataArray = array( 'prefix' => $prefix,
330 'field1' => $field1, 'field2' => $field2,
331 'field3' => $field3, 'field4' => $field4,
332 'value' => $data,
333 'value_without_hyphens' => str_replace( "-", "", $data ),
334 'value_with_spaces' => str_replace( "-", " ", $data ) );
335 return $dataArray;
336 }
337
338 // The array_merge makes sure missing elements gets an empty string instead of NULL
339 list ( $field1, $field2, $field3, $field4 ) = array_merge( preg_split( '#-#', $data ),
340 array( 0 => '', 1 => '', 2 => '', 3 => '' ) );
341 $isbn = array( 'field1' => $field1, 'field2' => $field2,
342 'field3' => $field3, 'field4' => $field4,
343 'value' => $data,
344 'value_without_hyphens' => str_replace( "-", "", $data ),
345 'value_with_spaces' => str_replace( "-", " ", $data ) );
346 return $isbn;
347 }
348
349 function classAttributeContent( $classAttribute )
350 {
351 $ISBN_13 = $classAttribute->attribute( self::CLASS_IS_ISBN13 );
352 $isbn13Info = new eZISBN13();
353 $content = array( 'ISBN13' => $ISBN_13,
354 'ranges' => $isbn13Info );
355 return $content;
356 }
357
358
362 function isIndexable()
363 {
364 return true;
365 }
366
370 function metaData( $contentObjectAttribute )
371 {
372 return $contentObjectAttribute->attribute( self::CONTENT_VALUE );
373 }
374
379 function toString( $contentObjectAttribute )
380 {
381 return $contentObjectAttribute->attribute( self::CONTENT_VALUE );
382 }
383
384 function fromString( $contentObjectAttribute, $string )
385 {
386 return $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $string );
387 }
388
392 function title( $data_instance, $name = null )
393 {
394 return $data_instance->attribute( self::CONTENT_VALUE );
395 }
396
401 function initializeClassAttribute( $classAttribute )
402 {
403 if ( $classAttribute->attribute( self::CLASS_IS_ISBN13 ) === null )
404 {
405 $classAttribute->setAttribute( self::CLASS_IS_ISBN13, 1 );
406 }
407 }
408
412 function hasObjectAttributeContent( $contentObjectAttribute )
413 {
414 return trim( $contentObjectAttribute->attribute( self::CONTENT_VALUE ) ) != '';
415 }
416
420 function cleanDBDataBeforeImport()
421 {
422 eZISBNGroup::cleanAll();
423 eZISBNGroupRange::cleanAll();
424 eZISBNRegistrantRange::cleanAll();
425 return true;
426 }
427
428 function supportsBatchInitializeObjectAttribute()
429 {
430 return true;
431 }
432 }
433
434 eZDataType::register( eZISBNType::DATA_TYPE_STRING, "eZISBNType" );
435
436 ?>
eZDataType
Base class for content datatypes.
Definition: ezdatatype.php:46
eZDataType\importDBDataFromDBAFile
importDBDataFromDBAFile( $dbaFilePath=false)
Definition: ezdatatype.php:1450
eZDataType\register
static register( $dataTypeString, $className)
Definition: ezdatatype.php:203
eZISBN13
The class eZISBN13 handles ISBN-13 numbers.
Definition: ezisbn13.php:21
eZISBNGroupRange\cleanAll
static cleanAll()
Definition: ezisbngrouprange.php:141
eZISBNGroup\cleanAll
static cleanAll()
Definition: ezisbngroup.php:114
eZISBNRegistrantRange\cleanAll
static cleanAll()
Definition: ezisbnregistrantrange.php:187
eZISBNType
Handles ISBN type strings.
Definition: ezisbntype.php:19
eZISBNType\isIndexable
isIndexable()
Definition: ezisbntype.php:362
eZISBNType\validateISBN13Checksum
validateISBN13Checksum( $isbnNr, &$error)
Definition: ezisbntype.php:176
eZISBNType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezisbntype.php:35
eZISBNType\initializeClassAttribute
initializeClassAttribute( $classAttribute)
Definition: ezisbntype.php:401
eZISBNType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezisbntype.php:20
eZISBNType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezisbntype.php:280
eZISBNType\customClassAttributeHTTPAction
customClassAttributeHTTPAction( $http, $action, $classAttribute)
Definition: ezisbntype.php:269
eZISBNType\metaData
metaData( $contentObjectAttribute)
Definition: ezisbntype.php:370
eZISBNType\storeClassAttributeContent
storeClassAttributeContent( $classAttribute, $content)
Definition: ezisbntype.php:307
eZISBNType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezisbntype.php:412
eZISBNType\CONTENT_VALUE
const CONTENT_VALUE
Definition: ezisbntype.php:22
eZISBNType\validateISBNChecksum
validateISBNChecksum( $isbnNr)
Definition: ezisbntype.php:144
eZISBNType\toString
toString( $contentObjectAttribute)
Definition: ezisbntype.php:379
eZISBNType\title
title( $data_instance, $name=null)
Definition: ezisbntype.php:392
eZISBNType\CLASS_IS_ISBN13
const CLASS_IS_ISBN13
Definition: ezisbntype.php:21
eZISBNType\cleanDBDataBeforeImport
cleanDBDataBeforeImport()
Definition: ezisbntype.php:420
eZISBNType\preStoreClassAttribute
preStoreClassAttribute( $classAttribute, $version)
Definition: ezisbntype.php:302
eZISBNType\__construct
__construct()
Definition: ezisbntype.php:24
eZISBNType\fromString
fromString( $contentObjectAttribute, $string)
Definition: ezisbntype.php:384
eZISBNType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: ezisbntype.php:428
eZISBNType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezisbntype.php:320
eZISBNType\convertISBN10toISBN13
static convertISBN10toISBN13( $isbnNr)
Definition: ezisbntype.php:189
eZISBNType\storeObjectAttribute
storeObjectAttribute( $attribute)
Definition: ezisbntype.php:298
eZISBNType\classAttributeContent
classAttributeContent( $classAttribute)
Definition: ezisbntype.php:349
eZISBNType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezisbntype.php:213
eZInputValidator\STATE_ACCEPTED
const STATE_ACCEPTED
Definition: ezinputvalidator.php:79
eZInputValidator\STATE_INVALID
const STATE_INVALID
Definition: ezinputvalidator.php:81
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.