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")
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
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...
122 my $dbh = C4::Context->dbh;
124 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
125 my $strsth ="Select * from branches ";
126 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
127 $strsth.= " order by branchname";
128 $sth=$dbh->prepare($strsth);
130 $sth = $dbh->prepare("Select * from branches order by branchname");
133 while (my $branch=$sth->fetchrow_hashref) {
134 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
135 $nsth->execute($branch->{'branchcode'});
136 while (my ($cat) = $nsth->fetchrow_array) {
137 # FIXME - This seems wrong. It ought to be
138 # $branch->{categorycodes}{$cat} = 1;
139 # otherwise, there's a namespace collision if there's a
140 # category with the same name as a field in the 'branches'
141 # table (i.e., don't create a category called "issuing").
142 # In addition, the current structure doesn't really allow
143 # you to list the categories that a branch belongs to:
144 # you'd have to list keys %$branch, and remove those keys
145 # that aren't fields in the "branches" table.
148 $branches{$branch->{'branchcode'}}=$branch;
155 my $dbh = C4::Context->dbh;
157 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
158 $sth->execute($branchcode);
159 my $branchname = $sth->fetchrow_array;
165 =head2 getallbranches
167 $branches = &getallbranches();
168 returns informations about ALL branches.
169 Create a branch selector with the following code
170 IndependantBranches Insensitive...
172 =head3 in PERL SCRIPT
174 my $branches = getallbranches;
176 foreach my $thisbranch (keys %$branches) {
177 my $selected = 1 if $thisbranch eq $branch;
178 my %row =(value => $thisbranch,
179 selected => $selected,
180 branchname => $branches->{$thisbranch}->{'branchname'},
182 push @branchloop, \%row;
187 <select name="branch">
188 <option value="">Default</option>
189 <!-- TMPL_LOOP name="branchloop" -->
190 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
198 # returns a reference to a hash of references to ALL branches...
200 my $dbh = C4::Context->dbh;
202 $sth = $dbh->prepare("Select * from branches order by branchname");
204 while (my $branch=$sth->fetchrow_hashref) {
205 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
206 $nsth->execute($branch->{'branchcode'});
207 while (my ($cat) = $nsth->fetchrow_array) {
208 # FIXME - This seems wrong. It ought to be
209 # $branch->{categorycodes}{$cat} = 1;
210 # otherwise, there's a namespace collision if there's a
211 # category with the same name as a field in the 'branches'
212 # table (i.e., don't create a category called "issuing").
213 # In addition, the current structure doesn't really allow
214 # you to list the categories that a branch belongs to:
215 # you'd have to list keys %$branch, and remove those keys
216 # that aren't fields in the "branches" table.
219 $branches{$branch->{'branchcode'}}=$branch;
226 $letters = &getletters($category);
227 returns informations about letters.
228 if needed, $category filters for letters given category
229 Create a letter selector with the following code
231 =head3 in PERL SCRIPT
233 my $letters = getletters($cat);
235 foreach my $thisletter (keys %$letters) {
236 my $selected = 1 if $thisletter eq $letter;
237 my %row =(value => $thisletter,
238 selected => $selected,
239 lettername => $letters->{$thisletter},
241 push @letterloop, \%row;
246 <select name="letter">
247 <option value="">Default</option>
248 <!-- TMPL_LOOP name="letterloop" -->
249 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
256 # returns a reference to a hash of references to ALL letters...
259 my $dbh = C4::Context->dbh;
262 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
264 $sth = $dbh->prepare("Select * from letter order by name");
268 while (my $letter=$sth->fetchrow_hashref) {
269 $letters{$letter->{'code'}}=$letter->{'name'};
272 return ($count,\%letters);
277 $itemtypes = &getitemtypes();
279 Returns information about existing itemtypes.
281 build a HTML select with the following code :
283 =head3 in PERL SCRIPT
285 my $itemtypes = getitemtypes;
287 foreach my $thisitemtype (sort keys %$itemtypes) {
288 my $selected = 1 if $thisitemtype eq $itemtype;
289 my %row =(value => $thisitemtype,
290 selected => $selected,
291 description => $itemtypes->{$thisitemtype}->{'description'},
293 push @itemtypesloop, \%row;
295 $template->param(itemtypeloop => \@itemtypesloop);
299 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
300 <select name="itemtype">
301 <option value="">Default</option>
302 <!-- TMPL_LOOP name="itemtypeloop" -->
303 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
306 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
307 <input type="submit" value="OK" class="button">
314 # returns a reference to a hash of references to branches...
316 my $dbh = C4::Context->dbh;
317 my $sth=$dbh->prepare("select * from itemtypes");
319 while (my $IT=$sth->fetchrow_hashref) {
320 $itemtypes{$IT->{'itemtype'}}=$IT;
322 return (\%itemtypes);
325 # FIXME this function is better and should replace getitemtypes everywhere
326 sub get_itemtypeinfos_of {
334 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
337 return get_infos_of($query, 'itemtype');
342 $authtypes = &getauthtypes();
344 Returns information about existing authtypes.
346 build a HTML select with the following code :
348 =head3 in PERL SCRIPT
350 my $authtypes = getauthtypes;
352 foreach my $thisauthtype (keys %$authtypes) {
353 my $selected = 1 if $thisauthtype eq $authtype;
354 my %row =(value => $thisauthtype,
355 selected => $selected,
356 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
358 push @authtypesloop, \%row;
360 $template->param(itemtypeloop => \@itemtypesloop);
364 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
365 <select name="authtype">
366 <!-- TMPL_LOOP name="authtypeloop" -->
367 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
370 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
371 <input type="submit" value="OK" class="button">
378 # returns a reference to a hash of references to authtypes...
380 my $dbh = C4::Context->dbh;
381 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
383 while (my $IT=$sth->fetchrow_hashref) {
384 $authtypes{$IT->{'authtypecode'}}=$IT;
386 return (\%authtypes);
390 my ($authtypecode) = @_;
391 # returns a reference to a hash of references to authtypes...
393 my $dbh = C4::Context->dbh;
394 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
395 $sth->execute($authtypecode);
396 my $res=$sth->fetchrow_hashref;
402 $frameworks = &getframework();
404 Returns information about existing frameworks
406 build a HTML select with the following code :
408 =head3 in PERL SCRIPT
410 my $frameworks = frameworks();
412 foreach my $thisframework (keys %$frameworks) {
413 my $selected = 1 if $thisframework eq $frameworkcode;
414 my %row =(value => $thisframework,
415 selected => $selected,
416 description => $frameworks->{$thisframework}->{'frameworktext'},
418 push @frameworksloop, \%row;
420 $template->param(frameworkloop => \@frameworksloop);
424 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
425 <select name="frameworkcode">
426 <option value="">Default</option>
427 <!-- TMPL_LOOP name="frameworkloop" -->
428 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
431 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
432 <input type="submit" value="OK" class="button">
439 # returns a reference to a hash of references to branches...
441 my $dbh = C4::Context->dbh;
442 my $sth=$dbh->prepare("select * from biblio_framework");
444 while (my $IT=$sth->fetchrow_hashref) {
445 $itemtypes{$IT->{'frameworkcode'}}=$IT;
447 return (\%itemtypes);
449 =head2 getframeworkinfo
451 $frameworkinfo = &getframeworkinfo($frameworkcode);
453 Returns information about an frameworkcode.
457 sub getframeworkinfo {
458 my ($frameworkcode) = @_;
459 my $dbh = C4::Context->dbh;
460 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
461 $sth->execute($frameworkcode);
462 my $res = $sth->fetchrow_hashref;
467 =head2 getitemtypeinfo
469 $itemtype = &getitemtype($itemtype);
471 Returns information about an itemtype.
475 sub getitemtypeinfo {
477 my $dbh = C4::Context->dbh;
478 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
479 $sth->execute($itemtype);
480 my $res = $sth->fetchrow_hashref;
482 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
487 sub getitemtypeimagesrcfromurl {
490 if (defined $imageurl and $imageurl !~ m/^http/) {
492 getitemtypeimagesrc()
500 sub getitemtypeimagedir {
502 C4::Context->intrahtdocs
503 .'/'.C4::Context->preference('template')
508 sub getitemtypeimagesrc {
511 .'/'.C4::Context->preference('template')
518 $printers = &getprinters($env);
519 @queues = keys %$printers;
521 Returns information about existing printer queues.
525 C<$printers> is a reference-to-hash whose keys are the print queues
526 defined in the printers table of the Koha database. The values are
527 references-to-hash, whose keys are the fields in the printers table.
534 my $dbh = C4::Context->dbh;
535 my $sth=$dbh->prepare("select * from printers");
537 while (my $printer=$sth->fetchrow_hashref) {
538 $printers{$printer->{'printqueue'}}=$printer;
544 my($query, $branches) = @_; # get branch for this query from branches
545 my $branch = $query->param('branch');
546 ($branch) || ($branch = $query->cookie('branch'));
547 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
551 =item getbranchdetail
553 $branchname = &getbranchdetail($branchcode);
555 Given the branch code, the function returns the corresponding
556 branch name for a comprehensive information display
562 my ($branchcode) = @_;
563 my $dbh = C4::Context->dbh;
564 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
565 $sth->execute($branchcode);
566 my $branchname = $sth->fetchrow_hashref();
569 } # sub getbranchname
572 sub getprinter ($$) {
573 my($query, $printers) = @_; # get printer for this query from printers
574 my $printer = $query->param('printer');
575 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
576 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
580 =item getalllanguages
582 (@languages) = &getalllanguages($type);
583 (@languages) = &getalllanguages($type,$theme);
585 Returns an array of all available languages.
589 sub getalllanguages {
594 if ($type eq 'opac') {
595 $htdocs=C4::Context->config('opachtdocs');
596 if ($theme and -d "$htdocs/$theme") {
597 opendir D, "$htdocs/$theme";
598 foreach my $language (readdir D) {
599 next if $language=~/^\./;
600 next if $language eq 'all';
601 next if $language=~ /png$/;
602 next if $language=~ /css$/;
603 next if $language=~ /CVS$/;
604 next if $language=~ /itemtypeimg$/;
605 push @languages, $language;
607 return sort @languages;
610 foreach my $theme (getallthemes('opac')) {
611 opendir D, "$htdocs/$theme";
612 foreach my $language (readdir D) {
613 next if $language=~/^\./;
614 next if $language eq 'all';
615 next if $language=~ /png$/;
616 next if $language=~ /css$/;
617 next if $language=~ /CVS$/;
618 next if $language=~ /itemtypeimg$/;
619 $lang->{$language}=1;
622 @languages=keys %$lang;
623 return sort @languages;
625 } elsif ($type eq 'intranet') {
626 $htdocs=C4::Context->config('intrahtdocs');
627 if ($theme and -d "$htdocs/$theme") {
628 opendir D, "$htdocs/$theme";
629 foreach my $language (readdir D) {
630 next if $language=~/^\./;
631 next if $language eq 'all';
632 next if $language=~ /png$/;
633 next if $language=~ /css$/;
634 next if $language=~ /CVS$/;
635 next if $language=~ /itemtypeimg$/;
636 push @languages, $language;
638 return sort @languages;
641 foreach my $theme (getallthemes('opac')) {
642 opendir D, "$htdocs/$theme";
643 foreach my $language (readdir D) {
644 next if $language=~/^\./;
645 next if $language eq 'all';
646 next if $language=~ /png$/;
647 next if $language=~ /css$/;
648 next if $language=~ /CVS$/;
649 next if $language=~ /itemtypeimg$/;
650 $lang->{$language}=1;
653 @languages=keys %$lang;
654 return sort @languages;
658 my $htdocs=C4::Context->config('intrahtdocs');
659 foreach my $theme (getallthemes('intranet')) {
660 opendir D, "$htdocs/$theme";
661 foreach my $language (readdir D) {
662 next if $language=~/^\./;
663 next if $language eq 'all';
664 next if $language=~ /png$/;
665 next if $language=~ /css$/;
666 next if $language=~ /CVS$/;
667 next if $language=~ /itemtypeimg$/;
668 $lang->{$language}=1;
671 $htdocs=C4::Context->config('opachtdocs');
672 foreach my $theme (getallthemes('opac')) {
673 opendir D, "$htdocs/$theme";
674 foreach my $language (readdir D) {
675 next if $language=~/^\./;
676 next if $language eq 'all';
677 next if $language=~ /png$/;
678 next if $language=~ /css$/;
679 next if $language=~ /CVS$/;
680 next if $language=~ /itemtypeimg$/;
681 $lang->{$language}=1;
684 @languages=keys %$lang;
685 return sort @languages;
691 (@themes) = &getallthemes('opac');
692 (@themes) = &getallthemes('intranet');
694 Returns an array of all available themes.
702 if ($type eq 'intranet') {
703 $htdocs=C4::Context->config('intrahtdocs');
705 $htdocs=C4::Context->config('opachtdocs');
707 opendir D, "$htdocs";
708 my @dirlist=readdir D;
709 foreach my $directory (@dirlist) {
710 -d "$htdocs/$directory/en" and push @themes, $directory;
717 Returns the number of pages to display in a pagination bar, given the number
718 of items and the number of items per page.
723 my ($nb_items, $nb_items_per_page) = @_;
725 return int(($nb_items - 1) / $nb_items_per_page) + 1;
729 =head2 getcities (OUEST-PROVENCE)
731 ($id_cityarrayref, $city_hashref) = &getcities();
733 Looks up the different city and zip in the database. Returns two
734 elements: a reference-to-array, which lists the zip city
735 codes, and a reference-to-hash, which maps the name of the city.
736 WHERE =>OUEST PROVENCE OR EXTERIEUR
740 #my ($type_city) = @_;
741 my $dbh = C4::Context->dbh;
742 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
743 #$sth->execute($type_city);
747 # insert empty value to create a empty choice in cgi popup
749 while (my $data=$sth->fetchrow_hashref){
751 push @id,$data->{'cityid'};
752 $city{$data->{'cityid'}}=$data->{'city_name'};
755 #test to know if the table contain some records if no the function return nothing
769 =head2 getroadtypes (OUEST-PROVENCE)
771 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
773 Looks up the different road type . Returns two
774 elements: a reference-to-array, which lists the id_roadtype
775 codes, and a reference-to-hash, which maps the road type of the road .
780 my $dbh = C4::Context->dbh;
781 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
785 # insert empty value to create a empty choice in cgi popup
786 while (my $data=$sth->fetchrow_hashref){
787 push @id,$data->{'roadtypeid'};
788 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
790 #test to know if the table contain some records if no the function return nothing
799 return(\@id,\%roadtype);
803 =head2 get_branchinfos_of
805 my $branchinfos_of = get_branchinfos_of(@branchcodes);
807 Associates a list of branchcodes to the information of the branch, taken in
810 Returns a href where keys are branchcodes and values are href where keys are
811 branch information key.
813 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
816 sub get_branchinfos_of {
817 my @branchcodes = @_;
823 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
825 return get_infos_of($query, 'branchcode');
828 =head2 get_notforloan_label_of
830 my $notforloan_label_of = get_notforloan_label_of();
832 Each authorised value of notforloan (information available in items and
833 itemtypes) is link to a single label.
835 Returns a href where keys are authorised values and values are corresponding
838 foreach my $authorised_value (keys %{$notforloan_label_of}) {
840 "authorised_value: %s => %s\n",
842 $notforloan_label_of->{$authorised_value}
847 sub get_notforloan_label_of {
848 my $dbh = C4::Context->dbh;
851 SELECT authorised_value
852 FROM marc_subfield_structure
853 WHERE kohafield = \'items.notforloan\'
856 my $sth = $dbh->prepare($query);
858 my ($statuscode) = $sth->fetchrow_array();
863 FROM authorised_values
866 $sth = $dbh->prepare($query);
867 $sth->execute($statuscode);
868 my %notforloan_label_of;
869 while (my $row = $sth->fetchrow_hashref) {
870 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
874 return \%notforloan_label_of;
879 Return a href where a key is associated to a href. You give a query, the
880 name of the key among the fields returned by the query. If you also give as
881 third argument the name of the value, the function returns a href of scalar.
890 # generic href of any information on the item, href of href.
891 my $iteminfos_of = get_infos_of($query, 'itemnumber');
892 print $iteminfos_of->{$itemnumber}{barcode};
894 # specific information, href of scalar
895 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
896 print $barcode_of_item->{$itemnumber};
900 my ($query, $key_name, $value_name) = @_;
902 my $dbh = C4::Context->dbh;
904 my $sth = $dbh->prepare($query);
908 while (my $row = $sth->fetchrow_hashref) {
909 if (defined $value_name) {
910 $infos_of{ $row->{$key_name} } = $row->{$value_name};
913 $infos_of{ $row->{$key_name} } = $row;