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.
55 &borrowercategories &getborrowercategory
57 &subfield_is_koha_internal_p
58 &getbranches &getbranch &getbranchdetail
59 &getprinters &getprinter
60 &getitemtypes &getitemtypeinfo
61 &getframeworks &getframeworkinfo
62 &getauthtypes &getauthtype
63 &getallthemes &getalllanguages
71 # removed slashifyDate => useless
75 $ethn_name = &fixEthnicity($ethn_code);
77 Takes an ethnicity code (e.g., "european" or "pi") and returns the
78 corresponding descriptive name from the C<ethnicity> table in the
79 Koha database ("European" or "Pacific Islander").
86 my $ethnicity = shift;
87 my $dbh = C4::Context->dbh;
88 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
89 $sth->execute($ethnicity);
90 my $data=$sth->fetchrow_hashref;
92 return $data->{'name'};
95 =head2 borrowercategories
97 ($codes_arrayref, $labels_hashref) = &borrowercategories();
99 Looks up the different types of borrowers in the database. Returns two
100 elements: a reference-to-array, which lists the borrower category
101 codes, and a reference-to-hash, which maps the borrower category codes
102 to category descriptions.
107 sub borrowercategories {
108 my $dbh = C4::Context->dbh;
109 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
113 while (my $data=$sth->fetchrow_hashref){
114 push @codes,$data->{'categorycode'};
115 $labels{$data->{'categorycode'}}=$data->{'description'};
118 return(\@codes,\%labels);
121 =item getborrowercategory
123 $description = &getborrowercategory($categorycode);
125 Given the borrower's category code, the function returns the corresponding
126 description for a comprehensive information display.
130 sub getborrowercategory
133 my $dbh = C4::Context->dbh;
134 my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
135 $sth->execute($catcode);
136 my $description = $sth->fetchrow();
139 } # sub getborrowercategory
142 =head2 ethnicitycategories
144 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
146 Looks up the different ethnic types in the database. Returns two
147 elements: a reference-to-array, which lists the ethnicity codes, and a
148 reference-to-hash, which maps the ethnicity codes to ethnicity
154 sub ethnicitycategories {
155 my $dbh = C4::Context->dbh;
156 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
160 while (my $data=$sth->fetchrow_hashref){
161 push @codes,$data->{'code'};
162 $labels{$data->{'code'}}=$data->{'name'};
165 return(\@codes,\%labels);
168 # FIXME.. this should be moved to a MARC-specific module
169 sub subfield_is_koha_internal_p ($) {
172 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
173 # But real MARC subfields are always single-character
174 # so it really is safer just to check the length
176 return length $subfield != 1;
181 $branches = &getbranches();
182 returns informations about branches.
183 Create a branch selector with the following code
184 Is branchIndependant sensitive
185 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
187 =head3 in PERL SCRIPT
189 my $branches = getbranches;
191 foreach my $thisbranch (sort keys %$branches) {
192 my $selected = 1 if $thisbranch eq $branch;
193 my %row =(value => $thisbranch,
194 selected => $selected,
195 branchname => $branches->{$thisbranch}->{'branchname'},
197 push @branchloop, \%row;
202 <select name="branch">
203 <option value="">Default</option>
204 <!-- TMPL_LOOP name="branchloop" -->
205 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
212 # returns a reference to a hash of references to branches...
214 my $dbh = C4::Context->dbh;
216 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
217 my $strsth ="Select * from branches ";
218 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
219 $strsth.= " order by branchname";
220 $sth=$dbh->prepare($strsth);
222 $sth = $dbh->prepare("Select * from branches order by branchname");
225 while (my $branch=$sth->fetchrow_hashref) {
226 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
227 $nsth->execute($branch->{'branchcode'});
228 while (my ($cat) = $nsth->fetchrow_array) {
229 # FIXME - This seems wrong. It ought to be
230 # $branch->{categorycodes}{$cat} = 1;
231 # otherwise, there's a namespace collision if there's a
232 # category with the same name as a field in the 'branches'
233 # table (i.e., don't create a category called "issuing").
234 # In addition, the current structure doesn't really allow
235 # you to list the categories that a branch belongs to:
236 # you'd have to list keys %$branch, and remove those keys
237 # that aren't fields in the "branches" table.
240 $branches{$branch->{'branchcode'}}=$branch;
245 =head2 getallbranches
247 $branches = &getallbranches();
248 returns informations about ALL branches.
249 Create a branch selector with the following code
250 IndependantBranches Insensitive...
252 =head3 in PERL SCRIPT
254 my $branches = getallbranches;
256 foreach my $thisbranch (keys %$branches) {
257 my $selected = 1 if $thisbranch eq $branch;
258 my %row =(value => $thisbranch,
259 selected => $selected,
260 branchname => $branches->{$thisbranch}->{'branchname'},
262 push @branchloop, \%row;
267 <select name="branch">
268 <option value="">Default</option>
269 <!-- TMPL_LOOP name="branchloop" -->
270 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
277 # returns a reference to a hash of references to ALL branches...
279 my $dbh = C4::Context->dbh;
281 $sth = $dbh->prepare("Select * from branches order by branchname");
283 while (my $branch=$sth->fetchrow_hashref) {
284 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
285 $nsth->execute($branch->{'branchcode'});
286 while (my ($cat) = $nsth->fetchrow_array) {
287 # FIXME - This seems wrong. It ought to be
288 # $branch->{categorycodes}{$cat} = 1;
289 # otherwise, there's a namespace collision if there's a
290 # category with the same name as a field in the 'branches'
291 # table (i.e., don't create a category called "issuing").
292 # In addition, the current structure doesn't really allow
293 # you to list the categories that a branch belongs to:
294 # you'd have to list keys %$branch, and remove those keys
295 # that aren't fields in the "branches" table.
298 $branches{$branch->{'branchcode'}}=$branch;
305 $itemtypes = &getitemtypes();
307 Returns information about existing itemtypes.
309 build a HTML select with the following code :
311 =head3 in PERL SCRIPT
313 my $itemtypes = getitemtypes;
315 foreach my $thisitemtype (sort keys %$itemtypes) {
316 my $selected = 1 if $thisitemtype eq $itemtype;
317 my %row =(value => $thisitemtype,
318 selected => $selected,
319 description => $itemtypes->{$thisitemtype}->{'description'},
321 push @itemtypesloop, \%row;
323 $template->param(itemtypeloop => \@itemtypesloop);
327 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
328 <select name="itemtype">
329 <option value="">Default</option>
330 <!-- TMPL_LOOP name="itemtypeloop" -->
331 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
334 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
335 <input type="submit" value="OK" class="button">
342 # returns a reference to a hash of references to branches...
344 my $dbh = C4::Context->dbh;
345 my $sth=$dbh->prepare("select * from itemtypes");
347 while (my $IT=$sth->fetchrow_hashref) {
348 $itemtypes{$IT->{'itemtype'}}=$IT;
350 return (\%itemtypes);
355 $authtypes = &getauthtypes();
357 Returns information about existing authtypes.
359 build a HTML select with the following code :
361 =head3 in PERL SCRIPT
363 my $authtypes = getauthtypes;
365 foreach my $thisauthtype (keys %$authtypes) {
366 my $selected = 1 if $thisauthtype eq $authtype;
367 my %row =(value => $thisauthtype,
368 selected => $selected,
369 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
371 push @authtypesloop, \%row;
373 $template->param(itemtypeloop => \@itemtypesloop);
377 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
378 <select name="authtype">
379 <!-- TMPL_LOOP name="authtypeloop" -->
380 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
383 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
384 <input type="submit" value="OK" class="button">
391 # returns a reference to a hash of references to authtypes...
393 my $dbh = C4::Context->dbh;
394 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
396 while (my $IT=$sth->fetchrow_hashref) {
397 $authtypes{$IT->{'authtypecode'}}=$IT;
399 return (\%authtypes);
403 my ($authtypecode) = @_;
404 # returns a reference to a hash of references to authtypes...
406 my $dbh = C4::Context->dbh;
407 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
408 $sth->execute($authtypecode);
409 my $res=$sth->fetchrow_hashref;
415 $frameworks = &getframework();
417 Returns information about existing frameworks
419 build a HTML select with the following code :
421 =head3 in PERL SCRIPT
423 my $frameworks = frameworks();
425 foreach my $thisframework (keys %$frameworks) {
426 my $selected = 1 if $thisframework eq $frameworkcode;
427 my %row =(value => $thisframework,
428 selected => $selected,
429 description => $frameworks->{$thisframework}->{'frameworktext'},
431 push @frameworksloop, \%row;
433 $template->param(frameworkloop => \@frameworksloop);
437 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
438 <select name="frameworkcode">
439 <option value="">Default</option>
440 <!-- TMPL_LOOP name="frameworkloop" -->
441 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
444 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
445 <input type="submit" value="OK" class="button">
452 # returns a reference to a hash of references to branches...
454 my $dbh = C4::Context->dbh;
455 my $sth=$dbh->prepare("select * from biblio_framework");
457 while (my $IT=$sth->fetchrow_hashref) {
458 $itemtypes{$IT->{'frameworkcode'}}=$IT;
460 return (\%itemtypes);
462 =head2 getframeworkinfo
464 $frameworkinfo = &getframeworkinfo($frameworkcode);
466 Returns information about an frameworkcode.
470 sub getframeworkinfo {
471 my ($frameworkcode) = @_;
472 my $dbh = C4::Context->dbh;
473 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
474 $sth->execute($frameworkcode);
475 my $res = $sth->fetchrow_hashref;
480 =head2 getitemtypeinfo
482 $itemtype = &getitemtype($itemtype);
484 Returns information about an itemtype.
488 sub getitemtypeinfo {
490 my $dbh = C4::Context->dbh;
491 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
492 $sth->execute($itemtype);
493 my $res = $sth->fetchrow_hashref;
499 $printers = &getprinters($env);
500 @queues = keys %$printers;
502 Returns information about existing printer queues.
506 C<$printers> is a reference-to-hash whose keys are the print queues
507 defined in the printers table of the Koha database. The values are
508 references-to-hash, whose keys are the fields in the printers table.
515 my $dbh = C4::Context->dbh;
516 my $sth=$dbh->prepare("select * from printers");
518 while (my $printer=$sth->fetchrow_hashref) {
519 $printers{$printer->{'printqueue'}}=$printer;
525 my($query, $branches) = @_; # get branch for this query from branches
526 my $branch = $query->param('branch');
527 ($branch) || ($branch = $query->cookie('branch'));
528 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
532 =item getbranchdetail
534 $branchname = &getbranchdetail($branchcode);
536 Given the branch code, the function returns the corresponding
537 branch name for a comprehensive information display
543 my ($branchcode) = @_;
544 my $dbh = C4::Context->dbh;
545 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
546 $sth->execute($branchcode);
547 my $branchname = $sth->fetchrow_hashref();
550 } # sub getbranchname
553 sub getprinter ($$) {
554 my($query, $printers) = @_; # get printer for this query from printers
555 my $printer = $query->param('printer');
556 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
557 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
561 =item getalllanguages
563 (@languages) = &getalllanguages($type);
564 (@languages) = &getalllanguages($type,$theme);
566 Returns an array of all available languages.
570 sub getalllanguages {
575 if ($type eq 'opac') {
576 $htdocs=C4::Context->config('opachtdocs');
577 if ($theme and -d "$htdocs/$theme") {
578 opendir D, "$htdocs/$theme";
579 foreach my $language (readdir D) {
580 next if $language=~/^\./;
581 next if $language eq 'all';
582 next if $language=~ /png$/;
583 next if $language=~ /css$/;
584 push @languages, $language;
586 return sort @languages;
589 foreach my $theme (getallthemes('opac')) {
590 opendir D, "$htdocs/$theme";
591 foreach my $language (readdir D) {
592 next if $language=~/^\./;
593 next if $language eq 'all';
594 next if $language=~ /png$/;
595 next if $language=~ /css$/;
596 $lang->{$language}=1;
599 @languages=keys %$lang;
600 return sort @languages;
602 } elsif ($type eq 'intranet') {
603 $htdocs=C4::Context->config('intrahtdocs');
604 if ($theme and -d "$htdocs/$theme") {
605 opendir D, "$htdocs/$theme";
606 foreach my $language (readdir D) {
607 next if $language=~/^\./;
608 next if $language eq 'all';
609 next if $language=~ /png$/;
610 next if $language=~ /css$/;
611 push @languages, $language;
613 return sort @languages;
616 foreach my $theme (getallthemes('opac')) {
617 opendir D, "$htdocs/$theme";
618 foreach my $language (readdir D) {
619 next if $language=~/^\./;
620 next if $language eq 'all';
621 next if $language=~ /png$/;
622 next if $language=~ /css$/;
623 $lang->{$language}=1;
626 @languages=keys %$lang;
627 return sort @languages;
631 my $htdocs=C4::Context->config('intrahtdocs');
632 foreach my $theme (getallthemes('intranet')) {
633 opendir D, "$htdocs/$theme";
634 foreach my $language (readdir D) {
635 next if $language=~/^\./;
636 next if $language eq 'all';
637 next if $language=~ /png$/;
638 next if $language=~ /css$/;
639 $lang->{$language}=1;
642 $htdocs=C4::Context->config('opachtdocs');
643 foreach my $theme (getallthemes('opac')) {
644 opendir D, "$htdocs/$theme";
645 foreach my $language (readdir D) {
646 next if $language=~/^\./;
647 next if $language eq 'all';
648 next if $language=~ /png$/;
649 next if $language=~ /css$/;
650 $lang->{$language}=1;
653 @languages=keys %$lang;
654 return sort @languages;
660 (@themes) = &getallthemes('opac');
661 (@themes) = &getallthemes('intranet');
663 Returns an array of all available themes.
671 if ($type eq 'intranet') {
672 $htdocs=C4::Context->config('intrahtdocs');
674 $htdocs=C4::Context->config('opachtdocs');
676 opendir D, "$htdocs";
677 my @dirlist=readdir D;
678 foreach my $directory (@dirlist) {
679 -d "$htdocs/$directory/en" and push @themes, $directory;