Code cleaning :
[koha.git] / C4 / Koha.pm
1 package C4::Koha;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 # $Id$
21
22 use strict;
23 require Exporter;
24 use C4::Context;
25
26 use vars qw($VERSION @ISA @EXPORT);
27
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
29
30 =head1 NAME
31
32 C4::Koha - Perl Module containing convenience functions for Koha scripts
33
34 =head1 SYNOPSIS
35
36   use C4::Koha;
37
38
39 =head1 DESCRIPTION
40
41 Koha.pm provides many functions for Koha scripts.
42
43 =head1 FUNCTIONS
44
45 =over 2
46
47 =cut
48
49 @ISA = qw(Exporter);
50 @EXPORT = qw(
51             &subfield_is_koha_internal_p
52             &getbranches &getbranch &getbranchdetail
53             &getprinters &getprinter
54             &GetItemTypes &getitemtypeinfo
55                         get_itemtypeinfos_of
56             &getframeworks &getframeworkinfo
57             &getauthtypes &getauthtype
58             &getallthemes &getalllanguages
59             &getallbranches &getletters
60             &getbranchname
61                         getnbpages
62                         getitemtypeimagedir
63                         getitemtypeimagesrc
64                         getitemtypeimagesrcfromurl
65             &getcities
66             &getroadtypes
67                         get_branchinfos_of
68                         get_notforloan_label_of
69                         get_infos_of
70             $DEBUG);
71
72 use vars qw();
73
74 my $DEBUG = 0;
75
76 # FIXME.. this should be moved to a MARC-specific module
77 sub subfield_is_koha_internal_p ($) {
78     my($subfield) = @_;
79
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
83
84     return length $subfield != 1;
85 }
86
87 =head2 getbranches
88
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
94   
95 =head3 in PERL SCRIPT
96
97 my $branches = getbranches;
98 my @branchloop;
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'},
104             );
105     push @branchloop, \%row;
106 }
107
108
109 =head3 in TEMPLATE  
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>
114             <!-- /TMPL_LOOP -->
115             </select>
116
117 =cut
118
119 sub getbranches {
120 # returns a reference to a hash of references to branches...
121         my ($type) = @_;
122     my %branches;
123     my $branch;
124     my $dbh = C4::Context->dbh;
125     my $sth;
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);
131     } else {
132         $sth = $dbh->prepare("Select * from branches order by branchname");
133     }
134     $sth->execute;
135     while ($branch=$sth->fetchrow_hashref) {
136         my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
137             if ($type){
138             $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ? and categorycode = ?");
139             $nsth->execute($branch->{'branchcode'},$type);
140         } else {
141             $nsth->execute($branch->{'branchcode'});
142         }
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.
153             $branch->{$cat} = 1;
154             }
155                         if ($type) {
156                 $branches{$branch->{'branchcode'}}=$branch;
157             }
158             }
159                 if (!$type){
160             $branches{$branch->{'branchcode'}}=$branch;
161         }
162
163     return (\%branches);
164 }
165
166 sub getbranchname {
167     my ($branchcode)=@_;
168     my $dbh = C4::Context->dbh;
169     my $sth;
170        $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
171     $sth->execute($branchcode);
172     my $branchname = $sth->fetchrow_array;
173     $sth->finish;
174     
175     return($branchname);
176 }
177
178 =head2 getallbranches
179
180   $branches = &getallbranches();
181   returns informations about ALL branches.
182   Create a branch selector with the following code
183   IndependantBranches Insensitive...
184   
185 =head3 in PERL SCRIPT
186
187 my $branches = getallbranches;
188 my @branchloop;
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'},
194             );
195     push @branchloop, \%row;
196 }
197
198
199 =head3 in TEMPLATE  
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>
204             <!-- /TMPL_LOOP -->
205             </select>
206
207 =cut
208
209
210 sub getallbranches {
211 # returns a reference to a hash of references to ALL branches...
212     my %branches;
213     my $dbh = C4::Context->dbh;
214     my $sth;
215        $sth = $dbh->prepare("Select * from branches order by branchname");
216     $sth->execute;
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.
230             $branch->{$cat} = 1;
231             }
232             $branches{$branch->{'branchcode'}}=$branch;
233     }
234     return (\%branches);
235 }
236
237 =head2 getletters
238
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
243   
244 =head3 in PERL SCRIPT
245
246 my $letters = getletters($cat);
247 my @letterloop;
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},
253             );
254     push @letterloop, \%row;
255 }
256
257
258 =head3 in TEMPLATE  
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>
263             <!-- /TMPL_LOOP -->
264             </select>
265
266 =cut
267
268 sub getletters {
269 # returns a reference to a hash of references to ALL letters...
270     my $cat =@_;
271     my %letters;
272     my $dbh = C4::Context->dbh;
273     my $sth;
274        if ($cat ne ""){
275         $sth = $dbh->prepare("Select * from letter where module = \'".$cat."\' order by name");
276     } else {
277         $sth = $dbh->prepare("Select * from letter order by name");
278     }
279     $sth->execute;
280     my $count;
281     while (my $letter=$sth->fetchrow_hashref) {
282             $letters{$letter->{'code'}}=$letter->{'name'};
283             $count++;
284     }
285     return ($count,\%letters);
286 }
287
288 =head2 GetItemTypes
289
290   $itemtypes = &GetItemTypes();
291
292 Returns information about existing itemtypes.
293
294 build a HTML select with the following code :
295
296 =head3 in PERL SCRIPT
297
298 my $itemtypes = GetItemTypes;
299 my @itemtypesloop;
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'},
305             );
306     push @itemtypesloop, \%row;
307 }
308 $template->param(itemtypeloop => \@itemtypesloop);
309
310 =head3 in TEMPLATE
311
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>
317     <!-- /TMPL_LOOP -->
318     </select>
319     <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
320     <input type="submit" value="OK" class="button">
321 </form>
322
323
324 =cut
325
326 sub GetItemTypes {
327 # returns a reference to a hash of references to branches...
328     my %itemtypes;
329     my $dbh = C4::Context->dbh;
330     my $query = qq|
331         SELECT *
332         FROM   itemtypes
333     |;
334     my $sth=$dbh->prepare($query);
335     $sth->execute;
336     while (my $IT=$sth->fetchrow_hashref) {
337             $itemtypes{$IT->{'itemtype'}}=$IT;
338     }
339     return (\%itemtypes);
340 }
341
342 # FIXME this function is better and should replace GetItemTypes everywhere
343 sub get_itemtypeinfos_of {
344     my @itemtypes = @_;
345
346     my $query = '
347 SELECT itemtype,
348        description,
349        notforloan
350   FROM itemtypes
351   WHERE itemtype IN ('.join(',', map({"'".$_."'"} @itemtypes)).')
352 ';
353
354     return get_infos_of($query, 'itemtype');
355 }
356
357 =head2 getauthtypes
358
359   $authtypes = &getauthtypes();
360
361 Returns information about existing authtypes.
362
363 build a HTML select with the following code :
364
365 =head3 in PERL SCRIPT
366
367 my $authtypes = getauthtypes;
368 my @authtypesloop;
369 foreach my $thisauthtype (keys %$authtypes) {
370     my $selected = 1 if $thisauthtype eq $authtype;
371     my %row =(value => $thisauthtype,
372                 selected => $selected,
373                 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
374             );
375     push @authtypesloop, \%row;
376 }
377 $template->param(itemtypeloop => \@itemtypesloop);
378
379 =head3 in TEMPLATE
380
381 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
382     <select name="authtype">
383     <!-- TMPL_LOOP name="authtypeloop" -->
384         <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
385     <!-- /TMPL_LOOP -->
386     </select>
387     <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
388     <input type="submit" value="OK" class="button">
389 </form>
390
391
392 =cut
393
394 sub getauthtypes {
395 # returns a reference to a hash of references to authtypes...
396     my %authtypes;
397     my $dbh = C4::Context->dbh;
398     my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
399     $sth->execute;
400     while (my $IT=$sth->fetchrow_hashref) {
401             $authtypes{$IT->{'authtypecode'}}=$IT;
402     }
403     return (\%authtypes);
404 }
405
406 sub getauthtype {
407     my ($authtypecode) = @_;
408 # returns a reference to a hash of references to authtypes...
409     my %authtypes;
410     my $dbh = C4::Context->dbh;
411     my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
412     $sth->execute($authtypecode);
413     my $res=$sth->fetchrow_hashref;
414     return $res;
415 }
416
417 =head2 getframework
418
419   $frameworks = &getframework();
420
421 Returns information about existing frameworks
422
423 build a HTML select with the following code :
424
425 =head3 in PERL SCRIPT
426
427 my $frameworks = frameworks();
428 my @frameworkloop;
429 foreach my $thisframework (keys %$frameworks) {
430     my $selected = 1 if $thisframework eq $frameworkcode;
431     my %row =(value => $thisframework,
432                 selected => $selected,
433                 description => $frameworks->{$thisframework}->{'frameworktext'},
434             );
435     push @frameworksloop, \%row;
436 }
437 $template->param(frameworkloop => \@frameworksloop);
438
439 =head3 in TEMPLATE
440
441 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
442     <select name="frameworkcode">
443         <option value="">Default</option>
444     <!-- TMPL_LOOP name="frameworkloop" -->
445         <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
446     <!-- /TMPL_LOOP -->
447     </select>
448     <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
449     <input type="submit" value="OK" class="button">
450 </form>
451
452
453 =cut
454
455 sub getframeworks {
456 # returns a reference to a hash of references to branches...
457     my %itemtypes;
458     my $dbh = C4::Context->dbh;
459     my $sth=$dbh->prepare("select * from biblio_framework");
460     $sth->execute;
461     while (my $IT=$sth->fetchrow_hashref) {
462             $itemtypes{$IT->{'frameworkcode'}}=$IT;
463     }
464     return (\%itemtypes);
465 }
466 =head2 getframeworkinfo
467
468   $frameworkinfo = &getframeworkinfo($frameworkcode);
469
470 Returns information about an frameworkcode.
471
472 =cut
473
474 sub getframeworkinfo {
475     my ($frameworkcode) = @_;
476     my $dbh = C4::Context->dbh;
477     my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
478     $sth->execute($frameworkcode);
479     my $res = $sth->fetchrow_hashref;
480     return $res;
481 }
482
483
484 =head2 getitemtypeinfo
485
486   $itemtype = &getitemtype($itemtype);
487
488 Returns information about an itemtype.
489
490 =cut
491
492 sub getitemtypeinfo {
493     my ($itemtype) = @_;
494     my $dbh = C4::Context->dbh;
495     my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
496     $sth->execute($itemtype);
497     my $res = $sth->fetchrow_hashref;
498
499         $res->{imageurl} = getitemtypeimagesrcfromurl($res->{imageurl});
500
501     return $res;
502 }
503
504 sub getitemtypeimagesrcfromurl {
505     my ($imageurl) = @_;
506
507     if (defined $imageurl and $imageurl !~ m/^http/) {
508         $imageurl =
509             getitemtypeimagesrc()
510             .'/'.$imageurl
511             ;
512     }
513
514     return $imageurl;
515 }
516
517 sub getitemtypeimagedir {
518     return
519         C4::Context->intrahtdocs
520         .'/'.C4::Context->preference('template')
521         .'/itemtypeimg'
522         ;
523 }
524
525 sub getitemtypeimagesrc {
526     return
527         '/intranet-tmpl'
528         .'/'.C4::Context->preference('template')
529         .'/itemtypeimg'
530         ;
531 }
532
533 =head2 getprinters
534
535   $printers = &getprinters($env);
536   @queues = keys %$printers;
537
538 Returns information about existing printer queues.
539
540 C<$env> is ignored.
541
542 C<$printers> is a reference-to-hash whose keys are the print queues
543 defined in the printers table of the Koha database. The values are
544 references-to-hash, whose keys are the fields in the printers table.
545
546 =cut
547
548 sub getprinters {
549     my ($env) = @_;
550     my %printers;
551     my $dbh = C4::Context->dbh;
552     my $sth=$dbh->prepare("select * from printers");
553     $sth->execute;
554     while (my $printer=$sth->fetchrow_hashref) {
555     $printers{$printer->{'printqueue'}}=$printer;
556     }
557     return (\%printers);
558 }
559
560 sub getbranch ($$) {
561     my($query, $branches) = @_; # get branch for this query from branches
562     my $branch = $query->param('branch');
563     ($branch) || ($branch = $query->cookie('branch'));
564     ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
565     return $branch;
566 }
567
568 =item getbranchdetail
569
570   $branchname = &getbranchdetail($branchcode);
571
572 Given the branch code, the function returns the corresponding
573 branch name for a comprehensive information display
574
575 =cut
576
577 sub getbranchdetail
578 {
579     my ($branchcode) = @_;
580     my $dbh = C4::Context->dbh;
581     my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
582     $sth->execute($branchcode);
583     my $branchname = $sth->fetchrow_hashref();
584     $sth->finish();
585     return $branchname;
586 } # sub getbranchname
587
588
589 sub getprinter ($$) {
590     my($query, $printers) = @_; # get printer for this query from printers
591     my $printer = $query->param('printer');
592     ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
593     ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
594     return $printer;
595 }
596
597 =item getalllanguages
598
599   (@languages) = &getalllanguages($type);
600   (@languages) = &getalllanguages($type,$theme);
601
602 Returns an array of all available languages.
603
604 =cut
605
606 sub getalllanguages {
607     my $type=shift;
608     my $theme=shift;
609     my $htdocs;
610     my @languages;
611     if ($type eq 'opac') {
612         $htdocs=C4::Context->config('opachtdocs');
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;
623             }
624             return sort @languages;
625         } else {
626             my $lang;
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;
637                 }
638             }
639             @languages=keys %$lang;
640             return sort @languages;
641         }
642     } elsif ($type eq 'intranet') {
643         $htdocs=C4::Context->config('intrahtdocs');
644         if ($theme and -d "$htdocs/$theme") {
645             opendir D, "$htdocs/$theme";
646             foreach my $language (readdir D) {
647                 next if $language=~/^\./;
648                 next if $language eq 'all';
649                 next if $language=~ /png$/;
650                 next if $language=~ /css$/;
651                 next if $language=~ /CVS$/;
652                 next if $language=~ /itemtypeimg$/;
653                 push @languages, $language;
654             }
655             return sort @languages;
656         } else {
657             my $lang;
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;
668                 }
669             }
670             @languages=keys %$lang;
671             return sort @languages;
672         }
673     } else {
674         my $lang;
675         my $htdocs=C4::Context->config('intrahtdocs');
676         foreach my $theme (getallthemes('intranet')) {
677             opendir D, "$htdocs/$theme";
678             foreach my $language (readdir D) {
679                 next if $language=~/^\./;
680                 next if $language eq 'all';
681                 next if $language=~ /png$/;
682                 next if $language=~ /css$/;
683                 next if $language=~ /CVS$/;
684                 next if $language=~ /itemtypeimg$/;
685                 $lang->{$language}=1;
686             }
687         }
688         $htdocs=C4::Context->config('opachtdocs');
689         foreach my $theme (getallthemes('opac')) {
690         opendir D, "$htdocs/$theme";
691         foreach my $language (readdir D) {
692             next if $language=~/^\./;
693             next if $language eq 'all';
694             next if $language=~ /png$/;
695             next if $language=~ /css$/;
696             next if $language=~ /CVS$/;
697             next if $language=~ /itemtypeimg$/;
698             $lang->{$language}=1;
699             }
700         }
701         @languages=keys %$lang;
702         return sort @languages;
703     }
704 }
705
706 =item getallthemes
707
708   (@themes) = &getallthemes('opac');
709   (@themes) = &getallthemes('intranet');
710
711 Returns an array of all available themes.
712
713 =cut
714
715 sub getallthemes {
716     my $type=shift;
717     my $htdocs;
718     my @themes;
719     if ($type eq 'intranet') {
720     $htdocs=C4::Context->config('intrahtdocs');
721     } else {
722     $htdocs=C4::Context->config('opachtdocs');
723     }
724     opendir D, "$htdocs";
725     my @dirlist=readdir D;
726     foreach my $directory (@dirlist) {
727     -d "$htdocs/$directory/en" and push @themes, $directory;
728     }
729     return @themes;
730 }
731
732 =item getnbpages
733
734 Returns the number of pages to display in a pagination bar, given the number
735 of items and the number of items per page.
736
737 =cut
738
739 sub getnbpages {
740     my ($nb_items, $nb_items_per_page) = @_;
741
742     return int(($nb_items - 1) / $nb_items_per_page) + 1;
743 }
744
745
746 =head2 getcities (OUEST-PROVENCE)
747
748   ($id_cityarrayref, $city_hashref) = &getcities();
749
750 Looks up the different city and zip in the database. Returns two
751 elements: a reference-to-array, which lists the zip city
752 codes, and a reference-to-hash, which maps the name of the city.
753 WHERE =>OUEST PROVENCE OR EXTERIEUR
754
755 =cut
756 sub getcities {
757     #my ($type_city) = @_;
758     my $dbh = C4::Context->dbh;
759     my $sth=$dbh->prepare("Select cityid,city_name from cities order by cityid  ");
760     #$sth->execute($type_city);
761     $sth->execute();    
762     my %city;
763     my @id;
764 #    insert empty value to create a empty choice in cgi popup 
765     
766 while (my $data=$sth->fetchrow_hashref){
767       
768     push @id,$data->{'cityid'};
769       $city{$data->{'cityid'}}=$data->{'city_name'};
770     }
771     
772     #test to know if the table contain some records if no the function return nothing
773     my $id=@id;
774     $sth->finish;
775     if ($id eq 0)
776     {
777     return();
778     }
779     else{
780     unshift (@id ,"");
781     return(\@id,\%city);
782     }
783 }
784
785
786 =head2 getroadtypes (OUEST-PROVENCE)
787
788   ($idroadtypearrayref, $roadttype_hashref) = &getroadtypes();
789
790 Looks up the different road type . Returns two
791 elements: a reference-to-array, which lists the id_roadtype
792 codes, and a reference-to-hash, which maps the road type of the road .
793
794
795 =cut
796 sub getroadtypes {
797     my $dbh = C4::Context->dbh;
798     my $sth=$dbh->prepare("Select roadtypeid,road_type from roadtype order by road_type  ");
799     $sth->execute();
800     my %roadtype;
801     my @id;
802 #    insert empty value to create a empty choice in cgi popup 
803 while (my $data=$sth->fetchrow_hashref){
804     push @id,$data->{'roadtypeid'};
805       $roadtype{$data->{'roadtypeid'}}=$data->{'road_type'};
806     }
807     #test to know if the table contain some records if no the function return nothing
808     my $id=@id;
809     $sth->finish;
810     if ($id eq 0)
811     {
812     return();
813     }
814     else{
815         unshift (@id ,"");
816         return(\@id,\%roadtype);
817     }
818 }
819
820 =head2 get_branchinfos_of
821
822   my $branchinfos_of = get_branchinfos_of(@branchcodes);
823
824 Associates a list of branchcodes to the information of the branch, taken in
825 branches table.
826
827 Returns a href where keys are branchcodes and values are href where keys are
828 branch information key.
829
830   print 'branchname is ', $branchinfos_of->{$code}->{branchname};
831
832 =cut
833 sub get_branchinfos_of {
834     my @branchcodes = @_;
835
836     my $query = '
837 SELECT branchcode,
838        branchname
839   FROM branches
840   WHERE branchcode IN ('.join(',', map({"'".$_."'"} @branchcodes)).')
841 ';
842     return get_infos_of($query, 'branchcode');
843 }
844
845 =head2 get_notforloan_label_of
846
847   my $notforloan_label_of = get_notforloan_label_of();
848
849 Each authorised value of notforloan (information available in items and
850 itemtypes) is link to a single label.
851
852 Returns a href where keys are authorised values and values are corresponding
853 labels.
854
855   foreach my $authorised_value (keys %{$notforloan_label_of}) {
856     printf(
857         "authorised_value: %s => %s\n",
858         $authorised_value,
859         $notforloan_label_of->{$authorised_value}
860     );
861   }
862
863 =cut
864 sub get_notforloan_label_of {
865     my $dbh = C4::Context->dbh;
866
867     my $query = '
868 SELECT authorised_value
869   FROM marc_subfield_structure
870   WHERE kohafield = \'items.notforloan\'
871   LIMIT 0, 1
872 ';
873     my $sth = $dbh->prepare($query);
874     $sth->execute();
875     my ($statuscode) = $sth->fetchrow_array();
876
877     $query = '
878 SELECT lib,
879        authorised_value
880   FROM authorised_values
881   WHERE category = ?
882 ';
883     $sth = $dbh->prepare($query);
884     $sth->execute($statuscode);
885     my %notforloan_label_of;
886     while (my $row = $sth->fetchrow_hashref) {
887         $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
888     }
889     $sth->finish;
890
891     return \%notforloan_label_of;
892 }
893
894 =head2 get_infos_of
895
896 Return a href where a key is associated to a href. You give a query, the
897 name of the key among the fields returned by the query. If you also give as
898 third argument the name of the value, the function returns a href of scalar.
899
900   my $query = '
901 SELECT itemnumber,
902        notforloan,
903        barcode
904   FROM items
905 ';
906
907   # generic href of any information on the item, href of href.
908   my $iteminfos_of = get_infos_of($query, 'itemnumber');
909   print $iteminfos_of->{$itemnumber}{barcode};
910
911   # specific information, href of scalar
912   my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
913   print $barcode_of_item->{$itemnumber};
914
915 =cut
916 sub get_infos_of {
917     my ($query, $key_name, $value_name) = @_;
918
919     my $dbh = C4::Context->dbh;
920
921     my $sth = $dbh->prepare($query);
922     $sth->execute();
923
924     my %infos_of;
925     while (my $row = $sth->fetchrow_hashref) {
926         if (defined $value_name) {
927             $infos_of{ $row->{$key_name} } = $row->{$value_name};
928         }
929         else {
930             $infos_of{ $row->{$key_name} } = $row;
931         }
932     }
933     $sth->finish;
934
935     return \%infos_of;
936 }
937
938 1;
939 __END__
940
941 =back
942
943 =head1 AUTHOR
944
945 Koha Team
946
947 =cut