Cleaned up the FIXME issues.
[koha.git] / C4 / Input.pm
1 package C4::Input; #assumes C4/Input
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 require Exporter;
23
24 use vars qw($VERSION @ISA @EXPORT);
25
26 # set the version for version checking
27 $VERSION = 0.01;
28
29 =head1 NAME
30
31 C4::Input - Miscellaneous sanity checks
32
33 =head1 SYNOPSIS
34
35   use C4::Input;
36
37 =head1 DESCRIPTION
38
39 This module provides functions to see whether a given library card
40 number or ISBN is valid.
41
42 =head1 FUNCTIONS
43
44 =over 2
45
46 =cut
47
48 @ISA = qw(Exporter);
49 @EXPORT = qw(
50         &checkdigit &checkvalidisbn
51 );
52
53 # FIXME - This is never used.
54 #sub checkflds {
55 #  my ($env,$reqflds,$data) = @_;
56 #  my $numrflds = @$reqflds;
57 #  my @probarr;
58 #  my $i = 0;
59 #  while ($i < $numrflds) {
60 #    if ($data->{@$reqflds[$i]} eq "") {
61 #      push(@probarr, @$reqflds[$i]);
62 #    }
63 #    $i++
64 #  }
65 #  return (\@probarr);
66 #}
67
68 =item checkdigit
69
70   $valid = &checkdigit($env, $cardnumber);
71
72 Takes a card number, computes its check digit, and compares it to the
73 checkdigit at the end of C<$cardnumber>. Returns a true value iff
74 C<$cardnumber> has a valid check digit.
75
76 C<$env> is ignored.
77
78 =cut
79 #'
80 sub checkdigit {
81   my ($env,$infl) =  @_;
82   $infl = uc $infl;
83   my @weightings = (8,4,6,3,5,2,1);
84   my $sum;
85   my $i = 1;
86   my $valid = 0;
87   #  print $infl."<br>";
88
89   foreach $i (1..7) {
90     my $temp1 = $weightings[$i-1];
91     my $temp2 = substr($infl,$i,1);
92     $sum += $temp1 * $temp2;
93 #    print "$sum $temp1 $temp2<br>";
94   }
95   my $rem = ($sum%11);
96   if ($rem == 10) {
97     $rem = "X";
98   }
99   #print $rem."<br>";
100   if ($rem eq substr($infl,8,1)) {
101     $valid = 1;
102   }
103   return $valid;
104 } # sub checkdigit
105
106 =item checkvalidisbn
107
108   $valid = &checkvalidisbn($isbn);
109
110 Returns a true value iff C<$isbn> is a valid ISBN: it must be ten
111 digits long (counting "X" as a digit), and must have a valid check
112 digit at the end.
113
114 =cut
115 #'
116 #--------------------------------------
117 # Determine if a number is a valid ISBN number, according to length
118 #   of 10 digits and valid checksum
119 sub checkvalidisbn {
120         use strict;
121         my ($q)=@_ ;    # Input: ISBN number
122
123         my $isbngood = 0; # Return: true or false
124
125         $q=~s/x$/X/g;           # upshift lower case X
126         $q=~s/[^X\d]//g;
127         $q=~s/X.//g;
128         
129         #return 0 if $q is not ten digits long
130         if ($length($q)!=10) {
131                 return 0;
132         }
133         
134         #If we get to here, length($q) must be 10
135         my $checksum=substr($q,9,1);
136         my $isbn=substr($q,0,9);
137         my $i;
138         my $c=0;
139         for ($i=0; $i<9; $i++) {
140             my $digit=substr($q,$i,1);
141             $c+=$digit*(10-$i);
142         }
143         $c %= 11;
144         ($c==10) && ($c='X');
145         $isbngood = $c eq $checksum;
146
147         return $isbngood;
148
149 } # sub checkvalidisbn
150
151 END { }       # module clean-up code here (global destructor)
152
153 1;
154 __END__
155
156 =back
157
158 =head1 AUTHOR
159
160 Koha Developement team <info@koha.org>
161
162 =cut