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
24 use vars qw($VERSION @ISA @EXPORT);
30 C4::Koha - Perl Module containing convenience functions for Koha scripts
37 $date = slashifyDate("01-01-2002")
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
63 getitemtypeimagesrcfromurl
67 get_notforloan_label_of
75 # FIXME.. this should be moved to a MARC-specific module
76 sub subfield_is_koha_internal_p ($) {
79 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
80 # But real MARC subfields are always single-character
81 # so it really is safer just to check the length
83 return length $subfield != 1;
88 $branches = &getbranches();
89 returns informations about branches.
90 Create a branch selector with the following code
91 Is branchIndependant sensitive
92 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
96 my $branches = getbranches;
98 foreach my $thisbranch (sort keys %$branches) {
99 my $selected = 1 if $thisbranch eq $branch;
100 my %row =(value => $thisbranch,
101 selected => $selected,
102 branchname => $branches->{$thisbranch}->{'branchname'},
104 push @branchloop, \%row;
109 <select name="branch">
110 <option value="">Default</option>
111 <!-- TMPL_LOOP name="branchloop" -->
112 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
119 # returns a reference to a hash of references to branches...
121 my $dbh = C4::Context->dbh;
123 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
124 my $strsth ="Select * from branches ";
125 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
126 $strsth.= " order by branchname";
127 $sth=$dbh->prepare($strsth);
129 $sth = $dbh->prepare("Select * from branches order by branchname");
132 while (my $branch=$sth->fetchrow_hashref) {
133 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
134 $nsth->execute($branch->{'branchcode'});
135 while (my ($cat) = $nsth->fetchrow_array) {
136 # FIXME - This seems wrong. It ought to be
137 # $branch->{categorycodes}{$cat} = 1;
138 # otherwise, there's a namespace collision if there's a
139 # category with the same name as a field in the 'branches'
140 # table (i.e., don't create a category called "issuing").
141 # In addition, the current structure doesn't really allow
142 # you to list the categories that a branch belongs to:
143 # you'd have to list keys %$branch, and remove those keys
144 # that aren't fields in the "branches" table.
147 $branches{$branch->{'branchcode'}}=$branch;
152 =head2 getallbranches
154 $branches = &getallbranches();
155 returns informations about ALL branches.
156 Create a branch selector with the following code
157 IndependantBranches Insensitive...
159 =head3 in PERL SCRIPT
161 my $branches = getallbranches;
163 foreach my $thisbranch (keys %$branches) {
164 my $selected = 1 if $thisbranch eq $branch;
165 my %row =(value => $thisbranch,
166 selected => $selected,
167 branchname => $branches->{$thisbranch}->{'branchname'},
169 push @branchloop, \%row;
174 <select name="branch">
175 <option value="">Default</option>
176 <!-- TMPL_LOOP name="branchloop" -->
177 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
184 # returns a reference to a hash of references to ALL branches...
186 my $dbh = C4::Context->dbh;
188 $sth = $dbh->prepare("Select * from branches order by branchname");
190 while (my $branch=$sth->fetchrow_hashref) {
191 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
192 $nsth->execute($branch->{'branchcode'});
193 while (my ($cat) = $nsth->fetchrow_array) {
194 # FIXME - This seems wrong. It ought to be
195 # $branch->{categorycodes}{$cat} = 1;
196 # otherwise, there's a namespace collision if there's a
197 # category with the same name as a field in the 'branches'
198 # table (i.e., don't create a category called "issuing").
199 # In addition, the current structure doesn't really allow
200 # you to list the categories that a branch belongs to:
201 # you'd have to list keys %$branch, and remove those keys
202 # that aren't fields in the "branches" table.
205 $branches{$branch->{'branchcode'}}=$branch;
212 $letters = &getletters($category);
213 returns informations about letters.
214 if needed, $category filters for letters given category
215 Create a letter selector with the following code
217 =head3 in PERL SCRIPT
219 my $letters = getletters($cat);
221 foreach my $thisletter (keys %$letters) {
222 my $selected = 1 if $thisletter eq $letter;
223 my %row =(value => $thisletter,
224 selected => $selected,
225 lettername => $letters->{$thisletter},
227 push @letterloop, \%row;
232 <select name="letter">
233 <option value="">Default</option>
234 <!-- TMPL_LOOP name="letterloop" -->
235 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
242 # returns a reference to a hash of references to ALL letters...
245 my $dbh = C4::Context->dbh;
248 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
250 $sth = $dbh->prepare("Select * from letter order by name");
254 while (my $letter=$sth->fetchrow_hashref) {
255 $letters{$letter->{'code'}}=$letter->{'name'};
258 return ($count,\%letters);
263 $itemtypes = &getitemtypes();
265 Returns information about existing itemtypes.
267 build a HTML select with the following code :
269 =head3 in PERL SCRIPT
271 my $itemtypes = getitemtypes;
273 foreach my $thisitemtype (sort keys %$itemtypes) {
274 my $selected = 1 if $thisitemtype eq $itemtype;
275 my %row =(value => $thisitemtype,
276 selected => $selected,
277 description => $itemtypes->{$thisitemtype}->{'description'},
279 push @itemtypesloop, \%row;
281 $template->param(itemtypeloop => \@itemtypesloop);
285 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
286 <select name="itemtype">
287 <option value="">Default</option>
288 <!-- TMPL_LOOP name="itemtypeloop" -->
289 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
292 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
293 <input type="submit" value="OK" class="button">
300 # returns a reference to a hash of references to branches...
302 my $dbh = C4::Context->dbh;
303 my $sth=$dbh->prepare("select * from itemtypes");
305 while (my $IT=$sth->fetchrow_hashref) {
306 $itemtypes{$IT->{'itemtype'}}=$IT;
308 return (\%itemtypes);
311 # FIXME this function is better and should replace getitemtypes everywhere
312 sub get_itemtypeinfos_of {
320 WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
323 return get_infos_of($query, 'itemtype');
328 $authtypes = &getauthtypes();
330 Returns information about existing authtypes.
332 build a HTML select with the following code :
334 =head3 in PERL SCRIPT
336 my $authtypes = getauthtypes;
338 foreach my $thisauthtype (keys %$authtypes) {
339 my $selected = 1 if $thisauthtype eq $authtype;
340 my %row =(value => $thisauthtype,
341 selected => $selected,
342 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
344 push @authtypesloop, \%row;
346 $template->param(itemtypeloop => \@itemtypesloop);
350 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
351 <select name="authtype">
352 <!-- TMPL_LOOP name="authtypeloop" -->
353 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
356 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
357 <input type="submit" value="OK" class="button">
364 # returns a reference to a hash of references to authtypes...
366 my $dbh = C4::Context->dbh;
367 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
369 while (my $IT=$sth->fetchrow_hashref) {
370 $authtypes{$IT->{'authtypecode'}}=$IT;
372 return (\%authtypes);
376 my ($authtypecode) = @_;
377 # returns a reference to a hash of references to authtypes...
379 my $dbh = C4::Context->dbh;
380 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
381 $sth->execute($authtypecode);
382 my $res=$sth->fetchrow_hashref;
388 $frameworks = &getframework();
390 Returns information about existing frameworks
392 build a HTML select with the following code :
394 =head3 in PERL SCRIPT
396 my $frameworks = frameworks();
398 foreach my $thisframework (keys %$frameworks) {
399 my $selected = 1 if $thisframework eq $frameworkcode;
400 my %row =(value => $thisframework,
401 selected => $selected,
402 description => $frameworks->{$thisframework}->{'frameworktext'},
404 push @frameworksloop, \%row;
406 $template->param(frameworkloop => \@frameworksloop);
410 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
411 <select name="frameworkcode">
412 <option value="">Default</option>
413 <!-- TMPL_LOOP name="frameworkloop" -->
414 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
417 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
418 <input type="submit" value="OK" class="button">
425 # returns a reference to a hash of references to branches...
427 my $dbh = C4::Context->dbh;
428 my $sth=$dbh->prepare("select * from biblio_framework");
430 while (my $IT=$sth->fetchrow_hashref) {
431 $itemtypes{$IT->{'frameworkcode'}}=$IT;
433 return (\%itemtypes);
435 =head2 getframeworkinfo
437 $frameworkinfo = &getframeworkinfo($frameworkcode);
439 Returns information about an frameworkcode.
443 sub getframeworkinfo {
444 my ($frameworkcode) = @_;
445 my $dbh = C4::Context->dbh;
446 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
447 $sth->execute($frameworkcode);
448 my $res = $sth->fetchrow_hashref;
453 =head2 getitemtypeinfo
455 $itemtype = &getitemtype($itemtype);
457 Returns information about an itemtype.
461 sub getitemtypeinfo {
463 my $dbh = C4::Context->dbh;
464 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
465 $sth->execute($itemtype);
466 my $res = $sth->fetchrow_hashref;
468 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
473 sub getitemtypeimagesrcfromurl {
476 if (defined $imageurl and $imageurl !~ m/^http/) {
478 getitemtypeimagesrc()
486 sub getitemtypeimagedir {
488 C4::Context->intrahtdocs
489 .'/'.C4::Context->preference('template')
494 sub getitemtypeimagesrc {
497 .'/'.C4::Context->preference('template')
504 $printers = &getprinters($env);
505 @queues = keys %$printers;
507 Returns information about existing printer queues.
511 C<$printers> is a reference-to-hash whose keys are the print queues
512 defined in the printers table of the Koha database. The values are
513 references-to-hash, whose keys are the fields in the printers table.
520 my $dbh = C4::Context->dbh;
521 my $sth=$dbh->prepare("select * from printers");
523 while (my $printer=$sth->fetchrow_hashref) {
524 $printers{$printer->{'printqueue'}}=$printer;
530 my($query, $branches) = @_; # get branch for this query from branches
531 my $branch = $query->param('branch');
532 ($branch) || ($branch = $query->cookie('branch'));
533 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
537 =item getbranchdetail
539 $branchname = &getbranchdetail($branchcode);
541 Given the branch code, the function returns the corresponding
542 branch name for a comprehensive information display
548 my ($branchcode) = @_;
549 my $dbh = C4::Context->dbh;
550 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
551 $sth->execute($branchcode);
552 my $branchname = $sth->fetchrow_hashref();
555 } # sub getbranchname
558 sub getprinter ($$) {
559 my($query, $printers) = @_; # get printer for this query from printers
560 my $printer = $query->param('printer');
561 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
562 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
566 =item getalllanguages
568 (@languages) = &getalllanguages($type);
569 (@languages) = &getalllanguages($type,$theme);
571 Returns an array of all available languages.
575 sub getalllanguages {
580 if ($type eq 'opac') {
581 $htdocs=C4::Context->config('opachtdocs');
582 if ($theme and -d "$htdocs/$theme") {
583 opendir D, "$htdocs/$theme";
584 foreach my $language (readdir D) {
585 next if $language=~/^\./;
586 next if $language eq 'all';
587 next if $language=~ /png$/;
588 next if $language=~ /css$/;
589 next if $language=~ /CVS$/;
590 next if $language=~ /itemtypeimg$/;
591 push @languages, $language;
593 return sort @languages;
596 foreach my $theme (getallthemes('opac')) {
597 opendir D, "$htdocs/$theme";
598 foreach my $language (readdir D) {
599 next if $language=~/^\./;
600 next if $language eq 'all';
601 next if $language=~ /png$/;
602 next if $language=~ /css$/;
603 next if $language=~ /CVS$/;
604 next if $language=~ /itemtypeimg$/;
605 $lang->{$language}=1;
608 @languages=keys %$lang;
609 return sort @languages;
611 } elsif ($type eq 'intranet') {
612 $htdocs=C4::Context->config('intrahtdocs');
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;
644 my $htdocs=C4::Context->config('intrahtdocs');
645 foreach my $theme (getallthemes('intranet')) {
646 opendir D, "$htdocs/$theme";
647 foreach my $language (readdir D) {
648 next if $language=~/^\./;
649 next if $language eq 'all';
650 next if $language=~ /png$/;
651 next if $language=~ /css$/;
652 next if $language=~ /CVS$/;
653 next if $language=~ /itemtypeimg$/;
654 $lang->{$language}=1;
657 $htdocs=C4::Context->config('opachtdocs');
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;
677 (@themes) = &getallthemes('opac');
678 (@themes) = &getallthemes('intranet');
680 Returns an array of all available themes.
688 if ($type eq 'intranet') {
689 $htdocs=C4::Context->config('intrahtdocs');
691 $htdocs=C4::Context->config('opachtdocs');
693 opendir D, "$htdocs";
694 my @dirlist=readdir D;
695 foreach my $directory (@dirlist) {
696 -d "$htdocs/$directory/en" and push @themes, $directory;
703 Returns the number of pages to display in a pagination bar, given the number
704 of items and the number of items per page.
709 my ($nb_items, $nb_items_per_page) = @_;
711 return int(($nb_items - 1) / $nb_items_per_page) + 1;
715 =head2 getcities (OUEST-PROVENCE)
717 ($id_cityarrayref, $city_hashref) = &getcities();
719 Looks up the different city and zip in the database. Returns two
720 elements: a reference-to-array, which lists the zip city
721 codes, and a reference-to-hash, which maps the name of the city.
722 WHERE =>OUEST PROVENCE OR EXTERIEUR
726 #my ($type_city) = @_;
727 my $dbh = C4::Context->dbh;
728 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
729 #$sth->execute($type_city);
733 # insert empty value to create a empty choice in cgi popup
735 while (my $data=$sth->fetchrow_hashref){
737 push @id,$data->{'cityid'};
738 $city{$data->{'cityid'}}=$data->{'city_name'};
741 #test to know if the table contain some records if no the function return nothing
755 =head2 getroadtypes (OUEST-PROVENCE)
757 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
759 Looks up the different road type . Returns two
760 elements: a reference-to-array, which lists the id_roadtype
761 codes, and a reference-to-hash, which maps the road type of the road .
766 my $dbh = C4::Context->dbh;
767 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
771 # insert empty value to create a empty choice in cgi popup
772 while (my $data=$sth->fetchrow_hashref){
773 push @id,$data->{'roadtypeid'};
774 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
776 #test to know if the table contain some records if no the function return nothing
785 return(\@id,\%roadtype);
789 =head2 get_branchinfos_of
791 my $branchinfos_of = get_branchinfos_of(@branchcodes);
793 Associates a list of branchcodes to the information of the branch, taken in
796 Returns a href where keys are branchcodes and values are href where keys are
797 branch information key.
799 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
802 sub get_branchinfos_of {
803 my @branchcodes = @_;
809 WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
811 return get_infos_of($query, 'branchcode');
814 =head2 get_notforloan_label_of
816 my $notforloan_label_of = get_notforloan_label_of();
818 Each authorised value of notforloan (information available in items and
819 itemtypes) is link to a single label.
821 Returns a href where keys are authorised values and values are corresponding
824 foreach my $authorised_value (keys %{$notforloan_label_of}) {
826 "authorised_value: %s => %s\n",
828 $notforloan_label_of->{$authorised_value}
833 sub get_notforloan_label_of {
834 my $dbh = C4::Context->dbh;
837 SELECT authorised_value
838 FROM marc_subfield_structure
839 WHERE kohafield = \'items.notforloan\'
842 my $sth = $dbh->prepare($query);
844 my ($statuscode) = $sth->fetchrow_array();
849 FROM authorised_values
852 $sth = $dbh->prepare($query);
853 $sth->execute($statuscode);
854 my %notforloan_label_of;
855 while (my $row = $sth->fetchrow_hashref) {
856 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
860 return \%notforloan_label_of;
865 Return a href where a key is associated to a href. You give a query, the
866 name of the key among the fields returned by the query. If you also give as
867 third argument the name of the value, the function returns a href of scalar.
876 # generic href of any information on the item, href of href.
877 my $iteminfos_of = get_infos_of($query, 'itemnumber');
878 print $iteminfos_of->{$itemnumber}{barcode};
880 # specific information, href of scalar
881 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
882 print $barcode_of_item->{$itemnumber};
886 my ($query, $key_name, $value_name) = @_;
888 my $dbh = C4::Context->dbh;
890 my $sth = $dbh->prepare($query);
894 while (my $row = $sth->fetchrow_hashref) {
895 if (defined $value_name) {
896 $infos_of{ $row->{$key_name} } = $row->{$value_name};
899 $infos_of{ $row->{$key_name} } = $row;