eZ Publish  [4.0]
ezisbnregistrantrange.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Created on: <17-Apr-2007 11:10:23 bjorn>
00004 //
00005 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.0.x
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00027 //
00028 
00029 /*! \file ezisbnregistrantrange.php
00030 */
00031 
00032 /*!
00033   \class eZISBNRegistrantRange ezisbnregistrantrange.php
00034   \brief The class eZISBNRegistrantRange handles Registrant ranges.
00035 
00036   Has information about how the different ranges the registrant element
00037   could be in. Example: From 00 to 19 and continues from 200-699.
00038   This means that the length of the registrant can differ from
00039   range to range.
00040 
00041   The registrant element is the third element in the ISBN-13 number, after
00042   the Prefix and Registration group number.
00043 
00044   Example: 978-0-11-000222-4 where 11 is the registrant number.
00045 
00046   The different Registrant ranges are described in more detail at
00047   http://www.isbn-international.org
00048 */
00049 
00050 //include_once( 'kernel/classes/ezpersistentobject.php' );
00051 
00052 class eZISBNRegistrantRange extends eZPersistentObject
00053 {
00054     /*!
00055      Constructor
00056     */
00057     function eZISBNRegistrantRange( $row )
00058     {
00059         $this->eZPersistentObject( $row );
00060     }
00061 
00062     /*!
00063       Definition of the ranges for ISBN Registrant.
00064     */
00065     static function definition()
00066     {
00067         return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00068                                                         'datatype' => 'integer',
00069                                                         'default' => 0,
00070                                                         'required' => true ),
00071                                          'from_number' => array( 'name' => 'FromNumber',
00072                                                                  'datatype' => 'integer',
00073                                                                  'default' => 0,
00074                                                                  'required' => true ),
00075                                          'to_number' => array( 'name' => 'ToNumber',
00076                                                                   'datatype' => 'integer',
00077                                                                   'default' => 0,
00078                                                                   'required' => true ),
00079                                          'registrant_from' => array( 'name' => 'RegistrantFrom',
00080                                                                  'datatype' => 'string',
00081                                                                  'default' => '',
00082                                                                  'required' => true ),
00083                                          'registrant_to' => array( 'name' => 'RegistrantTo',
00084                                                                   'datatype' => 'string',
00085                                                                   'default' => '',
00086                                                                   'required' => true ),
00087                                          'registrant_length' => array( 'name' => 'RegistrantLength',
00088                                                             'datatype' => 'integer',
00089                                                             'default' => 0,
00090                                                             'required' => true ),
00091                                          'isbn_group_id' => array( 'name' => 'ISBNGroupID',
00092                                                                      'datatype' => 'integer',
00093                                                                      'default' => 0,
00094                                                                      'required' => true ),
00095                                          ),
00096                       'keys' => array( 'id' ),
00097                       'increment_key' => 'id',
00098                       'class_name' => 'eZISBNRegistrantRange',
00099                       'name' => 'ezisbn_registrant_range' );
00100     }
00101 
00102     /*!
00103      \static
00104      \param $ISBNGroupID    The id that point to the ISBN Group object
00105                             (Which contain info about the area and the unique group number).
00106      \param $fromNumber     Group is starting from test number, which is based on. Example: 20000
00107                             the 5 numbers after the Prefix number and Registration Group number.
00108      \param $toNumber       Group is ending on test number, which is based on. Example: 69999
00109                             the 5 numbers after the Prefix number and Registration Group number.
00110      \param $registrantFrom Registrant number is starting on, based on the length set
00111                             in the registrant range. Is a string to support 0 in front. Example: 200
00112      \param $registrantTo   Registrant number ending on, based on the length set
00113                             in the registrant range. Is a string to support 0 in front. Example: 699
00114      \param $length         How many characters $registrantFrom and $registrantTo should have.
00115 
00116      Create a new registrant range for an ISBN group / area.
00117 
00118      \return A new eZISBNRegistrantRange object.
00119     */
00120     static function create( $ISBNGroupID, $fromNumber, $toNumber, $registrantFrom, $registrantTo, $length )
00121     {
00122         $row = array(
00123             'id' => null,
00124             'from_number' => $fromNumber,
00125             'to_number' => $toNumber,
00126             'registrant_from' => $registrantFrom,
00127             'registrant_to' => $registrantTo,
00128             'registrant_length' => $length,
00129             'isbn_group_id' => $ISBNGroupID );
00130         return new eZISBNRegistrantRange( $row );
00131     }
00132 
00133 
00134     /*!
00135      \static
00136      Removes the registrant area based on ID \a $id.
00137     */
00138     static function removeByID( $id )
00139     {
00140         eZPersistentObject::removeObject( eZISBNRegistrantRange::definition(),
00141                                           array( 'id' => $id ) );
00142     }
00143 
00144     /*!
00145      \static
00146 
00147      Fetch the registrant group for a unique registration group area.
00148 
00149      \param $groupID  The id that point to the ISBN Group object
00150                       (Which contain info about the area and the unique group number).
00151      \param $count    Will contain the count of objects returned and is sent
00152                       back in the reference variable.
00153      \param $asObject If the result should be returned as object or an array.
00154      \return the registrant list for an ISBN registration group id.
00155     */
00156     static function fetchListByGroupID( $groupID, $asObject = true )
00157     {
00158         $conditions = array( 'isbn_group_id' => $groupID );
00159         $sortArray = array( array( 'from_number' => 'asc' ) );
00160         return eZPersistentObject::fetchObjectList( eZISBNRegistrantRange::definition(),
00161                                                     null, $conditions, $sortArray, null,
00162                                                     $asObject );
00163     }
00164 
00165     /*!
00166      \static
00167 
00168      Will extract the registrant number based on the different ranges
00169      which is based on the 5 first digits after the Prefix field and the registration group number.
00170 
00171      \param $isbnNr Should be a stripped down ISBN number with just the digits (ean number).
00172      \param $group is an object of eZISBNGroup, which needs to be known before this function is called.
00173                    Contains information about the group itself.
00174      \param $groupRange is an object of eZISBNGroupRange, which needs to be known before this function is called.
00175                         Contains information about the valid ranges for the ISBN group.
00176      \param $registrantLength is the length of the Registrant in the range that was found.
00177                               Is sent back in the reference variable.
00178 
00179      \return the registrant range object if found and false if not found.
00180     */
00181     static function extractRegistrant( $isbnNr, $group, $groupRange, &$registrantLength )
00182     {
00183         $registrant = false;
00184         if ( $group instanceof eZISBNGroup and
00185              $groupRange instanceof eZISBNGroupRange )
00186         {
00187             $groupLength = $groupRange->attribute( 'group_length' );
00188             $groupID = $group->attribute( 'id' );
00189 
00190             $registrantOffset = 3 + $groupLength;
00191             $testSegment = substr( $isbnNr, $registrantOffset, 5 );
00192             if ( is_numeric( $testSegment ) )
00193             {
00194                 $conditions = array( 'from_number' => array( '<=', $testSegment ),
00195                                      'to_number' => array( '>=', $testSegment ),
00196                                      'isbn_group_id' => $groupID );
00197                 $groupRangeArray = eZPersistentObject::fetchObjectList( eZISBNRegistrantRange::definition(),
00198                                                                         null, $conditions, null, null,
00199                                                                         true );
00200                 if ( count( $groupRangeArray ) == 1 )
00201                 {
00202                     $length = $groupRangeArray[0]->attribute( 'registrant_length' );
00203 
00204                     // Copy the length to send it back as a reference.
00205                     $registrantLength = $length;
00206                     $registrant = $groupRangeArray[0];
00207                 }
00208             }
00209         }
00210         return $registrant;
00211     }
00212 
00213     /*!
00214      \static
00215      Removes all ISBN group ranges from the database.
00216     */
00217     static function cleanAll()
00218     {
00219         $db = eZDB::instance();
00220         $definition = eZISBNRegistrantRange::definition();
00221         $table = $definition['name'];
00222         $sql = "TRUNCATE TABLE " . $table;
00223         $db->query( $sql );
00224     }
00225 }
00226 
00227 ?>