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;
330 my $sth=$dbh->prepare("select * from itemtypes");
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 $authtypes = &getauthtypes();
357 Returns information about existing authtypes.
359 build a HTML select with the following code :
361 =head3 in PERL SCRIPT
363 my $authtypes = getauthtypes;
365 foreach my $thisauthtype (keys %$authtypes) {
366 my $selected = 1 if $thisauthtype eq $authtype;
367 my %row =(value => $thisauthtype,
368 selected => $selected,
369 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
371 push @authtypesloop, \%row;
373 $template->param(itemtypeloop => \@itemtypesloop);
377 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
378 <select name="authtype">
379 <!-- TMPL_LOOP name="authtypeloop" -->
380 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
383 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
384 <input type="submit" value="OK" class="button">
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 order by authtypetext");
396 while (my $IT=$sth->fetchrow_hashref) {
397 $authtypes{$IT->{'authtypecode'}}=$IT;
399 return (\%authtypes);
403 my ($authtypecode) = @_;
404 # returns a reference to a hash of references to authtypes...
406 my $dbh = C4::Context->dbh;
407 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
408 $sth->execute($authtypecode);
409 my $res=$sth->fetchrow_hashref;
415 $frameworks = &getframework();
417 Returns information about existing frameworks
419 build a HTML select with the following code :
421 =head3 in PERL SCRIPT
423 my $frameworks = frameworks();
425 foreach my $thisframework (keys %$frameworks) {
426 my $selected = 1 if $thisframework eq $frameworkcode;
427 my %row =(value => $thisframework,
428 selected => $selected,
429 description => $frameworks->{$thisframework}->{'frameworktext'},
431 push @frameworksloop, \%row;
433 $template->param(frameworkloop => \@frameworksloop);
437 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
438 <select name="frameworkcode">
439 <option value="">Default</option>
440 <!-- TMPL_LOOP name="frameworkloop" -->
441 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
444 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
445 <input type="submit" value="OK" class="button">
452 # returns a reference to a hash of references to branches...
454 my $dbh = C4::Context->dbh;
455 my $sth=$dbh->prepare("select * from biblio_framework");
457 while (my $IT=$sth->fetchrow_hashref) {
458 $itemtypes{$IT->{'frameworkcode'}}=$IT;
460 return (\%itemtypes);
462 =head2 getframeworkinfo
464 $frameworkinfo = &getframeworkinfo($frameworkcode);
466 Returns information about an frameworkcode.
470 sub getframeworkinfo {
471 my ($frameworkcode) = @_;
472 my $dbh = C4::Context->dbh;
473 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
474 $sth->execute($frameworkcode);
475 my $res = $sth->fetchrow_hashref;
480 =head2 getitemtypeinfo
482 $itemtype = &getitemtype($itemtype);
484 Returns information about an itemtype.
488 sub getitemtypeinfo {
490 my $dbh = C4::Context->dbh;
491 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
492 $sth->execute($itemtype);
493 my $res = $sth->fetchrow_hashref;
495 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
500 sub getitemtypeimagesrcfromurl {
503 if (defined $imageurl and $imageurl !~ m/^http/) {
505 getitemtypeimagesrc()
513 sub getitemtypeimagedir {
515 C4::Context->intrahtdocs
516 .'/'.C4::Context->preference('template')
521 sub getitemtypeimagesrc {
524 .'/'.C4::Context->preference('template')
531 $printers = &getprinters($env);
532 @queues = keys %$printers;
534 Returns information about existing printer queues.
538 C<$printers> is a reference-to-hash whose keys are the print queues
539 defined in the printers table of the Koha database. The values are
540 references-to-hash, whose keys are the fields in the printers table.
547 my $dbh = C4::Context->dbh;
548 my $sth=$dbh->prepare("select * from printers");
550 while (my $printer=$sth->fetchrow_hashref) {
551 $printers{$printer->{'printqueue'}}=$printer;
557 my($query, $branches) = @_; # get branch for this query from branches
558 my $branch = $query->param('branch');
559 ($branch) || ($branch = $query->cookie('branch'));
560 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
564 =item getbranchdetail
566 $branchname = &getbranchdetail($branchcode);
568 Given the branch code, the function returns the corresponding
569 branch name for a comprehensive information display
575 my ($branchcode) = @_;
576 my $dbh = C4::Context->dbh;
577 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
578 $sth->execute($branchcode);
579 my $branchname = $sth->fetchrow_hashref();
582 } # sub getbranchname
585 sub getprinter ($$) {
586 my($query, $printers) = @_; # get printer for this query from printers
587 my $printer = $query->param('printer');
588 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
589 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
593 =item getalllanguages
595 (@languages) = &getalllanguages($type);
596 (@languages) = &getalllanguages($type,$theme);
598 Returns an array of all available languages.
602 sub getalllanguages {
607 if ($type eq 'opac') {
608 $htdocs=C4::Context->config('opachtdocs');
609 if ($theme and -d "$htdocs/$theme") {
610 opendir D, "$htdocs/$theme";
611 foreach my $language (readdir D) {
612 next if $language=~/^\./;
613 next if $language eq 'all';
614 next if $language=~ /png$/;
615 next if $language=~ /css$/;
616 next if $language=~ /CVS$/;
617 next if $language=~ /itemtypeimg$/;
618 push @languages, $language;
620 return sort @languages;
623 foreach my $theme (getallthemes('opac')) {
624 opendir D, "$htdocs/$theme";
625 foreach my $language (readdir D) {
626 next if $language=~/^\./;
627 next if $language eq 'all';
628 next if $language=~ /png$/;
629 next if $language=~ /css$/;
630 next if $language=~ /CVS$/;
631 next if $language=~ /itemtypeimg$/;
632 $lang->{$language}=1;
635 @languages=keys %$lang;
636 return sort @languages;
638 } elsif ($type eq 'intranet') {
639 $htdocs=C4::Context->config('intrahtdocs');
640 if ($theme and -d "$htdocs/$theme") {
641 opendir D, "$htdocs/$theme";
642 foreach my $language (readdir D) {
643 next if $language=~/^\./;
644 next if $language eq 'all';
645 next if $language=~ /png$/;
646 next if $language=~ /css$/;
647 next if $language=~ /CVS$/;
648 next if $language=~ /itemtypeimg$/;
649 push @languages, $language;
651 return sort @languages;
654 foreach my $theme (getallthemes('opac')) {
655 opendir D, "$htdocs/$theme";
656 foreach my $language (readdir D) {
657 next if $language=~/^\./;
658 next if $language eq 'all';
659 next if $language=~ /png$/;
660 next if $language=~ /css$/;
661 next if $language=~ /CVS$/;
662 next if $language=~ /itemtypeimg$/;
663 $lang->{$language}=1;
666 @languages=keys %$lang;
667 return sort @languages;
671 my $htdocs=C4::Context->config('intrahtdocs');
672 foreach my $theme (getallthemes('intranet')) {
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 $htdocs=C4::Context->config('opachtdocs');
685 foreach my $theme (getallthemes('opac')) {
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 $lang->{$language}=1;
697 @languages=keys %$lang;
698 return sort @languages;
704 (@themes) = &getallthemes('opac');
705 (@themes) = &getallthemes('intranet');
707 Returns an array of all available themes.
715 if ($type eq 'intranet') {
716 $htdocs=C4::Context->config('intrahtdocs');
718 $htdocs=C4::Context->config('opachtdocs');
720 opendir D, "$htdocs";
721 my @dirlist=readdir D;
722 foreach my $directory (@dirlist) {
723 -d "$htdocs/$directory/en" and push @themes, $directory;
730 Returns the number of pages to display in a pagination bar, given the number
731 of items and the number of items per page.
736 my ($nb_items, $nb_items_per_page) = @_;
738 return int(($nb_items - 1) / $nb_items_per_page) + 1;
742 =head2 getcities (OUEST-PROVENCE)
744 ($id_cityarrayref, $city_hashref) = &getcities();
746 Looks up the different city and zip in the database. Returns two
747 elements: a reference-to-array, which lists the zip city
748 codes, and a reference-to-hash, which maps the name of the city.
749 WHERE =>OUEST PROVENCE OR EXTERIEUR
753 #my ($type_city) = @_;
754 my $dbh = C4::Context->dbh;
755 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
756 #$sth->execute($type_city);
760 # insert empty value to create a empty choice in cgi popup
762 while (my $data=$sth->fetchrow_hashref){
764 push @id,$data->{'cityid'};
765 $city{$data->{'cityid'}}=$data->{'city_name'};
768 #test to know if the table contain some records if no the function return nothing
782 =head2 getroadtypes (OUEST-PROVENCE)
784 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
786 Looks up the different road type . Returns two
787 elements: a reference-to-array, which lists the id_roadtype
788 codes, and a reference-to-hash, which maps the road type of the road .
793 my $dbh = C4::Context->dbh;
794 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
798 # insert empty value to create a empty choice in cgi popup
799 while (my $data=$sth->fetchrow_hashref){
800 push @id,$data->{'roadtypeid'};
801 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
803 #test to know if the table contain some records if no the function return nothing
812 return(\@id,\%roadtype);
816 =head2 get_branchinfos_of
818 my $branchinfos_of = get_branchinfos_of(@branchcodes);
820 Associates a list of branchcodes to the information of the branch, taken in
823 Returns a href where keys are branchcodes and values are href where keys are
824 branch information key.
826 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
829 sub get_branchinfos_of {
830 my @branchcodes = @_;
836 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
838 return get_infos_of($query, 'branchcode');
841 =head2 get_notforloan_label_of
843 my $notforloan_label_of = get_notforloan_label_of();
845 Each authorised value of notforloan (information available in items and
846 itemtypes) is link to a single label.
848 Returns a href where keys are authorised values and values are corresponding
851 foreach my $authorised_value (keys %{$notforloan_label_of}) {
853 "authorised_value: %s => %s\n",
855 $notforloan_label_of->{$authorised_value}
860 sub get_notforloan_label_of {
861 my $dbh = C4::Context->dbh;
864 SELECT authorised_value
865 FROM marc_subfield_structure
866 WHERE kohafield = \'items.notforloan\'
869 my $sth = $dbh->prepare($query);
871 my ($statuscode) = $sth->fetchrow_array();
876 FROM authorised_values
879 $sth = $dbh->prepare($query);
880 $sth->execute($statuscode);
881 my %notforloan_label_of;
882 while (my $row = $sth->fetchrow_hashref) {
883 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
887 return \%notforloan_label_of;
892 Return a href where a key is associated to a href. You give a query, the
893 name of the key among the fields returned by the query. If you also give as
894 third argument the name of the value, the function returns a href of scalar.
903 # generic href of any information on the item, href of href.
904 my $iteminfos_of = get_infos_of($query, 'itemnumber');
905 print $iteminfos_of->{$itemnumber}{barcode};
907 # specific information, href of scalar
908 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
909 print $barcode_of_item->{$itemnumber};
913 my ($query, $key_name, $value_name) = @_;
915 my $dbh = C4::Context->dbh;
917 my $sth = $dbh->prepare($query);
921 while (my $row = $sth->fetchrow_hashref) {
922 if (defined $value_name) {
923 $infos_of{ $row->{$key_name} } = $row->{$value_name};
926 $infos_of{ $row->{$key_name} } = $row;