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
61 &getframeworks &getframeworkinfo
71 $slash_date = &slashifyDate($dash_date);
73 Takes a string of the form "DD-MM-YYYY" (or anything separated by
74 dashes), converts it to the form "YYYY/MM/DD", and returns the result.
79 # accepts a date of the form xx-xx-xx[xx] and returns it in the
81 my @dateOut = split('-', shift);
82 return("$dateOut[2]/$dateOut[1]/$dateOut[0]")
87 $ethn_name = &fixEthnicity($ethn_code);
89 Takes an ethnicity code (e.g., "european" or "pi") and returns the
90 corresponding descriptive name from the C<ethnicity> table in the
91 Koha database ("European" or "Pacific Islander").
98 my $ethnicity = shift;
99 my $dbh = C4::Context->dbh;
100 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
101 $sth->execute($ethnicity);
102 my $data=$sth->fetchrow_hashref;
104 return $data->{'name'};
107 =head2 borrowercategories
109 ($codes_arrayref, $labels_hashref) = &borrowercategories();
111 Looks up the different types of borrowers in the database. Returns two
112 elements: a reference-to-array, which lists the borrower category
113 codes, and a reference-to-hash, which maps the borrower category codes
114 to category descriptions.
119 sub borrowercategories {
120 my $dbh = C4::Context->dbh;
121 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
125 while (my $data=$sth->fetchrow_hashref){
126 push @codes,$data->{'categorycode'};
127 $labels{$data->{'categorycode'}}=$data->{'description'};
130 return(\@codes,\%labels);
133 =head2 ethnicitycategories
135 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
137 Looks up the different ethnic types in the database. Returns two
138 elements: a reference-to-array, which lists the ethnicity codes, and a
139 reference-to-hash, which maps the ethnicity codes to ethnicity
145 sub ethnicitycategories {
146 my $dbh = C4::Context->dbh;
147 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
151 while (my $data=$sth->fetchrow_hashref){
152 push @codes,$data->{'code'};
153 $labels{$data->{'code'}}=$data->{'name'};
156 return(\@codes,\%labels);
159 # FIXME.. this should be moved to a MARC-specific module
160 sub subfield_is_koha_internal_p ($) {
163 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
164 # But real MARC subfields are always single-character
165 # so it really is safer just to check the length
167 return length $subfield != 1;
172 $branches = &getbranches();
173 returns informations about branches.
174 Create a branch selector with the following code
176 =head3 in PERL SCRIPT
178 my $branches = getbranches;
180 foreach my $thisbranch (keys %$branches) {
181 my $selected = 1 if $thisbranch eq $branch;
182 my %row =(value => $thisbranch,
183 selected => $selected,
184 branchname => $branches->{$thisbranch}->{'branchname'},
186 push @branchloop, \%row;
191 <select name="branch">
192 <option value="">Default</option>
193 <!-- TMPL_LOOP name="branchloop" -->
194 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
201 # returns a reference to a hash of references to branches...
203 my $dbh = C4::Context->dbh;
204 my $sth=$dbh->prepare("select * from branches order by branchname");
206 while (my $branch=$sth->fetchrow_hashref) {
207 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
208 $nsth->execute($branch->{'branchcode'});
209 while (my ($cat) = $nsth->fetchrow_array) {
210 # FIXME - This seems wrong. It ought to be
211 # $branch->{categorycodes}{$cat} = 1;
212 # otherwise, there's a namespace collision if there's a
213 # category with the same name as a field in the 'branches'
214 # table (i.e., don't create a category called "issuing").
215 # In addition, the current structure doesn't really allow
216 # you to list the categories that a branch belongs to:
217 # you'd have to list keys %$branch, and remove those keys
218 # that aren't fields in the "branches" table.
221 $branches{$branch->{'branchcode'}}=$branch;
228 $itemtypes = &getitemtypes();
230 Returns information about existing itemtypes.
232 build a HTML select with the following code :
234 =head3 in PERL SCRIPT
236 my $itemtypes = getitemtypes;
238 foreach my $thisitemtype (keys %$itemtypes) {
239 my $selected = 1 if $thisitemtype eq $itemtype;
240 my %row =(value => $thisitemtype,
241 selected => $selected,
242 description => $itemtypes->{$thisitemtype}->{'description'},
244 push @itemtypesloop, \%row;
246 $template->param(itemtypeloop => \@itemtypesloop);
250 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
251 <select name="itemtype">
252 <option value="">Default</option>
253 <!-- TMPL_LOOP name="itemtypeloop" -->
254 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
257 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
258 <input type="submit" value="OK" class="button">
265 # returns a reference to a hash of references to branches...
267 my $dbh = C4::Context->dbh;
268 my $sth=$dbh->prepare("select * from itemtypes order by description");
270 while (my $IT=$sth->fetchrow_hashref) {
271 $itemtypes{$IT->{'itemtype'}}=$IT;
273 return (\%itemtypes);
278 $authtypes = &getauthtypes();
280 Returns information about existing authtypes.
282 build a HTML select with the following code :
284 =head3 in PERL SCRIPT
286 my $authtypes = getauthtypes;
288 foreach my $thisauthtype (keys %$authtypes) {
289 my $selected = 1 if $thisauthtype eq $authtype;
290 my %row =(value => $thisauthtype,
291 selected => $selected,
292 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
294 push @authtypesloop, \%row;
296 $template->param(itemtypeloop => \@itemtypesloop);
300 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
301 <select name="authtype">
302 <!-- TMPL_LOOP name="authtypeloop" -->
303 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
306 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
307 <input type="submit" value="OK" class="button">
314 # returns a reference to a hash of references to authtypes...
316 my $dbh = C4::Context->dbh;
317 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
319 while (my $IT=$sth->fetchrow_hashref) {
320 $authtypes{$IT->{'authtypecode'}}=$IT;
322 return (\%authtypes);
327 $frameworks = &getframework();
329 Returns information about existing frameworks
331 build a HTML select with the following code :
333 =head3 in PERL SCRIPT
335 my $frameworks = frameworks();
337 foreach my $thisframework (keys %$frameworks) {
338 my $selected = 1 if $thisframework eq $frameworkcode;
339 my %row =(value => $thisframework,
340 selected => $selected,
341 description => $frameworks->{$thisframework}->{'frameworktext'},
343 push @frameworksloop, \%row;
345 $template->param(frameworkloop => \@frameworksloop);
349 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
350 <select name="frameworkcode">
351 <option value="">Default</option>
352 <!-- TMPL_LOOP name="frameworkloop" -->
353 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
356 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
357 <input type="submit" value="OK" class="button">
364 # returns a reference to a hash of references to branches...
366 my $dbh = C4::Context->dbh;
367 my $sth=$dbh->prepare("select * from biblio_framework");
369 while (my $IT=$sth->fetchrow_hashref) {
370 $itemtypes{$IT->{'frameworkcode'}}=$IT;
372 return (\%itemtypes);
374 =head2 getframeworkinfo
376 $frameworkinfo = &getframeworkinfo($frameworkcode);
378 Returns information about an frameworkcode.
382 sub getframeworkinfo {
383 my ($frameworkcode) = @_;
384 my $dbh = C4::Context->dbh;
385 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
386 $sth->execute($frameworkcode);
387 my $res = $sth->fetchrow_hashref;
392 =head2 getitemtypeinfo
394 $itemtype = &getitemtype($itemtype);
396 Returns information about an itemtype.
400 sub getitemtypeinfo {
402 my $dbh = C4::Context->dbh;
403 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
404 $sth->execute($itemtype);
405 my $res = $sth->fetchrow_hashref;
411 $printers = &getprinters($env);
412 @queues = keys %$printers;
414 Returns information about existing printer queues.
418 C<$printers> is a reference-to-hash whose keys are the print queues
419 defined in the printers table of the Koha database. The values are
420 references-to-hash, whose keys are the fields in the printers table.
427 my $dbh = C4::Context->dbh;
428 my $sth=$dbh->prepare("select * from printers");
430 while (my $printer=$sth->fetchrow_hashref) {
431 $printers{$printer->{'printqueue'}}=$printer;
436 my($query, $branches) = @_; # get branch for this query from branches
437 my $branch = $query->param('branch');
438 ($branch) || ($branch = $query->cookie('branch'));
439 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
443 sub getprinter ($$) {
444 my($query, $printers) = @_; # get printer for this query from printers
445 my $printer = $query->param('printer');
446 ($printer) || ($printer = $query->cookie('printer'));
447 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);