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.
163 my $dbh = C4::Context->dbh;
165 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
166 $sth->execute($branchcode);
167 my $branchname = $sth->fetchrow_array;
173 =head2 getallbranches
175 $branches = &getallbranches();
176 returns informations about ALL branches.
177 Create a branch selector with the following code
178 IndependantBranches Insensitive...
180 =head3 in PERL SCRIPT
182 my $branches = getallbranches;
184 foreach my $thisbranch (keys %$branches) {
185 my $selected = 1 if $thisbranch eq $branch;
186 my %row =(value => $thisbranch,
187 selected => $selected,
188 branchname => $branches->{$thisbranch}->{'branchname'},
190 push @branchloop, \%row;
195 <select name="branch">
196 <option value="">Default</option>
197 <!-- TMPL_LOOP name="branchloop" -->
198 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
206 # returns a reference to a hash of references to ALL branches...
208 my $dbh = C4::Context->dbh;
210 $sth = $dbh->prepare("Select * from branches order by branchname");
212 while (my $branch=$sth->fetchrow_hashref) {
213 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
214 $nsth->execute($branch->{'branchcode'});
215 while (my ($cat) = $nsth->fetchrow_array) {
216 # FIXME - This seems wrong. It ought to be
217 # $branch->{categorycodes}{$cat} = 1;
218 # otherwise, there's a namespace collision if there's a
219 # category with the same name as a field in the 'branches'
220 # table (i.e., don't create a category called "issuing").
221 # In addition, the current structure doesn't really allow
222 # you to list the categories that a branch belongs to:
223 # you'd have to list keys %$branch, and remove those keys
224 # that aren't fields in the "branches" table.
227 $branches{$branch->{'branchcode'}}=$branch;
234 $letters = &getletters($category);
235 returns informations about letters.
236 if needed, $category filters for letters given category
237 Create a letter selector with the following code
239 =head3 in PERL SCRIPT
241 my $letters = getletters($cat);
243 foreach my $thisletter (keys %$letters) {
244 my $selected = 1 if $thisletter eq $letter;
245 my %row =(value => $thisletter,
246 selected => $selected,
247 lettername => $letters->{$thisletter},
249 push @letterloop, \%row;
254 <select name="letter">
255 <option value="">Default</option>
256 <!-- TMPL_LOOP name="letterloop" -->
257 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
264 # returns a reference to a hash of references to ALL letters...
267 my $dbh = C4::Context->dbh;
270 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
272 $sth = $dbh->prepare("Select * from letter order by name");
276 while (my $letter=$sth->fetchrow_hashref) {
277 $letters{$letter->{'code'}}=$letter->{'name'};
280 return ($count,\%letters);
285 $itemtypes = &GetItemTypes();
287 Returns information about existing itemtypes.
289 build a HTML select with the following code :
291 =head3 in PERL SCRIPT
293 my $itemtypes = GetItemTypes;
295 foreach my $thisitemtype (sort keys %$itemtypes) {
296 my $selected = 1 if $thisitemtype eq $itemtype;
297 my %row =(value => $thisitemtype,
298 selected => $selected,
299 description => $itemtypes->{$thisitemtype}->{'description'},
301 push @itemtypesloop, \%row;
303 $template->param(itemtypeloop => \@itemtypesloop);
307 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
308 <select name="itemtype">
309 <option value="">Default</option>
310 <!-- TMPL_LOOP name="itemtypeloop" -->
311 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
314 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
315 <input type="submit" value="OK" class="button">
322 # returns a reference to a hash of references to branches...
324 my $dbh = C4::Context->dbh;
329 my $sth=$dbh->prepare($query);
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 my $dbh = C4::Context->dbh;
355 my $sth=$dbh->prepare("select description from itemtypes where itemtype=?");
356 $sth->execute($type);
357 my $dat=$sth->fetchrow_hashref;
359 return ($dat->{'description'});
363 $authtypes = &getauthtypes();
365 Returns information about existing authtypes.
367 build a HTML select with the following code :
369 =head3 in PERL SCRIPT
371 my $authtypes = getauthtypes;
373 foreach my $thisauthtype (keys %$authtypes) {
374 my $selected = 1 if $thisauthtype eq $authtype;
375 my %row =(value => $thisauthtype,
376 selected => $selected,
377 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
379 push @authtypesloop, \%row;
381 $template->param(itemtypeloop => \@itemtypesloop);
385 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
386 <select name="authtype">
387 <!-- TMPL_LOOP name="authtypeloop" -->
388 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
391 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
392 <input type="submit" value="OK" class="button">
399 # returns a reference to a hash of references to authtypes...
401 my $dbh = C4::Context->dbh;
402 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
404 while (my $IT=$sth->fetchrow_hashref) {
405 $authtypes{$IT->{'authtypecode'}}=$IT;
407 return (\%authtypes);
411 my ($authtypecode) = @_;
412 # returns a reference to a hash of references to authtypes...
414 my $dbh = C4::Context->dbh;
415 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
416 $sth->execute($authtypecode);
417 my $res=$sth->fetchrow_hashref;
423 $frameworks = &getframework();
425 Returns information about existing frameworks
427 build a HTML select with the following code :
429 =head3 in PERL SCRIPT
431 my $frameworks = frameworks();
433 foreach my $thisframework (keys %$frameworks) {
434 my $selected = 1 if $thisframework eq $frameworkcode;
435 my %row =(value => $thisframework,
436 selected => $selected,
437 description => $frameworks->{$thisframework}->{'frameworktext'},
439 push @frameworksloop, \%row;
441 $template->param(frameworkloop => \@frameworksloop);
445 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
446 <select name="frameworkcode">
447 <option value="">Default</option>
448 <!-- TMPL_LOOP name="frameworkloop" -->
449 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
452 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
453 <input type="submit" value="OK" class="button">
460 # returns a reference to a hash of references to branches...
462 my $dbh = C4::Context->dbh;
463 my $sth=$dbh->prepare("select * from biblios_framework");
465 while (my $IT=$sth->fetchrow_hashref) {
466 $itemtypes{$IT->{'frameworkcode'}}=$IT;
468 return (\%itemtypes);
470 =head2 getframeworkinfo
472 $frameworkinfo = &getframeworkinfo($frameworkcode);
474 Returns information about an frameworkcode.
478 sub getframeworkinfo {
479 my ($frameworkcode) = @_;
480 my $dbh = C4::Context->dbh;
481 my $sth=$dbh->prepare("select * from biblios_framework where frameworkcode=?");
482 $sth->execute($frameworkcode);
483 my $res = $sth->fetchrow_hashref;
488 =head2 getitemtypeinfo
490 $itemtype = &getitemtype($itemtype);
492 Returns information about an itemtype.
496 sub getitemtypeinfo {
498 my $dbh = C4::Context->dbh;
499 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
500 $sth->execute($itemtype);
501 my $res = $sth->fetchrow_hashref;
503 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
508 sub getitemtypeimagesrcfromurl {
511 if (defined $imageurl and $imageurl !~ m/^http/) {
513 getitemtypeimagesrc()
521 sub getitemtypeimagedir {
523 C4::Context->intrahtdocs
524 .'/'.C4::Context->preference('template')
529 sub getitemtypeimagesrc {
532 .'/'.C4::Context->preference('template')
539 $printers = &getprinters($env);
540 @queues = keys %$printers;
542 Returns information about existing printer queues.
546 C<$printers> is a reference-to-hash whose keys are the print queues
547 defined in the printers table of the Koha database. The values are
548 references-to-hash, whose keys are the fields in the printers table.
555 my $dbh = C4::Context->dbh;
556 my $sth=$dbh->prepare("select * from printers");
558 while (my $printer=$sth->fetchrow_hashref) {
559 $printers{$printer->{'printqueue'}}=$printer;
565 my($query, $branches) = @_; # get branch for this query from branches
566 my $branch = $query->param('branch');
567 ($branch) || ($branch = $query->cookie('branch'));
568 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
572 =item getbranchdetail
574 $branchname = &getbranchdetail($branchcode);
576 Given the branch code, the function returns the corresponding
577 branch name for a comprehensive information display
583 my ($branchcode) = @_;
584 my $dbh = C4::Context->dbh;
585 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
586 $sth->execute($branchcode);
587 my $branchname = $sth->fetchrow_hashref();
590 } # sub getbranchname
593 sub getprinter ($$) {
594 my($query, $printers) = @_; # get printer for this query from printers
595 my $printer = $query->param('printer');
596 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
597 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
601 =item getalllanguages
603 (@languages) = &getalllanguages($type);
604 (@languages) = &getalllanguages($type,$theme);
606 Returns an array of all available languages.
610 sub getalllanguages {
615 if ($type eq 'opac') {
616 $htdocs=C4::Context->config('opachtdocs');
617 if ($theme and -d "$htdocs/$theme") {
618 opendir D, "$htdocs/$theme";
619 foreach my $language (readdir D) {
620 next if $language=~/^\./;
621 next if $language eq 'all';
622 next if $language=~ /png$/;
623 next if $language=~ /css$/;
624 next if $language=~ /CVS$/;
625 next if $language=~ /itemtypeimg$/;
626 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
627 push @languages, $language;
629 return sort @languages;
632 foreach my $theme (getallthemes('opac')) {
633 opendir D, "$htdocs/$theme";
634 foreach my $language (readdir D) {
635 next if $language=~/^\./;
636 next if $language eq 'all';
637 next if $language=~ /png$/;
638 next if $language=~ /css$/;
639 next if $language=~ /CVS$/;
640 next if $language=~ /itemtypeimg$/;
641 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
642 $lang->{$language}=1;
645 @languages=keys %$lang;
646 return sort @languages;
648 } elsif ($type eq 'intranet') {
649 $htdocs=C4::Context->config('intrahtdocs');
650 if ($theme and -d "$htdocs/$theme") {
651 opendir D, "$htdocs/$theme";
652 foreach my $language (readdir D) {
653 next if $language=~/^\./;
654 next if $language eq 'all';
655 next if $language=~ /png$/;
656 next if $language=~ /css$/;
657 next if $language=~ /CVS$/;
658 next if $language=~ /itemtypeimg$/;
659 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
660 push @languages, $language;
662 return sort @languages;
665 foreach my $theme (getallthemes('opac')) {
666 opendir D, "$htdocs/$theme";
667 foreach my $language (readdir D) {
668 next if $language=~/^\./;
669 next if $language eq 'all';
670 next if $language=~ /png$/;
671 next if $language=~ /css$/;
672 next if $language=~ /CVS$/;
673 next if $language=~ /itemtypeimg$/;
674 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
675 $lang->{$language}=1;
678 @languages=keys %$lang;
679 return sort @languages;
683 my $htdocs=C4::Context->config('intrahtdocs');
684 foreach my $theme (getallthemes('intranet')) {
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 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
694 $lang->{$language}=1;
697 $htdocs=C4::Context->config('opachtdocs');
698 foreach my $theme (getallthemes('opac')) {
699 opendir D, "$htdocs/$theme";
700 foreach my $language (readdir D) {
701 next if $language=~/^\./;
702 next if $language eq 'all';
703 next if $language=~ /png$/;
704 next if $language=~ /css$/;
705 next if $language=~ /CVS$/;
706 next if $language=~ /itemtypeimg$/;
707 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
708 $lang->{$language}=1;
711 @languages=keys %$lang;
712 return sort @languages;
718 (@themes) = &getallthemes('opac');
719 (@themes) = &getallthemes('intranet');
721 Returns an array of all available themes.
729 if ($type eq 'intranet') {
730 $htdocs=C4::Context->config('intrahtdocs');
732 $htdocs=C4::Context->config('opachtdocs');
734 opendir D, "$htdocs";
735 my @dirlist=readdir D;
736 foreach my $directory (@dirlist) {
737 -d "$htdocs/$directory/en" and push @themes, $directory;
744 Returns the number of pages to display in a pagination bar, given the number
745 of items and the number of items per page.
750 my ($nb_items, $nb_items_per_page) = @_;
752 return int(($nb_items - 1) / $nb_items_per_page) + 1;
756 =head2 getcities (OUEST-PROVENCE)
758 ($id_cityarrayref, $city_hashref) = &getcities();
760 Looks up the different city and zip in the database. Returns two
761 elements: a reference-to-array, which lists the zip city
762 codes, and a reference-to-hash, which maps the name of the city.
763 WHERE =>OUEST PROVENCE OR EXTERIEUR
767 #my ($type_city) = @_;
768 my $dbh = C4::Context->dbh;
769 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
770 #$sth->execute($type_city);
774 # insert empty value to create a empty choice in cgi popup
776 while (my $data=$sth->fetchrow_hashref){
778 push @id,$data->{'cityid'};
779 $city{$data->{'cityid'}}=$data->{'city_name'};
782 #test to know if the table contain some records if no the function return nothing
796 =head2 getroadtypes (OUEST-PROVENCE)
798 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
800 Looks up the different road type . Returns two
801 elements: a reference-to-array, which lists the id_roadtype
802 codes, and a reference-to-hash, which maps the road type of the road .
807 my $dbh = C4::Context->dbh;
808 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
812 # insert empty value to create a empty choice in cgi popup
813 while (my $data=$sth->fetchrow_hashref){
814 push @id,$data->{'roadtypeid'};
815 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
817 #test to know if the table contain some records if no the function return nothing
826 return(\@id,\%roadtype);
830 =head2 get_branchinfos_of
832 my $branchinfos_of = get_branchinfos_of(@branchcodes);
834 Associates a list of branchcodes to the information of the branch, taken in
837 Returns a href where keys are branchcodes and values are href where keys are
838 branch information key.
840 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
843 sub get_branchinfos_of {
844 my @branchcodes = @_;
850 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
852 return get_infos_of($query, 'branchcode');
855 =head2 get_notforloan_label_of
857 my $notforloan_label_of = get_notforloan_label_of();
859 Each authorised value of notforloan (information available in items and
860 itemtypes) is link to a single label.
862 Returns a href where keys are authorised values and values are corresponding
865 foreach my $authorised_value (keys %{$notforloan_label_of}) {
867 "authorised_value: %s => %s\n",
869 $notforloan_label_of->{$authorised_value}
874 sub get_notforloan_label_of {
875 my $dbh = C4::Context->dbh;
876 my($tagfield,$tagsubfield)=MARCfind_marc_from_kohafield("notforloan","holdings");
878 SELECT authorised_value
879 FROM holdings_subfield_structure
880 WHERE tagfield =$tagfield and tagsubfield=$tagsubfield
883 my $sth = $dbh->prepare($query);
885 my ($statuscode) = $sth->fetchrow_array();
890 FROM authorised_values
893 $sth = $dbh->prepare($query);
894 $sth->execute($statuscode);
895 my %notforloan_label_of;
896 while (my $row = $sth->fetchrow_hashref) {
897 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
901 return \%notforloan_label_of;
906 Return a href where a key is associated to a href. You give a query, the
907 name of the key among the fields returned by the query. If you also give as
908 third argument the name of the value, the function returns a href of scalar.
917 # generic href of any information on the item, href of href.
918 my $iteminfos_of = get_infos_of($query, 'itemnumber');
919 print $iteminfos_of->{$itemnumber}{barcode};
921 # specific information, href of scalar
922 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
923 print $barcode_of_item->{$itemnumber};
927 my ($query, $key_name, $value_name) = @_;
929 my $dbh = C4::Context->dbh;
931 my $sth = $dbh->prepare($query);
935 while (my $row = $sth->fetchrow_hashref) {
936 if (defined $value_name) {
937 $infos_of{ $row->{$key_name} } = $row->{$value_name};
940 $infos_of{ $row->{$key_name} } = $row;