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
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 $letters = &getletters($category);
306 returns informations about letters.
307 if needed, $category filters for letters given category
308 Create a letter selector with the following code
310 =head3 in PERL SCRIPT
312 my $letters = getletters($cat);
314 foreach my $thisletter (keys %$letters) {
315 my $selected = 1 if $thisletter eq $letter;
316 my %row =(value => $thisletter,
317 selected => $selected,
318 lettername => $letters->{$thisletter},
320 push @letterloop, \%row;
325 <select name="letter">
326 <option value="">Default</option>
327 <!-- TMPL_LOOP name="letterloop" -->
328 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
335 # returns a reference to a hash of references to ALL letters...
338 my $dbh = C4::Context->dbh;
341 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
343 $sth = $dbh->prepare("Select * from letter order by name");
347 while (my $letter=$sth->fetchrow_hashref) {
348 $letters{$letter->{'code'}}=$letter->{'name'};
351 return ($count,\%letters);
356 $itemtypes = &getitemtypes();
358 Returns information about existing itemtypes.
360 build a HTML select with the following code :
362 =head3 in PERL SCRIPT
364 my $itemtypes = getitemtypes;
366 foreach my $thisitemtype (sort keys %$itemtypes) {
367 my $selected = 1 if $thisitemtype eq $itemtype;
368 my %row =(value => $thisitemtype,
369 selected => $selected,
370 description => $itemtypes->{$thisitemtype}->{'description'},
372 push @itemtypesloop, \%row;
374 $template->param(itemtypeloop => \@itemtypesloop);
378 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
379 <select name="itemtype">
380 <option value="">Default</option>
381 <!-- TMPL_LOOP name="itemtypeloop" -->
382 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
385 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
386 <input type="submit" value="OK" class="button">
393 # returns a reference to a hash of references to branches...
395 my $dbh = C4::Context->dbh;
396 my $sth=$dbh->prepare("select * from itemtypes");
398 while (my $IT=$sth->fetchrow_hashref) {
399 $itemtypes{$IT->{'itemtype'}}=$IT;
401 return (\%itemtypes);
406 $authtypes = &getauthtypes();
408 Returns information about existing authtypes.
410 build a HTML select with the following code :
412 =head3 in PERL SCRIPT
414 my $authtypes = getauthtypes;
416 foreach my $thisauthtype (keys %$authtypes) {
417 my $selected = 1 if $thisauthtype eq $authtype;
418 my %row =(value => $thisauthtype,
419 selected => $selected,
420 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
422 push @authtypesloop, \%row;
424 $template->param(itemtypeloop => \@itemtypesloop);
428 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
429 <select name="authtype">
430 <!-- TMPL_LOOP name="authtypeloop" -->
431 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
434 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
435 <input type="submit" value="OK" class="button">
442 # returns a reference to a hash of references to authtypes...
444 my $dbh = C4::Context->dbh;
445 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
447 while (my $IT=$sth->fetchrow_hashref) {
448 $authtypes{$IT->{'authtypecode'}}=$IT;
450 return (\%authtypes);
454 my ($authtypecode) = @_;
455 # returns a reference to a hash of references to authtypes...
457 my $dbh = C4::Context->dbh;
458 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
459 $sth->execute($authtypecode);
460 my $res=$sth->fetchrow_hashref;
466 $frameworks = &getframework();
468 Returns information about existing frameworks
470 build a HTML select with the following code :
472 =head3 in PERL SCRIPT
474 my $frameworks = frameworks();
476 foreach my $thisframework (keys %$frameworks) {
477 my $selected = 1 if $thisframework eq $frameworkcode;
478 my %row =(value => $thisframework,
479 selected => $selected,
480 description => $frameworks->{$thisframework}->{'frameworktext'},
482 push @frameworksloop, \%row;
484 $template->param(frameworkloop => \@frameworksloop);
488 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
489 <select name="frameworkcode">
490 <option value="">Default</option>
491 <!-- TMPL_LOOP name="frameworkloop" -->
492 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
495 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
496 <input type="submit" value="OK" class="button">
503 # returns a reference to a hash of references to branches...
505 my $dbh = C4::Context->dbh;
506 my $sth=$dbh->prepare("select * from biblio_framework");
508 while (my $IT=$sth->fetchrow_hashref) {
509 $itemtypes{$IT->{'frameworkcode'}}=$IT;
511 return (\%itemtypes);
513 =head2 getframeworkinfo
515 $frameworkinfo = &getframeworkinfo($frameworkcode);
517 Returns information about an frameworkcode.
521 sub getframeworkinfo {
522 my ($frameworkcode) = @_;
523 my $dbh = C4::Context->dbh;
524 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
525 $sth->execute($frameworkcode);
526 my $res = $sth->fetchrow_hashref;
531 =head2 getitemtypeinfo
533 $itemtype = &getitemtype($itemtype);
535 Returns information about an itemtype.
539 sub getitemtypeinfo {
541 my $dbh = C4::Context->dbh;
542 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
543 $sth->execute($itemtype);
544 my $res = $sth->fetchrow_hashref;
550 $printers = &getprinters($env);
551 @queues = keys %$printers;
553 Returns information about existing printer queues.
557 C<$printers> is a reference-to-hash whose keys are the print queues
558 defined in the printers table of the Koha database. The values are
559 references-to-hash, whose keys are the fields in the printers table.
566 my $dbh = C4::Context->dbh;
567 my $sth=$dbh->prepare("select * from printers");
569 while (my $printer=$sth->fetchrow_hashref) {
570 $printers{$printer->{'printqueue'}}=$printer;
576 my($query, $branches) = @_; # get branch for this query from branches
577 my $branch = $query->param('branch');
578 ($branch) || ($branch = $query->cookie('branch'));
579 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
583 =item getbranchdetail
585 $branchname = &getbranchdetail($branchcode);
587 Given the branch code, the function returns the corresponding
588 branch name for a comprehensive information display
594 my ($branchcode) = @_;
595 my $dbh = C4::Context->dbh;
596 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
597 $sth->execute($branchcode);
598 my $branchname = $sth->fetchrow_hashref();
601 } # sub getbranchname
604 sub getprinter ($$) {
605 my($query, $printers) = @_; # get printer for this query from printers
606 my $printer = $query->param('printer');
607 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
608 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
612 =item getalllanguages
614 (@languages) = &getalllanguages($type);
615 (@languages) = &getalllanguages($type,$theme);
617 Returns an array of all available languages.
621 sub getalllanguages {
626 if ($type eq 'opac') {
627 $htdocs=C4::Context->config('opachtdocs');
628 if ($theme and -d "$htdocs/$theme") {
629 opendir D, "$htdocs/$theme";
630 foreach my $language (readdir D) {
631 next if $language=~/^\./;
632 next if $language eq 'all';
633 next if $language=~ /png$/;
634 next if $language=~ /css$/;
635 push @languages, $language;
637 return sort @languages;
640 foreach my $theme (getallthemes('opac')) {
641 opendir D, "$htdocs/$theme";
642 foreach my $language (readdir D) {
643 next if $language=~/^\./;
644 next if $language eq 'all';
645 next if $language=~ /png$/;
646 next if $language=~ /css$/;
647 $lang->{$language}=1;
650 @languages=keys %$lang;
651 return sort @languages;
653 } elsif ($type eq 'intranet') {
654 $htdocs=C4::Context->config('intrahtdocs');
655 if ($theme and -d "$htdocs/$theme") {
656 opendir D, "$htdocs/$theme";
657 foreach my $language (readdir D) {
658 next if $language=~/^\./;
659 next if $language eq 'all';
660 next if $language=~ /png$/;
661 next if $language=~ /css$/;
662 push @languages, $language;
664 return sort @languages;
667 foreach my $theme (getallthemes('opac')) {
668 opendir D, "$htdocs/$theme";
669 foreach my $language (readdir D) {
670 next if $language=~/^\./;
671 next if $language eq 'all';
672 next if $language=~ /png$/;
673 next if $language=~ /css$/;
674 $lang->{$language}=1;
677 @languages=keys %$lang;
678 return sort @languages;
682 my $htdocs=C4::Context->config('intrahtdocs');
683 foreach my $theme (getallthemes('intranet')) {
684 opendir D, "$htdocs/$theme";
685 foreach my $language (readdir D) {
686 next if $language=~/^\./;
687 next if $language eq 'all';
688 next if $language=~ /png$/;
689 next if $language=~ /css$/;
690 $lang->{$language}=1;
693 $htdocs=C4::Context->config('opachtdocs');
694 foreach my $theme (getallthemes('opac')) {
695 opendir D, "$htdocs/$theme";
696 foreach my $language (readdir D) {
697 next if $language=~/^\./;
698 next if $language eq 'all';
699 next if $language=~ /png$/;
700 next if $language=~ /css$/;
701 $lang->{$language}=1;
704 @languages=keys %$lang;
705 return sort @languages;
711 (@themes) = &getallthemes('opac');
712 (@themes) = &getallthemes('intranet');
714 Returns an array of all available themes.
722 if ($type eq 'intranet') {
723 $htdocs=C4::Context->config('intrahtdocs');
725 $htdocs=C4::Context->config('opachtdocs');
727 opendir D, "$htdocs";
728 my @dirlist=readdir D;
729 foreach my $directory (@dirlist) {
730 -d "$htdocs/$directory/en" and push @themes, $directory;