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
62 &getauthtypes &getauthtype
63 &getallthemes &getalllanguages
72 $slash_date = &slashifyDate($dash_date);
74 Takes a string of the form "DD-MM-YYYY" (or anything separated by
75 dashes), converts it to the form "YYYY/MM/DD", and returns the result.
80 # accepts a date of the form xx-xx-xx[xx] and returns it in the
82 my @dateOut = split('-', shift);
83 return("$dateOut[2]/$dateOut[1]/$dateOut[0]")
88 $ethn_name = &fixEthnicity($ethn_code);
90 Takes an ethnicity code (e.g., "european" or "pi") and returns the
91 corresponding descriptive name from the C<ethnicity> table in the
92 Koha database ("European" or "Pacific Islander").
99 my $ethnicity = shift;
100 my $dbh = C4::Context->dbh;
101 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
102 $sth->execute($ethnicity);
103 my $data=$sth->fetchrow_hashref;
105 return $data->{'name'};
108 =head2 borrowercategories
110 ($codes_arrayref, $labels_hashref) = &borrowercategories();
112 Looks up the different types of borrowers in the database. Returns two
113 elements: a reference-to-array, which lists the borrower category
114 codes, and a reference-to-hash, which maps the borrower category codes
115 to category descriptions.
120 sub borrowercategories {
121 my $dbh = C4::Context->dbh;
122 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
126 while (my $data=$sth->fetchrow_hashref){
127 push @codes,$data->{'categorycode'};
128 $labels{$data->{'categorycode'}}=$data->{'description'};
131 return(\@codes,\%labels);
134 =head2 ethnicitycategories
136 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
138 Looks up the different ethnic types in the database. Returns two
139 elements: a reference-to-array, which lists the ethnicity codes, and a
140 reference-to-hash, which maps the ethnicity codes to ethnicity
146 sub ethnicitycategories {
147 my $dbh = C4::Context->dbh;
148 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
152 while (my $data=$sth->fetchrow_hashref){
153 push @codes,$data->{'code'};
154 $labels{$data->{'code'}}=$data->{'name'};
157 return(\@codes,\%labels);
160 # FIXME.. this should be moved to a MARC-specific module
161 sub subfield_is_koha_internal_p ($) {
164 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
165 # But real MARC subfields are always single-character
166 # so it really is safer just to check the length
168 return length $subfield != 1;
173 $branches = &getbranches();
174 returns informations about branches.
175 Create a branch selector with the following code
177 =head3 in PERL SCRIPT
179 my $branches = getbranches;
181 foreach my $thisbranch (sort keys %$branches) {
182 my $selected = 1 if $thisbranch eq $branch;
183 my %row =(value => $thisbranch,
184 selected => $selected,
185 branchname => $branches->{$thisbranch}->{'branchname'},
187 push @branchloop, \%row;
192 <select name="branch">
193 <option value="">Default</option>
194 <!-- TMPL_LOOP name="branchloop" -->
195 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
202 # returns a reference to a hash of references to branches...
204 my $dbh = C4::Context->dbh;
206 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
207 my $strsth ="Select * from branches ";
208 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
209 $strsth.= " order by branchname";
210 $sth=$dbh->prepare($strsth);
212 $sth = $dbh->prepare("Select * from branches order by branchname");
215 while (my $branch=$sth->fetchrow_hashref) {
216 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
217 $nsth->execute($branch->{'branchcode'});
218 while (my ($cat) = $nsth->fetchrow_array) {
219 # FIXME - This seems wrong. It ought to be
220 # $branch->{categorycodes}{$cat} = 1;
221 # otherwise, there's a namespace collision if there's a
222 # category with the same name as a field in the 'branches'
223 # table (i.e., don't create a category called "issuing").
224 # In addition, the current structure doesn't really allow
225 # you to list the categories that a branch belongs to:
226 # you'd have to list keys %$branch, and remove those keys
227 # that aren't fields in the "branches" table.
230 $branches{$branch->{'branchcode'}}=$branch;
237 $itemtypes = &getitemtypes();
239 Returns information about existing itemtypes.
241 build a HTML select with the following code :
243 =head3 in PERL SCRIPT
245 my $itemtypes = getitemtypes;
247 foreach my $thisitemtype (sort keys %$itemtypes) {
248 my $selected = 1 if $thisitemtype eq $itemtype;
249 my %row =(value => $thisitemtype,
250 selected => $selected,
251 description => $itemtypes->{$thisitemtype}->{'description'},
253 push @itemtypesloop, \%row;
255 $template->param(itemtypeloop => \@itemtypesloop);
259 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
260 <select name="itemtype">
261 <option value="">Default</option>
262 <!-- TMPL_LOOP name="itemtypeloop" -->
263 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
266 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
267 <input type="submit" value="OK" class="button">
274 # returns a reference to a hash of references to branches...
276 my $dbh = C4::Context->dbh;
277 my $sth=$dbh->prepare("select * from itemtypes");
279 while (my $IT=$sth->fetchrow_hashref) {
280 $itemtypes{$IT->{'itemtype'}}=$IT;
282 return (\%itemtypes);
287 $authtypes = &getauthtypes();
289 Returns information about existing authtypes.
291 build a HTML select with the following code :
293 =head3 in PERL SCRIPT
295 my $authtypes = getauthtypes;
297 foreach my $thisauthtype (keys %$authtypes) {
298 my $selected = 1 if $thisauthtype eq $authtype;
299 my %row =(value => $thisauthtype,
300 selected => $selected,
301 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
303 push @authtypesloop, \%row;
305 $template->param(itemtypeloop => \@itemtypesloop);
309 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
310 <select name="authtype">
311 <!-- TMPL_LOOP name="authtypeloop" -->
312 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
315 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
316 <input type="submit" value="OK" class="button">
323 # returns a reference to a hash of references to authtypes...
325 my $dbh = C4::Context->dbh;
326 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
328 while (my $IT=$sth->fetchrow_hashref) {
329 $authtypes{$IT->{'authtypecode'}}=$IT;
331 return (\%authtypes);
335 my ($authtypecode) = @_;
336 # returns a reference to a hash of references to authtypes...
338 my $dbh = C4::Context->dbh;
339 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
340 $sth->execute($authtypecode);
341 my $res=$sth->fetchrow_hashref;
347 $frameworks = &getframework();
349 Returns information about existing frameworks
351 build a HTML select with the following code :
353 =head3 in PERL SCRIPT
355 my $frameworks = frameworks();
357 foreach my $thisframework (keys %$frameworks) {
358 my $selected = 1 if $thisframework eq $frameworkcode;
359 my %row =(value => $thisframework,
360 selected => $selected,
361 description => $frameworks->{$thisframework}->{'frameworktext'},
363 push @frameworksloop, \%row;
365 $template->param(frameworkloop => \@frameworksloop);
369 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
370 <select name="frameworkcode">
371 <option value="">Default</option>
372 <!-- TMPL_LOOP name="frameworkloop" -->
373 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
376 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
377 <input type="submit" value="OK" class="button">
384 # returns a reference to a hash of references to branches...
386 my $dbh = C4::Context->dbh;
387 my $sth=$dbh->prepare("select * from biblio_framework");
389 while (my $IT=$sth->fetchrow_hashref) {
390 $itemtypes{$IT->{'frameworkcode'}}=$IT;
392 return (\%itemtypes);
394 =head2 getframeworkinfo
396 $frameworkinfo = &getframeworkinfo($frameworkcode);
398 Returns information about an frameworkcode.
402 sub getframeworkinfo {
403 my ($frameworkcode) = @_;
404 my $dbh = C4::Context->dbh;
405 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
406 $sth->execute($frameworkcode);
407 my $res = $sth->fetchrow_hashref;
412 =head2 getitemtypeinfo
414 $itemtype = &getitemtype($itemtype);
416 Returns information about an itemtype.
420 sub getitemtypeinfo {
422 my $dbh = C4::Context->dbh;
423 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
424 $sth->execute($itemtype);
425 my $res = $sth->fetchrow_hashref;
431 $printers = &getprinters($env);
432 @queues = keys %$printers;
434 Returns information about existing printer queues.
438 C<$printers> is a reference-to-hash whose keys are the print queues
439 defined in the printers table of the Koha database. The values are
440 references-to-hash, whose keys are the fields in the printers table.
447 my $dbh = C4::Context->dbh;
448 my $sth=$dbh->prepare("select * from printers");
450 while (my $printer=$sth->fetchrow_hashref) {
451 $printers{$printer->{'printqueue'}}=$printer;
456 my($query, $branches) = @_; # get branch for this query from branches
457 my $branch = $query->param('branch');
458 ($branch) || ($branch = $query->cookie('branch'));
459 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
463 sub getprinter ($$) {
464 my($query, $printers) = @_; # get printer for this query from printers
465 my $printer = $query->param('printer');
466 ($printer) || ($printer = $query->cookie('printer'));
467 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
471 =item getalllanguages
473 (@languages) = &getalllanguages($type);
474 (@languages) = &getalllanguages($type,$theme);
476 Returns an array of all available languages.
480 sub getalllanguages {
485 if ($type eq 'opac') {
486 $htdocs=C4::Context->config('opachtdocs');
487 if ($theme and -d "$htdocs/$theme") {
488 opendir D, "$htdocs/$theme";
489 foreach my $language (readdir D) {
490 next if $language=~/^\./;
491 next if $language eq 'all';
492 push @languages, $language;
494 return sort @languages;
497 foreach my $theme (getallthemes('opac')) {
498 opendir D, "$htdocs/$theme";
499 foreach my $language (readdir D) {
500 next if $language=~/^\./;
501 next if $language eq 'all';
502 $lang->{$language}=1;
505 @languages=keys %$lang;
506 return sort @languages;
508 } elsif ($type eq 'intranet') {
509 $htdocs=C4::Context->config('intrahtdocs');
510 if ($theme and -d "$htdocs/$theme") {
511 opendir D, "$htdocs/$theme";
512 foreach my $language (readdir D) {
513 next if $language=~/^\./;
514 next if $language eq 'all';
515 push @languages, $language;
517 return sort @languages;
520 foreach my $theme (getallthemes('opac')) {
521 opendir D, "$htdocs/$theme";
522 foreach my $language (readdir D) {
523 next if $language=~/^\./;
524 next if $language eq 'all';
525 $lang->{$language}=1;
528 @languages=keys %$lang;
529 return sort @languages;
533 my $htdocs=C4::Context->config('intrahtdocs');
534 foreach my $theme (getallthemes('intranet')) {
535 opendir D, "$htdocs/$theme";
536 foreach my $language (readdir D) {
537 next if $language=~/^\./;
538 next if $language eq 'all';
539 $lang->{$language}=1;
542 my $htdocs=C4::Context->config('opachtdocs');
543 foreach my $theme (getallthemes('opac')) {
544 opendir D, "$htdocs/$theme";
545 foreach my $language (readdir D) {
546 next if $language=~/^\./;
547 next if $language eq 'all';
548 $lang->{$language}=1;
551 @languages=keys %$lang;
552 return sort @languages;
558 (@themes) = &getallthemes('opac');
559 (@themes) = &getallthemes('intranet');
561 Returns an array of all available themes.
569 if ($type eq 'intranet') {
570 $htdocs=C4::Context->config('intrahtdocs');
572 $htdocs=C4::Context->config('opachtdocs');
574 opendir D, "$htdocs";
575 my @dirlist=readdir D;
576 foreach my $directory (@dirlist) {
577 -d "$htdocs/$directory/en" and push @themes, $directory;