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
25 our ($VERSION,@ISA,@EXPORT);
31 C4::Koha - Perl Module containing convenience functions for Koha scripts
40 Koha.pm provides many functions for Koha scripts.
52 &subfield_is_koha_internal_p
53 &GetPrinters &GetPrinter
54 &GetItemTypes &getitemtypeinfo
57 &getframeworks &getframeworkinfo
58 &getauthtypes &getauthtype
63 &getitemtypeimagesrcfromurl
65 &get_notforloan_label_of
70 &GetKohaAuthorisedValues
71 &GetManagedTagSubfields
80 $slash_date = &slashifyDate($dash_date);
82 Takes a string of the form "DD-MM-YYYY" (or anything separated by
83 dashes), converts it to the form "YYYY/MM/DD", and returns the result.
89 # accepts a date of the form xx-xx-xx[xx] and returns it in the
91 my @dateOut = split( '-', shift );
92 return ("$dateOut[2]/$dateOut[1]/$dateOut[0]");
98 my $string = DisplayISBN( $isbn );
104 if (length ($isbn)<13){
106 if ( substr( $isbn, 0, 1 ) <= 7 ) {
107 $seg1 = substr( $isbn, 0, 1 );
109 elsif ( substr( $isbn, 0, 2 ) <= 94 ) {
110 $seg1 = substr( $isbn, 0, 2 );
112 elsif ( substr( $isbn, 0, 3 ) <= 995 ) {
113 $seg1 = substr( $isbn, 0, 3 );
115 elsif ( substr( $isbn, 0, 4 ) <= 9989 ) {
116 $seg1 = substr( $isbn, 0, 4 );
119 $seg1 = substr( $isbn, 0, 5 );
121 my $x = substr( $isbn, length($seg1) );
123 if ( substr( $x, 0, 2 ) <= 19 ) {
125 # if(sTmp2 < 10) sTmp2 = "0" sTmp2;
126 $seg2 = substr( $x, 0, 2 );
128 elsif ( substr( $x, 0, 3 ) <= 699 ) {
129 $seg2 = substr( $x, 0, 3 );
131 elsif ( substr( $x, 0, 4 ) <= 8399 ) {
132 $seg2 = substr( $x, 0, 4 );
134 elsif ( substr( $x, 0, 5 ) <= 89999 ) {
135 $seg2 = substr( $x, 0, 5 );
137 elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
138 $seg2 = substr( $x, 0, 6 );
141 $seg2 = substr( $x, 0, 7 );
143 my $seg3 = substr( $x, length($seg2) );
144 $seg3 = substr( $seg3, 0, length($seg3) - 1 );
145 my $seg4 = substr( $x, -1, 1 );
146 return "$seg1-$seg2-$seg3-$seg4";
149 $seg1 = substr( $isbn, 0, 3 );
151 if ( substr( $isbn, 3, 1 ) <= 7 ) {
152 $seg2 = substr( $isbn, 3, 1 );
154 elsif ( substr( $isbn, 3, 2 ) <= 94 ) {
155 $seg2 = substr( $isbn, 3, 2 );
157 elsif ( substr( $isbn, 3, 3 ) <= 995 ) {
158 $seg2 = substr( $isbn, 3, 3 );
160 elsif ( substr( $isbn, 3, 4 ) <= 9989 ) {
161 $seg2 = substr( $isbn, 3, 4 );
164 $seg2 = substr( $isbn, 3, 5 );
166 my $x = substr( $isbn, length($seg2) +3);
168 if ( substr( $x, 0, 2 ) <= 19 ) {
170 # if(sTmp2 < 10) sTmp2 = "0" sTmp2;
171 $seg3 = substr( $x, 0, 2 );
173 elsif ( substr( $x, 0, 3 ) <= 699 ) {
174 $seg3 = substr( $x, 0, 3 );
176 elsif ( substr( $x, 0, 4 ) <= 8399 ) {
177 $seg3 = substr( $x, 0, 4 );
179 elsif ( substr( $x, 0, 5 ) <= 89999 ) {
180 $seg3 = substr( $x, 0, 5 );
182 elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
183 $seg3 = substr( $x, 0, 6 );
186 $seg3 = substr( $x, 0, 7 );
188 my $seg4 = substr( $x, length($seg3) );
189 $seg4 = substr( $seg4, 0, length($seg4) - 1 );
190 my $seg5 = substr( $x, -1, 1 );
191 return "$seg1-$seg2-$seg3-$seg4-$seg5";
195 # FIXME.. this should be moved to a MARC-specific module
196 sub subfield_is_koha_internal_p ($) {
199 # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
200 # But real MARC subfields are always single-character
201 # so it really is safer just to check the length
203 return length $subfield != 1;
208 $itemtypes = &GetItemTypes();
210 Returns information about existing itemtypes.
212 build a HTML select with the following code :
214 =head3 in PERL SCRIPT
216 my $itemtypes = GetItemTypes;
218 foreach my $thisitemtype (sort keys %$itemtypes) {
219 my $selected = 1 if $thisitemtype eq $itemtype;
220 my %row =(value => $thisitemtype,
221 selected => $selected,
222 description => $itemtypes->{$thisitemtype}->{'description'},
224 push @itemtypesloop, \%row;
226 $template->param(itemtypeloop => \@itemtypesloop);
230 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
231 <select name="itemtype">
232 <option value="">Default</option>
233 <!-- TMPL_LOOP name="itemtypeloop" -->
234 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
237 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
238 <input type="submit" value="OK" class="button">
245 # returns a reference to a hash of references to branches...
247 my $dbh = C4::Context->dbh;
252 my $sth = $dbh->prepare($query);
254 while ( my $IT = $sth->fetchrow_hashref ) {
255 $itemtypes{ $IT->{'itemtype'} } = $IT;
257 return ( \%itemtypes );
260 sub get_itemtypeinfos_of {
268 WHERE itemtype IN (' . join( ',', map( { "'" . $_ . "'" } @itemtypes ) ) . ')
271 return get_infos_of( $query, 'itemtype' );
274 # this is temporary until we separate collection codes and item types
278 my $dbh = C4::Context->dbh;
281 "SELECT * FROM authorised_values ORDER BY authorised_value");
283 while ( my $data = $sth->fetchrow_hashref ) {
284 if ( $data->{category} eq "CCODE" ) {
286 $results[$count] = $data;
292 return ( $count, @results );
297 $authtypes = &getauthtypes();
299 Returns information about existing authtypes.
301 build a HTML select with the following code :
303 =head3 in PERL SCRIPT
305 my $authtypes = getauthtypes;
307 foreach my $thisauthtype (keys %$authtypes) {
308 my $selected = 1 if $thisauthtype eq $authtype;
309 my %row =(value => $thisauthtype,
310 selected => $selected,
311 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
313 push @authtypesloop, \%row;
315 $template->param(itemtypeloop => \@itemtypesloop);
319 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
320 <select name="authtype">
321 <!-- TMPL_LOOP name="authtypeloop" -->
322 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
325 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
326 <input type="submit" value="OK" class="button">
334 # returns a reference to a hash of references to authtypes...
336 my $dbh = C4::Context->dbh;
337 my $sth = $dbh->prepare("select * from auth_types order by authtypetext");
339 while ( my $IT = $sth->fetchrow_hashref ) {
340 $authtypes{ $IT->{'authtypecode'} } = $IT;
342 return ( \%authtypes );
346 my ($authtypecode) = @_;
348 # returns a reference to a hash of references to authtypes...
350 my $dbh = C4::Context->dbh;
351 my $sth = $dbh->prepare("select * from auth_types where authtypecode=?");
352 $sth->execute($authtypecode);
353 my $res = $sth->fetchrow_hashref;
359 $frameworks = &getframework();
361 Returns information about existing frameworks
363 build a HTML select with the following code :
365 =head3 in PERL SCRIPT
367 my $frameworks = frameworks();
369 foreach my $thisframework (keys %$frameworks) {
370 my $selected = 1 if $thisframework eq $frameworkcode;
371 my %row =(value => $thisframework,
372 selected => $selected,
373 description => $frameworks->{$thisframework}->{'frameworktext'},
375 push @frameworksloop, \%row;
377 $template->param(frameworkloop => \@frameworksloop);
381 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
382 <select name="frameworkcode">
383 <option value="">Default</option>
384 <!-- TMPL_LOOP name="frameworkloop" -->
385 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
388 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
389 <input type="submit" value="OK" class="button">
397 # returns a reference to a hash of references to branches...
399 my $dbh = C4::Context->dbh;
400 my $sth = $dbh->prepare("select * from biblio_framework");
402 while ( my $IT = $sth->fetchrow_hashref ) {
403 $itemtypes{ $IT->{'frameworkcode'} } = $IT;
405 return ( \%itemtypes );
408 =head2 getframeworkinfo
410 $frameworkinfo = &getframeworkinfo($frameworkcode);
412 Returns information about an frameworkcode.
416 sub getframeworkinfo {
417 my ($frameworkcode) = @_;
418 my $dbh = C4::Context->dbh;
420 $dbh->prepare("select * from biblio_framework where frameworkcode=?");
421 $sth->execute($frameworkcode);
422 my $res = $sth->fetchrow_hashref;
426 =head2 getitemtypeinfo
428 $itemtype = &getitemtype($itemtype);
430 Returns information about an itemtype.
434 sub getitemtypeinfo {
436 my $dbh = C4::Context->dbh;
437 my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
438 $sth->execute($itemtype);
439 my $res = $sth->fetchrow_hashref;
441 $res->{imageurl} = getitemtypeimagesrcfromurl( $res->{imageurl} );
446 sub getitemtypeimagesrcfromurl {
449 if ( defined $imageurl and $imageurl !~ m/^http/ ) {
450 $imageurl = getitemtypeimagesrc() . '/' . $imageurl;
456 sub getitemtypeimagedir {
457 return C4::Context->opachtdocs . '/'
458 . C4::Context->preference('template')
462 sub getitemtypeimagesrc {
463 return '/opac-tmpl' . '/'
464 . C4::Context->preference('template')
470 $printers = &GetPrinters();
471 @queues = keys %$printers;
473 Returns information about existing printer queues.
475 C<$printers> is a reference-to-hash whose keys are the print queues
476 defined in the printers table of the Koha database. The values are
477 references-to-hash, whose keys are the fields in the printers table.
483 my $dbh = C4::Context->dbh;
484 my $sth = $dbh->prepare("select * from printers");
486 while ( my $printer = $sth->fetchrow_hashref ) {
487 $printers{ $printer->{'printqueue'} } = $printer;
489 return ( \%printers );
494 $printer = GetPrinter( $query, $printers );
498 sub GetPrinter ($$) {
499 my ( $query, $printers ) = @_; # get printer for this query from printers
500 my $printer = $query->param('printer');
501 my %cookie = $query->cookie('userenv');
502 ($printer) || ( $printer = $cookie{'printer'} ) || ( $printer = '' );
503 ( $printers->{$printer} ) || ( $printer = ( keys %$printers )[0] );
509 Returns the number of pages to display in a pagination bar, given the number
510 of items and the number of items per page.
515 my ( $nb_items, $nb_items_per_page ) = @_;
517 return int( ( $nb_items - 1 ) / $nb_items_per_page ) + 1;
522 (@themes) = &getallthemes('opac');
523 (@themes) = &getallthemes('intranet');
525 Returns an array of all available themes.
533 if ( $type eq 'intranet' ) {
534 $htdocs = C4::Context->config('intrahtdocs');
537 $htdocs = C4::Context->config('opachtdocs');
539 opendir D, "$htdocs";
540 my @dirlist = readdir D;
541 foreach my $directory (@dirlist) {
542 -d "$htdocs/$directory/en" and push @themes, $directory;
549 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
552 link_value => 'su-to',
553 label_value => 'Topics',
555 [ '600', '601', '602', '603', '604', '605', '606', '610' ],
559 link_value => 'su-geo',
560 label_value => 'Places',
565 link_value => 'su-ut',
566 label_value => 'Titles',
567 tags => [ '500', '501', '502', '503', '504', ],
572 label_value => 'Authors',
573 tags => [ '700', '701', '702', ],
578 label_value => 'Series',
583 link_value => 'branch',
584 label_value => 'Branches',
594 link_value => 'su-to',
595 label_value => 'Topics',
601 # link_value => 'su-na',
602 # label_value => 'People and Organizations',
603 # tags => ['600', '610', '611'],
607 link_value => 'su-geo',
608 label_value => 'Places',
613 link_value => 'su-ut',
614 label_value => 'Titles',
620 label_value => 'Authors',
621 tags => [ '100', '110', '700', ],
626 label_value => 'Series',
627 tags => [ '440', '490', ],
631 link_value => 'branch',
632 label_value => 'Branches',
644 Return a href where a key is associated to a href. You give a query, the
645 name of the key among the fields returned by the query. If you also give as
646 third argument the name of the value, the function returns a href of scalar.
655 # generic href of any information on the item, href of href.
656 my $iteminfos_of = get_infos_of($query, 'itemnumber');
657 print $iteminfos_of->{$itemnumber}{barcode};
659 # specific information, href of scalar
660 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
661 print $barcode_of_item->{$itemnumber};
666 my ( $query, $key_name, $value_name ) = @_;
668 my $dbh = C4::Context->dbh;
670 my $sth = $dbh->prepare($query);
674 while ( my $row = $sth->fetchrow_hashref ) {
675 if ( defined $value_name ) {
676 $infos_of{ $row->{$key_name} } = $row->{$value_name};
679 $infos_of{ $row->{$key_name} } = $row;
687 =head2 get_notforloan_label_of
689 my $notforloan_label_of = get_notforloan_label_of();
691 Each authorised value of notforloan (information available in items and
692 itemtypes) is link to a single label.
694 Returns a href where keys are authorised values and values are corresponding
697 foreach my $authorised_value (keys %{$notforloan_label_of}) {
699 "authorised_value: %s => %s\n",
701 $notforloan_label_of->{$authorised_value}
707 # FIXME - why not use GetAuthorisedValues ??
709 sub get_notforloan_label_of {
710 my $dbh = C4::Context->dbh;
713 SELECT authorised_value
714 FROM marc_subfield_structure
715 WHERE kohafield = \'items.notforloan\'
718 my $sth = $dbh->prepare($query);
720 my ($statuscode) = $sth->fetchrow_array();
725 FROM authorised_values
728 $sth = $dbh->prepare($query);
729 $sth->execute($statuscode);
730 my %notforloan_label_of;
731 while ( my $row = $sth->fetchrow_hashref ) {
732 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
736 return \%notforloan_label_of;
740 my ( $position, $type ) = @_;
741 my $dbh = C4::Context->dbh;
742 my $strsth = "SELECT * FROM z3950servers where 1";
743 $strsth .= " AND position=\"$position\"" if ($position);
744 $strsth .= " AND type=\"$type\"" if ($type);
745 my $rq = $dbh->prepare($strsth);
747 my @primaryserverloop;
749 while ( my $data = $rq->fetchrow_hashref ) {
751 $cell{label} = $data->{'description'};
752 $cell{id} = $data->{'name'};
755 . ( $data->{port} ? ":" . $data->{port} : "" ) . "/"
757 if ( $data->{host} );
758 $cell{checked} = $data->{checked};
759 push @primaryserverloop,
761 label => $data->{description},
764 value => $data->{host} . ":"
765 . $data->{port} . "/"
767 checked => "checked",
768 icon => $data->{icon},
769 zed => $data->{type} eq 'zed',
770 opensearch => $data->{type} eq 'opensearch'
773 return \@primaryserverloop;
776 sub displaySecondaryServers {
778 # my $secondary_servers_loop = [
779 # { inner_sup_servers_loop => [
780 # {label => "Google", id=>"GOOG", value=>"google",icon => "google.ico",opensearch => "1"},
781 # {label => "Yahoo", id=>"YAH", value=>"yahoo", icon =>"yahoo.ico", zed => "1"},
782 # {label => "Worldcat", id=>"WCT", value=>"worldcat", icon => "worldcat.gif", zed => "1"},
783 # {label => "Library of Congress", id=>"LOC", name=> "server", value=>"z3950.loc.gov:7090/Voyager", icon =>"loc.ico", zed => "1"},
787 return; #$secondary_servers_loop;
790 =head2 GetAuthorisedValues
792 $authvalues = GetAuthorisedValues($category);
794 this function get all authorised values from 'authosied_value' table into a reference to array which
795 each value containt an hashref.
797 Set C<$category> on input args if you want to limits your query to this one. This params is not mandatory.
801 sub GetAuthorisedValues {
802 my ($category,$selected) = @_;
805 my $dbh = C4::Context->dbh;
806 my $query = "SELECT * FROM authorised_values";
807 $query .= " WHERE category = '" . $category . "'" if $category;
809 my $sth = $dbh->prepare($query);
811 while (my $data=$sth->fetchrow_hashref) {
812 if ($selected eq $data->{'authorised_value'} ) {
813 $data->{'selected'} = 1;
815 $results[$count] = $data;
818 #my $data = $sth->fetchall_arrayref({});
819 return \@results; #$data;
824 $marcrecord = &fixEncoding($marcblob);
826 Returns a well encoded marcrecord.
831 my $record = MARC::Record->new_from_usmarc($marc);
832 if (C4::Context->preference("MARCFLAVOUR") eq "UNIMARC"){
834 my $targetcharset="utf8" if (C4::Context->preference("TemplateEncoding") eq "utf-8");
835 $targetcharset="latin1" if (C4::Context->preference("TemplateEncoding") eq "iso-8859-1");
836 my $decoder = guess_encoding($marc, qw/utf8 latin1/);
837 # die $decoder unless ref($decoder);
839 my $newRecord=MARC::Record->new();
840 foreach my $field ($record->fields()){
841 if ($field->tag()<'010'){
842 $newRecord->insert_grouped_field($field);
846 foreach my $subfield ($field->subfields()){
848 if (($newField->tag eq '100')) {
849 substr($subfield->[1],26,2,"0103") if ($targetcharset eq "latin1");
850 substr($subfield->[1],26,4,"5050") if ($targetcharset eq "utf8");
852 map {C4::Biblio::char_decode($_,"UNIMARC")} @$subfield;
853 $newField->add_subfields($subfield->[0]=>$subfield->[1]);
855 map {C4::Biblio::char_decode($_,"UNIMARC")} @$subfield;
856 $newField=MARC::Field->new($field->tag(),$field->indicator(1),$field->indicator(2),$subfield->[0]=>$subfield->[1]);
860 $newRecord->insert_grouped_field($newField);
863 # warn $newRecord->as_formatted();
873 =head2 GetKohaAuthorisedValues
875 Takes $dbh , $kohafield as parameters.
876 returns hashref of authvalCode => liblibrarian
877 or undef if no authvals defined for kohafield.
881 sub GetKohaAuthorisedValues {
882 my ($kohafield) = @_;
884 my $dbh = C4::Context->dbh;
885 my $sthnflstatus = $dbh->prepare('select authorised_value from marc_subfield_structure where kohafield=?');
886 $sthnflstatus->execute($kohafield);
887 my $authorised_valuecode = $sthnflstatus->fetchrow;
888 if ($authorised_valuecode) {
889 $sthnflstatus = $dbh->prepare("select authorised_value, lib from authorised_values where category=? ");
890 $sthnflstatus->execute($authorised_valuecode);
891 while ( my ($val, $lib) = $sthnflstatus->fetchrow_array ) {
898 =head2 GetManagedTagSubfields
902 $res = GetManagedTagSubfields();
904 Returns a reference to a big hash of hash, with the Marc structure fro the given frameworkcode
905 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
906 $frameworkcode : the framework code to read
914 sub GetManagedTagSubfields{
915 my $dbh=C4::Context->dbh;
916 my $rq=$dbh->prepare(qq|
918 DISTINCT CONCAT( marc_subfield_structure.tagfield, tagsubfield ) AS tagsubfield,
919 marc_subfield_structure.liblibrarian as subfielddesc,
920 marc_tag_structure.liblibrarian as tagdesc
921 FROM marc_subfield_structure
922 LEFT JOIN marc_tag_structure
923 ON marc_tag_structure.tagfield = marc_subfield_structure.tagfield
924 AND marc_tag_structure.frameworkcode = marc_subfield_structure.frameworkcode
925 WHERE marc_subfield_structure.tab>=0
926 ORDER BY tagsubfield|);
928 my $data=$rq->fetchall_arrayref({});