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")
38 $ethnicity = fixEthnicity('asian');
39 ($categories, $labels) = borrowercategories();
40 ($categories, $labels) = ethnicitycategories();
44 Koha.pm provides many functions for Koha scripts.
53 @EXPORT = qw(&slashifyDate
57 &subfield_is_koha_internal_p
58 &getbranches &getbranch
59 &getprinters &getprinter
60 &getitemtypes &getitemtypeinfo
61 &getframeworks &getframeworkinfo
62 &getauthtypes &getauthtype
63 &getallthemes &getalllanguages
72 $slash_date = &slashifyDate($dash_date);
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.
80 # accepts a date of the form xx-xx-xx[xx] and returns it in the
82 my @dateOut = split('-', shift);
83 return("$dateOut[2]/$dateOut[1]/$dateOut[0]")
88 $ethn_name = &fixEthnicity($ethn_code);
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").
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;
105 return $data->{'name'};
108 =head2 borrowercategories
110 ($codes_arrayref, $labels_hashref) = &borrowercategories();
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.
120 sub borrowercategories {
121 my $dbh = C4::Context->dbh;
122 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
126 while (my $data=$sth->fetchrow_hashref){
127 push @codes,$data->{'categorycode'};
128 $labels{$data->{'categorycode'}}=$data->{'description'};
131 return(\@codes,\%labels);
134 =head2 ethnicitycategories
136 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
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
146 sub ethnicitycategories {
147 my $dbh = C4::Context->dbh;
148 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
152 while (my $data=$sth->fetchrow_hashref){
153 push @codes,$data->{'code'};
154 $labels{$data->{'code'}}=$data->{'name'};
157 return(\@codes,\%labels);
160 # FIXME.. this should be moved to a MARC-specific module
161 sub subfield_is_koha_internal_p ($) {
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
168 return length $subfield != 1;
173 $branches = &getbranches();
174 returns informations about branches.
175 Create a branch selector with the following code
177 =head3 in PERL SCRIPT
179 my $branches = getbranches;
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'},
187 push @branchloop, \%row;
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>
202 # returns a reference to a hash of references to branches...
204 my $dbh = C4::Context->dbh;
205 my $sth=$dbh->prepare("select * from branches order by branchname");
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.
222 $branches{$branch->{'branchcode'}}=$branch;
229 $itemtypes = &getitemtypes();
231 Returns information about existing itemtypes.
233 build a HTML select with the following code :
235 =head3 in PERL SCRIPT
237 my $itemtypes = getitemtypes;
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'},
245 push @itemtypesloop, \%row;
247 $template->param(itemtypeloop => \@itemtypesloop);
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>
258 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
259 <input type="submit" value="OK" class="button">
266 # returns a reference to a hash of references to branches...
268 my $dbh = C4::Context->dbh;
269 my $sth=$dbh->prepare("select * from itemtypes order by description");
271 while (my $IT=$sth->fetchrow_hashref) {
272 $itemtypes{$IT->{'itemtype'}}=$IT;
274 return (\%itemtypes);
279 $authtypes = &getauthtypes();
281 Returns information about existing authtypes.
283 build a HTML select with the following code :
285 =head3 in PERL SCRIPT
287 my $authtypes = getauthtypes;
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'},
295 push @authtypesloop, \%row;
297 $template->param(itemtypeloop => \@itemtypesloop);
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>
307 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
308 <input type="submit" value="OK" class="button">
315 # returns a reference to a hash of references to authtypes...
317 my $dbh = C4::Context->dbh;
318 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
320 while (my $IT=$sth->fetchrow_hashref) {
321 $authtypes{$IT->{'authtypecode'}}=$IT;
323 return (\%authtypes);
327 my ($authtypecode) = @_;
328 # returns a reference to a hash of references to 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;
339 $frameworks = &getframework();
341 Returns information about existing frameworks
343 build a HTML select with the following code :
345 =head3 in PERL SCRIPT
347 my $frameworks = frameworks();
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'},
355 push @frameworksloop, \%row;
357 $template->param(frameworkloop => \@frameworksloop);
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>
368 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
369 <input type="submit" value="OK" class="button">
376 # returns a reference to a hash of references to branches...
378 my $dbh = C4::Context->dbh;
379 my $sth=$dbh->prepare("select * from biblio_framework");
381 while (my $IT=$sth->fetchrow_hashref) {
382 $itemtypes{$IT->{'frameworkcode'}}=$IT;
384 return (\%itemtypes);
386 =head2 getframeworkinfo
388 $frameworkinfo = &getframeworkinfo($frameworkcode);
390 Returns information about an frameworkcode.
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;
404 =head2 getitemtypeinfo
406 $itemtype = &getitemtype($itemtype);
408 Returns information about an itemtype.
412 sub getitemtypeinfo {
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;
423 $printers = &getprinters($env);
424 @queues = keys %$printers;
426 Returns information about existing printer queues.
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.
439 my $dbh = C4::Context->dbh;
440 my $sth=$dbh->prepare("select * from printers");
442 while (my $printer=$sth->fetchrow_hashref) {
443 $printers{$printer->{'printqueue'}}=$printer;
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]);
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]);
463 =item getalllanguages
465 (@languages) = &getalllanguages($type);
466 (@languages) = &getalllanguages($type,$theme);
468 Returns an array of all available languages.
472 sub getalllanguages {
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;
486 return sort @languages;
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;
497 @languages=keys %$lang;
498 return sort @languages;
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;
509 return sort @languages;
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;
520 @languages=keys %$lang;
521 return sort @languages;
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;
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;
543 @languages=keys %$lang;
544 return sort @languages;
550 (@themes) = &getallthemes('opac');
551 (@themes) = &getallthemes('intranet');
553 Returns an array of all available themes.
561 if ($type eq 'intranet') {
562 $htdocs=C4::Context->config('intrahtdocs');
564 $htdocs=C4::Context->config('opachtdocs');
566 opendir D, "$htdocs";
567 my @dirlist=readdir D;
568 foreach my $directory (@dirlist) {
569 -d "$htdocs/$directory/en" and push @themes, $directory;