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
68 getitemtypeimagesrcfromurl
75 # removed slashifyDate => useless
79 $ethn_name = &fixEthnicity($ethn_code);
81 Takes an ethnicity code (e.g., "european" or "pi") and returns the
82 corresponding descriptive name from the C<ethnicity> table in the
83 Koha database ("European" or "Pacific Islander").
90 my $ethnicity = shift;
91 my $dbh = C4::Context->dbh;
92 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
93 $sth->execute($ethnicity);
94 my $data=$sth->fetchrow_hashref;
96 return $data->{'name'};
99 =head2 borrowercategories
101 ($codes_arrayref, $labels_hashref) = &borrowercategories();
103 Looks up the different types of borrowers in the database. Returns two
104 elements: a reference-to-array, which lists the borrower category
105 codes, and a reference-to-hash, which maps the borrower category codes
106 to category descriptions.
111 sub borrowercategories {
112 my $dbh = C4::Context->dbh;
113 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
117 while (my $data=$sth->fetchrow_hashref){
118 push @codes,$data->{'categorycode'};
119 $labels{$data->{'categorycode'}}=$data->{'description'};
122 return(\@codes,\%labels);
125 =item getborrowercategory
127 $description = &getborrowercategory($categorycode);
129 Given the borrower's category code, the function returns the corresponding
130 description for a comprehensive information display.
134 sub getborrowercategory
137 my $dbh = C4::Context->dbh;
138 my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
139 $sth->execute($catcode);
140 my $description = $sth->fetchrow();
143 } # sub getborrowercategory
146 =head2 ethnicitycategories
148 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
150 Looks up the different ethnic types in the database. Returns two
151 elements: a reference-to-array, which lists the ethnicity codes, and a
152 reference-to-hash, which maps the ethnicity codes to ethnicity
158 sub ethnicitycategories {
159 my $dbh = C4::Context->dbh;
160 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
164 while (my $data=$sth->fetchrow_hashref){
165 push @codes,$data->{'code'};
166 $labels{$data->{'code'}}=$data->{'name'};
169 return(\@codes,\%labels);
172 # FIXME.. this should be moved to a MARC-specific module
173 sub subfield_is_koha_internal_p ($) {
176 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
177 # But real MARC subfields are always single-character
178 # so it really is safer just to check the length
180 return length $subfield != 1;
185 $branches = &getbranches();
186 returns informations about branches.
187 Create a branch selector with the following code
188 Is branchIndependant sensitive
189 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
191 =head3 in PERL SCRIPT
193 my $branches = getbranches;
195 foreach my $thisbranch (sort keys %$branches) {
196 my $selected = 1 if $thisbranch eq $branch;
197 my %row =(value => $thisbranch,
198 selected => $selected,
199 branchname => $branches->{$thisbranch}->{'branchname'},
201 push @branchloop, \%row;
206 <select name="branch">
207 <option value="">Default</option>
208 <!-- TMPL_LOOP name="branchloop" -->
209 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
216 # returns a reference to a hash of references to branches...
218 my $dbh = C4::Context->dbh;
220 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
221 my $strsth ="Select * from branches ";
222 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
223 $strsth.= " order by branchname";
224 $sth=$dbh->prepare($strsth);
226 $sth = $dbh->prepare("Select * from branches order by branchname");
229 while (my $branch=$sth->fetchrow_hashref) {
230 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
231 $nsth->execute($branch->{'branchcode'});
232 while (my ($cat) = $nsth->fetchrow_array) {
233 # FIXME - This seems wrong. It ought to be
234 # $branch->{categorycodes}{$cat} = 1;
235 # otherwise, there's a namespace collision if there's a
236 # category with the same name as a field in the 'branches'
237 # table (i.e., don't create a category called "issuing").
238 # In addition, the current structure doesn't really allow
239 # you to list the categories that a branch belongs to:
240 # you'd have to list keys %$branch, and remove those keys
241 # that aren't fields in the "branches" table.
244 $branches{$branch->{'branchcode'}}=$branch;
249 =head2 getallbranches
251 $branches = &getallbranches();
252 returns informations about ALL branches.
253 Create a branch selector with the following code
254 IndependantBranches Insensitive...
256 =head3 in PERL SCRIPT
258 my $branches = getallbranches;
260 foreach my $thisbranch (keys %$branches) {
261 my $selected = 1 if $thisbranch eq $branch;
262 my %row =(value => $thisbranch,
263 selected => $selected,
264 branchname => $branches->{$thisbranch}->{'branchname'},
266 push @branchloop, \%row;
271 <select name="branch">
272 <option value="">Default</option>
273 <!-- TMPL_LOOP name="branchloop" -->
274 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
281 # returns a reference to a hash of references to ALL branches...
283 my $dbh = C4::Context->dbh;
285 $sth = $dbh->prepare("Select * from branches order by branchname");
287 while (my $branch=$sth->fetchrow_hashref) {
288 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
289 $nsth->execute($branch->{'branchcode'});
290 while (my ($cat) = $nsth->fetchrow_array) {
291 # FIXME - This seems wrong. It ought to be
292 # $branch->{categorycodes}{$cat} = 1;
293 # otherwise, there's a namespace collision if there's a
294 # category with the same name as a field in the 'branches'
295 # table (i.e., don't create a category called "issuing").
296 # In addition, the current structure doesn't really allow
297 # you to list the categories that a branch belongs to:
298 # you'd have to list keys %$branch, and remove those keys
299 # that aren't fields in the "branches" table.
302 $branches{$branch->{'branchcode'}}=$branch;
309 $letters = &getletters($category);
310 returns informations about letters.
311 if needed, $category filters for letters given category
312 Create a letter selector with the following code
314 =head3 in PERL SCRIPT
316 my $letters = getletters($cat);
318 foreach my $thisletter (keys %$letters) {
319 my $selected = 1 if $thisletter eq $letter;
320 my %row =(value => $thisletter,
321 selected => $selected,
322 lettername => $letters->{$thisletter},
324 push @letterloop, \%row;
329 <select name="letter">
330 <option value="">Default</option>
331 <!-- TMPL_LOOP name="letterloop" -->
332 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
339 # returns a reference to a hash of references to ALL letters...
342 my $dbh = C4::Context->dbh;
345 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
347 $sth = $dbh->prepare("Select * from letter order by name");
351 while (my $letter=$sth->fetchrow_hashref) {
352 $letters{$letter->{'code'}}=$letter->{'name'};
355 return ($count,\%letters);
360 $itemtypes = &getitemtypes();
362 Returns information about existing itemtypes.
364 build a HTML select with the following code :
366 =head3 in PERL SCRIPT
368 my $itemtypes = getitemtypes;
370 foreach my $thisitemtype (sort keys %$itemtypes) {
371 my $selected = 1 if $thisitemtype eq $itemtype;
372 my %row =(value => $thisitemtype,
373 selected => $selected,
374 description => $itemtypes->{$thisitemtype}->{'description'},
376 push @itemtypesloop, \%row;
378 $template->param(itemtypeloop => \@itemtypesloop);
382 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
383 <select name="itemtype">
384 <option value="">Default</option>
385 <!-- TMPL_LOOP name="itemtypeloop" -->
386 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
389 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
390 <input type="submit" value="OK" class="button">
397 # returns a reference to a hash of references to branches...
399 my $dbh = C4::Context->dbh;
400 my $sth=$dbh->prepare("select * from itemtypes");
402 while (my $IT=$sth->fetchrow_hashref) {
403 $itemtypes{$IT->{'itemtype'}}=$IT;
405 return (\%itemtypes);
410 $authtypes = &getauthtypes();
412 Returns information about existing authtypes.
414 build a HTML select with the following code :
416 =head3 in PERL SCRIPT
418 my $authtypes = getauthtypes;
420 foreach my $thisauthtype (keys %$authtypes) {
421 my $selected = 1 if $thisauthtype eq $authtype;
422 my %row =(value => $thisauthtype,
423 selected => $selected,
424 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
426 push @authtypesloop, \%row;
428 $template->param(itemtypeloop => \@itemtypesloop);
432 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
433 <select name="authtype">
434 <!-- TMPL_LOOP name="authtypeloop" -->
435 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
438 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
439 <input type="submit" value="OK" class="button">
446 # returns a reference to a hash of references to authtypes...
448 my $dbh = C4::Context->dbh;
449 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
451 while (my $IT=$sth->fetchrow_hashref) {
452 $authtypes{$IT->{'authtypecode'}}=$IT;
454 return (\%authtypes);
458 my ($authtypecode) = @_;
459 # returns a reference to a hash of references to authtypes...
461 my $dbh = C4::Context->dbh;
462 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
463 $sth->execute($authtypecode);
464 my $res=$sth->fetchrow_hashref;
470 $frameworks = &getframework();
472 Returns information about existing frameworks
474 build a HTML select with the following code :
476 =head3 in PERL SCRIPT
478 my $frameworks = frameworks();
480 foreach my $thisframework (keys %$frameworks) {
481 my $selected = 1 if $thisframework eq $frameworkcode;
482 my %row =(value => $thisframework,
483 selected => $selected,
484 description => $frameworks->{$thisframework}->{'frameworktext'},
486 push @frameworksloop, \%row;
488 $template->param(frameworkloop => \@frameworksloop);
492 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
493 <select name="frameworkcode">
494 <option value="">Default</option>
495 <!-- TMPL_LOOP name="frameworkloop" -->
496 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
499 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
500 <input type="submit" value="OK" class="button">
507 # returns a reference to a hash of references to branches...
509 my $dbh = C4::Context->dbh;
510 my $sth=$dbh->prepare("select * from biblio_framework");
512 while (my $IT=$sth->fetchrow_hashref) {
513 $itemtypes{$IT->{'frameworkcode'}}=$IT;
515 return (\%itemtypes);
517 =head2 getframeworkinfo
519 $frameworkinfo = &getframeworkinfo($frameworkcode);
521 Returns information about an frameworkcode.
525 sub getframeworkinfo {
526 my ($frameworkcode) = @_;
527 my $dbh = C4::Context->dbh;
528 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
529 $sth->execute($frameworkcode);
530 my $res = $sth->fetchrow_hashref;
535 =head2 getitemtypeinfo
537 $itemtype = &getitemtype($itemtype);
539 Returns information about an itemtype.
543 sub getitemtypeinfo {
545 my $dbh = C4::Context->dbh;
546 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
547 $sth->execute($itemtype);
548 my $res = $sth->fetchrow_hashref;
550 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
555 sub getitemtypeimagesrcfromurl {
558 if (defined $imageurl and $imageurl !~ m/^http/) {
560 getitemtypeimagesrc()
568 sub getitemtypeimagedir {
570 C4::Context->intrahtdocs
571 .'/'.C4::Context->preference('template')
576 sub getitemtypeimagesrc {
579 .'/'.C4::Context->preference('template')
586 $printers = &getprinters($env);
587 @queues = keys %$printers;
589 Returns information about existing printer queues.
593 C<$printers> is a reference-to-hash whose keys are the print queues
594 defined in the printers table of the Koha database. The values are
595 references-to-hash, whose keys are the fields in the printers table.
602 my $dbh = C4::Context->dbh;
603 my $sth=$dbh->prepare("select * from printers");
605 while (my $printer=$sth->fetchrow_hashref) {
606 $printers{$printer->{'printqueue'}}=$printer;
612 my($query, $branches) = @_; # get branch for this query from branches
613 my $branch = $query->param('branch');
614 ($branch) || ($branch = $query->cookie('branch'));
615 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
619 =item getbranchdetail
621 $branchname = &getbranchdetail($branchcode);
623 Given the branch code, the function returns the corresponding
624 branch name for a comprehensive information display
630 my ($branchcode) = @_;
631 my $dbh = C4::Context->dbh;
632 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
633 $sth->execute($branchcode);
634 my $branchname = $sth->fetchrow_hashref();
637 } # sub getbranchname
640 sub getprinter ($$) {
641 my($query, $printers) = @_; # get printer for this query from printers
642 my $printer = $query->param('printer');
643 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
644 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
648 =item getalllanguages
650 (@languages) = &getalllanguages($type);
651 (@languages) = &getalllanguages($type,$theme);
653 Returns an array of all available languages.
657 sub getalllanguages {
662 if ($type eq 'opac') {
663 $htdocs=C4::Context->config('opachtdocs');
664 if ($theme and -d "$htdocs/$theme") {
665 opendir D, "$htdocs/$theme";
666 foreach my $language (readdir D) {
667 next if $language=~/^\./;
668 next if $language eq 'all';
669 next if $language=~ /png$/;
670 next if $language=~ /css$/;
671 push @languages, $language;
673 return sort @languages;
676 foreach my $theme (getallthemes('opac')) {
677 opendir D, "$htdocs/$theme";
678 foreach my $language (readdir D) {
679 next if $language=~/^\./;
680 next if $language eq 'all';
681 next if $language=~ /png$/;
682 next if $language=~ /css$/;
683 $lang->{$language}=1;
686 @languages=keys %$lang;
687 return sort @languages;
689 } elsif ($type eq 'intranet') {
690 $htdocs=C4::Context->config('intrahtdocs');
691 if ($theme and -d "$htdocs/$theme") {
692 opendir D, "$htdocs/$theme";
693 foreach my $language (readdir D) {
694 next if $language=~/^\./;
695 next if $language eq 'all';
696 next if $language=~ /png$/;
697 next if $language=~ /css$/;
698 push @languages, $language;
700 return sort @languages;
703 foreach my $theme (getallthemes('opac')) {
704 opendir D, "$htdocs/$theme";
705 foreach my $language (readdir D) {
706 next if $language=~/^\./;
707 next if $language eq 'all';
708 next if $language=~ /png$/;
709 next if $language=~ /css$/;
710 $lang->{$language}=1;
713 @languages=keys %$lang;
714 return sort @languages;
718 my $htdocs=C4::Context->config('intrahtdocs');
719 foreach my $theme (getallthemes('intranet')) {
720 opendir D, "$htdocs/$theme";
721 foreach my $language (readdir D) {
722 next if $language=~/^\./;
723 next if $language eq 'all';
724 next if $language=~ /png$/;
725 next if $language=~ /css$/;
726 $lang->{$language}=1;
729 $htdocs=C4::Context->config('opachtdocs');
730 foreach my $theme (getallthemes('opac')) {
731 opendir D, "$htdocs/$theme";
732 foreach my $language (readdir D) {
733 next if $language=~/^\./;
734 next if $language eq 'all';
735 next if $language=~ /png$/;
736 next if $language=~ /css$/;
737 $lang->{$language}=1;
740 @languages=keys %$lang;
741 return sort @languages;
747 (@themes) = &getallthemes('opac');
748 (@themes) = &getallthemes('intranet');
750 Returns an array of all available themes.
758 if ($type eq 'intranet') {
759 $htdocs=C4::Context->config('intrahtdocs');
761 $htdocs=C4::Context->config('opachtdocs');
763 opendir D, "$htdocs";
764 my @dirlist=readdir D;
765 foreach my $directory (@dirlist) {
766 -d "$htdocs/$directory/en" and push @themes, $directory;
773 Returns the number of pages to display in a pagination bar, given the number
774 of items and the number of items per page.
779 my ($nb_items, $nb_items_per_page) = @_;
781 return int(($nb_items - 1) / $nb_items_per_page) + 1;