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
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.
156 $branches{$branch->{'branchcode'}}=$branch;
160 $branches{$branch->{'branchcode'}}=$branch;
168 my $dbh = C4::Context->dbh;
170 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
171 $sth->execute($branchcode);
172 my $branchname = $sth->fetchrow_array;
178 =head2 getallbranches
180 $branches = &getallbranches();
181 returns informations about ALL branches.
182 Create a branch selector with the following code
183 IndependantBranches Insensitive...
185 =head3 in PERL SCRIPT
187 my $branches = getallbranches;
189 foreach my $thisbranch (keys %$branches) {
190 my $selected = 1 if $thisbranch eq $branch;
191 my %row =(value => $thisbranch,
192 selected => $selected,
193 branchname => $branches->{$thisbranch}->{'branchname'},
195 push @branchloop, \%row;
200 <select name="branch">
201 <option value="">Default</option>
202 <!-- TMPL_LOOP name="branchloop" -->
203 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
211 # returns a reference to a hash of references to ALL branches...
213 my $dbh = C4::Context->dbh;
215 $sth = $dbh->prepare("Select * from branches order by branchname");
217 while (my $branch=$sth->fetchrow_hashref) {
218 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
219 $nsth->execute($branch->{'branchcode'});
220 while (my ($cat) = $nsth->fetchrow_array) {
221 # FIXME - This seems wrong. It ought to be
222 # $branch->{categorycodes}{$cat} = 1;
223 # otherwise, there's a namespace collision if there's a
224 # category with the same name as a field in the 'branches'
225 # table (i.e., don't create a category called "issuing").
226 # In addition, the current structure doesn't really allow
227 # you to list the categories that a branch belongs to:
228 # you'd have to list keys %$branch, and remove those keys
229 # that aren't fields in the "branches" table.
232 $branches{$branch->{'branchcode'}}=$branch;
239 $letters = &getletters($category);
240 returns informations about letters.
241 if needed, $category filters for letters given category
242 Create a letter selector with the following code
244 =head3 in PERL SCRIPT
246 my $letters = getletters($cat);
248 foreach my $thisletter (keys %$letters) {
249 my $selected = 1 if $thisletter eq $letter;
250 my %row =(value => $thisletter,
251 selected => $selected,
252 lettername => $letters->{$thisletter},
254 push @letterloop, \%row;
259 <select name="letter">
260 <option value="">Default</option>
261 <!-- TMPL_LOOP name="letterloop" -->
262 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
269 # returns a reference to a hash of references to ALL letters...
272 my $dbh = C4::Context->dbh;
275 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
277 $sth = $dbh->prepare("Select * from letter order by name");
281 while (my $letter=$sth->fetchrow_hashref) {
282 $letters{$letter->{'code'}}=$letter->{'name'};
285 return ($count,\%letters);
290 $itemtypes = &GetItemTypes();
292 Returns information about existing itemtypes.
294 build a HTML select with the following code :
296 =head3 in PERL SCRIPT
298 my $itemtypes = GetItemTypes;
300 foreach my $thisitemtype (sort keys %$itemtypes) {
301 my $selected = 1 if $thisitemtype eq $itemtype;
302 my %row =(value => $thisitemtype,
303 selected => $selected,
304 description => $itemtypes->{$thisitemtype}->{'description'},
306 push @itemtypesloop, \%row;
308 $template->param(itemtypeloop => \@itemtypesloop);
312 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
313 <select name="itemtype">
314 <option value="">Default</option>
315 <!-- TMPL_LOOP name="itemtypeloop" -->
316 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
319 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
320 <input type="submit" value="OK" class="button">
327 # returns a reference to a hash of references to branches...
329 my $dbh = C4::Context->dbh;
334 my $sth=$dbh->prepare($query);
336 while (my $IT=$sth->fetchrow_hashref) {
337 $itemtypes{$IT->{'itemtype'}}=$IT;
339 return (\%itemtypes);
342 # FIXME this function is better and should replace GetItemTypes everywhere
343 sub get_itemtypeinfos_of {
351 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
354 return get_infos_of($query, 'itemtype');
359 $authtypes = &getauthtypes();
361 Returns information about existing authtypes.
363 build a HTML select with the following code :
365 =head3 in PERL SCRIPT
367 my $authtypes = getauthtypes;
369 foreach my $thisauthtype (keys %$authtypes) {
370 my $selected = 1 if $thisauthtype eq $authtype;
371 my %row =(value => $thisauthtype,
372 selected => $selected,
373 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
375 push @authtypesloop, \%row;
377 $template->param(itemtypeloop => \@itemtypesloop);
381 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
382 <select name="authtype">
383 <!-- TMPL_LOOP name="authtypeloop" -->
384 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
387 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
388 <input type="submit" value="OK" class="button">
395 # returns a reference to a hash of references to authtypes...
397 my $dbh = C4::Context->dbh;
398 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
400 while (my $IT=$sth->fetchrow_hashref) {
401 $authtypes{$IT->{'authtypecode'}}=$IT;
403 return (\%authtypes);
407 my ($authtypecode) = @_;
408 # returns a reference to a hash of references to authtypes...
410 my $dbh = C4::Context->dbh;
411 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
412 $sth->execute($authtypecode);
413 my $res=$sth->fetchrow_hashref;
419 $frameworks = &getframework();
421 Returns information about existing frameworks
423 build a HTML select with the following code :
425 =head3 in PERL SCRIPT
427 my $frameworks = frameworks();
429 foreach my $thisframework (keys %$frameworks) {
430 my $selected = 1 if $thisframework eq $frameworkcode;
431 my %row =(value => $thisframework,
432 selected => $selected,
433 description => $frameworks->{$thisframework}->{'frameworktext'},
435 push @frameworksloop, \%row;
437 $template->param(frameworkloop => \@frameworksloop);
441 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
442 <select name="frameworkcode">
443 <option value="">Default</option>
444 <!-- TMPL_LOOP name="frameworkloop" -->
445 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
448 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
449 <input type="submit" value="OK" class="button">
456 # returns a reference to a hash of references to branches...
458 my $dbh = C4::Context->dbh;
459 my $sth=$dbh->prepare("select * from biblio_framework");
461 while (my $IT=$sth->fetchrow_hashref) {
462 $itemtypes{$IT->{'frameworkcode'}}=$IT;
464 return (\%itemtypes);
466 =head2 getframeworkinfo
468 $frameworkinfo = &getframeworkinfo($frameworkcode);
470 Returns information about an frameworkcode.
474 sub getframeworkinfo {
475 my ($frameworkcode) = @_;
476 my $dbh = C4::Context->dbh;
477 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
478 $sth->execute($frameworkcode);
479 my $res = $sth->fetchrow_hashref;
484 =head2 getitemtypeinfo
486 $itemtype = &getitemtype($itemtype);
488 Returns information about an itemtype.
492 sub getitemtypeinfo {
494 my $dbh = C4::Context->dbh;
495 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
496 $sth->execute($itemtype);
497 my $res = $sth->fetchrow_hashref;
499 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
504 sub getitemtypeimagesrcfromurl {
507 if (defined $imageurl and $imageurl !~ m/^http/) {
509 getitemtypeimagesrc()
517 sub getitemtypeimagedir {
519 C4::Context->intrahtdocs
520 .'/'.C4::Context->preference('template')
525 sub getitemtypeimagesrc {
528 .'/'.C4::Context->preference('template')
535 $printers = &getprinters($env);
536 @queues = keys %$printers;
538 Returns information about existing printer queues.
542 C<$printers> is a reference-to-hash whose keys are the print queues
543 defined in the printers table of the Koha database. The values are
544 references-to-hash, whose keys are the fields in the printers table.
551 my $dbh = C4::Context->dbh;
552 my $sth=$dbh->prepare("select * from printers");
554 while (my $printer=$sth->fetchrow_hashref) {
555 $printers{$printer->{'printqueue'}}=$printer;
561 my($query, $branches) = @_; # get branch for this query from branches
562 my $branch = $query->param('branch');
563 ($branch) || ($branch = $query->cookie('branch'));
564 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
568 =item getbranchdetail
570 $branchname = &getbranchdetail($branchcode);
572 Given the branch code, the function returns the corresponding
573 branch name for a comprehensive information display
579 my ($branchcode) = @_;
580 my $dbh = C4::Context->dbh;
581 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
582 $sth->execute($branchcode);
583 my $branchname = $sth->fetchrow_hashref();
586 } # sub getbranchname
589 sub getprinter ($$) {
590 my($query, $printers) = @_; # get printer for this query from printers
591 my $printer = $query->param('printer');
592 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
593 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
597 =item getalllanguages
599 (@languages) = &getalllanguages($type);
600 (@languages) = &getalllanguages($type,$theme);
602 Returns an array of all available languages.
606 sub getalllanguages {
611 if ($type eq 'opac') {
612 $htdocs=C4::Context->config('opachtdocs');
613 if ($theme and -d "$htdocs/$theme") {
614 opendir D, "$htdocs/$theme";
615 foreach my $language (readdir D) {
616 next if $language=~/^\./;
617 next if $language eq 'all';
618 next if $language=~ /png$/;
619 next if $language=~ /css$/;
620 next if $language=~ /CVS$/;
621 next if $language=~ /itemtypeimg$/;
622 push @languages, $language;
624 return sort @languages;
627 foreach my $theme (getallthemes('opac')) {
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 $lang->{$language}=1;
639 @languages=keys %$lang;
640 return sort @languages;
642 } elsif ($type eq 'intranet') {
643 $htdocs=C4::Context->config('intrahtdocs');
644 if ($theme and -d "$htdocs/$theme") {
645 opendir D, "$htdocs/$theme";
646 foreach my $language (readdir D) {
647 next if $language=~/^\./;
648 next if $language eq 'all';
649 next if $language=~ /png$/;
650 next if $language=~ /css$/;
651 next if $language=~ /CVS$/;
652 next if $language=~ /itemtypeimg$/;
653 push @languages, $language;
655 return sort @languages;
658 foreach my $theme (getallthemes('opac')) {
659 opendir D, "$htdocs/$theme";
660 foreach my $language (readdir D) {
661 next if $language=~/^\./;
662 next if $language eq 'all';
663 next if $language=~ /png$/;
664 next if $language=~ /css$/;
665 next if $language=~ /CVS$/;
666 next if $language=~ /itemtypeimg$/;
667 $lang->{$language}=1;
670 @languages=keys %$lang;
671 return sort @languages;
675 my $htdocs=C4::Context->config('intrahtdocs');
676 foreach my $theme (getallthemes('intranet')) {
677 opendir D, "$htdocs/$theme";
678 foreach my $language (readdir D) {
679 next if $language=~/^\./;
680 next if $language eq 'all';
681 next if $language=~ /png$/;
682 next if $language=~ /css$/;
683 next if $language=~ /CVS$/;
684 next if $language=~ /itemtypeimg$/;
685 $lang->{$language}=1;
688 $htdocs=C4::Context->config('opachtdocs');
689 foreach my $theme (getallthemes('opac')) {
690 opendir D, "$htdocs/$theme";
691 foreach my $language (readdir D) {
692 next if $language=~/^\./;
693 next if $language eq 'all';
694 next if $language=~ /png$/;
695 next if $language=~ /css$/;
696 next if $language=~ /CVS$/;
697 next if $language=~ /itemtypeimg$/;
698 $lang->{$language}=1;
701 @languages=keys %$lang;
702 return sort @languages;
708 (@themes) = &getallthemes('opac');
709 (@themes) = &getallthemes('intranet');
711 Returns an array of all available themes.
719 if ($type eq 'intranet') {
720 $htdocs=C4::Context->config('intrahtdocs');
722 $htdocs=C4::Context->config('opachtdocs');
724 opendir D, "$htdocs";
725 my @dirlist=readdir D;
726 foreach my $directory (@dirlist) {
727 -d "$htdocs/$directory/en" and push @themes, $directory;
734 Returns the number of pages to display in a pagination bar, given the number
735 of items and the number of items per page.
740 my ($nb_items, $nb_items_per_page) = @_;
742 return int(($nb_items - 1) / $nb_items_per_page) + 1;
746 =head2 getcities (OUEST-PROVENCE)
748 ($id_cityarrayref, $city_hashref) = &getcities();
750 Looks up the different city and zip in the database. Returns two
751 elements: a reference-to-array, which lists the zip city
752 codes, and a reference-to-hash, which maps the name of the city.
753 WHERE =>OUEST PROVENCE OR EXTERIEUR
757 #my ($type_city) = @_;
758 my $dbh = C4::Context->dbh;
759 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
760 #$sth->execute($type_city);
764 # insert empty value to create a empty choice in cgi popup
766 while (my $data=$sth->fetchrow_hashref){
768 push @id,$data->{'cityid'};
769 $city{$data->{'cityid'}}=$data->{'city_name'};
772 #test to know if the table contain some records if no the function return nothing
786 =head2 getroadtypes (OUEST-PROVENCE)
788 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
790 Looks up the different road type . Returns two
791 elements: a reference-to-array, which lists the id_roadtype
792 codes, and a reference-to-hash, which maps the road type of the road .
797 my $dbh = C4::Context->dbh;
798 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
802 # insert empty value to create a empty choice in cgi popup
803 while (my $data=$sth->fetchrow_hashref){
804 push @id,$data->{'roadtypeid'};
805 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
807 #test to know if the table contain some records if no the function return nothing
816 return(\@id,\%roadtype);
820 =head2 get_branchinfos_of
822 my $branchinfos_of = get_branchinfos_of(@branchcodes);
824 Associates a list of branchcodes to the information of the branch, taken in
827 Returns a href where keys are branchcodes and values are href where keys are
828 branch information key.
830 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
833 sub get_branchinfos_of {
834 my @branchcodes = @_;
840 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
842 return get_infos_of($query, 'branchcode');
845 =head2 get_notforloan_label_of
847 my $notforloan_label_of = get_notforloan_label_of();
849 Each authorised value of notforloan (information available in items and
850 itemtypes) is link to a single label.
852 Returns a href where keys are authorised values and values are corresponding
855 foreach my $authorised_value (keys %{$notforloan_label_of}) {
857 "authorised_value: %s => %s\n",
859 $notforloan_label_of->{$authorised_value}
864 sub get_notforloan_label_of {
865 my $dbh = C4::Context->dbh;
868 SELECT authorised_value
869 FROM marc_subfield_structure
870 WHERE kohafield = \'items.notforloan\'
873 my $sth = $dbh->prepare($query);
875 my ($statuscode) = $sth->fetchrow_array();
880 FROM authorised_values
883 $sth = $dbh->prepare($query);
884 $sth->execute($statuscode);
885 my %notforloan_label_of;
886 while (my $row = $sth->fetchrow_hashref) {
887 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
891 return \%notforloan_label_of;
896 Return a href where a key is associated to a href. You give a query, the
897 name of the key among the fields returned by the query. If you also give as
898 third argument the name of the value, the function returns a href of scalar.
907 # generic href of any information on the item, href of href.
908 my $iteminfos_of = get_infos_of($query, 'itemnumber');
909 print $iteminfos_of->{$itemnumber}{barcode};
911 # specific information, href of scalar
912 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
913 print $barcode_of_item->{$itemnumber};
917 my ($query, $key_name, $value_name) = @_;
919 my $dbh = C4::Context->dbh;
921 my $sth = $dbh->prepare($query);
925 while (my $row = $sth->fetchrow_hashref) {
926 if (defined $value_name) {
927 $infos_of{ $row->{$key_name} } = $row->{$value_name};
930 $infos_of{ $row->{$key_name} } = $row;