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
55 &getframeworks &getframeworkinfo
56 &getauthtypes &getauthtype
57 &getallthemes &getalllanguages
58 &getallbranches &getletters
62 getitemtypeimagesrcfromurl
71 # FIXME.. this should be moved to a MARC-specific module
72 sub subfield_is_koha_internal_p ($) {
75 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
76 # But real MARC subfields are always single-character
77 # so it really is safer just to check the length
79 return length $subfield != 1;
84 $branches = &getbranches();
85 returns informations about branches.
86 Create a branch selector with the following code
87 Is branchIndependant sensitive
88 When IndependantBranches is set AND user is not superlibrarian, displays only user's branch
92 my $branches = getbranches;
94 foreach my $thisbranch (sort keys %$branches) {
95 my $selected = 1 if $thisbranch eq $branch;
96 my %row =(value => $thisbranch,
97 selected => $selected,
98 branchname => $branches->{$thisbranch}->{'branchname'},
100 push @branchloop, \%row;
105 <select name="branch">
106 <option value="">Default</option>
107 <!-- TMPL_LOOP name="branchloop" -->
108 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
115 # returns a reference to a hash of references to branches...
117 my $dbh = C4::Context->dbh;
119 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
120 my $strsth ="Select * from branches ";
121 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
122 $strsth.= " order by branchname";
123 $sth=$dbh->prepare($strsth);
125 $sth = $dbh->prepare("Select * from branches order by branchname");
128 while (my $branch=$sth->fetchrow_hashref) {
129 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
130 $nsth->execute($branch->{'branchcode'});
131 while (my ($cat) = $nsth->fetchrow_array) {
132 # FIXME - This seems wrong. It ought to be
133 # $branch->{categorycodes}{$cat} = 1;
134 # otherwise, there's a namespace collision if there's a
135 # category with the same name as a field in the 'branches'
136 # table (i.e., don't create a category called "issuing").
137 # In addition, the current structure doesn't really allow
138 # you to list the categories that a branch belongs to:
139 # you'd have to list keys %$branch, and remove those keys
140 # that aren't fields in the "branches" table.
143 $branches{$branch->{'branchcode'}}=$branch;
148 =head2 getallbranches
150 $branches = &getallbranches();
151 returns informations about ALL branches.
152 Create a branch selector with the following code
153 IndependantBranches Insensitive...
155 =head3 in PERL SCRIPT
157 my $branches = getallbranches;
159 foreach my $thisbranch (keys %$branches) {
160 my $selected = 1 if $thisbranch eq $branch;
161 my %row =(value => $thisbranch,
162 selected => $selected,
163 branchname => $branches->{$thisbranch}->{'branchname'},
165 push @branchloop, \%row;
170 <select name="branch">
171 <option value="">Default</option>
172 <!-- TMPL_LOOP name="branchloop" -->
173 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
180 # returns a reference to a hash of references to ALL branches...
182 my $dbh = C4::Context->dbh;
184 $sth = $dbh->prepare("Select * from branches order by branchname");
186 while (my $branch=$sth->fetchrow_hashref) {
187 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
188 $nsth->execute($branch->{'branchcode'});
189 while (my ($cat) = $nsth->fetchrow_array) {
190 # FIXME - This seems wrong. It ought to be
191 # $branch->{categorycodes}{$cat} = 1;
192 # otherwise, there's a namespace collision if there's a
193 # category with the same name as a field in the 'branches'
194 # table (i.e., don't create a category called "issuing").
195 # In addition, the current structure doesn't really allow
196 # you to list the categories that a branch belongs to:
197 # you'd have to list keys %$branch, and remove those keys
198 # that aren't fields in the "branches" table.
201 $branches{$branch->{'branchcode'}}=$branch;
208 $letters = &getletters($category);
209 returns informations about letters.
210 if needed, $category filters for letters given category
211 Create a letter selector with the following code
213 =head3 in PERL SCRIPT
215 my $letters = getletters($cat);
217 foreach my $thisletter (keys %$letters) {
218 my $selected = 1 if $thisletter eq $letter;
219 my %row =(value => $thisletter,
220 selected => $selected,
221 lettername => $letters->{$thisletter},
223 push @letterloop, \%row;
228 <select name="letter">
229 <option value="">Default</option>
230 <!-- TMPL_LOOP name="letterloop" -->
231 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
238 # returns a reference to a hash of references to ALL letters...
241 my $dbh = C4::Context->dbh;
244 $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
246 $sth = $dbh->prepare("Select * from letter order by name");
250 while (my $letter=$sth->fetchrow_hashref) {
251 $letters{$letter->{'code'}}=$letter->{'name'};
254 return ($count,\%letters);
259 $itemtypes = &getitemtypes();
261 Returns information about existing itemtypes.
263 build a HTML select with the following code :
265 =head3 in PERL SCRIPT
267 my $itemtypes = getitemtypes;
269 foreach my $thisitemtype (sort keys %$itemtypes) {
270 my $selected = 1 if $thisitemtype eq $itemtype;
271 my %row =(value => $thisitemtype,
272 selected => $selected,
273 description => $itemtypes->{$thisitemtype}->{'description'},
275 push @itemtypesloop, \%row;
277 $template->param(itemtypeloop => \@itemtypesloop);
281 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
282 <select name="itemtype">
283 <option value="">Default</option>
284 <!-- TMPL_LOOP name="itemtypeloop" -->
285 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
288 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
289 <input type="submit" value="OK" class="button">
296 # returns a reference to a hash of references to branches...
298 my $dbh = C4::Context->dbh;
299 my $sth=$dbh->prepare("select * from itemtypes");
301 while (my $IT=$sth->fetchrow_hashref) {
302 $itemtypes{$IT->{'itemtype'}}=$IT;
304 return (\%itemtypes);
309 $authtypes = &getauthtypes();
311 Returns information about existing authtypes.
313 build a HTML select with the following code :
315 =head3 in PERL SCRIPT
317 my $authtypes = getauthtypes;
319 foreach my $thisauthtype (keys %$authtypes) {
320 my $selected = 1 if $thisauthtype eq $authtype;
321 my %row =(value => $thisauthtype,
322 selected => $selected,
323 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
325 push @authtypesloop, \%row;
327 $template->param(itemtypeloop => \@itemtypesloop);
331 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
332 <select name="authtype">
333 <!-- TMPL_LOOP name="authtypeloop" -->
334 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
337 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
338 <input type="submit" value="OK" class="button">
345 # returns a reference to a hash of references to authtypes...
347 my $dbh = C4::Context->dbh;
348 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
350 while (my $IT=$sth->fetchrow_hashref) {
351 $authtypes{$IT->{'authtypecode'}}=$IT;
353 return (\%authtypes);
357 my ($authtypecode) = @_;
358 # returns a reference to a hash of references to authtypes...
360 my $dbh = C4::Context->dbh;
361 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
362 $sth->execute($authtypecode);
363 my $res=$sth->fetchrow_hashref;
369 $frameworks = &getframework();
371 Returns information about existing frameworks
373 build a HTML select with the following code :
375 =head3 in PERL SCRIPT
377 my $frameworks = frameworks();
379 foreach my $thisframework (keys %$frameworks) {
380 my $selected = 1 if $thisframework eq $frameworkcode;
381 my %row =(value => $thisframework,
382 selected => $selected,
383 description => $frameworks->{$thisframework}->{'frameworktext'},
385 push @frameworksloop, \%row;
387 $template->param(frameworkloop => \@frameworksloop);
391 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
392 <select name="frameworkcode">
393 <option value="">Default</option>
394 <!-- TMPL_LOOP name="frameworkloop" -->
395 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
398 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
399 <input type="submit" value="OK" class="button">
406 # returns a reference to a hash of references to branches...
408 my $dbh = C4::Context->dbh;
409 my $sth=$dbh->prepare("select * from biblio_framework");
411 while (my $IT=$sth->fetchrow_hashref) {
412 $itemtypes{$IT->{'frameworkcode'}}=$IT;
414 return (\%itemtypes);
416 =head2 getframeworkinfo
418 $frameworkinfo = &getframeworkinfo($frameworkcode);
420 Returns information about an frameworkcode.
424 sub getframeworkinfo {
425 my ($frameworkcode) = @_;
426 my $dbh = C4::Context->dbh;
427 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
428 $sth->execute($frameworkcode);
429 my $res = $sth->fetchrow_hashref;
434 =head2 getitemtypeinfo
436 $itemtype = &getitemtype($itemtype);
438 Returns information about an itemtype.
442 sub getitemtypeinfo {
444 my $dbh = C4::Context->dbh;
445 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
446 $sth->execute($itemtype);
447 my $res = $sth->fetchrow_hashref;
449 $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
454 sub getitemtypeimagesrcfromurl {
457 if (defined $imageurl and $imageurl !~ m/^http/) {
459 getitemtypeimagesrc()
467 sub getitemtypeimagedir {
469 C4::Context->intrahtdocs
470 .'/'.C4::Context->preference('template')
475 sub getitemtypeimagesrc {
478 .'/'.C4::Context->preference('template')
485 $printers = &getprinters($env);
486 @queues = keys %$printers;
488 Returns information about existing printer queues.
492 C<$printers> is a reference-to-hash whose keys are the print queues
493 defined in the printers table of the Koha database. The values are
494 references-to-hash, whose keys are the fields in the printers table.
501 my $dbh = C4::Context->dbh;
502 my $sth=$dbh->prepare("select * from printers");
504 while (my $printer=$sth->fetchrow_hashref) {
505 $printers{$printer->{'printqueue'}}=$printer;
511 my($query, $branches) = @_; # get branch for this query from branches
512 my $branch = $query->param('branch');
513 ($branch) || ($branch = $query->cookie('branch'));
514 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
518 =item getbranchdetail
520 $branchname = &getbranchdetail($branchcode);
522 Given the branch code, the function returns the corresponding
523 branch name for a comprehensive information display
529 my ($branchcode) = @_;
530 my $dbh = C4::Context->dbh;
531 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
532 $sth->execute($branchcode);
533 my $branchname = $sth->fetchrow_hashref();
536 } # sub getbranchname
539 sub getprinter ($$) {
540 my($query, $printers) = @_; # get printer for this query from printers
541 my $printer = $query->param('printer');
542 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
543 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
547 =item getalllanguages
549 (@languages) = &getalllanguages($type);
550 (@languages) = &getalllanguages($type,$theme);
552 Returns an array of all available languages.
556 sub getalllanguages {
561 if ($type eq 'opac') {
562 $htdocs=C4::Context->config('opachtdocs');
563 if ($theme and -d "$htdocs/$theme") {
564 opendir D, "$htdocs/$theme";
565 foreach my $language (readdir D) {
566 next if $language=~/^\./;
567 next if $language eq 'all';
568 next if $language=~ /png$/;
569 next if $language=~ /css$/;
570 next if $language=~ /CVS$/;
571 next if $language=~ /itemtypeimg$/;
572 push @languages, $language;
574 return sort @languages;
577 foreach my $theme (getallthemes('opac')) {
578 opendir D, "$htdocs/$theme";
579 foreach my $language (readdir D) {
580 next if $language=~/^\./;
581 next if $language eq 'all';
582 next if $language=~ /png$/;
583 next if $language=~ /css$/;
584 next if $language=~ /CVS$/;
585 next if $language=~ /itemtypeimg$/;
586 $lang->{$language}=1;
589 @languages=keys %$lang;
590 return sort @languages;
592 } elsif ($type eq 'intranet') {
593 $htdocs=C4::Context->config('intrahtdocs');
594 if ($theme and -d "$htdocs/$theme") {
595 opendir D, "$htdocs/$theme";
596 foreach my $language (readdir D) {
597 next if $language=~/^\./;
598 next if $language eq 'all';
599 next if $language=~ /png$/;
600 next if $language=~ /css$/;
601 next if $language=~ /CVS$/;
602 next if $language=~ /itemtypeimg$/;
603 push @languages, $language;
605 return sort @languages;
608 foreach my $theme (getallthemes('opac')) {
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 $lang->{$language}=1;
620 @languages=keys %$lang;
621 return sort @languages;
625 my $htdocs=C4::Context->config('intrahtdocs');
626 foreach my $theme (getallthemes('intranet')) {
627 opendir D, "$htdocs/$theme";
628 foreach my $language (readdir D) {
629 next if $language=~/^\./;
630 next if $language eq 'all';
631 next if $language=~ /png$/;
632 next if $language=~ /css$/;
633 next if $language=~ /CVS$/;
634 next if $language=~ /itemtypeimg$/;
635 $lang->{$language}=1;
638 $htdocs=C4::Context->config('opachtdocs');
639 foreach my $theme (getallthemes('opac')) {
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 $lang->{$language}=1;
651 @languages=keys %$lang;
652 return sort @languages;
658 (@themes) = &getallthemes('opac');
659 (@themes) = &getallthemes('intranet');
661 Returns an array of all available themes.
669 if ($type eq 'intranet') {
670 $htdocs=C4::Context->config('intrahtdocs');
672 $htdocs=C4::Context->config('opachtdocs');
674 opendir D, "$htdocs";
675 my @dirlist=readdir D;
676 foreach my $directory (@dirlist) {
677 -d "$htdocs/$directory/en" and push @themes, $directory;
684 Returns the number of pages to display in a pagination bar, given the number
685 of items and the number of items per page.
690 my ($nb_items, $nb_items_per_page) = @_;
692 return int(($nb_items - 1) / $nb_items_per_page) + 1;
696 =head2 getcities (OUEST-PROVENCE)
698 ($id_cityarrayref, $city_hashref) = &getcities();
700 Looks up the different city and zip in the database. Returns two
701 elements: a reference-to-array, which lists the zip city
702 codes, and a reference-to-hash, which maps the name of the city.
703 WHERE =>OUEST PROVENCE OR EXTERIEUR
707 #my ($type_city) = @_;
708 my $dbh = C4::Context->dbh;
709 my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid ");
710 #$sth->execute($type_city);
714 # insert empty value to create a empty choice in cgi popup
716 while (my $data=$sth->fetchrow_hashref){
718 push @id,$data->{'cityid'};
719 $city{$data->{'cityid'}}=$data->{'city_name'};
722 #test to know if the table contain some records if no the function return nothing
736 =head2 getroadtypes (OUEST-PROVENCE)
738 ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
740 Looks up the different road type . Returns two
741 elements: a reference-to-array, which lists the id_roadtype
742 codes, and a reference-to-hash, which maps the road type of the road .
747 my $dbh = C4::Context->dbh;
748 my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type ");
752 # insert empty value to create a empty choice in cgi popup
753 while (my $data=$sth->fetchrow_hashref){
754 push @id,$data->{'roadtypeid'};
755 $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
757 #test to know if the table contain some records if no the function return nothing
766 return(\@id,\%roadtype);