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
64 &getallbranches &getletters
72 # removed slashifyDate => useless
76 $ethn_name = &fixEthnicity($ethn_code);
78 Takes an ethnicity code (e.g., "european" or "pi") and returns the
79 corresponding descriptive name from the C<ethnicity> table in the
80 Koha database ("European" or "Pacific Islander").
87 my $ethnicity = shift;
88 my $dbh = C4::Context->dbh;
89 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
90 $sth->execute($ethnicity);
91 my $data=$sth->fetchrow_hashref;
93 return $data->{'name'};
96 =head2 borrowercategories
98 ($codes_arrayref, $labels_hashref) = &borrowercategories();
100 Looks up the different types of borrowers in the database. Returns two
101 elements: a reference-to-array, which lists the borrower category
102 codes, and a reference-to-hash, which maps the borrower category codes
103 to category descriptions.
108 sub borrowercategories {
109 my $dbh = C4::Context->dbh;
110 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
114 while (my $data=$sth->fetchrow_hashref){
115 push @codes,$data->{'categorycode'};
116 $labels{$data->{'categorycode'}}=$data->{'description'};
119 return(\@codes,\%labels);
122 =item getborrowercategory
124 $description = &getborrowercategory($categorycode);
126 Given the borrower's category code, the function returns the corresponding
127 description for a comprehensive information display.
131 sub getborrowercategory
134 my $dbh = C4::Context->dbh;
135 my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
136 $sth->execute($catcode);
137 my $description = $sth->fetchrow();
140 } # sub getborrowercategory
143 =head2 ethnicitycategories
145 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
147 Looks up the different ethnic types in the database. Returns two
148 elements: a reference-to-array, which lists the ethnicity codes, and a
149 reference-to-hash, which maps the ethnicity codes to ethnicity
155 sub ethnicitycategories {
156 my $dbh = C4::Context->dbh;
157 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
161 while (my $data=$sth->fetchrow_hashref){
162 push @codes,$data->{'code'};
163 $labels{$data->{'code'}}=$data->{'name'};
166 return(\@codes,\%labels);
169 # FIXME.. this should be moved to a MARC-specific module
170 sub subfield_is_koha_internal_p ($) {
173 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
174 # But real MARC subfields are always single-character
175 # so it really is safer just to check the length
177 return length $subfield != 1;
182 $branches = &getbranches();
183 returns informations about branches.
184 Create a branch selector with the following code
185 Is branchIndependant sensitive
186 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
188 =head3 in PERL SCRIPT
190 my $branches = getbranches;
192 foreach my $thisbranch (sort keys %$branches) {
193 my $selected = 1 if $thisbranch eq $branch;
194 my %row =(value => $thisbranch,
195 selected => $selected,
196 branchname => $branches->{$thisbranch}->{'branchname'},
198 push @branchloop, \%row;
203 <select name="branch">
204 <option value="">Default</option>
205 <!-- TMPL_LOOP name="branchloop" -->
206 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
213 # returns a reference to a hash of references to branches...
215 my $dbh = C4::Context->dbh;
217 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
218 my $strsth ="Select * from branches ";
219 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
220 $strsth.= " order by branchname";
221 $sth=$dbh->prepare($strsth);
223 $sth = $dbh->prepare("Select * from branches order by branchname");
226 while (my $branch=$sth->fetchrow_hashref) {
227 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
228 $nsth->execute($branch->{'branchcode'});
229 while (my ($cat) = $nsth->fetchrow_array) {
230 # FIXME - This seems wrong. It ought to be
231 # $branch->{categorycodes}{$cat} = 1;
232 # otherwise, there's a namespace collision if there's a
233 # category with the same name as a field in the 'branches'
234 # table (i.e., don't create a category called "issuing").
235 # In addition, the current structure doesn't really allow
236 # you to list the categories that a branch belongs to:
237 # you'd have to list keys %$branch, and remove those keys
238 # that aren't fields in the "branches" table.
241 $branches{$branch->{'branchcode'}}=$branch;
246 =head2 getallbranches
248 $branches = &getallbranches();
249 returns informations about ALL branches.
250 Create a branch selector with the following code
251 IndependantBranches Insensitive...
253 =head3 in PERL SCRIPT
255 my $branches = getallbranches;
257 foreach my $thisbranch (keys %$branches) {
258 my $selected = 1 if $thisbranch eq $branch;
259 my %row =(value => $thisbranch,
260 selected => $selected,
261 branchname => $branches->{$thisbranch}->{'branchname'},
263 push @branchloop, \%row;
268 <select name="branch">
269 <option value="">Default</option>
270 <!-- TMPL_LOOP name="branchloop" -->
271 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
278 # returns a reference to a hash of references to ALL branches...
280 my $dbh = C4::Context->dbh;
282 $sth = $dbh->prepare("Select * from branches order by branchname");
284 while (my $branch=$sth->fetchrow_hashref) {
285 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
286 $nsth->execute($branch->{'branchcode'});
287 while (my ($cat) = $nsth->fetchrow_array) {
288 # FIXME - This seems wrong. It ought to be
289 # $branch->{categorycodes}{$cat} = 1;
290 # otherwise, there's a namespace collision if there's a
291 # category with the same name as a field in the 'branches'
292 # table (i.e., don't create a category called "issuing").
293 # In addition, the current structure doesn't really allow
294 # you to list the categories that a branch belongs to:
295 # you'd have to list keys %$branch, and remove those keys
296 # that aren't fields in the "branches" table.
299 $branches{$branch->{'branchcode'}}=$branch;
306 $letters = &getletters($category);
307 returns informations about letters.
308 if needed, $category filters for letters given category
309 Create a letter selector with the following code
311 =head3 in PERL SCRIPT
313 my $letters = getletters($cat);
315 foreach my $thisletter (keys %$letters) {
316 my $selected = 1 if $thisletter eq $letter;
317 my %row =(value => $thisletter,
318 selected => $selected,
319 lettername => $letters->{$thisletter},
321 push @letterloop, \%row;
326 <select name="letter">
327 <option value="">Default</option>
328 <!-- TMPL_LOOP name="letterloop" -->
329 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
336 # returns a reference to a hash of references to ALL letters...
339 my $dbh = C4::Context->dbh;
342 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
344 $sth = $dbh->prepare("Select * from letter order by name");
348 while (my $letter=$sth->fetchrow_hashref) {
349 $letters{$letter->{'code'}}=$letter->{'name'};
352 return ($count,\%letters);
357 $itemtypes = &getitemtypes();
359 Returns information about existing itemtypes.
361 build a HTML select with the following code :
363 =head3 in PERL SCRIPT
365 my $itemtypes = getitemtypes;
367 foreach my $thisitemtype (sort keys %$itemtypes) {
368 my $selected = 1 if $thisitemtype eq $itemtype;
369 my %row =(value => $thisitemtype,
370 selected => $selected,
371 description => $itemtypes->{$thisitemtype}->{'description'},
373 push @itemtypesloop, \%row;
375 $template->param(itemtypeloop => \@itemtypesloop);
379 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
380 <select name="itemtype">
381 <option value="">Default</option>
382 <!-- TMPL_LOOP name="itemtypeloop" -->
383 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
386 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
387 <input type="submit" value="OK" class="button">
394 # returns a reference to a hash of references to branches...
396 my $dbh = C4::Context->dbh;
397 my $sth=$dbh->prepare("select * from itemtypes");
399 while (my $IT=$sth->fetchrow_hashref) {
400 $itemtypes{$IT->{'itemtype'}}=$IT;
402 return (\%itemtypes);
407 $authtypes = &getauthtypes();
409 Returns information about existing authtypes.
411 build a HTML select with the following code :
413 =head3 in PERL SCRIPT
415 my $authtypes = getauthtypes;
417 foreach my $thisauthtype (keys %$authtypes) {
418 my $selected = 1 if $thisauthtype eq $authtype;
419 my %row =(value => $thisauthtype,
420 selected => $selected,
421 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
423 push @authtypesloop, \%row;
425 $template->param(itemtypeloop => \@itemtypesloop);
429 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
430 <select name="authtype">
431 <!-- TMPL_LOOP name="authtypeloop" -->
432 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
435 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
436 <input type="submit" value="OK" class="button">
443 # returns a reference to a hash of references to authtypes...
445 my $dbh = C4::Context->dbh;
446 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
448 while (my $IT=$sth->fetchrow_hashref) {
449 $authtypes{$IT->{'authtypecode'}}=$IT;
451 return (\%authtypes);
455 my ($authtypecode) = @_;
456 # returns a reference to a hash of references to authtypes...
458 my $dbh = C4::Context->dbh;
459 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
460 $sth->execute($authtypecode);
461 my $res=$sth->fetchrow_hashref;
467 $frameworks = &getframework();
469 Returns information about existing frameworks
471 build a HTML select with the following code :
473 =head3 in PERL SCRIPT
475 my $frameworks = frameworks();
477 foreach my $thisframework (keys %$frameworks) {
478 my $selected = 1 if $thisframework eq $frameworkcode;
479 my %row =(value => $thisframework,
480 selected => $selected,
481 description => $frameworks->{$thisframework}->{'frameworktext'},
483 push @frameworksloop, \%row;
485 $template->param(frameworkloop => \@frameworksloop);
489 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
490 <select name="frameworkcode">
491 <option value="">Default</option>
492 <!-- TMPL_LOOP name="frameworkloop" -->
493 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
496 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
497 <input type="submit" value="OK" class="button">
504 # returns a reference to a hash of references to branches...
506 my $dbh = C4::Context->dbh;
507 my $sth=$dbh->prepare("select * from biblio_framework");
509 while (my $IT=$sth->fetchrow_hashref) {
510 $itemtypes{$IT->{'frameworkcode'}}=$IT;
512 return (\%itemtypes);
514 =head2 getframeworkinfo
516 $frameworkinfo = &getframeworkinfo($frameworkcode);
518 Returns information about an frameworkcode.
522 sub getframeworkinfo {
523 my ($frameworkcode) = @_;
524 my $dbh = C4::Context->dbh;
525 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
526 $sth->execute($frameworkcode);
527 my $res = $sth->fetchrow_hashref;
532 =head2 getitemtypeinfo
534 $itemtype = &getitemtype($itemtype);
536 Returns information about an itemtype.
540 sub getitemtypeinfo {
542 my $dbh = C4::Context->dbh;
543 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
544 $sth->execute($itemtype);
545 my $res = $sth->fetchrow_hashref;
551 $printers = &getprinters($env);
552 @queues = keys %$printers;
554 Returns information about existing printer queues.
558 C<$printers> is a reference-to-hash whose keys are the print queues
559 defined in the printers table of the Koha database. The values are
560 references-to-hash, whose keys are the fields in the printers table.
567 my $dbh = C4::Context->dbh;
568 my $sth=$dbh->prepare("select * from printers");
570 while (my $printer=$sth->fetchrow_hashref) {
571 $printers{$printer->{'printqueue'}}=$printer;
577 my($query, $branches) = @_; # get branch for this query from branches
578 my $branch = $query->param('branch');
579 ($branch) || ($branch = $query->cookie('branch'));
580 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
584 =item getbranchdetail
586 $branchname = &getbranchdetail($branchcode);
588 Given the branch code, the function returns the corresponding
589 branch name for a comprehensive information display
595 my ($branchcode) = @_;
596 my $dbh = C4::Context->dbh;
597 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
598 $sth->execute($branchcode);
599 my $branchname = $sth->fetchrow_hashref();
602 } # sub getbranchname
605 sub getprinter ($$) {
606 my($query, $printers) = @_; # get printer for this query from printers
607 my $printer = $query->param('printer');
608 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
609 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
613 =item getalllanguages
615 (@languages) = &getalllanguages($type);
616 (@languages) = &getalllanguages($type,$theme);
618 Returns an array of all available languages.
622 sub getalllanguages {
627 if ($type eq 'opac') {
628 $htdocs=C4::Context->config('opachtdocs');
629 if ($theme and -d "$htdocs/$theme") {
630 opendir D, "$htdocs/$theme";
631 foreach my $language (readdir D) {
632 next if $language=~/^\./;
633 next if $language eq 'all';
634 next if $language=~ /png$/;
635 next if $language=~ /css$/;
636 push @languages, $language;
638 return sort @languages;
641 foreach my $theme (getallthemes('opac')) {
642 opendir D, "$htdocs/$theme";
643 foreach my $language (readdir D) {
644 next if $language=~/^\./;
645 next if $language eq 'all';
646 next if $language=~ /png$/;
647 next if $language=~ /css$/;
648 $lang->{$language}=1;
651 @languages=keys %$lang;
652 return sort @languages;
654 } elsif ($type eq 'intranet') {
655 $htdocs=C4::Context->config('intrahtdocs');
656 if ($theme and -d "$htdocs/$theme") {
657 opendir D, "$htdocs/$theme";
658 foreach my $language (readdir D) {
659 next if $language=~/^\./;
660 next if $language eq 'all';
661 next if $language=~ /png$/;
662 next if $language=~ /css$/;
663 push @languages, $language;
665 return sort @languages;
668 foreach my $theme (getallthemes('opac')) {
669 opendir D, "$htdocs/$theme";
670 foreach my $language (readdir D) {
671 next if $language=~/^\./;
672 next if $language eq 'all';
673 next if $language=~ /png$/;
674 next if $language=~ /css$/;
675 $lang->{$language}=1;
678 @languages=keys %$lang;
679 return sort @languages;
683 my $htdocs=C4::Context->config('intrahtdocs');
684 foreach my $theme (getallthemes('intranet')) {
685 opendir D, "$htdocs/$theme";
686 foreach my $language (readdir D) {
687 next if $language=~/^\./;
688 next if $language eq 'all';
689 next if $language=~ /png$/;
690 next if $language=~ /css$/;
691 $lang->{$language}=1;
694 $htdocs=C4::Context->config('opachtdocs');
695 foreach my $theme (getallthemes('opac')) {
696 opendir D, "$htdocs/$theme";
697 foreach my $language (readdir D) {
698 next if $language=~/^\./;
699 next if $language eq 'all';
700 next if $language=~ /png$/;
701 next if $language=~ /css$/;
702 $lang->{$language}=1;
705 @languages=keys %$lang;
706 return sort @languages;
712 (@themes) = &getallthemes('opac');
713 (@themes) = &getallthemes('intranet');
715 Returns an array of all available themes.
723 if ($type eq 'intranet') {
724 $htdocs=C4::Context->config('intrahtdocs');
726 $htdocs=C4::Context->config('opachtdocs');
728 opendir D, "$htdocs";
729 my @dirlist=readdir D;
730 foreach my $directory (@dirlist) {
731 -d "$htdocs/$directory/en" and push @themes, $directory;
738 Returns the number of pages to display in a pagination bar, given the number
739 of items and the number of items per page.
744 my ($nb_items, $nb_items_per_page) = @_;
746 return int(($nb_items - 1) / $nb_items_per_page) + 1;