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.
55 &borrowercategories &getborrowercategory
57 &subfield_is_koha_internal_p
58 &getbranches &getbranch &getbranchname
59 &getprinters &getprinter
60 &getitemtypes &getitemtypeinfo
61 &getframeworks &getframeworkinfo
62 &getauthtypes &getauthtype
63 &getallthemes &getalllanguages
70 # removed slashifyDate => useless
74 $ethn_name = &fixEthnicity($ethn_code);
76 Takes an ethnicity code (e.g., "european" or "pi") and returns the
77 corresponding descriptive name from the C<ethnicity> table in the
78 Koha database ("European" or "Pacific Islander").
85 my $ethnicity = shift;
86 my $dbh = C4::Context->dbh;
87 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
88 $sth->execute($ethnicity);
89 my $data=$sth->fetchrow_hashref;
91 return $data->{'name'};
94 =head2 borrowercategories
96 ($codes_arrayref, $labels_hashref) = &borrowercategories();
98 Looks up the different types of borrowers in the database. Returns two
99 elements: a reference-to-array, which lists the borrower category
100 codes, and a reference-to-hash, which maps the borrower category codes
101 to category descriptions.
106 sub borrowercategories {
107 my $dbh = C4::Context->dbh;
108 my $sth=$dbh->prepare("Select categorycode,description from categories order by description");
112 while (my $data=$sth->fetchrow_hashref){
113 push @codes,$data->{'categorycode'};
114 $labels{$data->{'categorycode'}}=$data->{'description'};
117 return(\@codes,\%labels);
120 =item getborrowercategory
122 $description = &getborrowercategory($categorycode);
124 Given the borrower's category code, the function returns the corresponding
125 description for a comprehensive information display.
129 sub getborrowercategory
132 my $dbh = C4::Context->dbh;
133 my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
134 $sth->execute($catcode);
135 my $description = $sth->fetchrow();
138 } # sub getborrowercategory
141 =head2 ethnicitycategories
143 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
145 Looks up the different ethnic types in the database. Returns two
146 elements: a reference-to-array, which lists the ethnicity codes, and a
147 reference-to-hash, which maps the ethnicity codes to ethnicity
153 sub ethnicitycategories {
154 my $dbh = C4::Context->dbh;
155 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
159 while (my $data=$sth->fetchrow_hashref){
160 push @codes,$data->{'code'};
161 $labels{$data->{'code'}}=$data->{'name'};
164 return(\@codes,\%labels);
167 # FIXME.. this should be moved to a MARC-specific module
168 sub subfield_is_koha_internal_p ($) {
171 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
172 # But real MARC subfields are always single-character
173 # so it really is safer just to check the length
175 return length $subfield != 1;
180 $branches = &getbranches();
181 returns informations about branches.
182 Create a branch selector with the following code
184 =head3 in PERL SCRIPT
186 my $branches = getbranches;
188 foreach my $thisbranch (sort keys %$branches) {
189 my $selected = 1 if $thisbranch eq $branch;
190 my %row =(value => $thisbranch,
191 selected => $selected,
192 branchname => $branches->{$thisbranch}->{'branchname'},
194 push @branchloop, \%row;
199 <select name="branch">
200 <option value="">Default</option>
201 <!-- TMPL_LOOP name="branchloop" -->
202 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
209 # returns a reference to a hash of references to branches...
211 my $dbh = C4::Context->dbh;
213 if (C4::Context->preference("IndependantBranches") && (C4::Context->userenv->{flags}!=1)){
214 my $strsth ="Select * from branches ";
215 $strsth.= " WHERE branchcode = ".$dbh->quote(C4::Context->userenv->{branch});
216 $strsth.= " order by branchname";
217 $sth=$dbh->prepare($strsth);
219 $sth = $dbh->prepare("Select * from branches order by branchname");
222 while (my $branch=$sth->fetchrow_hashref) {
223 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
224 $nsth->execute($branch->{'branchcode'});
225 while (my ($cat) = $nsth->fetchrow_array) {
226 # FIXME - This seems wrong. It ought to be
227 # $branch->{categorycodes}{$cat} = 1;
228 # otherwise, there's a namespace collision if there's a
229 # category with the same name as a field in the 'branches'
230 # table (i.e., don't create a category called "issuing").
231 # In addition, the current structure doesn't really allow
232 # you to list the categories that a branch belongs to:
233 # you'd have to list keys %$branch, and remove those keys
234 # that aren't fields in the "branches" table.
237 $branches{$branch->{'branchcode'}}=$branch;
244 $itemtypes = &getitemtypes();
246 Returns information about existing itemtypes.
248 build a HTML select with the following code :
250 =head3 in PERL SCRIPT
252 my $itemtypes = getitemtypes;
254 foreach my $thisitemtype (sort keys %$itemtypes) {
255 my $selected = 1 if $thisitemtype eq $itemtype;
256 my %row =(value => $thisitemtype,
257 selected => $selected,
258 description => $itemtypes->{$thisitemtype}->{'description'},
260 push @itemtypesloop, \%row;
262 $template->param(itemtypeloop => \@itemtypesloop);
266 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
267 <select name="itemtype">
268 <option value="">Default</option>
269 <!-- TMPL_LOOP name="itemtypeloop" -->
270 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
273 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
274 <input type="submit" value="OK" class="button">
281 # returns a reference to a hash of references to branches...
283 my $dbh = C4::Context->dbh;
284 my $sth=$dbh->prepare("select * from itemtypes");
286 while (my $IT=$sth->fetchrow_hashref) {
287 $itemtypes{$IT->{'itemtype'}}=$IT;
289 return (\%itemtypes);
294 $authtypes = &getauthtypes();
296 Returns information about existing authtypes.
298 build a HTML select with the following code :
300 =head3 in PERL SCRIPT
302 my $authtypes = getauthtypes;
304 foreach my $thisauthtype (keys %$authtypes) {
305 my $selected = 1 if $thisauthtype eq $authtype;
306 my %row =(value => $thisauthtype,
307 selected => $selected,
308 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
310 push @authtypesloop, \%row;
312 $template->param(itemtypeloop => \@itemtypesloop);
316 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
317 <select name="authtype">
318 <!-- TMPL_LOOP name="authtypeloop" -->
319 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
322 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
323 <input type="submit" value="OK" class="button">
330 # returns a reference to a hash of references to authtypes...
332 my $dbh = C4::Context->dbh;
333 my $sth=$dbh->prepare("select * from auth_types order by authtypetext");
335 while (my $IT=$sth->fetchrow_hashref) {
336 $authtypes{$IT->{'authtypecode'}}=$IT;
338 return (\%authtypes);
342 my ($authtypecode) = @_;
343 # returns a reference to a hash of references to authtypes...
345 my $dbh = C4::Context->dbh;
346 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
347 $sth->execute($authtypecode);
348 my $res=$sth->fetchrow_hashref;
354 $frameworks = &getframework();
356 Returns information about existing frameworks
358 build a HTML select with the following code :
360 =head3 in PERL SCRIPT
362 my $frameworks = frameworks();
364 foreach my $thisframework (keys %$frameworks) {
365 my $selected = 1 if $thisframework eq $frameworkcode;
366 my %row =(value => $thisframework,
367 selected => $selected,
368 description => $frameworks->{$thisframework}->{'frameworktext'},
370 push @frameworksloop, \%row;
372 $template->param(frameworkloop => \@frameworksloop);
376 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
377 <select name="frameworkcode">
378 <option value="">Default</option>
379 <!-- TMPL_LOOP name="frameworkloop" -->
380 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
383 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
384 <input type="submit" value="OK" class="button">
391 # returns a reference to a hash of references to branches...
393 my $dbh = C4::Context->dbh;
394 my $sth=$dbh->prepare("select * from biblio_framework");
396 while (my $IT=$sth->fetchrow_hashref) {
397 $itemtypes{$IT->{'frameworkcode'}}=$IT;
399 return (\%itemtypes);
401 =head2 getframeworkinfo
403 $frameworkinfo = &getframeworkinfo($frameworkcode);
405 Returns information about an frameworkcode.
409 sub getframeworkinfo {
410 my ($frameworkcode) = @_;
411 my $dbh = C4::Context->dbh;
412 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
413 $sth->execute($frameworkcode);
414 my $res = $sth->fetchrow_hashref;
419 =head2 getitemtypeinfo
421 $itemtype = &getitemtype($itemtype);
423 Returns information about an itemtype.
427 sub getitemtypeinfo {
429 my $dbh = C4::Context->dbh;
430 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
431 $sth->execute($itemtype);
432 my $res = $sth->fetchrow_hashref;
438 $printers = &getprinters($env);
439 @queues = keys %$printers;
441 Returns information about existing printer queues.
445 C<$printers> is a reference-to-hash whose keys are the print queues
446 defined in the printers table of the Koha database. The values are
447 references-to-hash, whose keys are the fields in the printers table.
454 my $dbh = C4::Context->dbh;
455 my $sth=$dbh->prepare("select * from printers");
457 while (my $printer=$sth->fetchrow_hashref) {
458 $printers{$printer->{'printqueue'}}=$printer;
464 my($query, $branches) = @_; # get branch for this query from branches
465 my $branch = $query->param('branch');
466 ($branch) || ($branch = $query->cookie('branch'));
467 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
473 $branchname = &getbranchname($branchcode);
475 Given the branch code, the function returns the corresponding
476 branch name for a comprehensive information display
482 my ($branchcode) = @_;
483 my $dbh = C4::Context->dbh;
484 my $sth = $dbh->prepare("SELECT branchname FROM branches WHERE branchcode = ?");
485 $sth->execute($branchcode);
486 my $branchname = $sth->fetchrow();
489 } # sub getbranchname
491 sub getprinter ($$) {
492 my($query, $printers) = @_; # get printer for this query from printers
493 my $printer = $query->param('printer');
494 ($printer) || ($printer = $query->cookie('printer')) || ($printer='');
495 ($printers->{$printer}) || ($printer = (keys %$printers)[0]);
499 =item getalllanguages
501 (@languages) = &getalllanguages($type);
502 (@languages) = &getalllanguages($type,$theme);
504 Returns an array of all available languages.
508 sub getalllanguages {
513 if ($type eq 'opac') {
514 $htdocs=C4::Context->config('opachtdocs');
515 if ($theme and -d "$htdocs/$theme") {
516 opendir D, "$htdocs/$theme";
517 foreach my $language (readdir D) {
518 next if $language=~/^\./;
519 next if $language eq 'all';
520 push @languages, $language;
522 return sort @languages;
525 foreach my $theme (getallthemes('opac')) {
526 opendir D, "$htdocs/$theme";
527 foreach my $language (readdir D) {
528 next if $language=~/^\./;
529 next if $language eq 'all';
530 $lang->{$language}=1;
533 @languages=keys %$lang;
534 return sort @languages;
536 } elsif ($type eq 'intranet') {
537 $htdocs=C4::Context->config('intrahtdocs');
538 if ($theme and -d "$htdocs/$theme") {
539 opendir D, "$htdocs/$theme";
540 foreach my $language (readdir D) {
541 next if $language=~/^\./;
542 next if $language eq 'all';
543 push @languages, $language;
545 return sort @languages;
548 foreach my $theme (getallthemes('opac')) {
549 opendir D, "$htdocs/$theme";
550 foreach my $language (readdir D) {
551 next if $language=~/^\./;
552 next if $language eq 'all';
553 $lang->{$language}=1;
556 @languages=keys %$lang;
557 return sort @languages;
561 my $htdocs=C4::Context->config('intrahtdocs');
562 foreach my $theme (getallthemes('intranet')) {
563 opendir D, "$htdocs/$theme";
564 foreach my $language (readdir D) {
565 next if $language=~/^\./;
566 next if $language eq 'all';
567 $lang->{$language}=1;
570 $htdocs=C4::Context->config('opachtdocs');
571 foreach my $theme (getallthemes('opac')) {
572 opendir D, "$htdocs/$theme";
573 foreach my $language (readdir D) {
574 next if $language=~/^\./;
575 next if $language eq 'all';
576 $lang->{$language}=1;
579 @languages=keys %$lang;
580 return sort @languages;
586 (@themes) = &getallthemes('opac');
587 (@themes) = &getallthemes('intranet');
589 Returns an array of all available themes.
597 if ($type eq 'intranet') {
598 $htdocs=C4::Context->config('intrahtdocs');
600 $htdocs=C4::Context->config('opachtdocs');
602 opendir D, "$htdocs";
603 my @dirlist=readdir D;
604 foreach my $directory (@dirlist) {
605 -d "$htdocs/$directory/en" and push @themes, $directory;