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
78 # FIXME.. this should be moved to a MARC-specific module
79 sub subfield_is_koha_internal_p ($) {
82 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
83 # But real MARC subfields are always single-character
84 # so it really is safer just to check the length
86 return length $subfield != 1;
91 $branches = &GetBranches();
92 returns informations about branches.
93 Create a branch selector with the following code
94 Is branchIndependant sensitive
95 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
99 my $branches = GetBranches;
101 foreach my $thisbranch (sort keys %$branches) {
102 my $selected = 1 if $thisbranch eq $branch;
103 my %row =(value => $thisbranch,
104 selected => $selected,
105 branchname => $branches->{$thisbranch}->{'branchname'},
107 push @branchloop, \%row;
112 <select name="branch">
113 <option value="">Default</option>
114 <!-- TMPL_LOOP name="branchloop" -->
115 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
122 # returns a reference to a hash of references to branches...
126 my $dbh = C4::Context->dbh;
128 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
129 my $strsth ="Select * from branches ";
130 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
131 $strsth.= " order by branchname";
132 $sth=$dbh->prepare($strsth);
134 $sth = $dbh->prepare("Select * from branches order by branchname");
137 while ($branch=$sth->fetchrow_hashref) {
138 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
140 $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ? and categorycode = ?");
141 $nsth->execute($branch->{'branchcode'},$type);
143 $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ? ");
145 $nsth->execute($branch->{'branchcode'});
147 while (my ($cat) = $nsth->fetchrow_array) {
148 # FIXME - This seems wrong. It ought to be
149 # $branch->{categorycodes}{$cat} = 1;
150 # otherwise, there's a namespace collision if there's a
151 # category with the same name as a field in the 'branches'
152 # table (i.e., don't create a category called "issuing").
153 # In addition, the current structure doesn't really allow
154 # you to list the categories that a branch belongs to:
155 # you'd have to list keys %$branch, and remove those keys
156 # that aren't fields in the "branches" table.
159 $branches{$branch->{'branchcode'}}=$branch;
166 my $dbh = C4::Context->dbh;
168 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
169 $sth->execute($branchcode);
170 my $branchname = $sth->fetchrow_array;
176 =head2 getallbranches
178 @branches = &GetallBranches();
179 returns informations about ALL branches.
180 Create a branch selector with the following code
181 IndependantBranches Insensitive...
188 # returns an array to ALL branches...
190 my $dbh = C4::Context->dbh;
192 $sth = $dbh->prepare("Select * from branches order by branchname");
194 while (my $branch=$sth->fetchrow_hashref) {
195 push @branches,$branch;
202 $letters = &getletters($category);
203 returns informations about letters.
204 if needed, $category filters for letters given category
205 Create a letter selector with the following code
207 =head3 in PERL SCRIPT
209 my $letters = getletters($cat);
211 foreach my $thisletter (keys %$letters) {
212 my $selected = 1 if $thisletter eq $letter;
213 my %row =(value => $thisletter,
214 selected => $selected,
215 lettername => $letters->{$thisletter},
217 push @letterloop, \%row;
222 <select name="letter">
223 <option value="">Default</option>
224 <!-- TMPL_LOOP name="letterloop" -->
225 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
232 # returns a reference to a hash of references to ALL letters...
235 my $dbh = C4::Context->dbh;
238 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
240 $sth = $dbh->prepare("Select * from letter order by name");
244 while (my $letter=$sth->fetchrow_hashref) {
245 $letters{$letter->{'code'}}=$letter->{'name'};
248 return ($count,\%letters);
253 $itemtypes = &GetItemTypes();
255 Returns information about existing itemtypes.
257 build a HTML select with the following code :
259 =head3 in PERL SCRIPT
261 my $itemtypes = GetItemTypes;
263 foreach my $thisitemtype (sort keys %$itemtypes) {
264 my $selected = 1 if $thisitemtype eq $itemtype;
265 my %row =(value => $thisitemtype,
266 selected => $selected,
267 description => $itemtypes->{$thisitemtype}->{'description'},
269 push @itemtypesloop, \%row;
271 $template->param(itemtypeloop => \@itemtypesloop);
275 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
276 <select name="itemtype">
277 <option value="">Default</option>
278 <!-- TMPL_LOOP name="itemtypeloop" -->
279 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
282 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
283 <input type="submit" value="OK" class="button">
290 # returns a reference to a hash of references to branches...
292 my $dbh = C4::Context->dbh;
297 my $sth=$dbh->prepare($query);
299 while (my $IT=$sth->fetchrow_hashref) {
300 $itemtypes{$IT->{'itemtype'}}=$IT;
302 return (\%itemtypes);
305 # FIXME this function is better and should replace GetItemTypes everywhere
306 sub get_itemtypeinfos_of {
314 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
317 return get_infos_of($query, 'itemtype');
322 my $dbh = C4::Context->dbh;
323 my $sth=$dbh->prepare("select description from itemtypes where itemtype=?");
324 $sth->execute($type);
325 my $dat=$sth->fetchrow_hashref;
327 return ($dat->{'description'});
331 $authtypes = &getauthtypes();
333 Returns information about existing authtypes.
335 build a HTML select with the following code :
337 =head3 in PERL SCRIPT
339 my $authtypes = getauthtypes;
341 foreach my $thisauthtype (keys %$authtypes) {
342 my $selected = 1 if $thisauthtype eq $authtype;
343 my %row =(value => $thisauthtype,
344 selected => $selected,
345 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
347 push @authtypesloop, \%row;
349 $template->param(itemtypeloop => \@itemtypesloop);
353 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
354 <select name="authtype">
355 <!-- TMPL_LOOP name="authtypeloop" -->
356 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
359 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
360 <input type="submit" value="OK" class="button">
367 # returns a reference to a hash of references to authtypes...
369 my $dbh = C4::Context->dbh;
370 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
372 while (my $IT=$sth->fetchrow_hashref) {
373 $authtypes{$IT->{'authtypecode'}}=$IT;
375 return (\%authtypes);
379 my ($authtypecode) = @_;
380 # returns a reference to a hash of references to authtypes...
382 my $dbh = C4::Context->dbh;
383 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
384 $sth->execute($authtypecode);
385 my $res=$sth->fetchrow_hashref;
391 $frameworks = &getframework();
393 Returns information about existing frameworks
395 build a HTML select with the following code :
397 =head3 in PERL SCRIPT
399 my $frameworks = frameworks();
401 foreach my $thisframework (keys %$frameworks) {
402 my $selected = 1 if $thisframework eq $frameworkcode;
403 my %row =(value => $thisframework,
404 selected => $selected,
405 description => $frameworks->{$thisframework}->{'frameworktext'},
407 push @frameworksloop, \%row;
409 $template->param(frameworkloop => \@frameworksloop);
413 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
414 <select name="frameworkcode">
415 <option value="">Default</option>
416 <!-- TMPL_LOOP name="frameworkloop" -->
417 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
420 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
421 <input type="submit" value="OK" class="button">
428 # returns a reference to a hash of references to branches...
430 my $dbh = C4::Context->dbh;
431 my $sth=$dbh->prepare("select * from biblios_framework");
433 while (my $IT=$sth->fetchrow_hashref) {
434 $itemtypes{$IT->{'frameworkcode'}}=$IT;
436 return (\%itemtypes);
438 =head2 getframeworkinfo
440 $frameworkinfo = &getframeworkinfo($frameworkcode);
442 Returns information about an frameworkcode.
446 sub getframeworkinfo {
447 my ($frameworkcode) = @_;
448 my $dbh = C4::Context->dbh;
449 my $sth=$dbh->prepare("select * from biblios_framework where frameworkcode=?");
450 $sth->execute($frameworkcode);
451 my $res = $sth->fetchrow_hashref;
456 =head2 getitemtypeinfo
458 $itemtype = &getitemtype($itemtype);
460 Returns information about an itemtype.
464 sub getitemtypeinfo {
466 my $dbh = C4::Context->dbh;
467 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
468 $sth->execute($itemtype);
469 my $res = $sth->fetchrow_hashref;
471 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
476 sub getitemtypeimagesrcfromurl {
479 if (defined $imageurl and $imageurl !~ m/^http/) {
481 getitemtypeimagesrc()
489 sub getitemtypeimagedir {
491 C4::Context->intrahtdocs
492 .'/'.C4::Context->preference('template')
497 sub getitemtypeimagesrc {
500 .'/'.C4::Context->preference('template')
507 $printers = &getprinters($env);
508 @queues = keys %$printers;
510 Returns information about existing printer queues.
514 C<$printers> is a reference-to-hash whose keys are the print queues
515 defined in the printers table of the Koha database. The values are
516 references-to-hash, whose keys are the fields in the printers table.
523 my $dbh = C4::Context->dbh;
524 my $sth=$dbh->prepare("select * from printers");
526 while (my $printer=$sth->fetchrow_hashref) {
527 $printers{$printer->{'printqueue'}}=$printer;
533 my($query, $branches) = @_; # get branch for this query from branches
534 my $branch = $query->param('branch');
535 ($branch) || ($branch = $query->cookie('branch'));
536 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
540 =item getbranchdetail
542 $branchname = &getbranchdetail($branchcode);
544 Given the branch code, the function returns the corresponding
545 branch name for a comprehensive information display
551 my ($branchcode) = @_;
552 my $dbh = C4::Context->dbh;
553 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
554 $sth->execute($branchcode);
555 my $branchname = $sth->fetchrow_hashref();
558 } # sub getbranchname
561 sub getprinter ($$) {
562 my($query, $printers) = @_; # get printer for this query from printers
563 my $printer = $query->param('printer');
564 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
565 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
569 =item getalllanguages
571 (@languages) = &getalllanguages($type);
572 (@languages) = &getalllanguages($type,$theme);
574 Returns an array of all available languages.
578 sub getalllanguages {
583 if ($type eq 'opac') {
584 $htdocs=C4::Context->config('opachtdocs');
585 if ($theme and -d "$htdocs/$theme") {
586 opendir D, "$htdocs/$theme";
587 foreach my $language (readdir D) {
588 next if $language=~/^\./;
589 next if $language eq 'all';
590 next if $language=~ /png$/;
591 next if $language=~ /css$/;
592 next if $language=~ /CVS$/;
593 next if $language=~ /itemtypeimg$/;
594 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
595 push @languages, $language;
597 return sort @languages;
600 foreach my $theme (getallthemes('opac')) {
601 opendir D, "$htdocs/$theme";
602 foreach my $language (readdir D) {
603 next if $language=~/^\./;
604 next if $language eq 'all';
605 next if $language=~ /png$/;
606 next if $language=~ /css$/;
607 next if $language=~ /CVS$/;
608 next if $language=~ /itemtypeimg$/;
609 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
610 $lang->{$language}=1;
613 @languages=keys %$lang;
614 return sort @languages;
616 } elsif ($type eq 'intranet') {
617 $htdocs=C4::Context->config('intrahtdocs');
618 if ($theme and -d "$htdocs/$theme") {
619 opendir D, "$htdocs/$theme";
620 foreach my $language (readdir D) {
621 next if $language=~/^\./;
622 next if $language eq 'all';
623 next if $language=~ /png$/;
624 next if $language=~ /css$/;
625 next if $language=~ /CVS$/;
626 next if $language=~ /itemtypeimg$/;
627 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
628 push @languages, $language;
630 return sort @languages;
633 foreach my $theme (getallthemes('opac')) {
634 opendir D, "$htdocs/$theme";
635 foreach my $language (readdir D) {
636 next if $language=~/^\./;
637 next if $language eq 'all';
638 next if $language=~ /png$/;
639 next if $language=~ /css$/;
640 next if $language=~ /CVS$/;
641 next if $language=~ /itemtypeimg$/;
642 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
643 $lang->{$language}=1;
646 @languages=keys %$lang;
647 return sort @languages;
651 my $htdocs=C4::Context->config('intrahtdocs');
652 foreach my $theme (getallthemes('intranet')) {
653 opendir D, "$htdocs/$theme";
654 foreach my $language (readdir D) {
655 next if $language=~/^\./;
656 next if $language eq 'all';
657 next if $language=~ /png$/;
658 next if $language=~ /css$/;
659 next if $language=~ /CVS$/;
660 next if $language=~ /itemtypeimg$/;
661 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
662 $lang->{$language}=1;
665 $htdocs=C4::Context->config('opachtdocs');
666 foreach my $theme (getallthemes('opac')) {
667 opendir D, "$htdocs/$theme";
668 foreach my $language (readdir D) {
669 next if $language=~/^\./;
670 next if $language eq 'all';
671 next if $language=~ /png$/;
672 next if $language=~ /css$/;
673 next if $language=~ /CVS$/;
674 next if $language=~ /itemtypeimg$/;
675 next if $language=~ /\.txt$/i; #Don't read the readme.txt !
676 $lang->{$language}=1;
679 @languages=keys %$lang;
680 return sort @languages;
686 (@themes) = &getallthemes('opac');
687 (@themes) = &getallthemes('intranet');
689 Returns an array of all available themes.
697 if ($type eq 'intranet') {
698 $htdocs=C4::Context->config('intrahtdocs');
700 $htdocs=C4::Context->config('opachtdocs');
702 opendir D, "$htdocs";
703 my @dirlist=readdir D;
704 foreach my $directory (@dirlist) {
705 -d "$htdocs/$directory/en" and push @themes, $directory;
712 Returns the number of pages to display in a pagination bar, given the number
713 of items and the number of items per page.
718 my ($nb_items, $nb_items_per_page) = @_;
720 return int(($nb_items - 1) / $nb_items_per_page) + 1;
724 =head2 getcities (OUEST-PROVENCE)
726 ($id_cityarrayref, $city_hashref) = &getcities();
728 Looks up the different city and zip in the database. Returns two
729 elements: a reference-to-array, which lists the zip city
730 codes, and a reference-to-hash, which maps the name of the city.
731 WHERE =>OUEST PROVENCE OR EXTERIEUR
735 #my ($type_city) = @_;
736 my $dbh = C4::Context->dbh;
737 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
738 #$sth->execute($type_city);
742 # insert empty value to create a empty choice in cgi popup
744 while (my $data=$sth->fetchrow_hashref){
746 push @id,$data->{'cityid'};
747 $city{$data->{'cityid'}}=$data->{'city_name'};
750 #test to know if the table contain some records if no the function return nothing
764 =head2 getroadtypes (OUEST-PROVENCE)
766 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
768 Looks up the different road type . Returns two
769 elements: a reference-to-array, which lists the id_roadtype
770 codes, and a reference-to-hash, which maps the road type of the road .
775 my $dbh = C4::Context->dbh;
776 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
780 # insert empty value to create a empty choice in cgi popup
781 while (my $data=$sth->fetchrow_hashref){
782 push @id,$data->{'roadtypeid'};
783 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
785 #test to know if the table contain some records if no the function return nothing
794 return(\@id,\%roadtype);
798 =head2 get_branchinfos_of
800 my $branchinfos_of = get_branchinfos_of(@branchcodes);
802 Associates a list of branchcodes to the information of the branch, taken in
805 Returns a href where keys are branchcodes and values are href where keys are
806 branch information key.
808 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
811 sub get_branchinfos_of {
812 my @branchcodes = @_;
818 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
820 return get_infos_of($query, 'branchcode');
823 =head2 get_notforloan_label_of
825 my $notforloan_label_of = get_notforloan_label_of();
827 Each authorised value of notforloan (information available in items and
828 itemtypes) is link to a single label.
830 Returns a href where keys are authorised values and values are corresponding
833 foreach my $authorised_value (keys %{$notforloan_label_of}) {
835 "authorised_value: %s => %s\n",
837 $notforloan_label_of->{$authorised_value}
842 sub get_notforloan_label_of {
843 my $dbh = C4::Context->dbh;
844 my($tagfield,$tagsubfield)=MARCfind_marc_from_kohafield("notforloan","holdings");
846 SELECT authorised_value
847 FROM holdings_subfield_structure
848 WHERE tagfield =$tagfield and tagsubfield=$tagsubfield
851 my $sth = $dbh->prepare($query);
853 my ($statuscode) = $sth->fetchrow_array();
858 FROM authorised_values
861 $sth = $dbh->prepare($query);
862 $sth->execute($statuscode);
863 my %notforloan_label_of;
864 while (my $row = $sth->fetchrow_hashref) {
865 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
869 return \%notforloan_label_of;
874 Return a href where a key is associated to a href. You give a query, the
875 name of the key among the fields returned by the query. If you also give as
876 third argument the name of the value, the function returns a href of scalar.
885 # generic href of any information on the item, href of href.
886 my $iteminfos_of = get_infos_of($query, 'itemnumber');
887 print $iteminfos_of->{$itemnumber}{barcode};
889 # specific information, href of scalar
890 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
891 print $barcode_of_item->{$itemnumber};
895 my ($query, $key_name, $value_name) = @_;
897 my $dbh = C4::Context->dbh;
899 my $sth = $dbh->prepare($query);
903 while (my $row = $sth->fetchrow_hashref) {
904 if (defined $value_name) {
905 $infos_of{ $row->{$key_name} } = $row->{$value_name};
908 $infos_of{ $row->{$key_name} } = $row;
916 ###Subfields is an array as well although MARC21 has them all in "a" in case UNIMARC has differing subfields
917 my $dbh=C4::Context->dbh;
919 my $sth=$dbh->prepare("SELECT facets_label,attr FROM koha_attr where (facets_label<>'' ) group by facets_label");
920 my $sth2=$dbh->prepare("SELECT * FROM koha_attr where facets_label=?");
922 while (my ($label,$attr)=$sth->fetchrow){
923 $sth2->execute($label);
924 my (@tags,@subfield);
925 while (my $data=$sth2->fetchrow_hashref){
926 push @tags,$data->{tagfield} ;
927 push @subfield,$data->{tagsubfield} ;
930 link_value =>"kohafield=$attr",
931 label_value =>$label,
933 subfield =>\@subfield,