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
27 use vars qw($VERSION @ISA @EXPORT);
29 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
33 C4::Koha - Perl Module containing convenience functions for Koha scripts
42 Koha.pm provides many functions for Koha scripts.
52 &subfield_is_koha_internal_p
53 &GetBranches &getbranch &getbranchdetail
54 &getprinters &getprinter
55 &GetItemTypes &getitemtypeinfo &ItemType
57 &getframeworks &getframeworkinfo
58 &getauthtypes &getauthtype
59 &getallthemes &getalllanguages
60 &GetallBranches &getletters
65 getitemtypeimagesrcfromurl
69 get_notforloan_label_of
79 # FIXME.. this should be moved to a MARC-specific module
80 sub subfield_is_koha_internal_p ($) {
83 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
84 # But real MARC subfields are always single-character
85 # so it really is safer just to check the length
87 return length $subfield != 1;
92 $branches = &GetBranches();
93 returns informations about branches.
94 Create a branch selector with the following code
95 Is branchIndependant sensitive
96 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
100 my $branches = GetBranches;
102 foreach my $thisbranch (sort keys %$branches) {
103 my $selected = 1 if $thisbranch eq $branch;
104 my %row =(value => $thisbranch,
105 selected => $selected,
106 branchname => $branches->{$thisbranch}->{'branchname'},
108 push @branchloop, \%row;
113 <select name="branch">
114 <option value="">Default</option>
115 <!-- TMPL_LOOP name="branchloop" -->
116 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
123 # returns a reference to a hash of references to branches...
127 my $dbh = C4::Context->dbh;
129 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
130 my $strsth ="Select * from branches ";
131 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
132 $strsth.= " order by branchname";
133 $sth=$dbh->prepare($strsth);
135 $sth = $dbh->prepare("Select * from branches order by branchname");
138 while ($branch=$sth->fetchrow_hashref) {
139 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
141 $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ? and categorycode = ?");
142 $nsth->execute($branch->{'branchcode'},$type);
144 $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ? ");
146 $nsth->execute($branch->{'branchcode'});
148 while (my ($cat) = $nsth->fetchrow_array) {
149 # FIXME - This seems wrong. It ought to be
150 # $branch->{categorycodes}{$cat} = 1;
151 # otherwise, there's a namespace collision if there's a
152 # category with the same name as a field in the 'branches'
153 # table (i.e., don't create a category called "issuing").
154 # In addition, the current structure doesn't really allow
155 # you to list the categories that a branch belongs to:
156 # you'd have to list keys %$branch, and remove those keys
157 # that aren't fields in the "branches" table.
160 $branches{$branch->{'branchcode'}}=$branch;
167 my $dbh = C4::Context->dbh;
169 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
170 $sth->execute($branchcode);
171 my $branchname = $sth->fetchrow_array;
177 =head2 getallbranches
179 @branches = &GetallBranches();
180 returns informations about ALL branches.
181 Create a branch selector with the following code
182 IndependantBranches Insensitive...
189 # returns an array to ALL branches...
191 my $dbh = C4::Context->dbh;
193 $sth = $dbh->prepare("Select * from branches order by branchname");
195 while (my $branch=$sth->fetchrow_hashref) {
196 push @branches,$branch;
203 $letters = &getletters($category);
204 returns informations about letters.
205 if needed, $category filters for letters given category
206 Create a letter selector with the following code
208 =head3 in PERL SCRIPT
210 my $letters = getletters($cat);
212 foreach my $thisletter (keys %$letters) {
213 my $selected = 1 if $thisletter eq $letter;
214 my %row =(value => $thisletter,
215 selected => $selected,
216 lettername => $letters->{$thisletter},
218 push @letterloop, \%row;
223 <select name="letter">
224 <option value="">Default</option>
225 <!-- TMPL_LOOP name="letterloop" -->
226 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
233 # returns a reference to a hash of references to ALL letters...
236 my $dbh = C4::Context->dbh;
239 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
241 $sth = $dbh->prepare("Select * from letter order by name");
245 while (my $letter=$sth->fetchrow_hashref) {
246 $letters{$letter->{'code'}}=$letter->{'name'};
249 return ($count,\%letters);
254 $itemtypes = &GetItemTypes();
256 Returns information about existing itemtypes.
258 build a HTML select with the following code :
260 =head3 in PERL SCRIPT
262 my $itemtypes = GetItemTypes;
264 foreach my $thisitemtype (sort keys %$itemtypes) {
265 my $selected = 1 if $thisitemtype eq $itemtype;
266 my %row =(value => $thisitemtype,
267 selected => $selected,
268 description => $itemtypes->{$thisitemtype}->{'description'},
270 push @itemtypesloop, \%row;
272 $template->param(itemtypeloop => \@itemtypesloop);
276 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
277 <select name="itemtype">
278 <option value="">Default</option>
279 <!-- TMPL_LOOP name="itemtypeloop" -->
280 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
283 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
284 <input type="submit" value="OK" class="button">
291 # returns a reference to a hash of references to branches...
293 my $dbh = C4::Context->dbh;
298 my $sth=$dbh->prepare($query);
300 while (my $IT=$sth->fetchrow_hashref) {
301 $itemtypes{$IT->{'itemtype'}}=$IT;
303 return (\%itemtypes);
306 # FIXME this function is better and should replace GetItemTypes everywhere
307 sub get_itemtypeinfos_of {
315 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
318 return get_infos_of($query, 'itemtype');
323 my $dbh = C4::Context->dbh;
324 my $sth=$dbh->prepare("select description from itemtypes where itemtype=?");
325 $sth->execute($type);
326 my $dat=$sth->fetchrow_hashref;
328 return ($dat->{'description'});
332 $authtypes = &getauthtypes();
334 Returns information about existing authtypes.
336 build a HTML select with the following code :
338 =head3 in PERL SCRIPT
340 my $authtypes = getauthtypes;
342 foreach my $thisauthtype (keys %$authtypes) {
343 my $selected = 1 if $thisauthtype eq $authtype;
344 my %row =(value => $thisauthtype,
345 selected => $selected,
346 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
348 push @authtypesloop, \%row;
350 $template->param(itemtypeloop => \@itemtypesloop);
354 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
355 <select name="authtype">
356 <!-- TMPL_LOOP name="authtypeloop" -->
357 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
360 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
361 <input type="submit" value="OK" class="button">
368 # returns a reference to a hash of references to authtypes...
370 my $dbh = C4::Context->dbh;
371 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
373 while (my $IT=$sth->fetchrow_hashref) {
374 $authtypes{$IT->{'authtypecode'}}=$IT;
376 return (\%authtypes);
380 my ($authtypecode) = @_;
381 # returns a reference to a hash of references to authtypes...
383 my $dbh = C4::Context->dbh;
384 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
385 $sth->execute($authtypecode);
386 my $res=$sth->fetchrow_hashref;
392 $frameworks = &getframework();
394 Returns information about existing frameworks
396 build a HTML select with the following code :
398 =head3 in PERL SCRIPT
400 my $frameworks = frameworks();
402 foreach my $thisframework (keys %$frameworks) {
403 my $selected = 1 if $thisframework eq $frameworkcode;
404 my %row =(value => $thisframework,
405 selected => $selected,
406 description => $frameworks->{$thisframework}->{'frameworktext'},
408 push @frameworksloop, \%row;
410 $template->param(frameworkloop => \@frameworksloop);
414 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
415 <select name="frameworkcode">
416 <option value="">Default</option>
417 <!-- TMPL_LOOP name="frameworkloop" -->
418 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
421 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
422 <input type="submit" value="OK" class="button">
429 # returns a reference to a hash of references to branches...
431 my $dbh = C4::Context->dbh;
432 my $sth=$dbh->prepare("select * from biblios_framework");
434 while (my $IT=$sth->fetchrow_hashref) {
435 $itemtypes{$IT->{'frameworkcode'}}=$IT;
437 return (\%itemtypes);
439 =head2 getframeworkinfo
441 $frameworkinfo = &getframeworkinfo($frameworkcode);
443 Returns information about an frameworkcode.
447 sub getframeworkinfo {
448 my ($frameworkcode) = @_;
449 my $dbh = C4::Context->dbh;
450 my $sth=$dbh->prepare("select * from biblios_framework where frameworkcode=?");
451 $sth->execute($frameworkcode);
452 my $res = $sth->fetchrow_hashref;
457 =head2 getitemtypeinfo
459 $itemtype = &getitemtype($itemtype);
461 Returns information about an itemtype.
465 sub getitemtypeinfo {
467 my $dbh = C4::Context->dbh;
468 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
469 $sth->execute($itemtype);
470 my $res = $sth->fetchrow_hashref;
472 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
477 sub getitemtypeimagesrcfromurl {
480 if (defined $imageurl and $imageurl !~ m/^http/) {
482 getitemtypeimagesrc()
490 sub getitemtypeimagedir {
492 C4::Context->intrahtdocs
493 .'/'.C4::Context->preference('template')
498 sub getitemtypeimagesrc {
501 .'/'.C4::Context->preference('template')
508 $printers = &getprinters($env);
509 @queues = keys %$printers;
511 Returns information about existing printer queues.
515 C<$printers> is a reference-to-hash whose keys are the print queues
516 defined in the printers table of the Koha database. The values are
517 references-to-hash, whose keys are the fields in the printers table.
524 my $dbh = C4::Context->dbh;
525 my $sth=$dbh->prepare("select * from printers");
527 while (my $printer=$sth->fetchrow_hashref) {
528 $printers{$printer->{'printqueue'}}=$printer;
534 my($query, $branches) = @_; # get branch for this query from branches
535 my $branch = $query->param('branch');
536 ($branch) || ($branch = $query->cookie('branch'));
537 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
541 =item getbranchdetail
543 $branchname = &getbranchdetail($branchcode);
545 Given the branch code, the function returns the corresponding
546 branch name for a comprehensive information display
552 my ($branchcode) = @_;
553 my $dbh = C4::Context->dbh;
554 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
555 $sth->execute($branchcode);
556 my $branchname = $sth->fetchrow_hashref();
559 } # sub getbranchname
562 sub getprinter ($$) {
563 my($query, $printers) = @_; # get printer for this query from printers
564 my $printer = $query->param('printer');
565 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
566 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
570 =item getalllanguages
572 (@languages) = &getalllanguages($type);
573 (@languages) = &getalllanguages($type,$theme);
575 Returns an array of all available languages.
579 sub getalllanguages {
584 if ($type eq 'opac') {
585 $htdocs=C4::Context->config('opachtdocs');
586 if ($theme and -d "$htdocs/$theme") {
587 opendir D, "$htdocs/$theme";
588 foreach my $language (readdir D) {
589 next if $language=~/^\./;
590 next if $language eq 'all';
591 next if $language=~ /png$/;
592 next if $language=~ /css$/;
593 next if $language=~ /CVS$/;
594 next if $language=~ /itemtypeimg$/;
595 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
596 push @languages, $language;
598 return sort @languages;
601 foreach my $theme (getallthemes('opac')) {
602 opendir D, "$htdocs/$theme";
603 foreach my $language (readdir D) {
604 next if $language=~/^\./;
605 next if $language eq 'all';
606 next if $language=~ /png$/;
607 next if $language=~ /css$/;
608 next if $language=~ /CVS$/;
609 next if $language=~ /itemtypeimg$/;
610 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
611 $lang->{$language}=1;
614 @languages=keys %$lang;
615 return sort @languages;
617 } elsif ($type eq 'intranet') {
618 $htdocs=C4::Context->config('intrahtdocs');
619 if ($theme and -d "$htdocs/$theme") {
620 opendir D, "$htdocs/$theme";
621 foreach my $language (readdir D) {
622 next if $language=~/^\./;
623 next if $language eq 'all';
624 next if $language=~ /png$/;
625 next if $language=~ /css$/;
626 next if $language=~ /CVS$/;
627 next if $language=~ /itemtypeimg$/;
628 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
629 push @languages, $language;
631 return sort @languages;
634 foreach my $theme (getallthemes('opac')) {
635 opendir D, "$htdocs/$theme";
636 foreach my $language (readdir D) {
637 next if $language=~/^\./;
638 next if $language eq 'all';
639 next if $language=~ /png$/;
640 next if $language=~ /css$/;
641 next if $language=~ /CVS$/;
642 next if $language=~ /itemtypeimg$/;
643 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
644 $lang->{$language}=1;
647 @languages=keys %$lang;
648 return sort @languages;
652 my $htdocs=C4::Context->config('intrahtdocs');
653 foreach my $theme (getallthemes('intranet')) {
654 opendir D, "$htdocs/$theme";
655 foreach my $language (readdir D) {
656 next if $language=~/^\./;
657 next if $language eq 'all';
658 next if $language=~ /png$/;
659 next if $language=~ /css$/;
660 next if $language=~ /CVS$/;
661 next if $language=~ /itemtypeimg$/;
662 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
663 $lang->{$language}=1;
666 $htdocs=C4::Context->config('opachtdocs');
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 next if $language=~ /CVS$/;
675 next if $language=~ /itemtypeimg$/;
676 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
677 $lang->{$language}=1;
680 @languages=keys %$lang;
681 return sort @languages;
687 (@themes) = &getallthemes('opac');
688 (@themes) = &getallthemes('intranet');
690 Returns an array of all available themes.
698 if ($type eq 'intranet') {
699 $htdocs=C4::Context->config('intrahtdocs');
701 $htdocs=C4::Context->config('opachtdocs');
703 opendir D, "$htdocs";
704 my @dirlist=readdir D;
705 foreach my $directory (@dirlist) {
706 -d "$htdocs/$directory/en" and push @themes, $directory;
713 Returns the number of pages to display in a pagination bar, given the number
714 of items and the number of items per page.
719 my ($nb_items, $nb_items_per_page) = @_;
721 return int(($nb_items - 1) / $nb_items_per_page) + 1;
725 =head2 getcities (OUEST-PROVENCE)
727 ($id_cityarrayref, $city_hashref) = &getcities();
729 Looks up the different city and zip in the database. Returns two
730 elements: a reference-to-array, which lists the zip city
731 codes, and a reference-to-hash, which maps the name of the city.
732 WHERE =>OUEST PROVENCE OR EXTERIEUR
736 #my ($type_city) = @_;
737 my $dbh = C4::Context->dbh;
738 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
739 #$sth->execute($type_city);
743 # insert empty value to create a empty choice in cgi popup
745 while (my $data=$sth->fetchrow_hashref){
747 push @id,$data->{'cityid'};
748 $city{$data->{'cityid'}}=$data->{'city_name'};
751 #test to know if the table contain some records if no the function return nothing
765 =head2 getroadtypes (OUEST-PROVENCE)
767 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
769 Looks up the different road type . Returns two
770 elements: a reference-to-array, which lists the id_roadtype
771 codes, and a reference-to-hash, which maps the road type of the road .
776 my $dbh = C4::Context->dbh;
777 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
781 # insert empty value to create a empty choice in cgi popup
782 while (my $data=$sth->fetchrow_hashref){
783 push @id,$data->{'roadtypeid'};
784 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
786 #test to know if the table contain some records if no the function return nothing
795 return(\@id,\%roadtype);
799 =head2 get_branchinfos_of
801 my $branchinfos_of = get_branchinfos_of(@branchcodes);
803 Associates a list of branchcodes to the information of the branch, taken in
806 Returns a href where keys are branchcodes and values are href where keys are
807 branch information key.
809 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
812 sub get_branchinfos_of {
813 my @branchcodes = @_;
819 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
821 return get_infos_of($query, 'branchcode');
824 =head2 get_notforloan_label_of
826 my $notforloan_label_of = get_notforloan_label_of();
828 Each authorised value of notforloan (information available in items and
829 itemtypes) is link to a single label.
831 Returns a href where keys are authorised values and values are corresponding
834 foreach my $authorised_value (keys %{$notforloan_label_of}) {
836 "authorised_value: %s => %s\n",
838 $notforloan_label_of->{$authorised_value}
843 sub get_notforloan_label_of {
844 my $dbh = C4::Context->dbh;
845 my($tagfield,$tagsubfield)=MARCfind_marc_from_kohafield("notforloan","holdings");
847 SELECT authorised_value
848 FROM holdings_subfield_structure
849 WHERE tagfield =$tagfield and tagsubfield=$tagsubfield
852 my $sth = $dbh->prepare($query);
854 my ($statuscode) = $sth->fetchrow_array();
859 FROM authorised_values
862 $sth = $dbh->prepare($query);
863 $sth->execute($statuscode);
864 my %notforloan_label_of;
865 while (my $row = $sth->fetchrow_hashref) {
866 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
870 return \%notforloan_label_of;
875 Return a href where a key is associated to a href. You give a query, the
876 name of the key among the fields returned by the query. If you also give as
877 third argument the name of the value, the function returns a href of scalar.
886 # generic href of any information on the item, href of href.
887 my $iteminfos_of = get_infos_of($query, 'itemnumber');
888 print $iteminfos_of->{$itemnumber}{barcode};
890 # specific information, href of scalar
891 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
892 print $barcode_of_item->{$itemnumber};
896 my ($query, $key_name, $value_name) = @_;
898 my $dbh = C4::Context->dbh;
900 my $sth = $dbh->prepare($query);
904 while (my $row = $sth->fetchrow_hashref) {
905 if (defined $value_name) {
906 $infos_of{ $row->{$key_name} } = $row->{$value_name};
909 $infos_of{ $row->{$key_name} } = $row;
917 ###Subfields is an array as well although MARC21 has them all in "a" in case UNIMARC has differing subfields
918 my $dbh=C4::Context->dbh;
920 my $lang=$query->cookie('KohaOpacLanguage');
921 $lang="en" unless $lang;
923 my $sth=$dbh->prepare("SELECT facets_label_$lang,kohafield FROM facets where (facets_label_$lang<>'' ) group by facets_label_$lang");
924 my $sth2=$dbh->prepare("SELECT * FROM facets where facets_label_$lang=?");
926 while (my ($label,$kohafield)=$sth->fetchrow){
927 $sth2->execute($label);
928 my (@tags,@subfield);
929 while (my $data=$sth2->fetchrow_hashref){
930 push @tags,$data->{tagfield} ;
931 push @subfield,$data->{subfield} ;
934 link_value =>"kohafield=$kohafield",
935 label_value =>$label,
937 subfield =>\@subfield,