[Koha-patches] [PATCH 2/2] Bug: 6539 - Used Business::ISBN to validate the ISBN intead of rolling my own

Peter Lorimer peter at oslo.ie
Wed Jun 29 16:57:24 CEST 2011


---
 C4/Search.pm |   69 ++++++++++++++++++---------------------------------------
 1 files changed, 22 insertions(+), 47 deletions(-)

diff --git a/C4/Search.pm b/C4/Search.pm
index be96946..5648faa 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -33,7 +33,7 @@ use C4::Debug;
 use C4::Items;
 use YAML;
 use URI::Escape;
-use POSIX;
+use Business::ISBN;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
 
@@ -1424,7 +1424,15 @@ sub searchResults {
     #find branchname
     #get branch information.....
     my %branches;
     my $bsth =$dbh->prepare("SELECT branchcode,branchname FROM branches ");                
     $bsth->execute();
     while ( my $bdata = $bsth->fetchrow_hashref ) {
         $branches{ $bdata->{'branchcode'} } = $bdata->{'branchname'};
@@ -1687,6 +1695,16 @@ sub searchResults {
                     $item_in_transit_count++ if $transfertwhen ne '';
 		    $item_onhold_count++     if $reservestatus eq 'Waiting';
                     $item->{status} = $item->{wthdrawn} . "-" . $item->{itemlost} . "-" . $item->{damaged} . "-" . $item->{notforloan};

 # can place hold on item ?
                    if ((!$item->{damaged} || C4::Context->preference('AllowHoldsOnDamagedItems'))
                      && !$item->{itemlost}
                      && !$item->{withdrawn}
                    ) {
                        $can_place_holds = 1;
                    }
                    

                     $other_count++;
 
                     my $key = $prefix . $item->{status};
@@ -2596,9 +2614,9 @@ $template->param ( MYLOOP => C4::Search::z3950_search_args($searchscalar) )
 
 sub z3950_search_args {
     my $bibrec = shift;
-  
+    my $isbn = Business::ISBN->new($bibrec); 
  
-    if (validate_isbn($bibrec) == 1)
+    if (defined $isbn && $isbn->is_valid)
     {
     $bibrec = { isbn => $bibrec } if !ref $bibrec;
 }
@@ -2644,51 +2662,8 @@ OR adds a new authority record
 =cut
 
 
-=head
-BOOL validate_isbn($isbn_number)
 
-This function validates an isbn number by:
 
-1. Checking it is 10 or 13 characters long
-2. If it is 10 characters long, checks the lasd character is an 'X' and all other characters are digits
-3. If it is 13 characters long, checks it begins with '97' and all other characters are digits
-
-=cut
-sub validate_isbn { 
-  my $isbn = shift; 
-    
-    # Handle ISBNs with spaces or hyphens in them 
-    $isbn =~ s/\s+//g;
-    $isbn =~ s/\-//g;
-    
-    # Is it the correct length ?
-         if (length($isbn) != 10 && length($isbn) != 13)
-         { return 0; } 
-                     else {   
-                         
-                         # Is the last char 'x' for ISBN10 ?  
-                            my @chars = split('', $isbn); 
-                              if (length($isbn) == 10 && uc($chars[9]) eq 'X')
-                               {
-                                    # Are all but the last characters digits ? 
-                                    if (isdigit(substr($isbn,0,length($isbn)-1)) )
-                                     { return 1;}
-                               }
-  
-                    # Is it 13 chars long and begin with '97' ?
-                   if ( ($chars[0] eq '9' && $chars[1] eq '7') && length($isbn) == 13)
-                   {
-                       # is it made up of digits? 
-                    if (isdigit($isbn))  
-                     { return 1;}
-         
-      
-                    }
-      
-                 } 
-     # If this function has not yet successfully returned the return failure
-     return 0;
-}
 
 sub BiblioAddAuthorities{
   my ( $record, $frameworkcode ) = @_;
-- 
1.7.4.1



More information about the Koha-patches mailing list