3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
24 use vars qw($VERSION @ISA @EXPORT);
30 C4::Koha - Perl Module containing convenience functions for Koha scripts
37 $date = slashifyDate("01-01-2002")
38 $ethnicity = fixEthnicity('asian');
39 ($categories, $labels) = borrowercategories();
40 ($categories, $labels) = ethnicitycategories();
44 Koha.pm provides many functions for Koha scripts.
53 @EXPORT = qw(&slashifyDate
57 &subfield_is_koha_internal_p
58 &getbranches &getbranch
59 &getprinters &getprinter
60 &getitemtypes &getitemtypeinfo
69 $slash_date = &slashifyDate($dash_date);
71 Takes a string of the form "DD-MM-YYYY" (or anything separated by
72 dashes), converts it to the form "YYYY/MM/DD", and returns the result.
77 # accepts a date of the form xx-xx-xx[xx] and returns it in the
79 my @dateOut = split('-', shift);
80 return("$dateOut[2]/$dateOut[1]/$dateOut[0]")
85 $ethn_name = &fixEthnicity($ethn_code);
87 Takes an ethnicity code (e.g., "european" or "pi") and returns the
88 corresponding descriptive name from the C<ethnicity> table in the
89 Koha database ("European" or "Pacific Islander").
96 my $ethnicity = shift;
97 my $dbh = C4::Context->dbh;
98 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
99 $sth->execute($ethnicity);
100 my $data=$sth->fetchrow_hashref;
102 return $data->{'name'};
105 =head2 borrowercategories
107 ($codes_arrayref, $labels_hashref) = &borrowercategories();
109 Looks up the different types of borrowers in the database. Returns two
110 elements: a reference-to-array, which lists the borrower category
111 codes, and a reference-to-hash, which maps the borrower category codes
112 to category descriptions.
117 sub borrowercategories {
118 my $dbh = C4::Context->dbh;
119 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
123 while (my $data=$sth->fetchrow_hashref){
124 push @codes,$data->{'categorycode'};
125 $labels{$data->{'categorycode'}}=$data->{'description'};
128 return(\@codes,\%labels);
131 =head2 ethnicitycategories
133 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
135 Looks up the different ethnic types in the database. Returns two
136 elements: a reference-to-array, which lists the ethnicity codes, and a
137 reference-to-hash, which maps the ethnicity codes to ethnicity
143 sub ethnicitycategories {
144 my $dbh = C4::Context->dbh;
145 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
149 while (my $data=$sth->fetchrow_hashref){
150 push @codes,$data->{'code'};
151 $labels{$data->{'code'}}=$data->{'name'};
154 return(\@codes,\%labels);
157 # FIXME.. this should be moved to a MARC-specific module
158 sub subfield_is_koha_internal_p ($) {
161 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
162 # But real MARC subfields are always single-character
163 # so it really is safer just to check the length
165 return length $subfield != 1;
170 $branches = &getbranches();
171 returns informations about branches.
172 Create a branch selector with the following code
174 =head3 in PERL SCRIPT
176 my $branches = getbranches;
178 foreach my $thisbranch (keys %$branches) {
179 my $selected = 1 if $thisbranch eq $branch;
180 my %row =(value => $thisbranch,
181 selected => $selected,
182 branchname => $branches->{$thisbranch}->{'branchname'},
184 push @branchloop, \%row;
189 <select name="branch">
190 <option value="">Default</option>
191 <!-- TMPL_LOOP name="branchloop" -->
192 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
199 # returns a reference to a hash of references to branches...
201 my $dbh = C4::Context->dbh;
202 my $sth=$dbh->prepare("select * from branches order by branchname");
204 while (my $branch=$sth->fetchrow_hashref) {
205 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
206 $nsth->execute($branch->{'branchcode'});
207 while (my ($cat) = $nsth->fetchrow_array) {
208 # FIXME - This seems wrong. It ought to be
209 # $branch->{categorycodes}{$cat} = 1;
210 # otherwise, there's a namespace collision if there's a
211 # category with the same name as a field in the 'branches'
212 # table (i.e., don't create a category called "issuing").
213 # In addition, the current structure doesn't really allow
214 # you to list the categories that a branch belongs to:
215 # you'd have to list keys %$branch, and remove those keys
216 # that aren't fields in the "branches" table.
219 $branches{$branch->{'branchcode'}}=$branch;
226 $itemtypes = &getitemtypes();
228 Returns information about existing itemtypes.
230 build a HTML select with the following code :
232 =head3 in PERL SCRIPT
234 my $itemtypes = getitemtypes;
236 foreach my $thisitemtype (keys %$itemtypes) {
237 my $selected = 1 if $thisitemtype eq $itemtype;
238 my %row =(value => $thisitemtype,
239 selected => $selected,
240 description => $itemtypes->{$thisitemtype}->{'description'},
242 push @itemtypesloop, \%row;
244 $template->param(itemtypeloop => \@itemtypesloop);
248 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
249 <select name="itemtype">
250 <option value="">Default</option>
251 <!-- TMPL_LOOP name="itemtypeloop" -->
252 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
255 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
256 <input type="submit" value="OK" class="button">
263 # returns a reference to a hash of references to branches...
265 my $dbh = C4::Context->dbh;
266 my $sth=$dbh->prepare("select * from itemtypes order by description");
268 while (my $IT=$sth->fetchrow_hashref) {
269 $itemtypes{$IT->{'itemtype'}}=$IT;
271 return (\%itemtypes);
277 $itemtype = &getitemtype($itemtype);
279 Returns information about an itemtype.
283 sub getitemtypeinfo {
285 my $dbh = C4::Context->dbh;
286 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
287 $sth->execute($itemtype);
288 my $res = $sth->fetchrow_hashref;
294 $printers = &getprinters($env);
295 @queues = keys %$printers;
297 Returns information about existing printer queues.
301 C<$printers> is a reference-to-hash whose keys are the print queues
302 defined in the printers table of the Koha database. The values are
303 references-to-hash, whose keys are the fields in the printers table.
310 my $dbh = C4::Context->dbh;
311 my $sth=$dbh->prepare("select * from printers");
313 while (my $printer=$sth->fetchrow_hashref) {
314 $printers{$printer->{'printqueue'}}=$printer;
319 my($query, $branches) = @_; # get branch for this query from branches
320 my $branch = $query->param('branch');
321 ($branch) || ($branch = $query->cookie('branch'));
322 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
326 sub getprinter ($$) {
327 my($query, $printers) = @_; # get printer for this query from printers
328 my $printer = $query->param('printer');
329 ($printer) || ($printer = $query->cookie('printer'));
330 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);