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...
123 my $dbh = C4::Context->dbh;
125 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
126 my $strsth ="Select * from branches ";
127 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
128 $strsth.= " order by branchname";
129 $sth=$dbh->prepare($strsth);
131 $sth = $dbh->prepare("Select * from branches order by branchname");
134 while (my $branch=$sth->fetchrow_hashref) {
135 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
137 $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ? and categorycode = ?");
138 $nsth->execute($branch->{'branchcode'},$type);
140 $nsth->execute($branch->{'branchcode'});
142 while (my ($cat) = $nsth->fetchrow_array) {
143 # FIXME - This seems wrong. It ought to be
144 # $branch->{categorycodes}{$cat} = 1;
145 # otherwise, there's a namespace collision if there's a
146 # category with the same name as a field in the 'branches'
147 # table (i.e., don't create a category called "issuing").
148 # In addition, the current structure doesn't really allow
149 # you to list the categories that a branch belongs to:
150 # you'd have to list keys %$branch, and remove those keys
151 # that aren't fields in the "branches" table.
155 $branches{$branch->{'branchcode'}}=$branch;
159 $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...
184 =head3 in PERL SCRIPT
186 my $branches = getallbranches;
188 foreach my $thisbranch (keys %$branches) {
189 my $selected = 1 if $thisbranch eq $branch;
190 my %row =(value => $thisbranch,
191 selected => $selected,
192 branchname => $branches->{$thisbranch}->{'branchname'},
194 push @branchloop, \%row;
199 <select name="branch">
200 <option value="">Default</option>
201 <!-- TMPL_LOOP name="branchloop" -->
202 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
210 # returns a reference to a hash of references to ALL branches...
212 my $dbh = C4::Context->dbh;
214 $sth = $dbh->prepare("Select * from branches order by branchname");
216 while (my $branch=$sth->fetchrow_hashref) {
217 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
218 $nsth->execute($branch->{'branchcode'});
219 while (my ($cat) = $nsth->fetchrow_array) {
220 # FIXME - This seems wrong. It ought to be
221 # $branch->{categorycodes}{$cat} = 1;
222 # otherwise, there's a namespace collision if there's a
223 # category with the same name as a field in the 'branches'
224 # table (i.e., don't create a category called "issuing").
225 # In addition, the current structure doesn't really allow
226 # you to list the categories that a branch belongs to:
227 # you'd have to list keys %$branch, and remove those keys
228 # that aren't fields in the "branches" table.
231 $branches{$branch->{'branchcode'}}=$branch;
238 $letters = &getletters($category);
239 returns informations about letters.
240 if needed, $category filters for letters given category
241 Create a letter selector with the following code
243 =head3 in PERL SCRIPT
245 my $letters = getletters($cat);
247 foreach my $thisletter (keys %$letters) {
248 my $selected = 1 if $thisletter eq $letter;
249 my %row =(value => $thisletter,
250 selected => $selected,
251 lettername => $letters->{$thisletter},
253 push @letterloop, \%row;
258 <select name="letter">
259 <option value="">Default</option>
260 <!-- TMPL_LOOP name="letterloop" -->
261 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
268 # returns a reference to a hash of references to ALL letters...
271 my $dbh = C4::Context->dbh;
274 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
276 $sth = $dbh->prepare("Select * from letter order by name");
280 while (my $letter=$sth->fetchrow_hashref) {
281 $letters{$letter->{'code'}}=$letter->{'name'};
284 return ($count,\%letters);
289 $itemtypes = &getitemtypes();
291 Returns information about existing itemtypes.
293 build a HTML select with the following code :
295 =head3 in PERL SCRIPT
297 my $itemtypes = getitemtypes;
299 foreach my $thisitemtype (sort keys %$itemtypes) {
300 my $selected = 1 if $thisitemtype eq $itemtype;
301 my %row =(value => $thisitemtype,
302 selected => $selected,
303 description => $itemtypes->{$thisitemtype}->{'description'},
305 push @itemtypesloop, \%row;
307 $template->param(itemtypeloop => \@itemtypesloop);
311 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
312 <select name="itemtype">
313 <option value="">Default</option>
314 <!-- TMPL_LOOP name="itemtypeloop" -->
315 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
318 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
319 <input type="submit" value="OK" class="button">
326 # returns a reference to a hash of references to branches...
328 my $dbh = C4::Context->dbh;
329 my $sth=$dbh->prepare("select * from itemtypes");
331 while (my $IT=$sth->fetchrow_hashref) {
332 $itemtypes{$IT->{'itemtype'}}=$IT;
334 return (\%itemtypes);
337 # FIXME this function is better and should replace getitemtypes everywhere
338 sub get_itemtypeinfos_of {
346 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
349 return get_infos_of($query, 'itemtype');
354 $authtypes = &getauthtypes();
356 Returns information about existing authtypes.
358 build a HTML select with the following code :
360 =head3 in PERL SCRIPT
362 my $authtypes = getauthtypes;
364 foreach my $thisauthtype (keys %$authtypes) {
365 my $selected = 1 if $thisauthtype eq $authtype;
366 my %row =(value => $thisauthtype,
367 selected => $selected,
368 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
370 push @authtypesloop, \%row;
372 $template->param(itemtypeloop => \@itemtypesloop);
376 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
377 <select name="authtype">
378 <!-- TMPL_LOOP name="authtypeloop" -->
379 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
382 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
383 <input type="submit" value="OK" class="button">
390 # returns a reference to a hash of references to authtypes...
392 my $dbh = C4::Context->dbh;
393 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
395 while (my $IT=$sth->fetchrow_hashref) {
396 $authtypes{$IT->{'authtypecode'}}=$IT;
398 return (\%authtypes);
402 my ($authtypecode) = @_;
403 # returns a reference to a hash of references to authtypes...
405 my $dbh = C4::Context->dbh;
406 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
407 $sth->execute($authtypecode);
408 my $res=$sth->fetchrow_hashref;
414 $frameworks = &getframework();
416 Returns information about existing frameworks
418 build a HTML select with the following code :
420 =head3 in PERL SCRIPT
422 my $frameworks = frameworks();
424 foreach my $thisframework (keys %$frameworks) {
425 my $selected = 1 if $thisframework eq $frameworkcode;
426 my %row =(value => $thisframework,
427 selected => $selected,
428 description => $frameworks->{$thisframework}->{'frameworktext'},
430 push @frameworksloop, \%row;
432 $template->param(frameworkloop => \@frameworksloop);
436 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
437 <select name="frameworkcode">
438 <option value="">Default</option>
439 <!-- TMPL_LOOP name="frameworkloop" -->
440 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
443 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
444 <input type="submit" value="OK" class="button">
451 # returns a reference to a hash of references to branches...
453 my $dbh = C4::Context->dbh;
454 my $sth=$dbh->prepare("select * from biblio_framework");
456 while (my $IT=$sth->fetchrow_hashref) {
457 $itemtypes{$IT->{'frameworkcode'}}=$IT;
459 return (\%itemtypes);
461 =head2 getframeworkinfo
463 $frameworkinfo = &getframeworkinfo($frameworkcode);
465 Returns information about an frameworkcode.
469 sub getframeworkinfo {
470 my ($frameworkcode) = @_;
471 my $dbh = C4::Context->dbh;
472 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
473 $sth->execute($frameworkcode);
474 my $res = $sth->fetchrow_hashref;
479 =head2 getitemtypeinfo
481 $itemtype = &getitemtype($itemtype);
483 Returns information about an itemtype.
487 sub getitemtypeinfo {
489 my $dbh = C4::Context->dbh;
490 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
491 $sth->execute($itemtype);
492 my $res = $sth->fetchrow_hashref;
494 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
499 sub getitemtypeimagesrcfromurl {
502 if (defined $imageurl and $imageurl !~ m/^http/) {
504 getitemtypeimagesrc()
512 sub getitemtypeimagedir {
514 C4::Context->intrahtdocs
515 .'/'.C4::Context->preference('template')
520 sub getitemtypeimagesrc {
523 .'/'.C4::Context->preference('template')
530 $printers = &getprinters($env);
531 @queues = keys %$printers;
533 Returns information about existing printer queues.
537 C<$printers> is a reference-to-hash whose keys are the print queues
538 defined in the printers table of the Koha database. The values are
539 references-to-hash, whose keys are the fields in the printers table.
546 my $dbh = C4::Context->dbh;
547 my $sth=$dbh->prepare("select * from printers");
549 while (my $printer=$sth->fetchrow_hashref) {
550 $printers{$printer->{'printqueue'}}=$printer;
556 my($query, $branches) = @_; # get branch for this query from branches
557 my $branch = $query->param('branch');
558 ($branch) || ($branch = $query->cookie('branch'));
559 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
563 =item getbranchdetail
565 $branchname = &getbranchdetail($branchcode);
567 Given the branch code, the function returns the corresponding
568 branch name for a comprehensive information display
574 my ($branchcode) = @_;
575 my $dbh = C4::Context->dbh;
576 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
577 $sth->execute($branchcode);
578 my $branchname = $sth->fetchrow_hashref();
581 } # sub getbranchname
584 sub getprinter ($$) {
585 my($query, $printers) = @_; # get printer for this query from printers
586 my $printer = $query->param('printer');
587 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
588 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
592 =item getalllanguages
594 (@languages) = &getalllanguages($type);
595 (@languages) = &getalllanguages($type,$theme);
597 Returns an array of all available languages.
601 sub getalllanguages {
606 if ($type eq 'opac') {
607 $htdocs=C4::Context->config('opachtdocs');
608 if ($theme and -d "$htdocs/$theme") {
609 opendir D, "$htdocs/$theme";
610 foreach my $language (readdir D) {
611 next if $language=~/^\./;
612 next if $language eq 'all';
613 next if $language=~ /png$/;
614 next if $language=~ /css$/;
615 next if $language=~ /CVS$/;
616 next if $language=~ /itemtypeimg$/;
617 push @languages, $language;
619 return sort @languages;
622 foreach my $theme (getallthemes('opac')) {
623 opendir D, "$htdocs/$theme";
624 foreach my $language (readdir D) {
625 next if $language=~/^\./;
626 next if $language eq 'all';
627 next if $language=~ /png$/;
628 next if $language=~ /css$/;
629 next if $language=~ /CVS$/;
630 next if $language=~ /itemtypeimg$/;
631 $lang->{$language}=1;
634 @languages=keys %$lang;
635 return sort @languages;
637 } elsif ($type eq 'intranet') {
638 $htdocs=C4::Context->config('intrahtdocs');
639 if ($theme and -d "$htdocs/$theme") {
640 opendir D, "$htdocs/$theme";
641 foreach my $language (readdir D) {
642 next if $language=~/^\./;
643 next if $language eq 'all';
644 next if $language=~ /png$/;
645 next if $language=~ /css$/;
646 next if $language=~ /CVS$/;
647 next if $language=~ /itemtypeimg$/;
648 push @languages, $language;
650 return sort @languages;
653 foreach my $theme (getallthemes('opac')) {
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 $lang->{$language}=1;
665 @languages=keys %$lang;
666 return sort @languages;
670 my $htdocs=C4::Context->config('intrahtdocs');
671 foreach my $theme (getallthemes('intranet')) {
672 opendir D, "$htdocs/$theme";
673 foreach my $language (readdir D) {
674 next if $language=~/^\./;
675 next if $language eq 'all';
676 next if $language=~ /png$/;
677 next if $language=~ /css$/;
678 next if $language=~ /CVS$/;
679 next if $language=~ /itemtypeimg$/;
680 $lang->{$language}=1;
683 $htdocs=C4::Context->config('opachtdocs');
684 foreach my $theme (getallthemes('opac')) {
685 opendir D, "$htdocs/$theme";
686 foreach my $language (readdir D) {
687 next if $language=~/^\./;
688 next if $language eq 'all';
689 next if $language=~ /png$/;
690 next if $language=~ /css$/;
691 next if $language=~ /CVS$/;
692 next if $language=~ /itemtypeimg$/;
693 $lang->{$language}=1;
696 @languages=keys %$lang;
697 return sort @languages;
703 (@themes) = &getallthemes('opac');
704 (@themes) = &getallthemes('intranet');
706 Returns an array of all available themes.
714 if ($type eq 'intranet') {
715 $htdocs=C4::Context->config('intrahtdocs');
717 $htdocs=C4::Context->config('opachtdocs');
719 opendir D, "$htdocs";
720 my @dirlist=readdir D;
721 foreach my $directory (@dirlist) {
722 -d "$htdocs/$directory/en" and push @themes, $directory;
729 Returns the number of pages to display in a pagination bar, given the number
730 of items and the number of items per page.
735 my ($nb_items, $nb_items_per_page) = @_;
737 return int(($nb_items - 1) / $nb_items_per_page) + 1;
741 =head2 getcities (OUEST-PROVENCE)
743 ($id_cityarrayref, $city_hashref) = &getcities();
745 Looks up the different city and zip in the database. Returns two
746 elements: a reference-to-array, which lists the zip city
747 codes, and a reference-to-hash, which maps the name of the city.
748 WHERE =>OUEST PROVENCE OR EXTERIEUR
752 #my ($type_city) = @_;
753 my $dbh = C4::Context->dbh;
754 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
755 #$sth->execute($type_city);
759 # insert empty value to create a empty choice in cgi popup
761 while (my $data=$sth->fetchrow_hashref){
763 push @id,$data->{'cityid'};
764 $city{$data->{'cityid'}}=$data->{'city_name'};
767 #test to know if the table contain some records if no the function return nothing
781 =head2 getroadtypes (OUEST-PROVENCE)
783 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
785 Looks up the different road type . Returns two
786 elements: a reference-to-array, which lists the id_roadtype
787 codes, and a reference-to-hash, which maps the road type of the road .
792 my $dbh = C4::Context->dbh;
793 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
797 # insert empty value to create a empty choice in cgi popup
798 while (my $data=$sth->fetchrow_hashref){
799 push @id,$data->{'roadtypeid'};
800 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
802 #test to know if the table contain some records if no the function return nothing
811 return(\@id,\%roadtype);
815 =head2 get_branchinfos_of
817 my $branchinfos_of = get_branchinfos_of(@branchcodes);
819 Associates a list of branchcodes to the information of the branch, taken in
822 Returns a href where keys are branchcodes and values are href where keys are
823 branch information key.
825 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
828 sub get_branchinfos_of {
829 my @branchcodes = @_;
835 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
837 return get_infos_of($query, 'branchcode');
840 =head2 get_notforloan_label_of
842 my $notforloan_label_of = get_notforloan_label_of();
844 Each authorised value of notforloan (information available in items and
845 itemtypes) is link to a single label.
847 Returns a href where keys are authorised values and values are corresponding
850 foreach my $authorised_value (keys %{$notforloan_label_of}) {
852 "authorised_value: %s => %s\n",
854 $notforloan_label_of->{$authorised_value}
859 sub get_notforloan_label_of {
860 my $dbh = C4::Context->dbh;
863 SELECT authorised_value
864 FROM marc_subfield_structure
865 WHERE kohafield = \'items.notforloan\'
868 my $sth = $dbh->prepare($query);
870 my ($statuscode) = $sth->fetchrow_array();
875 FROM authorised_values
878 $sth = $dbh->prepare($query);
879 $sth->execute($statuscode);
880 my %notforloan_label_of;
881 while (my $row = $sth->fetchrow_hashref) {
882 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
886 return \%notforloan_label_of;
891 Return a href where a key is associated to a href. You give a query, the
892 name of the key among the fields returned by the query. If you also give as
893 third argument the name of the value, the function returns a href of scalar.
902 # generic href of any information on the item, href of href.
903 my $iteminfos_of = get_infos_of($query, 'itemnumber');
904 print $iteminfos_of->{$itemnumber}{barcode};
906 # specific information, href of scalar
907 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
908 print $barcode_of_item->{$itemnumber};
912 my ($query, $key_name, $value_name) = @_;
914 my $dbh = C4::Context->dbh;
916 my $sth = $dbh->prepare($query);
920 while (my $row = $sth->fetchrow_hashref) {
921 if (defined $value_name) {
922 $infos_of{ $row->{$key_name} } = $row->{$value_name};
925 $infos_of{ $row->{$key_name} } = $row;