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 = $dbh->prepare("select categorycode from branchrelations where branchcode = ? ");
143 $nsth->execute($branch->{'branchcode'});
145 while (my ($cat) = $nsth->fetchrow_array) {
146 # FIXME - This seems wrong. It ought to be
147 # $branch->{categorycodes}{$cat} = 1;
148 # otherwise, there's a namespace collision if there's a
149 # category with the same name as a field in the 'branches'
150 # table (i.e., don't create a category called "issuing").
151 # In addition, the current structure doesn't really allow
152 # you to list the categories that a branch belongs to:
153 # you'd have to list keys %$branch, and remove those keys
154 # that aren't fields in the "branches" table.
157 $branches{$branch->{'branchcode'}}=$branch;
164 my $dbh = C4::Context->dbh;
166 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
167 $sth->execute($branchcode);
168 my $branchname = $sth->fetchrow_array;
174 =head2 getallbranches
176 $branches = &getallbranches();
177 returns informations about ALL branches.
178 Create a branch selector with the following code
179 IndependantBranches Insensitive...
181 =head3 in PERL SCRIPT
183 my $branches = getallbranches;
185 foreach my $thisbranch (keys %$branches) {
186 my $selected = 1 if $thisbranch eq $branch;
187 my %row =(value => $thisbranch,
188 selected => $selected,
189 branchname => $branches->{$thisbranch}->{'branchname'},
191 push @branchloop, \%row;
196 <select name="branch">
197 <option value="">Default</option>
198 <!-- TMPL_LOOP name="branchloop" -->
199 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
207 # returns a reference to a hash of references to ALL branches...
209 my $dbh = C4::Context->dbh;
211 $sth = $dbh->prepare("Select * from branches order by branchname");
213 while (my $branch=$sth->fetchrow_hashref) {
214 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
215 $nsth->execute($branch->{'branchcode'});
216 while (my ($cat) = $nsth->fetchrow_array) {
217 # FIXME - This seems wrong. It ought to be
218 # $branch->{categorycodes}{$cat} = 1;
219 # otherwise, there's a namespace collision if there's a
220 # category with the same name as a field in the 'branches'
221 # table (i.e., don't create a category called "issuing").
222 # In addition, the current structure doesn't really allow
223 # you to list the categories that a branch belongs to:
224 # you'd have to list keys %$branch, and remove those keys
225 # that aren't fields in the "branches" table.
228 $branches{$branch->{'branchcode'}}=$branch;
235 $letters = &getletters($category);
236 returns informations about letters.
237 if needed, $category filters for letters given category
238 Create a letter selector with the following code
240 =head3 in PERL SCRIPT
242 my $letters = getletters($cat);
244 foreach my $thisletter (keys %$letters) {
245 my $selected = 1 if $thisletter eq $letter;
246 my %row =(value => $thisletter,
247 selected => $selected,
248 lettername => $letters->{$thisletter},
250 push @letterloop, \%row;
255 <select name="letter">
256 <option value="">Default</option>
257 <!-- TMPL_LOOP name="letterloop" -->
258 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
265 # returns a reference to a hash of references to ALL letters...
268 my $dbh = C4::Context->dbh;
271 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
273 $sth = $dbh->prepare("Select * from letter order by name");
277 while (my $letter=$sth->fetchrow_hashref) {
278 $letters{$letter->{'code'}}=$letter->{'name'};
281 return ($count,\%letters);
286 $itemtypes = &GetItemTypes();
288 Returns information about existing itemtypes.
290 build a HTML select with the following code :
292 =head3 in PERL SCRIPT
294 my $itemtypes = GetItemTypes;
296 foreach my $thisitemtype (sort keys %$itemtypes) {
297 my $selected = 1 if $thisitemtype eq $itemtype;
298 my %row =(value => $thisitemtype,
299 selected => $selected,
300 description => $itemtypes->{$thisitemtype}->{'description'},
302 push @itemtypesloop, \%row;
304 $template->param(itemtypeloop => \@itemtypesloop);
308 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
309 <select name="itemtype">
310 <option value="">Default</option>
311 <!-- TMPL_LOOP name="itemtypeloop" -->
312 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
315 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
316 <input type="submit" value="OK" class="button">
323 # returns a reference to a hash of references to branches...
325 my $dbh = C4::Context->dbh;
330 my $sth=$dbh->prepare($query);
332 while (my $IT=$sth->fetchrow_hashref) {
333 $itemtypes{$IT->{'itemtype'}}=$IT;
335 return (\%itemtypes);
338 # FIXME this function is better and should replace GetItemTypes everywhere
339 sub get_itemtypeinfos_of {
347 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
350 return get_infos_of($query, 'itemtype');
355 my $dbh = C4::Context->dbh;
356 my $sth=$dbh->prepare("select description from itemtypes where itemtype=?");
357 $sth->execute($type);
358 my $dat=$sth->fetchrow_hashref;
360 return ($dat->{'description'});
364 $authtypes = &getauthtypes();
366 Returns information about existing authtypes.
368 build a HTML select with the following code :
370 =head3 in PERL SCRIPT
372 my $authtypes = getauthtypes;
374 foreach my $thisauthtype (keys %$authtypes) {
375 my $selected = 1 if $thisauthtype eq $authtype;
376 my %row =(value => $thisauthtype,
377 selected => $selected,
378 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
380 push @authtypesloop, \%row;
382 $template->param(itemtypeloop => \@itemtypesloop);
386 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
387 <select name="authtype">
388 <!-- TMPL_LOOP name="authtypeloop" -->
389 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
392 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
393 <input type="submit" value="OK" class="button">
400 # returns a reference to a hash of references to authtypes...
402 my $dbh = C4::Context->dbh;
403 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
405 while (my $IT=$sth->fetchrow_hashref) {
406 $authtypes{$IT->{'authtypecode'}}=$IT;
408 return (\%authtypes);
412 my ($authtypecode) = @_;
413 # returns a reference to a hash of references to authtypes...
415 my $dbh = C4::Context->dbh;
416 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
417 $sth->execute($authtypecode);
418 my $res=$sth->fetchrow_hashref;
424 $frameworks = &getframework();
426 Returns information about existing frameworks
428 build a HTML select with the following code :
430 =head3 in PERL SCRIPT
432 my $frameworks = frameworks();
434 foreach my $thisframework (keys %$frameworks) {
435 my $selected = 1 if $thisframework eq $frameworkcode;
436 my %row =(value => $thisframework,
437 selected => $selected,
438 description => $frameworks->{$thisframework}->{'frameworktext'},
440 push @frameworksloop, \%row;
442 $template->param(frameworkloop => \@frameworksloop);
446 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
447 <select name="frameworkcode">
448 <option value="">Default</option>
449 <!-- TMPL_LOOP name="frameworkloop" -->
450 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
453 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
454 <input type="submit" value="OK" class="button">
461 # returns a reference to a hash of references to branches...
463 my $dbh = C4::Context->dbh;
464 my $sth=$dbh->prepare("select * from biblios_framework");
466 while (my $IT=$sth->fetchrow_hashref) {
467 $itemtypes{$IT->{'frameworkcode'}}=$IT;
469 return (\%itemtypes);
471 =head2 getframeworkinfo
473 $frameworkinfo = &getframeworkinfo($frameworkcode);
475 Returns information about an frameworkcode.
479 sub getframeworkinfo {
480 my ($frameworkcode) = @_;
481 my $dbh = C4::Context->dbh;
482 my $sth=$dbh->prepare("select * from biblios_framework where frameworkcode=?");
483 $sth->execute($frameworkcode);
484 my $res = $sth->fetchrow_hashref;
489 =head2 getitemtypeinfo
491 $itemtype = &getitemtype($itemtype);
493 Returns information about an itemtype.
497 sub getitemtypeinfo {
499 my $dbh = C4::Context->dbh;
500 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
501 $sth->execute($itemtype);
502 my $res = $sth->fetchrow_hashref;
504 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
509 sub getitemtypeimagesrcfromurl {
512 if (defined $imageurl and $imageurl !~ m/^http/) {
514 getitemtypeimagesrc()
522 sub getitemtypeimagedir {
524 C4::Context->intrahtdocs
525 .'/'.C4::Context->preference('template')
530 sub getitemtypeimagesrc {
533 .'/'.C4::Context->preference('template')
540 $printers = &getprinters($env);
541 @queues = keys %$printers;
543 Returns information about existing printer queues.
547 C<$printers> is a reference-to-hash whose keys are the print queues
548 defined in the printers table of the Koha database. The values are
549 references-to-hash, whose keys are the fields in the printers table.
556 my $dbh = C4::Context->dbh;
557 my $sth=$dbh->prepare("select * from printers");
559 while (my $printer=$sth->fetchrow_hashref) {
560 $printers{$printer->{'printqueue'}}=$printer;
566 my($query, $branches) = @_; # get branch for this query from branches
567 my $branch = $query->param('branch');
568 ($branch) || ($branch = $query->cookie('branch'));
569 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
573 =item getbranchdetail
575 $branchname = &getbranchdetail($branchcode);
577 Given the branch code, the function returns the corresponding
578 branch name for a comprehensive information display
584 my ($branchcode) = @_;
585 my $dbh = C4::Context->dbh;
586 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
587 $sth->execute($branchcode);
588 my $branchname = $sth->fetchrow_hashref();
591 } # sub getbranchname
594 sub getprinter ($$) {
595 my($query, $printers) = @_; # get printer for this query from printers
596 my $printer = $query->param('printer');
597 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
598 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
602 =item getalllanguages
604 (@languages) = &getalllanguages($type);
605 (@languages) = &getalllanguages($type,$theme);
607 Returns an array of all available languages.
611 sub getalllanguages {
616 if ($type eq 'opac') {
617 $htdocs=C4::Context->config('opachtdocs');
618 if ($theme and -d "$htdocs/$theme") {
619 opendir D, "$htdocs/$theme";
620 foreach my $language (readdir D) {
621 next if $language=~/^\./;
622 next if $language eq 'all';
623 next if $language=~ /png$/;
624 next if $language=~ /css$/;
625 next if $language=~ /CVS$/;
626 next if $language=~ /itemtypeimg$/;
627 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
628 push @languages, $language;
630 return sort @languages;
633 foreach my $theme (getallthemes('opac')) {
634 opendir D, "$htdocs/$theme";
635 foreach my $language (readdir D) {
636 next if $language=~/^\./;
637 next if $language eq 'all';
638 next if $language=~ /png$/;
639 next if $language=~ /css$/;
640 next if $language=~ /CVS$/;
641 next if $language=~ /itemtypeimg$/;
642 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
643 $lang->{$language}=1;
646 @languages=keys %$lang;
647 return sort @languages;
649 } elsif ($type eq 'intranet') {
650 $htdocs=C4::Context->config('intrahtdocs');
651 if ($theme and -d "$htdocs/$theme") {
652 opendir D, "$htdocs/$theme";
653 foreach my $language (readdir D) {
654 next if $language=~/^\./;
655 next if $language eq 'all';
656 next if $language=~ /png$/;
657 next if $language=~ /css$/;
658 next if $language=~ /CVS$/;
659 next if $language=~ /itemtypeimg$/;
660 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
661 push @languages, $language;
663 return sort @languages;
666 foreach my $theme (getallthemes('opac')) {
667 opendir D, "$htdocs/$theme";
668 foreach my $language (readdir D) {
669 next if $language=~/^\./;
670 next if $language eq 'all';
671 next if $language=~ /png$/;
672 next if $language=~ /css$/;
673 next if $language=~ /CVS$/;
674 next if $language=~ /itemtypeimg$/;
675 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
676 $lang->{$language}=1;
679 @languages=keys %$lang;
680 return sort @languages;
684 my $htdocs=C4::Context->config('intrahtdocs');
685 foreach my $theme (getallthemes('intranet')) {
686 opendir D, "$htdocs/$theme";
687 foreach my $language (readdir D) {
688 next if $language=~/^\./;
689 next if $language eq 'all';
690 next if $language=~ /png$/;
691 next if $language=~ /css$/;
692 next if $language=~ /CVS$/;
693 next if $language=~ /itemtypeimg$/;
694 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
695 $lang->{$language}=1;
698 $htdocs=C4::Context->config('opachtdocs');
699 foreach my $theme (getallthemes('opac')) {
700 opendir D, "$htdocs/$theme";
701 foreach my $language (readdir D) {
702 next if $language=~/^\./;
703 next if $language eq 'all';
704 next if $language=~ /png$/;
705 next if $language=~ /css$/;
706 next if $language=~ /CVS$/;
707 next if $language=~ /itemtypeimg$/;
708 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
709 $lang->{$language}=1;
712 @languages=keys %$lang;
713 return sort @languages;
719 (@themes) = &getallthemes('opac');
720 (@themes) = &getallthemes('intranet');
722 Returns an array of all available themes.
730 if ($type eq 'intranet') {
731 $htdocs=C4::Context->config('intrahtdocs');
733 $htdocs=C4::Context->config('opachtdocs');
735 opendir D, "$htdocs";
736 my @dirlist=readdir D;
737 foreach my $directory (@dirlist) {
738 -d "$htdocs/$directory/en" and push @themes, $directory;
745 Returns the number of pages to display in a pagination bar, given the number
746 of items and the number of items per page.
751 my ($nb_items, $nb_items_per_page) = @_;
753 return int(($nb_items - 1) / $nb_items_per_page) + 1;
757 =head2 getcities (OUEST-PROVENCE)
759 ($id_cityarrayref, $city_hashref) = &getcities();
761 Looks up the different city and zip in the database. Returns two
762 elements: a reference-to-array, which lists the zip city
763 codes, and a reference-to-hash, which maps the name of the city.
764 WHERE =>OUEST PROVENCE OR EXTERIEUR
768 #my ($type_city) = @_;
769 my $dbh = C4::Context->dbh;
770 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
771 #$sth->execute($type_city);
775 # insert empty value to create a empty choice in cgi popup
777 while (my $data=$sth->fetchrow_hashref){
779 push @id,$data->{'cityid'};
780 $city{$data->{'cityid'}}=$data->{'city_name'};
783 #test to know if the table contain some records if no the function return nothing
797 =head2 getroadtypes (OUEST-PROVENCE)
799 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
801 Looks up the different road type . Returns two
802 elements: a reference-to-array, which lists the id_roadtype
803 codes, and a reference-to-hash, which maps the road type of the road .
808 my $dbh = C4::Context->dbh;
809 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
813 # insert empty value to create a empty choice in cgi popup
814 while (my $data=$sth->fetchrow_hashref){
815 push @id,$data->{'roadtypeid'};
816 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
818 #test to know if the table contain some records if no the function return nothing
827 return(\@id,\%roadtype);
831 =head2 get_branchinfos_of
833 my $branchinfos_of = get_branchinfos_of(@branchcodes);
835 Associates a list of branchcodes to the information of the branch, taken in
838 Returns a href where keys are branchcodes and values are href where keys are
839 branch information key.
841 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
844 sub get_branchinfos_of {
845 my @branchcodes = @_;
851 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
853 return get_infos_of($query, 'branchcode');
856 =head2 get_notforloan_label_of
858 my $notforloan_label_of = get_notforloan_label_of();
860 Each authorised value of notforloan (information available in items and
861 itemtypes) is link to a single label.
863 Returns a href where keys are authorised values and values are corresponding
866 foreach my $authorised_value (keys %{$notforloan_label_of}) {
868 "authorised_value: %s => %s\n",
870 $notforloan_label_of->{$authorised_value}
875 sub get_notforloan_label_of {
876 my $dbh = C4::Context->dbh;
877 my($tagfield,$tagsubfield)=MARCfind_marc_from_kohafield("notforloan","holdings");
879 SELECT authorised_value
880 FROM holdings_subfield_structure
881 WHERE tagfield =$tagfield and tagsubfield=$tagsubfield
884 my $sth = $dbh->prepare($query);
886 my ($statuscode) = $sth->fetchrow_array();
891 FROM authorised_values
894 $sth = $dbh->prepare($query);
895 $sth->execute($statuscode);
896 my %notforloan_label_of;
897 while (my $row = $sth->fetchrow_hashref) {
898 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
902 return \%notforloan_label_of;
907 Return a href where a key is associated to a href. You give a query, the
908 name of the key among the fields returned by the query. If you also give as
909 third argument the name of the value, the function returns a href of scalar.
918 # generic href of any information on the item, href of href.
919 my $iteminfos_of = get_infos_of($query, 'itemnumber');
920 print $iteminfos_of->{$itemnumber}{barcode};
922 # specific information, href of scalar
923 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
924 print $barcode_of_item->{$itemnumber};
928 my ($query, $key_name, $value_name) = @_;
930 my $dbh = C4::Context->dbh;
932 my $sth = $dbh->prepare($query);
936 while (my $row = $sth->fetchrow_hashref) {
937 if (defined $value_name) {
938 $infos_of{ $row->{$key_name} } = $row->{$value_name};
941 $infos_of{ $row->{$key_name} } = $row;