00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 include_once( "kernel/classes/ezdatatype.php" );
00038
00039 define( "EZ_DATATYPESTRING_ISBN", "ezisbn" );
00040
00041 class eZISBNType extends eZDataType
00042 {
00043 function eZISBNType( )
00044 {
00045 $this->eZDataType( EZ_DATATYPESTRING_ISBN, ezi18n( 'kernel/classes/datatypes', "ISBN", 'Datatype name' ),
00046 array( 'serialize_supported' => true,
00047 'object_serialize_map' => array( 'data_text' => 'isbn' ) ) );
00048 }
00049
00050
00051
00052
00053
00054 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00055 {
00056 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00057 $classContent = $classAttribute->content();
00058 if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
00059 {
00060 $number13 = $http->hasPostVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00061 ? $http->postVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00062 : false;
00063 if ( !$contentObjectAttribute->validateIsRequired() and ( !$number13 or $number13 == '' ) )
00064 {
00065 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00066 }
00067 $number13 = str_replace( "-", "", $number13 );
00068 $number13 = str_replace( " ", "", $number13 );
00069 $error = '';
00070 $valid = $this->validateISBN13Checksum ( $number13, $error );
00071
00072 if ( $valid )
00073 {
00074 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00075 }
00076 else
00077 {
00078 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00079 'The ISBN number is not correct. ' ) . $error );
00080 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00081 }
00082
00083 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00084 }
00085
00086 $field1 = $http->postVariable( $base . "_isbn_field1_" . $contentObjectAttribute->attribute( "id" ) );
00087 $field2 = $http->postVariable( $base . "_isbn_field2_" . $contentObjectAttribute->attribute( "id" ) );
00088 $field3 = $http->postVariable( $base . "_isbn_field3_" . $contentObjectAttribute->attribute( "id" ) );
00089 $field4 = $http->postVariable( $base . "_isbn_field4_" . $contentObjectAttribute->attribute( "id" ) );
00090 $isbn = $field1 . '-' . $field2 . '-' . $field3 . '-' . $field4;
00091
00092 $isbn = strtoupper( $isbn );
00093
00094 if ( !$contentObjectAttribute->validateIsRequired() and $isbn == "---" )
00095 {
00096 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00097 }
00098
00099 if ( preg_match( "#^[0-9]{1,2}\-[0-9]+\-[0-9]+\-[0-9X]{1}$#", $isbn ) )
00100 {
00101 $digits = str_replace( "-", "", $isbn );
00102 $valid = $this->validateISBNChecksum ( $digits );
00103 if ( $valid )
00104 {
00105 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00106 }
00107 else
00108 {
00109 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00110 'The ISBN number is not correct. Please check the input for mistakes.' ) );
00111 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00112 }
00113 }
00114 else
00115 {
00116 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00117 'The ISBN number is not correct. Please check the input for mistakes.' ) );
00118 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00119 }
00120 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 function validateISBNChecksum ( $isbnNr )
00130 {
00131 $result = 0;
00132 for ( $i = 10; $i > 0; $i-- )
00133 {
00134 if ( ( $i == 1 ) and ( $isbnNr{9} == 'X' ) )
00135 {
00136 $result += 10 * $i;
00137 }
00138 else
00139 {
00140 $result += $isbnNr{10-$i} * $i;
00141 }
00142 }
00143 return ( $result % 11 == 0 );
00144 }
00145
00146
00147
00148
00149
00150
00151
00152 function validateISBN13Checksum ( $isbnNr, &$error )
00153 {
00154 if ( !$isbnNr )
00155 return false;
00156
00157 if ( substr( $isbnNr, 0, 3 ) != '978' && substr( $isbnNr, 0, 3 ) != '979' )
00158 {
00159 $error = ezi18n( 'kernel/classes/datatypes',
00160 '13 digit ISBN must start with 978 or 979' );
00161 return false;
00162 }
00163 $isbnNr = strtoupper( $isbnNr );
00164 $checksum13 = 0;
00165 $weight13 = 1;
00166 if ( strlen( $isbnNr ) != 13 )
00167 {
00168 $error = ezi18n( 'kernel/classes/datatypes', 'ISBN length is invalid' );
00169 return false;
00170 }
00171
00172
00173 $val = 0;
00174 for ( $i = 0; $i < 13; $i++ )
00175 {
00176 $val = $isbnNr{$i};
00177 if ( $isbnNr{$i} == 'X' )
00178 {
00179 $error = ezi18n( 'kernel/classes/datatypes', 'X not valid in ISBN 13' );
00180 return false;
00181 }
00182 $checksum13 = $checksum13 + $weight13 * $val;
00183 $weight13 = ( $weight13 + 2 ) % 4;
00184 }
00185 if ( ( $checksum13 % 10 ) != 0 )
00186 {
00187
00188 $error = ezi18n( 'kernel/classes/datatypes', 'Bad checksum' );
00189 return false;
00190 }
00191
00192 return true;
00193 }
00194
00195
00196
00197
00198 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00199 {
00200 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00201 $classContent = $classAttribute->content();
00202 if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
00203 {
00204 $number13 = $http->hasPostVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00205 ? $http->postVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00206 : false;
00207 if ( !$number13 )
00208 return true;
00209
00210 $isbn = strtoupper( $number13 );
00211 $isbn = preg_replace( "# +#", " ", $isbn );
00212 $isbn = preg_replace( "#-+#", "-", $isbn );
00213 $contentObjectAttribute->setAttribute( "data_text", $isbn );
00214 return true;
00215 }
00216
00217 $field1 = $http->postVariable( $base . "_isbn_field1_" . $contentObjectAttribute->attribute( "id" ) );
00218 $field2 = $http->postVariable( $base . "_isbn_field2_" . $contentObjectAttribute->attribute( "id" ) );
00219 $field3 = $http->postVariable( $base . "_isbn_field3_" . $contentObjectAttribute->attribute( "id" ) );
00220 $field4 = $http->postVariable( $base . "_isbn_field4_" . $contentObjectAttribute->attribute( "id" ) );
00221
00222 if ( !$field1 and !$field2 and !$field3 and !$field4 )
00223 return true;
00224
00225 $isbn = $field1 . '-' . $field2 . '-' . $field3 . '-' . $field4;
00226 $isbn = strtoupper( $isbn );
00227 $contentObjectAttribute->setAttribute( "data_text", $isbn );
00228 return true;
00229 }
00230
00231
00232
00233
00234 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00235 {
00236 $classAttributeID = $classAttribute->attribute( 'id' );
00237 $content = $classAttribute->content();
00238
00239 if ( $http->hasPostVariable( $base . '_ezisbn_13_value_' . $classAttributeID . '_exists' ) )
00240 {
00241 $content['ISBN13'] = $http->hasPostVariable( $base . '_ezisbn_13_value_' . $classAttributeID ) ? 1 : 0;
00242 }
00243 $classAttribute->setContent( $content );
00244 $classAttribute->store();
00245 return true;
00246 }
00247
00248
00249
00250
00251 function storeObjectAttribute( &$attribute )
00252 {
00253 }
00254
00255
00256
00257
00258 function preStoreClassAttribute( &$classAttribute, $version )
00259 {
00260 $content = $classAttribute->content();
00261 return eZISBNType::storeClassAttributeContent( $classAttribute, $content );
00262 }
00263
00264 function storeClassAttributeContent( &$classAttribute, $content )
00265 {
00266 if ( is_array( $content ) )
00267 {
00268 $ISBN_13 = $content['ISBN13'];
00269 $classAttribute->setAttribute( 'data_int1', $ISBN_13 );
00270 }
00271 return false;
00272 }
00273
00274
00275
00276
00277 function &objectAttributeContent( &$contentObjectAttribute )
00278 {
00279 $data = $contentObjectAttribute->attribute( "data_text" );
00280 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00281 $classContent = $classAttribute->content();
00282 if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
00283 {
00284 return $data;
00285 }
00286
00287
00288 list ( $field1, $field2, $field3, $field4 ) = array_merge( preg_split( '#-#', $data ),
00289 array( 0 => '', 1 => '', 2 => '', 3 => '' ) );
00290 $isbn = array( "field1" => $field1, "field2" => $field2,
00291 "field3" => $field3, "field4" => $field4 );
00292 return $isbn;
00293 }
00294
00295
00296
00297
00298 function &classAttributeContent( &$classAttribute )
00299 {
00300 $ISBN_13 = $classAttribute->attribute( 'data_int1' );
00301 $content = array( 'ISBN13' => $ISBN_13 );
00302 return $content;
00303 }
00304
00305
00306
00307
00308
00309
00310 function isIndexable()
00311 {
00312 return true;
00313 }
00314
00315
00316
00317
00318 function metaData( $contentObjectAttribute )
00319 {
00320 return $contentObjectAttribute->attribute( "data_text" );
00321 }
00322
00323
00324
00325
00326
00327 function toString( $contentObjectAttribute )
00328 {
00329 return $contentObjectAttribute->attribute( 'data_text' );
00330 }
00331
00332 function fromString( &$contentObjectAttribute, $string )
00333 {
00334 return $contentObjectAttribute->setAttribute( 'data_text', $string );
00335 }
00336
00337
00338
00339
00340 function title( &$data_instance )
00341 {
00342 return $data_instance->attribute( "data_text" );
00343 }
00344
00345 function hasObjectAttributeContent( &$contentObjectAttribute )
00346 {
00347 return trim( $contentObjectAttribute->attribute( "data_text" ) ) != '';
00348 }
00349 }
00350
00351 eZDataType::register( EZ_DATATYPESTRING_ISBN, "ezisbntype" );
00352
00353 ?>