Bug 10006 - holding branch is not available to XSLT display
[koha.git] / C4 / Input.pm
blobba304607a716cefcd1eecb9ebdfd645394880029
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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 use warnings;
24 require Exporter;
25 use C4::Context;
26 use CGI;
28 use vars qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
31 $VERSION = 3.07.00.049;
33 =head1 NAME
35 C4::Input - Miscellaneous sanity checks
37 =head1 SYNOPSIS
39 use C4::Input;
41 =head1 DESCRIPTION
43 This module provides functions to see whether a given library card
44 number or ISBN is valid.
46 =head1 FUNCTIONS
48 =over 2
50 =cut
52 @ISA = qw(Exporter);
53 @EXPORT = qw(
54 &checkdigit
55 &buildCGIsort
58 =item checkdigit
60 $valid = &checkdigit($cardnumber $nounique);
62 Takes a card number, computes its check digit, and compares it to the
63 checkdigit at the end of C<$cardnumber>. Returns a true value iff
64 C<$cardnumber> has a valid check digit.
66 =cut
69 sub checkdigit ($;$) {
71 my ($infl, $nounique) = @_;
72 $infl = uc $infl;
74 # Check to make sure the cardnumber is unique
76 #FIXME: We should make the error for a nonunique cardnumber
77 #different from the one where the checkdigit on the number is
78 #not correct
80 unless ( $nounique )
82 my $query=qq{SELECT * FROM borrowers WHERE cardnumber=?};
83 my $sth=C4::Context->prepare($query);
84 $sth->execute($infl);
85 my %results = $sth->fetchrow_hashref();
86 if ( $sth->rows != 0 )
88 return 0;
91 if (C4::Context->preference("checkdigit") eq "none") {
92 return 1;
95 my @weightings = (8,4,6,3,5,2,1);
96 my $sum;
97 foreach my $i (1..7) {
98 my $temp1 = $weightings[$i-1];
99 my $temp2 = substr($infl,$i,1);
100 $sum += $temp1 * $temp2;
102 my $rem = ($sum%11);
103 if ($rem == 10) {
104 $rem = "X";
106 if ($rem eq substr($infl,8,1)) {
107 return 1;
109 return 0;
110 } # sub checkdigit
112 =item buildCGISort
114 $CGIScrollingList = &buildCGISort($name string, $input_name string);
116 Returns the scrolling list with name $input_name, built on authorised Values named $name.
117 Returns NULL if no authorised values found
119 =cut
121 sub buildCGIsort {
122 my ( $name, $input_name, $data ) = @_;
123 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
125 my $dbh=C4::Context->dbh;
126 my $query = qq{
127 SELECT *
128 FROM authorised_values
130 $query .= qq{
131 LEFT JOIN authorised_values_branches ON ( id = av_id )
132 } if $branch_limit;
133 $query .= qq{
134 WHERE category = ?
136 $query .= qq{ AND ( branchcode = ? OR branchcode IS NULL )} if $branch_limit;
137 $query .= qq{ GROUP BY lib ORDER BY lib};
139 my $sth=$dbh->prepare($query);
140 $sth->execute( $name, $branch_limit ? $branch_limit : () );
141 my $CGISort;
142 if ($sth->rows>0){
143 my @values;
144 my %labels;
146 for (my $i =0;$i<$sth->rows;$i++){
147 my $results = $sth->fetchrow_hashref;
148 push @values, $results->{authorised_value};
149 $labels{$results->{authorised_value}}=$results->{lib};
151 $CGISort= CGI::scrolling_list(
152 -name => $input_name,
153 -id => $input_name,
154 -values => \@values,
155 -labels => \%labels,
156 -default=> $data,
157 -size => 1,
158 -multiple => 0);
160 $sth->finish;
161 return $CGISort;
164 END { } # module clean-up code here (global destructor)
167 __END__
169 =back
171 =head1 AUTHOR
173 Koha Development Team <http://koha-community.org/>
175 =cut