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
26 use vars qw($VERSION @ISA @EXPORT);
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
32 C4::Koha - Perl Module containing convenience functions for Koha scripts
41 Koha.pm provides many functions for Koha scripts.
51 &subfield_is_koha_internal_p
52 &GetBranches &getbranch &getbranchdetail
53 &getprinters &getprinter
54 &GetItemTypes &getitemtypeinfo &ItemType
56 &getframeworks &getframeworkinfo
57 &getauthtypes &getauthtype
58 &getallthemes &getalllanguages
59 &getallbranches &getletters
64 getitemtypeimagesrcfromurl
68 get_notforloan_label_of
76 # FIXME.. this should be moved to a MARC-specific module
77 sub subfield_is_koha_internal_p ($) {
80 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
81 # But real MARC subfields are always single-character
82 # so it really is safer just to check the length
84 return length $subfield != 1;
89 $branches = &GetBranches();
90 returns informations about branches.
91 Create a branch selector with the following code
92 Is branchIndependant sensitive
93 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
97 my $branches = GetBranches;
99 foreach my $thisbranch (sort keys %$branches) {
100 my $selected = 1 if $thisbranch eq $branch;
101 my %row =(value => $thisbranch,
102 selected => $selected,
103 branchname => $branches->{$thisbranch}->{'branchname'},
105 push @branchloop, \%row;
110 <select name="branch">
111 <option value="">Default</option>
112 <!-- TMPL_LOOP name="branchloop" -->
113 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
120 # returns a reference to a hash of references to branches...
124 my $dbh = C4::Context->dbh;
126 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
127 my $strsth ="Select * from branches ";
128 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
129 $strsth.= " order by branchname";
130 $sth=$dbh->prepare($strsth);
132 $sth = $dbh->prepare("Select * from branches order by branchname");
135 while ($branch=$sth->fetchrow_hashref) {
136 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
138 $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ? and categorycode = ?");
139 $nsth->execute($branch->{'branchcode'},$type);
141 $nsth->execute($branch->{'branchcode'});
143 while (my ($cat) = $nsth->fetchrow_array) {
144 # FIXME - This seems wrong. It ought to be
145 # $branch->{categorycodes}{$cat} = 1;
146 # otherwise, there's a namespace collision if there's a
147 # category with the same name as a field in the 'branches'
148 # table (i.e., don't create a category called "issuing").
149 # In addition, the current structure doesn't really allow
150 # you to list the categories that a branch belongs to:
151 # you'd have to list keys %$branch, and remove those keys
152 # that aren't fields in the "branches" table.
161 my $dbh = C4::Context->dbh;
163 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
164 $sth->execute($branchcode);
165 my $branchname = $sth->fetchrow_array;
171 =head2 getallbranches
173 $branches = &getallbranches();
174 returns informations about ALL branches.
175 Create a branch selector with the following code
176 IndependantBranches Insensitive...
178 =head3 in PERL SCRIPT
180 my $branches = getallbranches;
182 foreach my $thisbranch (keys %$branches) {
183 my $selected = 1 if $thisbranch eq $branch;
184 my %row =(value => $thisbranch,
185 selected => $selected,
186 branchname => $branches->{$thisbranch}->{'branchname'},
188 push @branchloop, \%row;
193 <select name="branch">
194 <option value="">Default</option>
195 <!-- TMPL_LOOP name="branchloop" -->
196 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
204 # returns a reference to a hash of references to ALL branches...
206 my $dbh = C4::Context->dbh;
208 $sth = $dbh->prepare("Select * from branches order by branchname");
210 while (my $branch=$sth->fetchrow_hashref) {
211 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
212 $nsth->execute($branch->{'branchcode'});
213 while (my ($cat) = $nsth->fetchrow_array) {
214 # FIXME - This seems wrong. It ought to be
215 # $branch->{categorycodes}{$cat} = 1;
216 # otherwise, there's a namespace collision if there's a
217 # category with the same name as a field in the 'branches'
218 # table (i.e., don't create a category called "issuing").
219 # In addition, the current structure doesn't really allow
220 # you to list the categories that a branch belongs to:
221 # you'd have to list keys %$branch, and remove those keys
222 # that aren't fields in the "branches" table.
225 $branches{$branch->{'branchcode'}}=$branch;
232 $letters = &getletters($category);
233 returns informations about letters.
234 if needed, $category filters for letters given category
235 Create a letter selector with the following code
237 =head3 in PERL SCRIPT
239 my $letters = getletters($cat);
241 foreach my $thisletter (keys %$letters) {
242 my $selected = 1 if $thisletter eq $letter;
243 my %row =(value => $thisletter,
244 selected => $selected,
245 lettername => $letters->{$thisletter},
247 push @letterloop, \%row;
252 <select name="letter">
253 <option value="">Default</option>
254 <!-- TMPL_LOOP name="letterloop" -->
255 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
262 # returns a reference to a hash of references to ALL letters...
265 my $dbh = C4::Context->dbh;
268 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
270 $sth = $dbh->prepare("Select * from letter order by name");
274 while (my $letter=$sth->fetchrow_hashref) {
275 $letters{$letter->{'code'}}=$letter->{'name'};
278 return ($count,\%letters);
283 $itemtypes = &GetItemTypes();
285 Returns information about existing itemtypes.
287 build a HTML select with the following code :
289 =head3 in PERL SCRIPT
291 my $itemtypes = GetItemTypes;
293 foreach my $thisitemtype (sort keys %$itemtypes) {
294 my $selected = 1 if $thisitemtype eq $itemtype;
295 my %row =(value => $thisitemtype,
296 selected => $selected,
297 description => $itemtypes->{$thisitemtype}->{'description'},
299 push @itemtypesloop, \%row;
301 $template->param(itemtypeloop => \@itemtypesloop);
305 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
306 <select name="itemtype">
307 <option value="">Default</option>
308 <!-- TMPL_LOOP name="itemtypeloop" -->
309 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
312 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
313 <input type="submit" value="OK" class="button">
320 # returns a reference to a hash of references to branches...
322 my $dbh = C4::Context->dbh;
327 my $sth=$dbh->prepare($query);
329 while (my $IT=$sth->fetchrow_hashref) {
330 $itemtypes{$IT->{'itemtype'}}=$IT;
332 return (\%itemtypes);
335 # FIXME this function is better and should replace GetItemTypes everywhere
336 sub get_itemtypeinfos_of {
344 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
347 return get_infos_of($query, 'itemtype');
352 my $dbh = C4::Context->dbh;
353 my $sth=$dbh->prepare("select description from itemtypes where itemtype=?");
354 $sth->execute($type);
355 my $dat=$sth->fetchrow_hashref;
357 return ($dat->{'description'});
361 $authtypes = &getauthtypes();
363 Returns information about existing authtypes.
365 build a HTML select with the following code :
367 =head3 in PERL SCRIPT
369 my $authtypes = getauthtypes;
371 foreach my $thisauthtype (keys %$authtypes) {
372 my $selected = 1 if $thisauthtype eq $authtype;
373 my %row =(value => $thisauthtype,
374 selected => $selected,
375 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
377 push @authtypesloop, \%row;
379 $template->param(itemtypeloop => \@itemtypesloop);
383 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
384 <select name="authtype">
385 <!-- TMPL_LOOP name="authtypeloop" -->
386 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></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 authtypes...
399 my $dbh = C4::Context->dbh;
400 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
402 while (my $IT=$sth->fetchrow_hashref) {
403 $authtypes{$IT->{'authtypecode'}}=$IT;
405 return (\%authtypes);
409 my ($authtypecode) = @_;
410 # returns a reference to a hash of references to authtypes...
412 my $dbh = C4::Context->dbh;
413 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
414 $sth->execute($authtypecode);
415 my $res=$sth->fetchrow_hashref;
421 $frameworks = &getframework();
423 Returns information about existing frameworks
425 build a HTML select with the following code :
427 =head3 in PERL SCRIPT
429 my $frameworks = frameworks();
431 foreach my $thisframework (keys %$frameworks) {
432 my $selected = 1 if $thisframework eq $frameworkcode;
433 my %row =(value => $thisframework,
434 selected => $selected,
435 description => $frameworks->{$thisframework}->{'frameworktext'},
437 push @frameworksloop, \%row;
439 $template->param(frameworkloop => \@frameworksloop);
443 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
444 <select name="frameworkcode">
445 <option value="">Default</option>
446 <!-- TMPL_LOOP name="frameworkloop" -->
447 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
450 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
451 <input type="submit" value="OK" class="button">
458 # returns a reference to a hash of references to branches...
460 my $dbh = C4::Context->dbh;
461 my $sth=$dbh->prepare("select * from biblios_framework");
463 while (my $IT=$sth->fetchrow_hashref) {
464 $itemtypes{$IT->{'frameworkcode'}}=$IT;
466 return (\%itemtypes);
468 =head2 getframeworkinfo
470 $frameworkinfo = &getframeworkinfo($frameworkcode);
472 Returns information about an frameworkcode.
476 sub getframeworkinfo {
477 my ($frameworkcode) = @_;
478 my $dbh = C4::Context->dbh;
479 my $sth=$dbh->prepare("select * from biblios_framework where frameworkcode=?");
480 $sth->execute($frameworkcode);
481 my $res = $sth->fetchrow_hashref;
486 =head2 getitemtypeinfo
488 $itemtype = &getitemtype($itemtype);
490 Returns information about an itemtype.
494 sub getitemtypeinfo {
496 my $dbh = C4::Context->dbh;
497 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
498 $sth->execute($itemtype);
499 my $res = $sth->fetchrow_hashref;
501 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
506 sub getitemtypeimagesrcfromurl {
509 if (defined $imageurl and $imageurl !~ m/^http/) {
511 getitemtypeimagesrc()
519 sub getitemtypeimagedir {
521 C4::Context->intrahtdocs
522 .'/'.C4::Context->preference('template')
527 sub getitemtypeimagesrc {
530 .'/'.C4::Context->preference('template')
537 $printers = &getprinters($env);
538 @queues = keys %$printers;
540 Returns information about existing printer queues.
544 C<$printers> is a reference-to-hash whose keys are the print queues
545 defined in the printers table of the Koha database. The values are
546 references-to-hash, whose keys are the fields in the printers table.
553 my $dbh = C4::Context->dbh;
554 my $sth=$dbh->prepare("select * from printers");
556 while (my $printer=$sth->fetchrow_hashref) {
557 $printers{$printer->{'printqueue'}}=$printer;
563 my($query, $branches) = @_; # get branch for this query from branches
564 my $branch = $query->param('branch');
565 ($branch) || ($branch = $query->cookie('branch'));
566 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
570 =item getbranchdetail
572 $branchname = &getbranchdetail($branchcode);
574 Given the branch code, the function returns the corresponding
575 branch name for a comprehensive information display
581 my ($branchcode) = @_;
582 my $dbh = C4::Context->dbh;
583 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
584 $sth->execute($branchcode);
585 my $branchname = $sth->fetchrow_hashref();
588 } # sub getbranchname
591 sub getprinter ($$) {
592 my($query, $printers) = @_; # get printer for this query from printers
593 my $printer = $query->param('printer');
594 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
595 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
599 =item getalllanguages
601 (@languages) = &getalllanguages($type);
602 (@languages) = &getalllanguages($type,$theme);
604 Returns an array of all available languages.
608 sub getalllanguages {
613 if ($type eq 'opac') {
614 $htdocs=C4::Context->config('opachtdocs');
615 if ($theme and -d "$htdocs/$theme") {
616 opendir D, "$htdocs/$theme";
617 foreach my $language (readdir D) {
618 next if $language=~/^\./;
619 next if $language eq 'all';
620 next if $language=~ /png$/;
621 next if $language=~ /css$/;
622 next if $language=~ /CVS$/;
623 next if $language=~ /itemtypeimg$/;
624 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
625 push @languages, $language;
627 return sort @languages;
630 foreach my $theme (getallthemes('opac')) {
631 opendir D, "$htdocs/$theme";
632 foreach my $language (readdir D) {
633 next if $language=~/^\./;
634 next if $language eq 'all';
635 next if $language=~ /png$/;
636 next if $language=~ /css$/;
637 next if $language=~ /CVS$/;
638 next if $language=~ /itemtypeimg$/;
639 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
640 $lang->{$language}=1;
643 @languages=keys %$lang;
644 return sort @languages;
646 } elsif ($type eq 'intranet') {
647 $htdocs=C4::Context->config('intrahtdocs');
648 if ($theme and -d "$htdocs/$theme") {
649 opendir D, "$htdocs/$theme";
650 foreach my $language (readdir D) {
651 next if $language=~/^\./;
652 next if $language eq 'all';
653 next if $language=~ /png$/;
654 next if $language=~ /css$/;
655 next if $language=~ /CVS$/;
656 next if $language=~ /itemtypeimg$/;
657 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
658 push @languages, $language;
660 return sort @languages;
663 foreach my $theme (getallthemes('opac')) {
664 opendir D, "$htdocs/$theme";
665 foreach my $language (readdir D) {
666 next if $language=~/^\./;
667 next if $language eq 'all';
668 next if $language=~ /png$/;
669 next if $language=~ /css$/;
670 next if $language=~ /CVS$/;
671 next if $language=~ /itemtypeimg$/;
672 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
673 $lang->{$language}=1;
676 @languages=keys %$lang;
677 return sort @languages;
681 my $htdocs=C4::Context->config('intrahtdocs');
682 foreach my $theme (getallthemes('intranet')) {
683 opendir D, "$htdocs/$theme";
684 foreach my $language (readdir D) {
685 next if $language=~/^\./;
686 next if $language eq 'all';
687 next if $language=~ /png$/;
688 next if $language=~ /css$/;
689 next if $language=~ /CVS$/;
690 next if $language=~ /itemtypeimg$/;
691 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
692 $lang->{$language}=1;
695 $htdocs=C4::Context->config('opachtdocs');
696 foreach my $theme (getallthemes('opac')) {
697 opendir D, "$htdocs/$theme";
698 foreach my $language (readdir D) {
699 next if $language=~/^\./;
700 next if $language eq 'all';
701 next if $language=~ /png$/;
702 next if $language=~ /css$/;
703 next if $language=~ /CVS$/;
704 next if $language=~ /itemtypeimg$/;
705 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
706 $lang->{$language}=1;
709 @languages=keys %$lang;
710 return sort @languages;
716 (@themes) = &getallthemes('opac');
717 (@themes) = &getallthemes('intranet');
719 Returns an array of all available themes.
727 if ($type eq 'intranet') {
728 $htdocs=C4::Context->config('intrahtdocs');
730 $htdocs=C4::Context->config('opachtdocs');
732 opendir D, "$htdocs";
733 my @dirlist=readdir D;
734 foreach my $directory (@dirlist) {
735 -d "$htdocs/$directory/en" and push @themes, $directory;
742 Returns the number of pages to display in a pagination bar, given the number
743 of items and the number of items per page.
748 my ($nb_items, $nb_items_per_page) = @_;
750 return int(($nb_items - 1) / $nb_items_per_page) + 1;
754 =head2 getcities (OUEST-PROVENCE)
756 ($id_cityarrayref, $city_hashref) = &getcities();
758 Looks up the different city and zip in the database. Returns two
759 elements: a reference-to-array, which lists the zip city
760 codes, and a reference-to-hash, which maps the name of the city.
761 WHERE =>OUEST PROVENCE OR EXTERIEUR
765 #my ($type_city) = @_;
766 my $dbh = C4::Context->dbh;
767 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
768 #$sth->execute($type_city);
772 # insert empty value to create a empty choice in cgi popup
774 while (my $data=$sth->fetchrow_hashref){
776 push @id,$data->{'cityid'};
777 $city{$data->{'cityid'}}=$data->{'city_name'};
780 #test to know if the table contain some records if no the function return nothing
794 =head2 getroadtypes (OUEST-PROVENCE)
796 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
798 Looks up the different road type . Returns two
799 elements: a reference-to-array, which lists the id_roadtype
800 codes, and a reference-to-hash, which maps the road type of the road .
805 my $dbh = C4::Context->dbh;
806 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
810 # insert empty value to create a empty choice in cgi popup
811 while (my $data=$sth->fetchrow_hashref){
812 push @id,$data->{'roadtypeid'};
813 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
815 #test to know if the table contain some records if no the function return nothing
824 return(\@id,\%roadtype);
828 =head2 get_branchinfos_of
830 my $branchinfos_of = get_branchinfos_of(@branchcodes);
832 Associates a list of branchcodes to the information of the branch, taken in
835 Returns a href where keys are branchcodes and values are href where keys are
836 branch information key.
838 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
841 sub get_branchinfos_of {
842 my @branchcodes = @_;
848 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
850 return get_infos_of($query, 'branchcode');
853 =head2 get_notforloan_label_of
855 my $notforloan_label_of = get_notforloan_label_of();
857 Each authorised value of notforloan (information available in items and
858 itemtypes) is link to a single label.
860 Returns a href where keys are authorised values and values are corresponding
863 foreach my $authorised_value (keys %{$notforloan_label_of}) {
865 "authorised_value: %s => %s\n",
867 $notforloan_label_of->{$authorised_value}
872 sub get_notforloan_label_of {
873 my $dbh = C4::Context->dbh;
874 my($tagfield,$tagsubfield)=MARCfind_marc_from_kohafield("notforloan","holdings");
876 SELECT authorised_value
877 FROM holdings_subfield_structure
878 WHERE tagfield =$tagfield and tagsubfield=$tagsubfield
881 my $sth = $dbh->prepare($query);
883 my ($statuscode) = $sth->fetchrow_array();
888 FROM authorised_values
891 $sth = $dbh->prepare($query);
892 $sth->execute($statuscode);
893 my %notforloan_label_of;
894 while (my $row = $sth->fetchrow_hashref) {
895 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
899 return \%notforloan_label_of;
904 Return a href where a key is associated to a href. You give a query, the
905 name of the key among the fields returned by the query. If you also give as
906 third argument the name of the value, the function returns a href of scalar.
915 # generic href of any information on the item, href of href.
916 my $iteminfos_of = get_infos_of($query, 'itemnumber');
917 print $iteminfos_of->{$itemnumber}{barcode};
919 # specific information, href of scalar
920 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
921 print $barcode_of_item->{$itemnumber};
925 my ($query, $key_name, $value_name) = @_;
927 my $dbh = C4::Context->dbh;
929 my $sth = $dbh->prepare($query);
933 while (my $row = $sth->fetchrow_hashref) {
934 if (defined $value_name) {
935 $infos_of{ $row->{$key_name} } = $row->{$value_name};
938 $infos_of{ $row->{$key_name} } = $row;