moving language chooser to the main page.
[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 use strict;
21 require Exporter;
22 use C4::Context;
23
24 use vars qw($VERSION @ISA @EXPORT);
25
26 $VERSION = 0.01;
27
28 =head1 NAME
29
30 C4::Koha - Perl Module containing convenience functions for Koha scripts
31
32 =head1 SYNOPSIS
33
34   use C4::Koha;
35
36
37   $date = slashifyDate("01-01-2002")
38   $ethnicity = fixEthnicity('asian');
39   ($categories, $labels) = borrowercategories();
40   ($categories, $labels) = ethnicitycategories();
41
42 =head1 DESCRIPTION
43
44 Koha.pm provides many functions for Koha scripts.
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 @ISA = qw(Exporter);
53 @EXPORT = qw(&slashifyDate
54                         &fixEthnicity
55                         &borrowercategories
56                         &ethnicitycategories
57                         &subfield_is_koha_internal_p
58                         &getbranches &getbranch
59                         &getprinters &getprinter
60                         &getitemtypes &getitemtypeinfo
61                         &getframeworks &getframeworkinfo
62                         &getauthtypes &getauthtype
63                         &getallthemes &getalllanguages 
64                         $DEBUG);
65
66 use vars qw();
67
68 my $DEBUG = 0;
69
70 =head2 slashifyDate
71
72   $slash_date = &slashifyDate($dash_date);
73
74 Takes a string of the form "DD-MM-YYYY" (or anything separated by
75 dashes), converts it to the form "YYYY/MM/DD", and returns the result.
76
77 =cut
78
79 sub slashifyDate {
80     # accepts a date of the form xx-xx-xx[xx] and returns it in the
81     # form xx/xx/xx[xx]
82     my @dateOut = split('-', shift);
83     return("$dateOut[2]/$dateOut[1]/$dateOut[0]")
84 }
85
86 =head2 fixEthnicity
87
88   $ethn_name = &fixEthnicity($ethn_code);
89
90 Takes an ethnicity code (e.g., "european" or "pi") and returns the
91 corresponding descriptive name from the C<ethnicity> table in the
92 Koha database ("European" or "Pacific Islander").
93
94 =cut
95 #'
96
97 sub fixEthnicity($) {
98
99     my $ethnicity = shift;
100     my $dbh = C4::Context->dbh;
101     my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
102     $sth->execute($ethnicity);
103     my $data=$sth->fetchrow_hashref;
104     $sth->finish;
105     return $data->{'name'};
106 }
107
108 =head2 borrowercategories
109
110   ($codes_arrayref, $labels_hashref) = &borrowercategories();
111
112 Looks up the different types of borrowers in the database. Returns two
113 elements: a reference-to-array, which lists the borrower category
114 codes, and a reference-to-hash, which maps the borrower category codes
115 to category descriptions.
116
117 =cut
118 #'
119
120 sub borrowercategories {
121     my $dbh = C4::Context->dbh;
122     my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
123     $sth->execute;
124     my %labels;
125     my @codes;
126     while (my $data=$sth->fetchrow_hashref){
127       push @codes,$data->{'categorycode'};
128       $labels{$data->{'categorycode'}}=$data->{'description'};
129     }
130     $sth->finish;
131     return(\@codes,\%labels);
132 }
133
134 =head2 ethnicitycategories
135
136   ($codes_arrayref, $labels_hashref) = &ethnicitycategories();
137
138 Looks up the different ethnic types in the database. Returns two
139 elements: a reference-to-array, which lists the ethnicity codes, and a
140 reference-to-hash, which maps the ethnicity codes to ethnicity
141 descriptions.
142
143 =cut
144 #'
145
146 sub ethnicitycategories {
147     my $dbh = C4::Context->dbh;
148     my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
149     $sth->execute;
150     my %labels;
151     my @codes;
152     while (my $data=$sth->fetchrow_hashref){
153       push @codes,$data->{'code'};
154       $labels{$data->{'code'}}=$data->{'name'};
155     }
156     $sth->finish;
157     return(\@codes,\%labels);
158 }
159
160 # FIXME.. this should be moved to a MARC-specific module
161 sub subfield_is_koha_internal_p ($) {
162     my($subfield) = @_;
163
164     # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
165     # But real MARC subfields are always single-character
166     # so it really is safer just to check the length
167
168     return length $subfield != 1;
169 }
170
171 =head2 getbranches
172
173   $branches = &getbranches();
174   returns informations about branches.
175   Create a branch selector with the following code
176   
177 =head3 in PERL SCRIPT
178
179 my $branches = getbranches;
180 my @branchloop;
181 foreach my $thisbranch (keys %$branches) {
182         my $selected = 1 if $thisbranch eq $branch;
183         my %row =(value => $thisbranch,
184                                 selected => $selected,
185                                 branchname => $branches->{$thisbranch}->{'branchname'},
186                         );
187         push @branchloop, \%row;
188 }
189
190
191 =head3 in TEMPLATE  
192                         <select name="branch">
193                                 <option value="">Default</option>
194                         <!-- TMPL_LOOP name="branchloop" -->
195                                 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
196                         <!-- /TMPL_LOOP -->
197                         </select>
198
199 =cut
200
201 sub getbranches {
202 # returns a reference to a hash of references to branches...
203         my %branches;
204         my $dbh = C4::Context->dbh;
205         my $sth=$dbh->prepare("select * from branches order by branchname");
206         $sth->execute;
207         while (my $branch=$sth->fetchrow_hashref) {
208                 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
209                 $nsth->execute($branch->{'branchcode'});
210                 while (my ($cat) = $nsth->fetchrow_array) {
211                         # FIXME - This seems wrong. It ought to be
212                         # $branch->{categorycodes}{$cat} = 1;
213                         # otherwise, there's a namespace collision if there's a
214                         # category with the same name as a field in the 'branches'
215                         # table (i.e., don't create a category called "issuing").
216                         # In addition, the current structure doesn't really allow
217                         # you to list the categories that a branch belongs to:
218                         # you'd have to list keys %$branch, and remove those keys
219                         # that aren't fields in the "branches" table.
220                         $branch->{$cat} = 1;
221                         }
222                         $branches{$branch->{'branchcode'}}=$branch;
223         }
224         return (\%branches);
225 }
226
227 =head2 getitemtypes
228
229   $itemtypes = &getitemtypes();
230
231 Returns information about existing itemtypes.
232
233 build a HTML select with the following code :
234
235 =head3 in PERL SCRIPT
236
237 my $itemtypes = getitemtypes;
238 my @itemtypesloop;
239 foreach my $thisitemtype (keys %$itemtypes) {
240         my $selected = 1 if $thisitemtype eq $itemtype;
241         my %row =(value => $thisitemtype,
242                                 selected => $selected,
243                                 description => $itemtypes->{$thisitemtype}->{'description'},
244                         );
245         push @itemtypesloop, \%row;
246 }
247 $template->param(itemtypeloop => \@itemtypesloop);
248
249 =head3 in TEMPLATE
250
251 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
252         <select name="itemtype">
253                 <option value="">Default</option>
254         <!-- TMPL_LOOP name="itemtypeloop" -->
255                 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
256         <!-- /TMPL_LOOP -->
257         </select>
258         <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
259         <input type="submit" value="OK" class="button">
260 </form>
261
262
263 =cut
264
265 sub getitemtypes {
266 # returns a reference to a hash of references to branches...
267         my %itemtypes;
268         my $dbh = C4::Context->dbh;
269         my $sth=$dbh->prepare("select * from itemtypes order by description");
270         $sth->execute;
271         while (my $IT=$sth->fetchrow_hashref) {
272                         $itemtypes{$IT->{'itemtype'}}=$IT;
273         }
274         return (\%itemtypes);
275 }
276
277 =head2 getauthtypes
278
279   $authtypes = &getauthtypes();
280
281 Returns information about existing authtypes.
282
283 build a HTML select with the following code :
284
285 =head3 in PERL SCRIPT
286
287 my $authtypes = getauthtypes;
288 my @authtypesloop;
289 foreach my $thisauthtype (keys %$authtypes) {
290         my $selected = 1 if $thisauthtype eq $authtype;
291         my %row =(value => $thisauthtype,
292                                 selected => $selected,
293                                 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
294                         );
295         push @authtypesloop, \%row;
296 }
297 $template->param(itemtypeloop => \@itemtypesloop);
298
299 =head3 in TEMPLATE
300
301 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
302         <select name="authtype">
303         <!-- TMPL_LOOP name="authtypeloop" -->
304                 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
305         <!-- /TMPL_LOOP -->
306         </select>
307         <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
308         <input type="submit" value="OK" class="button">
309 </form>
310
311
312 =cut
313
314 sub getauthtypes {
315 # returns a reference to a hash of references to authtypes...
316         my %authtypes;
317         my $dbh = C4::Context->dbh;
318         my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
319         $sth->execute;
320         while (my $IT=$sth->fetchrow_hashref) {
321                         $authtypes{$IT->{'authtypecode'}}=$IT;
322         }
323         return (\%authtypes);
324 }
325
326 sub getauthtype {
327         my ($authtypecode) = @_;
328 # returns a reference to a hash of references to authtypes...
329         my %authtypes;
330         my $dbh = C4::Context->dbh;
331         my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
332         $sth->execute($authtypecode);
333         my $res=$sth->fetchrow_hashref;
334         return $res;
335 }
336
337 =head2 getframework
338
339   $frameworks = &getframework();
340
341 Returns information about existing frameworks
342
343 build a HTML select with the following code :
344
345 =head3 in PERL SCRIPT
346
347 my $frameworks = frameworks();
348 my @frameworkloop;
349 foreach my $thisframework (keys %$frameworks) {
350         my $selected = 1 if $thisframework eq $frameworkcode;
351         my %row =(value => $thisframework,
352                                 selected => $selected,
353                                 description => $frameworks->{$thisframework}->{'frameworktext'},
354                         );
355         push @frameworksloop, \%row;
356 }
357 $template->param(frameworkloop => \@frameworksloop);
358
359 =head3 in TEMPLATE
360
361 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
362         <select name="frameworkcode">
363                 <option value="">Default</option>
364         <!-- TMPL_LOOP name="frameworkloop" -->
365                 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
366         <!-- /TMPL_LOOP -->
367         </select>
368         <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
369         <input type="submit" value="OK" class="button">
370 </form>
371
372
373 =cut
374
375 sub getframeworks {
376 # returns a reference to a hash of references to branches...
377         my %itemtypes;
378         my $dbh = C4::Context->dbh;
379         my $sth=$dbh->prepare("select * from biblio_framework");
380         $sth->execute;
381         while (my $IT=$sth->fetchrow_hashref) {
382                         $itemtypes{$IT->{'frameworkcode'}}=$IT;
383         }
384         return (\%itemtypes);
385 }
386 =head2 getframeworkinfo
387
388   $frameworkinfo = &getframeworkinfo($frameworkcode);
389
390 Returns information about an frameworkcode.
391
392 =cut
393
394 sub getframeworkinfo {
395         my ($frameworkcode) = @_;
396         my $dbh = C4::Context->dbh;
397         my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
398         $sth->execute($frameworkcode);
399         my $res = $sth->fetchrow_hashref;
400         return $res;
401 }
402
403
404 =head2 getitemtypeinfo
405
406   $itemtype = &getitemtype($itemtype);
407
408 Returns information about an itemtype.
409
410 =cut
411
412 sub getitemtypeinfo {
413         my ($itemtype) = @_;
414         my $dbh = C4::Context->dbh;
415         my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
416         $sth->execute($itemtype);
417         my $res = $sth->fetchrow_hashref;
418         return $res;
419 }
420
421 =head2 getprinters
422
423   $printers = &getprinters($env);
424   @queues = keys %$printers;
425
426 Returns information about existing printer queues.
427
428 C<$env> is ignored.
429
430 C<$printers> is a reference-to-hash whose keys are the print queues
431 defined in the printers table of the Koha database. The values are
432 references-to-hash, whose keys are the fields in the printers table.
433
434 =cut
435
436 sub getprinters {
437     my ($env) = @_;
438     my %printers;
439     my $dbh = C4::Context->dbh;
440     my $sth=$dbh->prepare("select * from printers");
441     $sth->execute;
442     while (my $printer=$sth->fetchrow_hashref) {
443         $printers{$printer->{'printqueue'}}=$printer;
444     }
445     return (\%printers);
446 }
447 sub getbranch ($$) {
448     my($query, $branches) = @_; # get branch for this query from branches
449     my $branch = $query->param('branch');
450     ($branch) || ($branch = $query->cookie('branch'));
451     ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
452     return $branch;
453 }
454
455 sub getprinter ($$) {
456     my($query, $printers) = @_; # get printer for this query from printers
457     my $printer = $query->param('printer');
458     ($printer) || ($printer = $query->cookie('printer'));
459     ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
460     return $printer;
461 }
462
463 =item getalllanguages
464
465   (@languages) = &getalllanguages($type);
466   (@languages) = &getalllanguages($type,$theme);
467
468 Returns an array of all available languages.
469
470 =cut
471
472 sub getalllanguages {
473     my $type=shift;
474     my $theme=shift;
475     my $htdocs;
476     my @languages;
477     if ($type eq 'opac') {
478         $htdocs=C4::Context->config('opachtdocs');
479         if ($theme and -d "$htdocs/$theme") {
480             opendir D, "$htdocs/$theme";
481             foreach my $language (readdir D) {
482                 next if $language=~/^\./;
483                 next if $language eq 'all';
484                 push @languages, $language;
485             }
486             return sort @languages;
487         } else {
488             my $lang;
489             foreach my $theme (getallthemes('opac')) {
490                 opendir D, "$htdocs/$theme";
491                 foreach my $language (readdir D) {
492                     next if $language=~/^\./;
493                     next if $language eq 'all';
494                     $lang->{$language}=1;
495                 }
496             }
497             @languages=keys %$lang;
498             return sort @languages;
499         }
500     } elsif ($type eq 'intranet') {
501         $htdocs=C4::Context->config('intrahtdocs');
502         if ($theme and -d "$htdocs/$theme") {
503             opendir D, "$htdocs/$theme";
504             foreach my $language (readdir D) {
505                 next if $language=~/^\./;
506                 next if $language eq 'all';
507                 push @languages, $language;
508             }
509             return sort @languages;
510         } else {
511             my $lang;
512             foreach my $theme (getallthemes('opac')) {
513                 opendir D, "$htdocs/$theme";
514                 foreach my $language (readdir D) {
515                     next if $language=~/^\./;
516                     next if $language eq 'all';
517                     $lang->{$language}=1;
518                 }
519             }
520             @languages=keys %$lang;
521             return sort @languages;
522         }
523     } else {
524         my $lang;
525         my $htdocs=C4::Context->config('intrahtdocs');
526         foreach my $theme (getallthemes('intranet')) {
527             opendir D, "$htdocs/$theme";
528             foreach my $language (readdir D) {
529                 next if $language=~/^\./;
530                 next if $language eq 'all';
531                 $lang->{$language}=1;
532             }
533         }
534         my $htdocs=C4::Context->config('opachtdocs');
535         foreach my $theme (getallthemes('opac')) {
536             opendir D, "$htdocs/$theme";
537             foreach my $language (readdir D) {
538                 next if $language=~/^\./;
539                 next if $language eq 'all';
540                 $lang->{$language}=1;
541             }
542         }
543         @languages=keys %$lang;
544         return sort @languages;
545     }
546 }
547
548 =item getallthemes
549
550   (@themes) = &getallthemes('opac');
551   (@themes) = &getallthemes('intranet');
552
553 Returns an array of all available themes.
554
555 =cut
556
557 sub getallthemes {
558     my $type=shift;
559     my $htdocs;
560     my @themes;
561     if ($type eq 'intranet') {
562         $htdocs=C4::Context->config('intrahtdocs');
563     } else {
564         $htdocs=C4::Context->config('opachtdocs');
565     }
566     opendir D, "$htdocs";
567     my @dirlist=readdir D;
568     foreach my $directory (@dirlist) {
569         -d "$htdocs/$directory/en" and push @themes, $directory;
570     }
571     return @themes;
572 }
573
574
575 1;
576 __END__
577
578 =back
579
580 =head1 AUTHOR
581
582 Koha Team
583
584 =cut