3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2010 Nelsonville Public Library
5 # Parts copyright 2010 BibLibre
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 #use warnings; FIXME - Bug 2505
28 use Koha::DateUtils qw(dt_from_string);
30 use Koha::MarcSubfieldStructures;
31 use DateTime::Format::MySQL;
33 use autouse 'Data::cselectall_arrayref' => qw(Dumper);
34 use DBI qw(:sql_types);
35 use vars qw(@ISA @EXPORT @EXPORT_OK $DEBUG);
41 &GetPrinters &GetPrinter
42 &GetItemTypes &getitemtypeinfo
43 &GetItemTypesCategorized &GetItemTypesByCategory
44 &getframeworks &getframeworkinfo
50 &get_notforloan_label_of
53 &getitemtypeimagelocation
55 &GetAuthorisedValueCategories
56 &GetKohaAuthorisedValues
57 &GetKohaAuthorisedValuesFromField
58 &GetKohaAuthorisedValuesMapping
59 &GetAuthorisedValueByCode
64 &GetNormalizedOCLCNumber
74 @EXPORT_OK = qw( GetDailyQuote );
79 C4::Koha - Perl Module containing convenience functions for Koha scripts
87 Koha.pm provides many functions for Koha scripts.
95 $itemtypes = &GetItemTypes( style => $style );
97 Returns information about existing itemtypes.
100 style: either 'array' or 'hash', defaults to 'hash'.
101 'array' returns an arrayref,
102 'hash' return a hashref with the itemtype value as the key
104 build a HTML select with the following code :
106 =head3 in PERL SCRIPT
108 my $itemtypes = GetItemTypes;
110 foreach my $thisitemtype (sort keys %$itemtypes) {
111 my $selected = 1 if $thisitemtype eq $itemtype;
112 my %row =(value => $thisitemtype,
113 selected => $selected,
114 description => $itemtypes->{$thisitemtype}->{'description'},
116 push @itemtypesloop, \%row;
118 $template->param(itemtypeloop => \@itemtypesloop);
122 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
123 <select name="itemtype">
124 <option value="">Default</option>
125 <!-- TMPL_LOOP name="itemtypeloop" -->
126 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
129 <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
130 <input type="submit" value="OK" class="button">
137 my $style = defined( $params{'style'} ) ? $params{'style'} : 'hash';
139 require C4::Languages;
140 my $language = C4::Languages::getlanguage();
141 # returns a reference to a hash of references to itemtypes...
142 my $dbh = C4::Context->dbh;
146 itemtypes.description,
147 itemtypes.rentalcharge,
148 itemtypes.notforloan,
151 itemtypes.checkinmsg,
152 itemtypes.checkinmsgtype,
153 itemtypes.sip_media_type,
154 itemtypes.hideinopac,
155 itemtypes.searchcategory,
156 COALESCE( localization.translation, itemtypes.description ) AS translated_description
158 LEFT JOIN localization ON itemtypes.itemtype = localization.code
159 AND localization.entity = 'itemtypes'
160 AND localization.lang = ?
163 my $sth = $dbh->prepare($query);
164 $sth->execute( $language );
166 if ( $style eq 'hash' ) {
168 while ( my $IT = $sth->fetchrow_hashref ) {
169 $itemtypes{ $IT->{'itemtype'} } = $IT;
171 return ( \%itemtypes );
173 return [ sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @{ $sth->fetchall_arrayref( {} ) } ];
177 =head2 GetItemTypesCategorized
179 $categories = GetItemTypesCategorized();
181 Returns a hashref containing search categories.
182 A search category will be put in the hash if at least one of its itemtypes is visible in OPAC.
183 The categories must be part of Authorized Values (ITEMTYPECAT)
187 sub GetItemTypesCategorized {
188 my $dbh = C4::Context->dbh;
189 # Order is important, so that partially hidden (some items are not visible in OPAC) search
190 # categories will be visible. hideinopac=0 must be last.
192 SELECT itemtype, description, imageurl, hideinopac, 0 as 'iscat' FROM itemtypes WHERE ISNULL(searchcategory) or length(searchcategory) = 0
194 SELECT DISTINCT searchcategory AS `itemtype`,
195 authorised_values.lib_opac AS description,
196 authorised_values.imageurl AS imageurl,
197 hideinopac, 1 as 'iscat'
199 LEFT JOIN authorised_values ON searchcategory = authorised_value
200 WHERE searchcategory > '' and hideinopac=1
202 SELECT DISTINCT searchcategory AS `itemtype`,
203 authorised_values.lib_opac AS description,
204 authorised_values.imageurl AS imageurl,
205 hideinopac, 1 as 'iscat'
207 LEFT JOIN authorised_values ON searchcategory = authorised_value
208 WHERE searchcategory > '' and hideinopac=0
210 return ($dbh->selectall_hashref($query,'itemtype'));
213 =head2 GetItemTypesByCategory
215 @results = GetItemTypesByCategory( $searchcategory );
217 Returns the itemtype code of all itemtypes included in a searchcategory.
221 sub GetItemTypesByCategory {
225 my $dbh = C4::Context->dbh;
226 my $query = qq|SELECT itemtype FROM itemtypes WHERE searchcategory=?|;
227 my $tmp=$dbh->selectcol_arrayref($query,undef,$category);
233 $frameworks = &getframework();
235 Returns information about existing frameworks
237 build a HTML select with the following code :
239 =head3 in PERL SCRIPT
241 my $frameworks = getframeworks();
243 foreach my $thisframework (keys %$frameworks) {
244 my $selected = 1 if $thisframework eq $frameworkcode;
246 value => $thisframework,
247 selected => $selected,
248 description => $frameworks->{$thisframework}->{'frameworktext'},
250 push @frameworksloop, \%row;
252 $template->param(frameworkloop => \@frameworksloop);
256 <form action="[% script_name %] method=post>
257 <select name="frameworkcode">
258 <option value="">Default</option>
259 [% FOREACH framework IN frameworkloop %]
260 [% IF ( framework.selected ) %]
261 <option value="[% framework.value %]" selected="selected">[% framework.description %]</option>
263 <option value="[% framework.value %]">[% framework.description %]</option>
267 <input type=text name=searchfield value="[% searchfield %]">
268 <input type="submit" value="OK" class="button">
275 # returns a reference to a hash of references to branches...
277 my $dbh = C4::Context->dbh;
278 my $sth = $dbh->prepare("select * from biblio_framework");
280 while ( my $IT = $sth->fetchrow_hashref ) {
281 $itemtypes{ $IT->{'frameworkcode'} } = $IT;
283 return ( \%itemtypes );
286 =head2 GetFrameworksLoop
288 $frameworks = GetFrameworksLoop( $frameworkcode );
290 Returns the loop suggested on getframework(), but ordered by framework description.
292 build a HTML select with the following code :
294 =head3 in PERL SCRIPT
296 $template->param( frameworkloop => GetFrameworksLoop( $frameworkcode ) );
300 Same as getframework()
302 <form action="[% script_name %] method=post>
303 <select name="frameworkcode">
304 <option value="">Default</option>
305 [% FOREACH framework IN frameworkloop %]
306 [% IF ( framework.selected ) %]
307 <option value="[% framework.value %]" selected="selected">[% framework.description %]</option>
309 <option value="[% framework.value %]">[% framework.description %]</option>
313 <input type=text name=searchfield value="[% searchfield %]">
314 <input type="submit" value="OK" class="button">
319 sub GetFrameworksLoop {
320 my $frameworkcode = shift;
321 my $frameworks = getframeworks();
323 foreach my $thisframework (sort { uc($frameworks->{$a}->{'frameworktext'}) cmp uc($frameworks->{$b}->{'frameworktext'}) } keys %$frameworks) {
324 my $selected = ( $thisframework eq $frameworkcode ) ? 1 : undef;
326 value => $thisframework,
327 selected => $selected,
328 description => $frameworks->{$thisframework}->{'frameworktext'},
330 push @frameworkloop, \%row;
332 return \@frameworkloop;
335 =head2 getframeworkinfo
337 $frameworkinfo = &getframeworkinfo($frameworkcode);
339 Returns information about an frameworkcode.
343 sub getframeworkinfo {
344 my ($frameworkcode) = @_;
345 my $dbh = C4::Context->dbh;
347 $dbh->prepare("select * from biblio_framework where frameworkcode=?");
348 $sth->execute($frameworkcode);
349 my $res = $sth->fetchrow_hashref;
353 =head2 getitemtypeinfo
355 $itemtype = &getitemtypeinfo($itemtype, [$interface]);
357 Returns information about an itemtype. The optional $interface argument
358 sets which interface ('opac' or 'intranet') to return the imageurl for.
359 Defaults to intranet.
363 sub getitemtypeinfo {
364 my ($itemtype, $interface) = @_;
365 my $dbh = C4::Context->dbh;
366 require C4::Languages;
367 my $language = C4::Languages::getlanguage();
368 my $it = $dbh->selectrow_hashref(q|
371 itemtypes.description,
372 itemtypes.rentalcharge,
373 itemtypes.notforloan,
376 itemtypes.checkinmsg,
377 itemtypes.checkinmsgtype,
378 itemtypes.sip_media_type,
379 COALESCE( localization.translation, itemtypes.description ) AS translated_description
381 LEFT JOIN localization ON itemtypes.itemtype = localization.code
382 AND localization.entity = 'itemtypes'
383 AND localization.lang = ?
384 WHERE itemtypes.itemtype = ?
385 |, undef, $language, $itemtype );
387 $it->{imageurl} = getitemtypeimagelocation( ( ( defined $interface && $interface eq 'opac' ) ? 'opac' : 'intranet' ), $it->{imageurl} );
392 =head2 getitemtypeimagedir
394 my $directory = getitemtypeimagedir( 'opac' );
396 pass in 'opac' or 'intranet'. Defaults to 'opac'.
398 returns the full path to the appropriate directory containing images.
402 sub getitemtypeimagedir {
403 my $src = shift || 'opac';
404 if ($src eq 'intranet') {
405 return C4::Context->config('intrahtdocs') . '/' .C4::Context->preference('template') . '/img/itemtypeimg';
407 return C4::Context->config('opachtdocs') . '/' . C4::Context->preference('opacthemes') . '/itemtypeimg';
411 sub getitemtypeimagesrc {
412 my $src = shift || 'opac';
413 if ($src eq 'intranet') {
414 return '/intranet-tmpl' . '/' . C4::Context->preference('template') . '/img/itemtypeimg';
416 return '/opac-tmpl' . '/' . C4::Context->preference('opacthemes') . '/itemtypeimg';
420 sub getitemtypeimagelocation {
421 my ( $src, $image ) = @_;
423 return '' if ( !$image );
426 my $scheme = ( URI::Split::uri_split( $image ) )[0];
428 return $image if ( $scheme );
430 return getitemtypeimagesrc( $src ) . '/' . $image;
433 =head3 _getImagesFromDirectory
435 Find all of the image files in a directory in the filesystem
437 parameters: a directory name
439 returns: a list of images in that directory.
441 Notes: this does not traverse into subdirectories. See
442 _getSubdirectoryNames for help with that.
443 Images are assumed to be files with .gif or .png file extensions.
444 The image names returned do not have the directory name on them.
448 sub _getImagesFromDirectory {
449 my $directoryname = shift;
450 return unless defined $directoryname;
451 return unless -d $directoryname;
453 if ( opendir ( my $dh, $directoryname ) ) {
454 my @images = grep { /\.(gif|png)$/i } readdir( $dh );
456 @images = sort(@images);
459 warn "unable to opendir $directoryname: $!";
464 =head3 _getSubdirectoryNames
466 Find all of the directories in a directory in the filesystem
468 parameters: a directory name
470 returns: a list of subdirectories in that directory.
472 Notes: this does not traverse into subdirectories. Only the first
473 level of subdirectories are returned.
474 The directory names returned don't have the parent directory name on them.
478 sub _getSubdirectoryNames {
479 my $directoryname = shift;
480 return unless defined $directoryname;
481 return unless -d $directoryname;
483 if ( opendir ( my $dh, $directoryname ) ) {
484 my @directories = grep { -d File::Spec->catfile( $directoryname, $_ ) && ! ( /^\./ ) } readdir( $dh );
488 warn "unable to opendir $directoryname: $!";
495 returns: a listref of hashrefs. Each hash represents another collection of images.
497 { imagesetname => 'npl', # the name of the image set (npl is the original one)
498 images => listref of image hashrefs
501 each image is represented by a hashref like this:
503 { KohaImage => 'npl/image.gif',
504 StaffImageUrl => '/intranet-tmpl/prog/img/itemtypeimg/npl/image.gif',
505 OpacImageURL => '/opac-tmpl/prog/itemtypeimg/npl/image.gif'
506 checked => 0 or 1: was this the image passed to this method?
507 Note: I'd like to remove this somehow.
514 my $checked = $params{'checked'} || '';
516 my $paths = { staff => { filesystem => getitemtypeimagedir('intranet'),
517 url => getitemtypeimagesrc('intranet'),
519 opac => { filesystem => getitemtypeimagedir('opac'),
520 url => getitemtypeimagesrc('opac'),
524 my @imagesets = (); # list of hasrefs of image set data to pass to template
525 my @subdirectories = _getSubdirectoryNames( $paths->{'staff'}{'filesystem'} );
526 foreach my $imagesubdir ( @subdirectories ) {
527 warn $imagesubdir if $DEBUG;
528 my @imagelist = (); # hashrefs of image info
529 my @imagenames = _getImagesFromDirectory( File::Spec->catfile( $paths->{'staff'}{'filesystem'}, $imagesubdir ) );
530 my $imagesetactive = 0;
531 foreach my $thisimage ( @imagenames ) {
533 { KohaImage => "$imagesubdir/$thisimage",
534 StaffImageUrl => join( '/', $paths->{'staff'}{'url'}, $imagesubdir, $thisimage ),
535 OpacImageUrl => join( '/', $paths->{'opac'}{'url'}, $imagesubdir, $thisimage ),
536 checked => "$imagesubdir/$thisimage" eq $checked ? 1 : 0,
539 $imagesetactive = 1 if "$imagesubdir/$thisimage" eq $checked;
541 push @imagesets, { imagesetname => $imagesubdir,
542 imagesetactive => $imagesetactive,
543 images => \@imagelist };
551 $printers = &GetPrinters();
552 @queues = keys %$printers;
554 Returns information about existing printer queues.
556 C<$printers> is a reference-to-hash whose keys are the print queues
557 defined in the printers table of the Koha database. The values are
558 references-to-hash, whose keys are the fields in the printers table.
564 my $dbh = C4::Context->dbh;
565 my $sth = $dbh->prepare("select * from printers");
567 while ( my $printer = $sth->fetchrow_hashref ) {
568 $printers{ $printer->{'printqueue'} } = $printer;
570 return ( \%printers );
575 $printer = GetPrinter( $query, $printers );
580 my ( $query, $printers ) = @_; # get printer for this query from printers
581 my $printer = $query->param('printer');
582 my %cookie = $query->cookie('userenv');
583 ($printer) || ( $printer = $cookie{'printer'} ) || ( $printer = '' );
584 ( $printers->{$printer} ) || ( $printer = ( keys %$printers )[0] );
590 Returns the number of pages to display in a pagination bar, given the number
591 of items and the number of items per page.
596 my ( $nb_items, $nb_items_per_page ) = @_;
598 return int( ( $nb_items - 1 ) / $nb_items_per_page ) + 1;
603 (@themes) = &getallthemes('opac');
604 (@themes) = &getallthemes('intranet');
606 Returns an array of all available themes.
614 if ( $type eq 'intranet' ) {
615 $htdocs = C4::Context->config('intrahtdocs');
618 $htdocs = C4::Context->config('opachtdocs');
620 opendir D, "$htdocs";
621 my @dirlist = readdir D;
622 foreach my $directory (@dirlist) {
623 next if $directory eq 'lib';
624 -d "$htdocs/$directory/en" and push @themes, $directory;
631 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
636 tags => [ qw/ 600ab 601ab 602a 604at 605a 606ax 610a / ],
642 tags => [ qw/ 607a / ],
648 tags => [ qw/ 500a 501a 503a / ],
654 tags => [ qw/ 700ab 701ab 702ab / ],
655 sep => C4::Context->preference("UNIMARCAuthorsFacetsSeparator"),
660 tags => [ qw/ 225a / ],
666 tags => [ qw/ 995e / ],
670 unless ( Koha::Libraries->search->count == 1 )
672 my $DisplayLibraryFacets = C4::Context->preference('DisplayLibraryFacets');
673 if ( $DisplayLibraryFacets eq 'both'
674 || $DisplayLibraryFacets eq 'holding' )
679 idx => 'holdingbranch',
680 label => 'HoldingLibrary',
681 tags => [qw / 995c /],
686 if ( $DisplayLibraryFacets eq 'both'
687 || $DisplayLibraryFacets eq 'home' )
693 label => 'HomeLibrary',
694 tags => [qw / 995b /],
705 tags => [ qw/ 650a / ],
710 # label => 'People and Organizations',
711 # tags => [ qw/ 600a 610a 611a / ],
717 tags => [ qw/ 651a / ],
723 tags => [ qw/ 630a / ],
729 tags => [ qw/ 100a 110a 700a / ],
735 tags => [ qw/ 440a 490a / ],
740 label => 'ItemTypes',
741 tags => [ qw/ 952y 942c / ],
747 tags => [ qw / 952c / ],
751 unless ( Koha::Libraries->search->count == 1 )
753 my $DisplayLibraryFacets = C4::Context->preference('DisplayLibraryFacets');
754 if ( $DisplayLibraryFacets eq 'both'
755 || $DisplayLibraryFacets eq 'holding' )
760 idx => 'holdingbranch',
761 label => 'HoldingLibrary',
762 tags => [qw / 952b /],
767 if ( $DisplayLibraryFacets eq 'both'
768 || $DisplayLibraryFacets eq 'home' )
774 label => 'HomeLibrary',
775 tags => [qw / 952a /],
786 Return a href where a key is associated to a href. You give a query,
787 the name of the key among the fields returned by the query. If you
788 also give as third argument the name of the value, the function
789 returns a href of scalar. The optional 4th argument is an arrayref of
790 items passed to the C<execute()> call. It is designed to bind
791 parameters to any placeholders in your SQL.
800 # generic href of any information on the item, href of href.
801 my $iteminfos_of = get_infos_of($query, 'itemnumber');
802 print $iteminfos_of->{$itemnumber}{barcode};
804 # specific information, href of scalar
805 my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
806 print $barcode_of_item->{$itemnumber};
811 my ( $query, $key_name, $value_name, $bind_params ) = @_;
813 my $dbh = C4::Context->dbh;
815 my $sth = $dbh->prepare($query);
816 $sth->execute( @$bind_params );
819 while ( my $row = $sth->fetchrow_hashref ) {
820 if ( defined $value_name ) {
821 $infos_of{ $row->{$key_name} } = $row->{$value_name};
824 $infos_of{ $row->{$key_name} } = $row;
832 =head2 get_notforloan_label_of
834 my $notforloan_label_of = get_notforloan_label_of();
836 Each authorised value of notforloan (information available in items and
837 itemtypes) is link to a single label.
839 Returns a href where keys are authorised values and values are corresponding
842 foreach my $authorised_value (keys %{$notforloan_label_of}) {
844 "authorised_value: %s => %s\n",
846 $notforloan_label_of->{$authorised_value}
852 # FIXME - why not use GetAuthorisedValues ??
854 sub get_notforloan_label_of {
855 my $dbh = C4::Context->dbh;
858 SELECT authorised_value
859 FROM marc_subfield_structure
860 WHERE kohafield = \'items.notforloan\'
863 my $sth = $dbh->prepare($query);
865 my ($statuscode) = $sth->fetchrow_array();
870 FROM authorised_values
873 $sth = $dbh->prepare($query);
874 $sth->execute($statuscode);
875 my %notforloan_label_of;
876 while ( my $row = $sth->fetchrow_hashref ) {
877 $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
881 return \%notforloan_label_of;
884 =head2 GetAuthValCode
886 $authvalcode = GetAuthValCode($kohafield,$frameworkcode);
891 my ($kohafield,$fwcode) = @_;
892 my $dbh = C4::Context->dbh;
893 $fwcode='' unless $fwcode;
894 my $sth = $dbh->prepare('select authorised_value from marc_subfield_structure where kohafield=? and frameworkcode=?');
895 $sth->execute($kohafield,$fwcode);
896 my ($authvalcode) = $sth->fetchrow_array;
900 =head2 GetAuthValCodeFromField
902 $authvalcode = GetAuthValCodeFromField($field,$subfield,$frameworkcode);
904 C<$subfield> can be undefined
908 sub GetAuthValCodeFromField {
909 my ($field,$subfield,$fwcode) = @_;
910 my $dbh = C4::Context->dbh;
911 $fwcode='' unless $fwcode;
913 if (defined $subfield) {
914 $sth = $dbh->prepare('select authorised_value from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?');
915 $sth->execute($field,$subfield,$fwcode);
917 $sth = $dbh->prepare('select authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?');
918 $sth->execute($field,$fwcode);
920 my ($authvalcode) = $sth->fetchrow_array;
924 =head2 GetAuthorisedValues
926 $authvalues = GetAuthorisedValues([$category]);
928 This function returns all authorised values from the'authorised_value' table in a reference to array of hashrefs.
930 C<$category> returns authorised values for just one category (optional).
932 C<$opac> If set to a true value, displays OPAC descriptions rather than normal ones when they exist.
936 sub GetAuthorisedValues {
937 my ( $category, $opac ) = @_;
939 # Is this cached already?
940 $opac = $opac ? 1 : 0; # normalise to be safe
942 C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
944 "AuthorisedValues-$category-$opac-$branch_limit";
945 my $cache = Koha::Caches->get_instance();
946 my $result = $cache->get_from_cache($cache_key);
947 return $result if $result;
950 my $dbh = C4::Context->dbh;
953 FROM authorised_values av
956 LEFT JOIN authorised_values_branches ON ( id = av_id )
961 push @where_strings, "category = ?";
962 push @where_args, $category;
965 push @where_strings, "( branchcode = ? OR branchcode IS NULL )";
966 push @where_args, $branch_limit;
968 if(@where_strings > 0) {
969 $query .= " WHERE " . join(" AND ", @where_strings);
971 $query .= ' ORDER BY category, ' . (
972 $opac ? 'COALESCE(lib_opac, lib)'
976 my $sth = $dbh->prepare($query);
978 $sth->execute( @where_args );
979 while (my $data=$sth->fetchrow_hashref) {
980 if ($opac && $data->{lib_opac}) {
981 $data->{lib} = $data->{lib_opac};
983 push @results, $data;
987 $cache->set_in_cache( $cache_key, \@results, { expiry => 5 } );
991 =head2 GetAuthorisedValueCategories
993 $auth_categories = GetAuthorisedValueCategories();
995 Return an arrayref of all of the available authorised
1000 sub GetAuthorisedValueCategories {
1001 my $dbh = C4::Context->dbh;
1002 my $sth = $dbh->prepare("SELECT DISTINCT category FROM authorised_values ORDER BY category");
1005 while (defined (my $category = $sth->fetchrow_array) ) {
1006 push @results, $category;
1011 =head2 GetAuthorisedValueByCode
1013 $authorised_value = GetAuthorisedValueByCode( $category, $authvalcode, $opac );
1015 Return the lib attribute from authorised_values from the row identified
1016 by the passed category and code
1020 sub GetAuthorisedValueByCode {
1021 my ( $category, $authvalcode, $opac ) = @_;
1023 my $field = $opac ? 'lib_opac' : 'lib';
1024 my $dbh = C4::Context->dbh;
1025 my $sth = $dbh->prepare("SELECT $field FROM authorised_values WHERE category=? AND authorised_value =?");
1026 $sth->execute( $category, $authvalcode );
1027 while ( my $data = $sth->fetchrow_hashref ) {
1028 return $data->{ $field };
1032 =head2 GetKohaAuthorisedValues
1034 Takes $kohafield, $fwcode as parameters.
1036 If $opac parameter is set to a true value, displays OPAC descriptions rather than normal ones when they exist.
1038 Returns hashref of Code => description
1040 Returns undef if no authorised value category is defined for the kohafield.
1044 sub GetKohaAuthorisedValues {
1045 my ($kohafield,$fwcode,$opac) = @_;
1046 $fwcode='' unless $fwcode;
1048 my $dbh = C4::Context->dbh;
1049 my $avcode = GetAuthValCode($kohafield,$fwcode);
1051 my $sth = $dbh->prepare("select authorised_value, lib, lib_opac from authorised_values where category=? ");
1052 $sth->execute($avcode);
1053 while ( my ($val, $lib, $lib_opac) = $sth->fetchrow_array ) {
1054 $values{$val} = ($opac && $lib_opac) ? $lib_opac : $lib;
1062 =head2 GetKohaAuthorisedValuesFromField
1064 Takes $field, $subfield, $fwcode as parameters.
1066 If $opac parameter is set to a true value, displays OPAC descriptions rather than normal ones when they exist.
1067 $subfield can be undefined
1069 Returns hashref of Code => description
1071 Returns undef if no authorised value category is defined for the given field and subfield
1075 sub GetKohaAuthorisedValuesFromField {
1076 my ($field, $subfield, $fwcode,$opac) = @_;
1077 $fwcode='' unless $fwcode;
1079 my $dbh = C4::Context->dbh;
1080 my $avcode = GetAuthValCodeFromField($field, $subfield, $fwcode);
1082 my $sth = $dbh->prepare("select authorised_value, lib, lib_opac from authorised_values where category=? ");
1083 $sth->execute($avcode);
1084 while ( my ($val, $lib, $lib_opac) = $sth->fetchrow_array ) {
1085 $values{$val} = ($opac && $lib_opac) ? $lib_opac : $lib;
1093 =head2 GetKohaAuthorisedValuesMapping
1095 Takes a hash as a parameter. The interface key indicates the
1096 description to use in the mapping.
1099 "{kohafield},{frameworkcode},{authorised_value}" => "{description}"
1100 for all the kohafields, frameworkcodes, and authorised values.
1102 Returns undef if nothing is found.
1106 sub GetKohaAuthorisedValuesMapping {
1107 my ($parameter) = @_;
1108 my $interface = $parameter->{'interface'} // '';
1110 my $query_mapping = q{
1111 SELECT TA.kohafield,TA.authorised_value AS category,
1112 TA.frameworkcode,TB.authorised_value,
1113 IF(TB.lib_opac>'',TB.lib_opac,TB.lib) AS OPAC,
1114 TB.lib AS Intranet,TB.lib_opac
1115 FROM marc_subfield_structure AS TA JOIN
1116 authorised_values as TB ON
1117 TA.authorised_value=TB.category
1118 WHERE TA.kohafield>'' AND TA.authorised_value>'';
1120 my $dbh = C4::Context->dbh;
1121 my $sth = $dbh->prepare($query_mapping);
1124 if ($interface eq 'opac') {
1125 while (my $row = $sth->fetchrow_hashref) {
1126 $avmapping->{$row->{kohafield}.",".$row->{frameworkcode}.",".$row->{authorised_value}} = $row->{OPAC};
1130 while (my $row = $sth->fetchrow_hashref) {
1131 $avmapping->{$row->{kohafield}.",".$row->{frameworkcode}.",".$row->{authorised_value}} = $row->{Intranet};
1139 my $escaped_string = C4::Koha::xml_escape($string);
1141 Convert &, <, >, ', and " in a string to XML entities
1147 return '' unless defined $str;
1148 $str =~ s/&/&/g;
1151 $str =~ s/'/'/g;
1152 $str =~ s/"/"/g;
1156 =head2 display_marc_indicators
1158 my $display_form = C4::Koha::display_marc_indicators($field);
1160 C<$field> is a MARC::Field object
1162 Generate a display form of the indicators of a variable
1163 MARC field, replacing any blanks with '#'.
1167 sub display_marc_indicators {
1169 my $indicators = '';
1170 if ($field && $field->tag() >= 10) {
1171 $indicators = $field->indicator(1) . $field->indicator(2);
1172 $indicators =~ s/ /#/g;
1177 sub GetNormalizedUPC {
1178 my ($marcrecord,$marcflavour) = @_;
1180 return unless $marcrecord;
1181 if ($marcflavour eq 'UNIMARC') {
1182 my @fields = $marcrecord->field('072');
1183 foreach my $field (@fields) {
1184 my $upc = _normalize_match_point($field->subfield('a'));
1191 else { # assume marc21 if not unimarc
1192 my @fields = $marcrecord->field('024');
1193 foreach my $field (@fields) {
1194 my $indicator = $field->indicator(1);
1195 my $upc = _normalize_match_point($field->subfield('a'));
1196 if ($upc && $indicator == 1 ) {
1203 # Normalizes and returns the first valid ISBN found in the record
1204 # ISBN13 are converted into ISBN10. This is required to get some book cover images.
1205 sub GetNormalizedISBN {
1206 my ($isbn,$marcrecord,$marcflavour) = @_;
1208 # Koha attempts to store multiple ISBNs in biblioitems.isbn, separated by " | "
1209 # anything after " | " should be removed, along with the delimiter
1210 ($isbn) = split(/\|/, $isbn );
1211 return _isbn_cleanup($isbn);
1214 return unless $marcrecord;
1216 if ($marcflavour eq 'UNIMARC') {
1217 my @fields = $marcrecord->field('010');
1218 foreach my $field (@fields) {
1219 my $isbn = $field->subfield('a');
1221 return _isbn_cleanup($isbn);
1225 else { # assume marc21 if not unimarc
1226 my @fields = $marcrecord->field('020');
1227 foreach my $field (@fields) {
1228 $isbn = $field->subfield('a');
1230 return _isbn_cleanup($isbn);
1236 sub GetNormalizedEAN {
1237 my ($marcrecord,$marcflavour) = @_;
1239 return unless $marcrecord;
1241 if ($marcflavour eq 'UNIMARC') {
1242 my @fields = $marcrecord->field('073');
1243 foreach my $field (@fields) {
1244 my $ean = _normalize_match_point($field->subfield('a'));
1250 else { # assume marc21 if not unimarc
1251 my @fields = $marcrecord->field('024');
1252 foreach my $field (@fields) {
1253 my $indicator = $field->indicator(1);
1254 my $ean = _normalize_match_point($field->subfield('a'));
1255 if ( $ean && $indicator == 3 ) {
1262 sub GetNormalizedOCLCNumber {
1263 my ($marcrecord,$marcflavour) = @_;
1264 return unless $marcrecord;
1266 if ($marcflavour ne 'UNIMARC' ) {
1267 my @fields = $marcrecord->field('035');
1268 foreach my $field (@fields) {
1269 my $oclc = $field->subfield('a');
1270 if ($oclc =~ /OCoLC/) {
1271 $oclc =~ s/\(OCoLC\)//;
1281 sub GetAuthvalueDropbox {
1282 my ( $authcat, $default ) = @_;
1283 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
1284 my $dbh = C4::Context->dbh;
1288 FROM authorised_values
1291 LEFT JOIN authorised_values_branches ON ( id = av_id )
1296 $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
1297 $query .= " GROUP BY lib ORDER BY category, lib, lib_opac";
1298 my $sth = $dbh->prepare($query);
1299 $sth->execute( $authcat, $branch_limit ? $branch_limit : () );
1302 my $option_list = [];
1303 my @authorised_values = ( q{} );
1304 while (my $av = $sth->fetchrow_hashref) {
1305 push @{$option_list}, {
1306 value => $av->{authorised_value},
1307 label => $av->{lib},
1308 default => ($default eq $av->{authorised_value}),
1312 if ( @{$option_list} ) {
1313 return $option_list;
1319 =head2 GetDailyQuote($opts)
1321 Takes a hashref of options
1323 Currently supported options are:
1325 'id' An exact quote id
1326 'random' Select a random quote
1327 noop When no option is passed in, this sub will return the quote timestamped for the current day
1329 The function returns an anonymous hash following this format:
1332 'source' => 'source-of-quote',
1333 'timestamp' => 'timestamp-value',
1334 'text' => 'text-of-quote',
1340 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
1341 # at least for default option
1345 my $dbh = C4::Context->dbh;
1350 $query = 'SELECT * FROM quotes WHERE id = ?';
1351 $sth = $dbh->prepare($query);
1352 $sth->execute($opts{'id'});
1353 $quote = $sth->fetchrow_hashref();
1355 elsif ($opts{'random'}) {
1356 # Fall through... we also return a random quote as a catch-all if all else fails
1359 $query = 'SELECT * FROM quotes WHERE timestamp LIKE CONCAT(CURRENT_DATE,\'%\') ORDER BY timestamp DESC LIMIT 0,1';
1360 $sth = $dbh->prepare($query);
1362 $quote = $sth->fetchrow_hashref();
1364 unless ($quote) { # if there are not matches, choose a random quote
1365 # get a list of all available quote ids
1366 $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
1368 my $range = ($sth->fetchrow_array)[0];
1369 # chose a random id within that range if there is more than one quote
1370 my $offset = int(rand($range));
1372 $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
1373 $sth = C4::Context->dbh->prepare($query);
1374 # see http://www.perlmonks.org/?node_id=837422 for why
1375 # we're being verbose and using bind_param
1376 $sth->bind_param(1, $offset, SQL_INTEGER);
1378 $quote = $sth->fetchrow_hashref();
1379 # update the timestamp for that quote
1380 $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
1381 $sth = C4::Context->dbh->prepare($query);
1383 DateTime::Format::MySQL->format_datetime( dt_from_string() ),
1390 sub _normalize_match_point {
1391 my $match_point = shift;
1392 (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
1393 $normalized_match_point =~ s/-//g;
1395 return $normalized_match_point;
1400 return NormalizeISBN(
1403 format => 'ISBN-10',
1409 =head2 NormalizedISBN
1411 my $isbns = NormalizedISBN({
1413 strip_hyphens => [0,1],
1414 format => ['ISBN-10', 'ISBN-13']
1417 Returns an isbn validated by Business::ISBN.
1418 Optionally strips hyphens and/or forces the isbn
1419 to be of the specified format.
1421 If the string cannot be validated as an isbn,
1429 my $string = $params->{isbn};
1430 my $strip_hyphens = $params->{strip_hyphens};
1431 my $format = $params->{format};
1433 return unless $string;
1435 my $isbn = Business::ISBN->new($string);
1437 if ( $isbn && $isbn->is_valid() ) {
1439 if ( $format eq 'ISBN-10' ) {
1440 $isbn = $isbn->as_isbn10();
1442 elsif ( $format eq 'ISBN-13' ) {
1443 $isbn = $isbn->as_isbn13();
1445 return unless $isbn;
1447 if ($strip_hyphens) {
1448 $string = $isbn->as_string( [] );
1450 $string = $isbn->as_string();
1457 =head2 GetVariationsOfISBN
1459 my @isbns = GetVariationsOfISBN( $isbn );
1461 Returns a list of variations of the given isbn in
1462 both ISBN-10 and ISBN-13 formats, with and without
1465 In a scalar context, the isbns are returned as a
1466 string delimited by ' | '.
1470 sub GetVariationsOfISBN {
1473 return unless $isbn;
1477 push( @isbns, NormalizeISBN({ isbn => $isbn }) );
1478 push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10' }) );
1479 push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13' }) );
1480 push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10', strip_hyphens => 1 }) );
1481 push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13', strip_hyphens => 1 }) );
1483 # Strip out any "empty" strings from the array
1484 @isbns = grep { defined($_) && $_ =~ /\S/ } @isbns;
1486 return wantarray ? @isbns : join( " | ", @isbns );
1489 =head2 GetVariationsOfISBNs
1491 my @isbns = GetVariationsOfISBNs( @isbns );
1493 Returns a list of variations of the given isbns in
1494 both ISBN-10 and ISBN-13 formats, with and without
1497 In a scalar context, the isbns are returned as a
1498 string delimited by ' | '.
1502 sub GetVariationsOfISBNs {
1505 @isbns = map { GetVariationsOfISBN( $_ ) } @isbns;
1507 return wantarray ? @isbns : join( " | ", @isbns );
1510 =head2 IsKohaFieldLinked
1512 my $is_linked = IsKohaFieldLinked({
1513 kohafield => $kohafield,
1514 frameworkcode => $frameworkcode,
1517 Return 1 if the field is linked
1521 sub IsKohaFieldLinked {
1522 my ( $params ) = @_;
1523 my $kohafield = $params->{kohafield};
1524 my $frameworkcode = $params->{frameworkcode} || '';
1525 my $dbh = C4::Context->dbh;
1526 my $is_linked = $dbh->selectcol_arrayref( q|
1528 FROM marc_subfield_structure
1529 WHERE frameworkcode = ?
1531 |,{}, $frameworkcode, $kohafield );
1532 return $is_linked->[0];