eZ Publish  [4.0]
ezisbntype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZISBNType class
00004 //
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 /*!
00031   \class eZISBNType ezisbntype.php
00032   \brief Handles ISBN type strings
00033   \ingroup eZDatatype
00034 
00035 */
00036 
00037 //include_once( "kernel/classes/ezdatatype.php" );
00038 
00039 class eZISBNType extends eZDataType
00040 {
00041     const DATA_TYPE_STRING = "ezisbn";
00042     const CLASS_IS_ISBN13 = 'data_int1';
00043     const CONTENT_VALUE = 'data_text';
00044 
00045     function eZISBNType()
00046     {
00047         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "ISBN", 'Datatype name' ),
00048                            array( 'serialize_supported' => true,
00049                                   'object_serialize_map' => array( self::CONTENT_VALUE => 'isbn' ) ) );
00050     }
00051 
00052     /*!
00053      Validates the input and returns true if the input was
00054      valid for this datatype.
00055     */
00056     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00057     {
00058         $classAttribute = $contentObjectAttribute->contentClassAttribute();
00059         $classContent = $classAttribute->content();
00060         if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
00061         {
00062             //include_once( 'kernel/classes/datatypes/ezisbn/ezisbn13.php' );
00063             $number13 = $http->hasPostVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00064                         ? $http->postVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00065                         : false;
00066 
00067             if ( $contentObjectAttribute->validateIsRequired() and ( !$number13 or $number13 == '' ) )
00068             {
00069                 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00070                                                                      'Input required.' ) );
00071 
00072                 return eZInputValidator::STATE_INVALID;
00073             }
00074             else if ( !$contentObjectAttribute->validateIsRequired() and ( !$number13 or $number13 == '' ) )
00075             {
00076                 return eZInputValidator::STATE_ACCEPTED;
00077             }
00078 
00079             // Should also accept ISBN-10 values, which should be automatically converted to ISBN-13 later.
00080             $isbn10TestNumber = preg_replace( "/[\s|\-]/", "", trim( $number13 ) );
00081             if ( strlen( $isbn10TestNumber ) == 10 )
00082             {
00083                 $status = $this->validateISBNChecksum( $isbn10TestNumber );
00084                 if ( $status === true )
00085                 {
00086                     return eZInputValidator::STATE_ACCEPTED;
00087                 }
00088                 else
00089                 {
00090                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00091                                                                          'The ISBN number should be ISBN13, but seems to be ISBN10.' ) );
00092                     return eZInputValidator::STATE_INVALID;
00093                 }
00094             }
00095 
00096             $isbn13 = new eZISBN13();
00097             $valid = $isbn13->validate( $number13, $error );
00098 
00099             if ( $valid )
00100             {
00101                 return eZInputValidator::STATE_ACCEPTED;
00102             }
00103             else
00104             {
00105                 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00106                                                                      'The ISBN number is not correct. ' ) . $error );
00107                 return eZInputValidator::STATE_INVALID;
00108             }
00109 
00110             return eZInputValidator::STATE_INVALID;
00111         }
00112 
00113         $field1 = $http->postVariable( $base . "_isbn_field1_" . $contentObjectAttribute->attribute( "id" ) );
00114         $field2 = $http->postVariable( $base . "_isbn_field2_" . $contentObjectAttribute->attribute( "id" ) );
00115         $field3 = $http->postVariable( $base . "_isbn_field3_" . $contentObjectAttribute->attribute( "id" ) );
00116         $field4 = $http->postVariable( $base . "_isbn_field4_" . $contentObjectAttribute->attribute( "id" ) );
00117         $isbn = $field1 . '-' . $field2 . '-' . $field3 . '-' . $field4;
00118 
00119         $isbn = strtoupper( $isbn );
00120         if ( !$contentObjectAttribute->validateIsRequired() and $isbn == "---" )
00121         {
00122             return eZInputValidator::STATE_ACCEPTED;
00123         }
00124 
00125         if ( preg_match( "#^[0-9]{1,2}\-[0-9]+\-[0-9]+\-[0-9X]{1}$#", $isbn ) )
00126         {
00127             $digits = str_replace( "-", "", $isbn );
00128             if ( strlen( $digits ) == 10 )
00129             {
00130                 $valid = $this->validateISBNChecksum ( $digits );
00131             }
00132             else
00133             {
00134                 $valid = false;
00135             }
00136 
00137             if ( $valid )
00138             {
00139                 return eZInputValidator::STATE_ACCEPTED;
00140             }
00141             else
00142             {
00143                 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00144                                                                      'The ISBN number is not correct. Please check the input for mistakes.' ) );
00145                 return eZInputValidator::STATE_INVALID;
00146             }
00147         }
00148         else
00149         {
00150             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00151                                                                  'The ISBN number is not correct. Please check the input for mistakes.' ) );
00152             return eZInputValidator::STATE_INVALID;
00153         }
00154         return eZInputValidator::STATE_INVALID;
00155     }
00156 
00157 
00158     /*!
00159      \private
00160      Validates the ISBN number \a $isbnNr.
00161      All characters should be numeric except the last digit that may be the character X,
00162      which should be calculated as 10.
00163      \param $isbnNr A string containing the number without any dashes.
00164      \return \c true if it is valid.
00165     */
00166     function validateISBNChecksum ( $isbnNr )
00167     {
00168         $result = 0;
00169         $isbnNr = strtoupper( $isbnNr );
00170         for ( $i = 10; $i > 0; $i-- )
00171         {
00172             if ( is_numeric( $isbnNr{$i-1} ) or ( $i == 10  and $isbnNr{$i-1} == 'X' ) )
00173             {
00174                 if ( ( $i == 1 ) and ( $isbnNr{9} == 'X' ) )
00175                 {
00176                     $result += 10 * $i;
00177                 }
00178                 else
00179                 {
00180                     $result += $isbnNr{10-$i} * $i;
00181                 }
00182             }
00183             else
00184             {
00185                 return false;
00186             }
00187         }
00188         return ( $result % 11 == 0 );
00189     }
00190 
00191     /*!
00192      \private
00193      \deprecated, should use the class eZISBN13 instead.
00194      Validates the ISBN-13 number \a $isbnNr.
00195      \param $isbnNr A string containing the number without any dashes.
00196      \return \c true if it is valid.
00197     */
00198     function validateISBN13Checksum ( $isbnNr, &$error )
00199     {
00200         $isbn13 = new eZISBN13();
00201         $status = $isbn13->validateISBN13Checksum( $isbnNr, $error );
00202 
00203         return $status;
00204     }
00205 
00206     /*!
00207       Calculate the ISBN-13 checkdigit and return a valid ISBN-13 number
00208       based on an ISBN-10 number as input.
00209       \return a valid ISBN-13 number.
00210     */
00211     static function convertISBN10toISBN13( $isbnNr )
00212     {
00213         $isbnNr = 978 . substr( $isbnNr, 0, 9 );
00214 
00215         $weight13 = 1;
00216         $checksum13 = 0;
00217         $val = 0;
00218 
00219         for ( $i = 0; $i < 12; $i++ )
00220         {
00221             $val = $isbnNr{$i};
00222             $checksum13 = $checksum13 + $weight13 * $val;
00223             $weight13 = ( $weight13 + 2 ) % 4;
00224         }
00225 
00226         $checkDigit = ( 10 - ( $checksum13 % 10 ) ) % 10;
00227         $isbnNr .= $checkDigit;
00228 
00229         return $isbnNr;
00230     }
00231 
00232     /*!
00233      Fetches the http post var string input and stores it in the data instance.
00234     */
00235     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00236     {
00237         $classAttribute = $contentObjectAttribute->contentClassAttribute();
00238         $classContent = $classAttribute->content();
00239         if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
00240         {
00241             $number13 = $http->hasPostVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00242                         ? $http->postVariable( $base . "_isbn_13_" . $contentObjectAttribute->attribute( "id" ) )
00243                         : false;
00244             if ( $number13 === false )
00245                 return true;
00246 
00247             if ( !$contentObjectAttribute->validateIsRequired() and ( !$number13 or $number13 == '' ) )
00248             {
00249                 return true;
00250             }
00251 
00252             // Test if we have an ISBN-10 number. This should be automatically converted to ISBN-13 if found.
00253             $isbn10TestNumber = preg_replace( "/[\s|\-]/", "", trim( $number13 ) );
00254             if ( strlen( $isbn10TestNumber ) == 10 )
00255             {
00256                 if ( $contentObjectAttribute->IsValid == eZInputValidator::STATE_ACCEPTED )
00257                 {
00258                     // Convert the ISBN-10 number to ISBN-13.
00259                     $number13 = $this->convertISBN10toISBN13( $isbn10TestNumber );
00260                 }
00261                 else
00262                 {
00263                     // Add the value so the added value will be shown back to the user with an error message.
00264                     $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $number13 );
00265                     return true;
00266                 }
00267             }
00268 
00269             // Extract the different parts and set the hyphens correctly.
00270             $isbn13 = new eZISBN13();
00271             $isbn13Value = $isbn13->formatedISBNValue( $number13, $error );
00272             $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $isbn13Value );
00273             return true;
00274         }
00275 
00276         $field1 = $http->postVariable( $base . "_isbn_field1_" . $contentObjectAttribute->attribute( "id" ) );
00277         $field2 = $http->postVariable( $base . "_isbn_field2_" . $contentObjectAttribute->attribute( "id" ) );
00278         $field3 = $http->postVariable( $base . "_isbn_field3_" . $contentObjectAttribute->attribute( "id" ) );
00279         $field4 = $http->postVariable( $base . "_isbn_field4_" . $contentObjectAttribute->attribute( "id" ) );
00280         // If $fields are empty if should not store empty content to db.
00281         if ( !$field1 and !$field2 and !$field3 and !$field4 )
00282             return true;
00283 
00284         $isbn = $field1 . '-' . $field2 . '-' . $field3 . '-' . $field4;
00285         $isbn = strtoupper( $isbn );
00286         $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $isbn );
00287         return true;
00288     }
00289 
00290     /*!
00291      \reimp
00292     */
00293     function customClassAttributeHTTPAction( $http, $action, $classAttribute )
00294     {
00295         switch ( $action )
00296         {
00297             case 'ImportISBN13Data':
00298             {
00299                 $this->importDBDataFromDBAFile();
00300             }break;
00301         }
00302     }
00303 
00304     /*!
00305      \reimp
00306     */
00307     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00308     {
00309         $classAttributeID = $classAttribute->attribute( 'id' );
00310         $content = $classAttribute->content();
00311 
00312         if ( $http->hasPostVariable( $base . '_ezisbn_13_value_' . $classAttributeID . '_exists' ) )
00313         {
00314              $content['ISBN13'] = $http->hasPostVariable( $base . '_ezisbn_13_value_' . $classAttributeID ) ? 1 : 0;
00315         }
00316 
00317         $classAttribute->setContent( $content );
00318         $classAttribute->store();
00319         return true;
00320     }
00321 
00322     /*!
00323      Store the content.
00324     */
00325     function storeObjectAttribute( $attribute )
00326     {
00327     }
00328 
00329     /*!
00330      \reimp
00331     */
00332     function preStoreClassAttribute( $classAttribute, $version )
00333     {
00334         return eZISBNType::storeClassAttributeContent( $classAttribute, $classAttribute->content() );
00335     }
00336 
00337     function storeClassAttributeContent( $classAttribute, $content )
00338     {
00339         if ( is_array( $content ) )
00340         {
00341             $ISBN_13 = $content['ISBN13'];
00342             $classAttribute->setAttribute( self::CLASS_IS_ISBN13, $ISBN_13 );
00343         }
00344         return false;
00345     }
00346 
00347     /*!
00348      Returns the content.
00349     */
00350     function objectAttributeContent( $contentObjectAttribute )
00351     {
00352         $data = $contentObjectAttribute->attribute( self::CONTENT_VALUE );
00353         $classAttribute = $contentObjectAttribute->contentClassAttribute();
00354         $classContent = $classAttribute->content();
00355         if ( isset( $classContent['ISBN13'] ) and $classContent['ISBN13'] )
00356         {
00357             list ( $prefix, $field1, $field2, $field3, $field4 ) = array_merge( preg_split( '#-#', $data ),
00358                                                                        array( 0 => '', 1 => '', 2 => '', 3 => '', 4 => '' ) );
00359             $dataArray = array(  'prefix' => $prefix,
00360                                  'field1' => $field1, 'field2' => $field2,
00361                                  'field3' => $field3, 'field4' => $field4,
00362                                  'value' => $data,
00363                                  'value_without_hyphens' => str_replace( "-", "", $data ),
00364                                  'value_with_spaces' => str_replace( "-", " ", $data ) );
00365             return $dataArray;
00366         }
00367 
00368         // The array_merge makes sure missing elements gets an empty string instead of NULL
00369         list ( $field1, $field2, $field3, $field4 ) = array_merge( preg_split( '#-#', $data ),
00370                                                                    array( 0 => '', 1 => '', 2 => '', 3 => '' ) );
00371         $isbn = array( 'field1' => $field1, 'field2' => $field2,
00372                        'field3' => $field3, 'field4' => $field4,
00373                        'value' => $data,
00374                        'value_without_hyphens' => str_replace( "-", "", $data ),
00375                        'value_with_spaces' => str_replace( "-", " ", $data ) );
00376         return $isbn;
00377     }
00378 
00379     /*!
00380      \reimp
00381     */
00382     function classAttributeContent( $classAttribute )
00383     {
00384         //include_once( 'kernel/classes/datatypes/ezisbn/ezisbn13.php' );
00385 
00386         $ISBN_13 = $classAttribute->attribute( self::CLASS_IS_ISBN13 );
00387         $isbn13Info = new eZISBN13();
00388         $content = array( 'ISBN13' => $ISBN_13,
00389                           'ranges' => $isbn13Info );
00390         return $content;
00391     }
00392 
00393 
00394     /*!
00395      \reimp
00396      ISBN numbers are indexable, returns \c true.
00397     */
00398     function isIndexable()
00399     {
00400         return true;
00401     }
00402 
00403     /*!
00404      Returns the meta data used for storing search indeces.
00405     */
00406     function metaData( $contentObjectAttribute )
00407     {
00408         return $contentObjectAttribute->attribute( self::CONTENT_VALUE );
00409     }
00410 
00411     /*!
00412      \return string representation of an contentobjectattribute data for simplified export
00413 
00414     */
00415     function toString( $contentObjectAttribute )
00416     {
00417         return $contentObjectAttribute->attribute( self::CONTENT_VALUE );
00418     }
00419 
00420     function fromString( $contentObjectAttribute, $string )
00421     {
00422         return $contentObjectAttribute->setAttribute( self::CONTENT_VALUE, $string );
00423     }
00424 
00425     /*!
00426      Returns the text.
00427     */
00428     function title( $data_instance, $name = null )
00429     {
00430         return $data_instance->attribute( self::CONTENT_VALUE );
00431     }
00432 
00433     /*!
00434      Initializes the class attribute.
00435      data_int will be se default to 1, as this is ISBN-13.
00436     */
00437     function initializeClassAttribute( $classAttribute )
00438     {
00439         if ( $classAttribute->attribute( self::CLASS_IS_ISBN13 ) === null )
00440         {
00441             $classAttribute->setAttribute( self::CLASS_IS_ISBN13, 1 );
00442         }
00443     }
00444 
00445     /*!
00446       Check if an ISBN value exist in the datatype.
00447     */
00448     function hasObjectAttributeContent( $contentObjectAttribute )
00449     {
00450         return trim( $contentObjectAttribute->attribute( self::CONTENT_VALUE ) ) != '';
00451     }
00452 
00453     /*!
00454       \reimp
00455       See also eZDataType:cleanDBDataBeforeImport().
00456     */
00457     function cleanDBDataBeforeImport()
00458     {
00459         //include_once( 'kernel/classes/datatypes/ezisbn/ezisbngroup.php' );
00460         //include_once( 'kernel/classes/datatypes/ezisbn/ezisbngrouprange.php' );
00461         //include_once( 'kernel/classes/datatypes/ezisbn/ezisbnregistrantrange.php' );
00462         eZISBNGroup::cleanAll();
00463         eZISBNGroupRange::cleanAll();
00464         eZISBNRegistrantRange::cleanAll();
00465         return true;
00466     }
00467 }
00468 
00469 eZDataType::register( eZISBNType::DATA_TYPE_STRING, "eZISBNType" );
00470 
00471 ?>