fix for bug 2488: OPACItemsResultsDisplay/singlebranchmode
[koha.git] / C4 / Input.pm
blob6f0173d82f31ff3a6381e6dc9c06dba5668424ae
1 package C4::Input; #assumes C4/Input
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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
21 use strict;
22 require Exporter;
23 use C4::Context;
24 use CGI;
26 use vars qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
29 $VERSION = 0.01;
31 =head1 NAME
33 C4::Input - Miscellaneous sanity checks
35 =head1 SYNOPSIS
37 use C4::Input;
39 =head1 DESCRIPTION
41 This module provides functions to see whether a given library card
42 number or ISBN is valid.
44 =head1 FUNCTIONS
46 =over 2
48 =cut
50 @ISA = qw(Exporter);
51 @EXPORT = qw(
52 &checkdigit &checkvalidisbn
53 &buildCGIsort
56 =item checkdigit
58 $valid = &checkdigit($cardnumber $nounique);
60 Takes a card number, computes its check digit, and compares it to the
61 checkdigit at the end of C<$cardnumber>. Returns a true value iff
62 C<$cardnumber> has a valid check digit.
64 =cut
67 sub checkdigit ($;$) {
69 my ($infl, $nounique) = @_;
70 $infl = uc $infl;
72 # Check to make sure the cardnumber is unique
74 #FIXME: We should make the error for a nonunique cardnumber
75 #different from the one where the checkdigit on the number is
76 #not correct
78 unless ( $nounique )
80 my $query=qq{SELECT * FROM borrowers WHERE cardnumber=?};
81 my $sth=C4::Context->prepare($query);
82 $sth->execute($infl);
83 my %results = $sth->fetchrow_hashref();
84 if ( $sth->rows != 0 )
86 return 0;
89 if (C4::Context->preference("checkdigit") eq "none") {
90 return 1;
93 my @weightings = (8,4,6,3,5,2,1);
94 my $sum;
95 foreach my $i (1..7) {
96 my $temp1 = $weightings[$i-1];
97 my $temp2 = substr($infl,$i,1);
98 $sum += $temp1 * $temp2;
100 my $rem = ($sum%11);
101 if ($rem == 10) {
102 $rem = "X";
104 if ($rem eq substr($infl,8,1)) {
105 return 1;
107 return 0;
108 } # sub checkdigit
110 =item checkvalidisbn # Obsolete Function!
112 $valid = &checkvalidisbn($isbn);
114 Returns a true value iff C<$isbn> is a valid ISBN: it must be ten
115 digits long (counting "X" as a digit), and must have a valid check
116 digit at the end.
118 sub checkvalidisbn ($) { # Obsolete Function!
119 my ($q) = shift or return undef;
120 $q=~s/[^Xx\d]//g;
121 /(\d{9})(X|\d)/i or
122 /(\d{12})(X|\d)/i or return 0;
123 my $checksum = $2;
124 my $isbn = $1;
125 my $c = 0;
126 my $max = length $isbn;
127 for (my $i=0; $i<$max; $i++) {
128 my $digit=substr($q,$i,1);
129 $c+=$digit*(10-$i);
131 $c %= 11;
132 ($c==10) and $c = 'X';
133 return ($c eq $checksum) ? 1 : 0;
136 =item buildCGISort
138 $CGIScrollingList = &buildCGISort($name string, $input_name string);
140 Returns the scrolling list with name $input_name, built on authorised Values named $name.
141 Returns NULL if no authorised values found
143 =cut
145 sub buildCGIsort {
146 my ($name,$input_name,$data) = @_;
147 my $dbh=C4::Context->dbh;
148 my $query=qq{SELECT * FROM authorised_values WHERE category=? order by lib};
149 my $sth=$dbh->prepare($query);
150 $sth->execute($name);
151 my $CGISort;
152 if ($sth->rows>0){
153 my @values;
154 my %labels;
156 for (my $i =0;$i<$sth->rows;$i++){
157 my $results = $sth->fetchrow_hashref;
158 push @values, $results->{authorised_value};
159 $labels{$results->{authorised_value}}=$results->{lib};
161 unshift(@values,"");
162 $CGISort= CGI::scrolling_list(
163 -name => $input_name,
164 -values => \@values,
165 -labels => \%labels,
166 -default=> $data,
167 -size => 1,
168 -multiple => 0);
170 $sth->finish;
171 return $CGISort;
173 END { } # module clean-up code here (global destructor)
176 __END__
178 =back
180 =head1 AUTHOR
182 Koha Developement team <info@koha.org>
184 =cut