Deleting ignore_errors(1) pour MARC::Charset
[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 use C4::Context;
24 use CGI;
25
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 =head1 NAME
32
33 C4::Input - Miscellaneous sanity checks
34
35 =head1 SYNOPSIS
36
37   use C4::Input;
38
39 =head1 DESCRIPTION
40
41 This module provides functions to see whether a given library card
42 number or ISBN is valid.
43
44 =head1 FUNCTIONS
45
46 =over 2
47
48 =cut
49
50 @ISA = qw(Exporter);
51 @EXPORT = qw(
52         &checkdigit &checkvalidisbn
53         &buildCGIsort
54 );
55
56 # FIXME - This is never used.
57 #sub checkflds {
58 #  my ($env,$reqflds,$data) = @_;
59 #  my $numrflds = @$reqflds;
60 #  my @probarr;
61 #  my $i = 0;
62 #  while ($i < $numrflds) {
63 #    if ($data->{@$reqflds[$i]} eq "") {
64 #      push(@probarr, @$reqflds[$i]);
65 #    }
66 #    $i++
67 #  }
68 #  return (\@probarr);
69 #}
70
71 =item checkdigit
72
73   $valid = &checkdigit($env, $cardnumber $nounique);
74
75 Takes a card number, computes its check digit, and compares it to the
76 checkdigit at the end of C<$cardnumber>. Returns a true value iff
77 C<$cardnumber> has a valid check digit.
78
79 C<$env> is ignored.
80
81 =cut
82 #'
83 sub checkdigit {
84
85         my ($env,$infl, $nounique) =  @_;
86         $infl = uc $infl;
87
88
89         #Check to make sure the cardnumber is unique
90
91         #FIXME: We should make the error for a nonunique cardnumber
92         #different from the one where the checkdigit on the number is
93         #not correct
94
95         unless ( $nounique )
96         {
97                 my $dbh=C4::Context->dbh;
98                 my $query=qq{SELECT * FROM borrowers WHERE cardnumber=?};
99                 my $sth=$dbh->prepare($query);
100                 $sth->execute($infl);
101                 my %results = $sth->fetchrow_hashref();
102                 if ( $sth->rows != 0 )
103                 {
104                         return 0;
105                 }
106         }
107         if (C4::Context->preference("checkdigit") eq "none") {
108                 return 1;
109         }
110
111         my @weightings = (8,4,6,3,5,2,1);
112         my $sum;
113         my $i = 1;
114         my $valid = 0;
115
116         foreach $i (1..7) {
117                 my $temp1 = $weightings[$i-1];
118                 my $temp2 = substr($infl,$i,1);
119                 $sum += $temp1 * $temp2;
120         }
121         my $rem = ($sum%11);
122         if ($rem == 10) {
123         $rem = "X";
124         }
125         if ($rem eq substr($infl,8,1)) {
126                 $valid = 1;
127         }
128         return $valid;
129 } # sub checkdigit
130
131 =item checkvalidisbn
132
133   $valid = &checkvalidisbn($isbn);
134
135 Returns a true value iff C<$isbn> is a valid ISBN: it must be ten
136 digits long (counting "X" as a digit), and must have a valid check
137 digit at the end.
138
139 =cut
140 #'
141 #--------------------------------------
142 # Determine if a number is a valid ISBN number, according to length
143 #   of 10 digits and valid checksum
144 sub checkvalidisbn {
145         use strict;
146         my ($q)=@_ ;    # Input: ISBN number
147         
148         my $isbngood = 0; # Return: true or false
149         
150         $q=~s/x$/X/g;   # upshift lower case X
151         $q=~s/[^X\d]//g;
152         $q=~s/X.//g;
153         
154                 #return 0 if $q is not ten digits long
155                 if (length($q)!=10) {
156                         return 0;
157                 }
158                 
159                 #If we get to here, length($q) must be 10
160         my $checksum=substr($q,9,1);
161         my $isbn=substr($q,0,9);
162         my $i;
163         my $c=0;
164         for ($i=0; $i<9; $i++) {
165                 my $digit=substr($q,$i,1);
166                 $c+=$digit*(10-$i);
167         }
168         $c %= 11;
169         ($c==10) && ($c='X');
170         $isbngood = $c eq $checksum;
171         return $isbngood;
172
173 } # sub checkvalidisbn
174
175 =item buildCGISort
176
177   $CGIScrollingList = &BuildCGISort($name string, $input_name string);
178
179 Returns the scrolling list with name $input_name, built on authorised Values named $name.
180 Returns NULL if no authorised values found
181
182 =cut
183 sub buildCGIsort {
184     use strict;
185         my ($name,$input_name,$data) = @_;
186         my $dbh=C4::Context->dbh;
187         my $query=qq{SELECT * FROM authorised_values WHERE category=? order by lib};
188         my $sth=$dbh->prepare($query);
189         $sth->execute($name);
190         my $CGISort;
191         if ($sth->rows>0){
192                 my @values;
193                 my %labels;
194                 
195                 for (my $i =0;$i<$sth->rows;$i++){
196                         my $results = $sth->fetchrow_hashref;
197                         push @values, $results->{authorised_value};
198                         $labels{$results->{authorised_value}}=$results->{lib};
199                 }
200                 unshift(@values,"");
201                 $CGISort= CGI::scrolling_list(
202                                         -name => $input_name,
203                                         -values => \@values,
204                                         -labels => \%labels,
205                                         -default=> $data,
206                                         -size => 1,
207                                         -multiple => 0);
208         }
209         $sth->finish; 
210         return $CGISort;
211 }
212 END { }       # module clean-up code here (global destructor)
213
214 1;
215 __END__
216
217 =back
218
219 =head1 AUTHOR
220
221 Koha Developement team <info@koha.org>
222
223 =cut