X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FKoha.pm;h=ec65be9691de62a9e6196298947fb403228a9810;hb=352d9cd2fd0ea7ddc075094e3f9d310abc894ac4;hp=f19a017c19fff9635e2212f1a59a0e61ca7f85ff;hpb=509d673f10bf8e03529602b922d1fab603457ee2;p=koha.git
diff --git a/C4/Koha.pm b/C4/Koha.pm
index f19a017c19..ec65be9691 100644
--- a/C4/Koha.pm
+++ b/C4/Koha.pm
@@ -24,11 +24,13 @@ use strict;
#use warnings; FIXME - Bug 2505
use C4::Context;
-
-use Memoize;
-use DateTime;
+use C4::Branch qw(GetBranchesCount);
+use Koha::Cache;
+use Koha::DateUtils qw(dt_from_string);
use DateTime::Format::MySQL;
+use Business::ISBN;
use autouse 'Data::Dumper' => qw(Dumper);
+use DBI qw(:sql_types);
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
@@ -41,10 +43,10 @@ BEGIN {
&subfield_is_koha_internal_p
&GetPrinters &GetPrinter
&GetItemTypes &getitemtypeinfo
- &GetCcodes
&GetSupportName &GetSupportList
&get_itemtypeinfos_of
&getframeworks &getframeworkinfo
+ &GetFrameworksLoop
&getauthtypes &getauthtype
&getallthemes
&getFacets
@@ -57,27 +59,30 @@ BEGIN {
&getitemtypeimagelocation
&GetAuthorisedValues
&GetAuthorisedValueCategories
+ &IsAuthorisedValueCategory
&GetKohaAuthorisedValues
&GetKohaAuthorisedValuesFromField
&GetKohaAuthorisedValueLib
&GetAuthorisedValueByCode
&GetKohaImageurlFromAuthorisedValues
&GetAuthValCode
+ &AddAuthorisedValue
&GetNormalizedUPC
&GetNormalizedISBN
&GetNormalizedEAN
&GetNormalizedOCLCNumber
&xml_escape
+ &GetVariationsOfISBN
+ &GetVariationsOfISBNs
+ &NormalizeISBN
+
$DEBUG
);
$DEBUG = 0;
@EXPORT_OK = qw( GetDailyQuote );
}
-# expensive functions
-memoize('GetAuthorisedValues');
-
=head1 NAME
C4::Koha - Perl Module containing convenience functions for Koha scripts
@@ -112,7 +117,7 @@ sub slashifyDate {
}
# FIXME.. this should be moved to a MARC-specific module
-sub subfield_is_koha_internal_p ($) {
+sub subfield_is_koha_internal_p {
my ($subfield) = @_;
# We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
@@ -172,22 +177,22 @@ build a HTML select with the following code :
=head3 in TEMPLATE
-
+
=cut
sub GetSupportList{
my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
- if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
+ if (!$advanced_search_types or $advanced_search_types =~ /itemtypes/) {
my $query = qq|
SELECT *
FROM itemtypes
@@ -204,10 +209,15 @@ sub GetSupportList{
}
=head2 GetItemTypes
- $itemtypes = &GetItemTypes();
+ $itemtypes = &GetItemTypes( style => $style );
Returns information about existing itemtypes.
+Params:
+ style: either 'array' or 'hash', defaults to 'hash'.
+ 'array' returns an arrayref,
+ 'hash' return a hashref with the itemtype value as the key
+
build a HTML select with the following code :
=head3 in PERL SCRIPT
@@ -240,6 +250,8 @@ build a HTML select with the following code :
=cut
sub GetItemTypes {
+ my ( %params ) = @_;
+ my $style = defined( $params{'style'} ) ? $params{'style'} : 'hash';
# returns a reference to a hash of references to itemtypes...
my %itemtypes;
@@ -250,10 +262,15 @@ sub GetItemTypes {
|;
my $sth = $dbh->prepare($query);
$sth->execute;
- while ( my $IT = $sth->fetchrow_hashref ) {
- $itemtypes{ $IT->{'itemtype'} } = $IT;
+
+ if ( $style eq 'hash' ) {
+ while ( my $IT = $sth->fetchrow_hashref ) {
+ $itemtypes{ $IT->{'itemtype'} } = $IT;
+ }
+ return ( \%itemtypes );
+ } else {
+ return $sth->fetchall_arrayref({});
}
- return ( \%itemtypes );
}
sub get_itemtypeinfos_of {
@@ -272,27 +289,6 @@ END_SQL
return get_infos_of( $query, 'itemtype', undef, \@itemtypes );
}
-# this is temporary until we separate collection codes and item types
-sub GetCcodes {
- my $count = 0;
- my @results;
- my $dbh = C4::Context->dbh;
- my $sth =
- $dbh->prepare(
- "SELECT * FROM authorised_values ORDER BY authorised_value");
- $sth->execute;
- while ( my $data = $sth->fetchrow_hashref ) {
- if ( $data->{category} eq "CCODE" ) {
- $count++;
- $results[$count] = $data;
-
- #warn "data: $data";
- }
- }
- $sth->finish;
- return ( $count, @results );
-}
-
=head2 getauthtypes
$authtypes = &getauthtypes();
@@ -365,12 +361,13 @@ build a HTML select with the following code :
=head3 in PERL SCRIPT
- my $frameworks = frameworks();
+ my $frameworks = getframeworks();
my @frameworkloop;
foreach my $thisframework (keys %$frameworks) {
my $selected = 1 if $thisframework eq $frameworkcode;
- my %row =(value => $thisframework,
- selected => $selected,
+ my %row =(
+ value => $thisframework,
+ selected => $selected,
description => $frameworks->{$thisframework}->{'frameworktext'},
);
push @frameworksloop, \%row;
@@ -379,14 +376,18 @@ build a HTML select with the following code :
=head3 in TEMPLATE
-
@@ -405,6 +406,55 @@ sub getframeworks {
return ( \%itemtypes );
}
+=head2 GetFrameworksLoop
+
+ $frameworks = GetFrameworksLoop( $frameworkcode );
+
+Returns the loop suggested on getframework(), but ordered by framework description.
+
+build a HTML select with the following code :
+
+=head3 in PERL SCRIPT
+
+ $template->param( frameworkloop => GetFrameworksLoop( $frameworkcode ) );
+
+=head3 in TEMPLATE
+
+ Same as getframework()
+
+
+
+=cut
+
+sub GetFrameworksLoop {
+ my $frameworkcode = shift;
+ my $frameworks = getframeworks();
+ my @frameworkloop;
+ foreach my $thisframework (sort { uc($frameworks->{$a}->{'frameworktext'}) cmp uc($frameworks->{$b}->{'frameworktext'}) } keys %$frameworks) {
+ my $selected = ( $thisframework eq $frameworkcode ) ? 1 : undef;
+ my %row = (
+ value => $thisframework,
+ selected => $selected,
+ description => $frameworks->{$thisframework}->{'frameworktext'},
+ );
+ push @frameworkloop, \%row;
+ }
+ return \@frameworkloop;
+}
+
=head2 getframeworkinfo
$frameworkinfo = &getframeworkinfo($frameworkcode);
@@ -425,20 +475,22 @@ sub getframeworkinfo {
=head2 getitemtypeinfo
- $itemtype = &getitemtype($itemtype);
+ $itemtype = &getitemtypeinfo($itemtype, [$interface]);
-Returns information about an itemtype.
+Returns information about an itemtype. The optional $interface argument
+sets which interface ('opac' or 'intranet') to return the imageurl for.
+Defaults to intranet.
=cut
sub getitemtypeinfo {
- my ($itemtype) = @_;
+ my ($itemtype, $interface) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
$sth->execute($itemtype);
my $res = $sth->fetchrow_hashref;
- $res->{imageurl} = getitemtypeimagelocation( 'intranet', $res->{imageurl} );
+ $res->{imageurl} = getitemtypeimagelocation( ( ( defined $interface && $interface eq 'opac' ) ? 'opac' : 'intranet' ), $res->{imageurl} );
return $res;
}
@@ -471,7 +523,7 @@ sub getitemtypeimagesrc {
}
}
-sub getitemtypeimagelocation($$) {
+sub getitemtypeimagelocation {
my ( $src, $image ) = @_;
return '' if ( !$image );
@@ -630,7 +682,7 @@ sub GetPrinters {
=cut
-sub GetPrinter ($$) {
+sub GetPrinter {
my ( $query, $printers ) = @_; # get printer for this query from printers
my $printer = $query->param('printer');
my %cookie = $query->cookie('userenv');
@@ -674,6 +726,7 @@ sub getallthemes {
opendir D, "$htdocs";
my @dirlist = readdir D;
foreach my $directory (@dirlist) {
+ next if $directory eq 'lib';
-d "$htdocs/$directory/en" and push @themes, $directory;
}
return @themes;
@@ -686,26 +739,26 @@ sub getFacets {
{
idx => 'su-to',
label => 'Topics',
- tags => [ qw/ 600a 601a 602a 603a 604a 605a 606ax 610a/ ],
+ tags => [ qw/ 600ab 601ab 602a 604at 605a 606ax 610a / ],
sep => ' - ',
},
{
idx => 'su-geo',
label => 'Places',
- tags => [ qw/ 651a / ],
+ tags => [ qw/ 607a / ],
sep => ' - ',
},
{
idx => 'su-ut',
label => 'Titles',
- tags => [ qw/ 500a 501a 502a 503a 504a / ],
+ tags => [ qw/ 500a 501a 503a / ],
sep => ', ',
},
{
idx => 'au',
label => 'Authors',
tags => [ qw/ 700ab 701ab 702ab / ],
- sep => ', ',
+ sep => C4::Context->preference("UNIMARCAuthorsFacetsSeparator"),
},
{
idx => 'se',
@@ -713,14 +766,43 @@ sub getFacets {
tags => [ qw/ 225a / ],
sep => ', ',
},
- ];
- my $library_facet = {
- idx => 'branch',
- label => 'Libraries',
- tags => [ qw/ 995b / ],
- expanded => '1',
- };
- push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
+ {
+ idx => 'location',
+ label => 'Location',
+ tags => [ qw/ 995e / ],
+ }
+ ];
+
+ unless ( C4::Context->preference("singleBranchMode")
+ || GetBranchesCount() == 1 )
+ {
+ my $DisplayLibraryFacets = C4::Context->preference('DisplayLibraryFacets');
+ if ( $DisplayLibraryFacets eq 'both'
+ || $DisplayLibraryFacets eq 'holding' )
+ {
+ push(
+ @$facets,
+ {
+ idx => 'holdingbranch',
+ label => 'HoldingLibrary',
+ tags => [qw / 995c /],
+ }
+ );
+ }
+
+ if ( $DisplayLibraryFacets eq 'both'
+ || $DisplayLibraryFacets eq 'home' )
+ {
+ push(
+ @$facets,
+ {
+ idx => 'homebranch',
+ label => 'HomeLibrary',
+ tags => [qw / 995b /],
+ }
+ );
+ }
+ }
}
else {
$facets = [
@@ -760,16 +842,49 @@ sub getFacets {
tags => [ qw/ 440a 490a / ],
sep => ', ',
},
- ];
- my $library_facet;
- $library_facet = {
- idx => 'branch',
- label => 'Libraries',
- tags => [ qw/ 952b / ],
+ {
+ idx => 'itype',
+ label => 'ItemTypes',
+ tags => [ qw/ 952y 942c / ],
sep => ', ',
- expanded => '1',
- };
- push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
+ },
+ {
+ idx => 'location',
+ label => 'Location',
+ tags => [ qw / 952c / ],
+ },
+ ];
+
+ unless ( C4::Context->preference("singleBranchMode")
+ || GetBranchesCount() == 1 )
+ {
+ my $DisplayLibraryFacets = C4::Context->preference('DisplayLibraryFacets');
+ if ( $DisplayLibraryFacets eq 'both'
+ || $DisplayLibraryFacets eq 'holding' )
+ {
+ push(
+ @$facets,
+ {
+ idx => 'holdingbranch',
+ label => 'HoldingLibrary',
+ tags => [qw / 952b /],
+ }
+ );
+ }
+
+ if ( $DisplayLibraryFacets eq 'both'
+ || $DisplayLibraryFacets eq 'home' )
+ {
+ push(
+ @$facets,
+ {
+ idx => 'homebranch',
+ label => 'HomeLibrary',
+ tags => [qw / 952a /],
+ }
+ );
+ }
+ }
}
return $facets;
}
@@ -886,13 +1001,13 @@ like:
{
'checked' => 'checked',
- 'encoding' => 'MARC-8'
+ 'encoding' => 'utf8',
'icon' => undef,
'id' => 'LIBRARY OF CONGRESS',
'label' => '',
'name' => 'server',
'opensearch' => '',
- 'value' => 'z3950.loc.gov:7090/',
+ 'value' => 'lx2.loc.gov:210/',
'zed' => 1,
},
@@ -1008,33 +1123,84 @@ This function returns all authorised values from the'authorised_value' table in
C<$category> returns authorised values for just one category (optional).
+C<$selected> adds a "selected => 1" entry to the hash if the
+authorised_value matches it. B this feature should be considered
+deprecated as it may be removed in the future.
+
C<$opac> If set to a true value, displays OPAC descriptions rather than normal ones when they exist.
=cut
sub GetAuthorisedValues {
- my ($category,$selected,$opac) = @_;
+ my ( $category, $selected, $opac ) = @_;
+
+ # TODO: the "selected" feature should be replaced by a utility function
+ # somewhere else, it doesn't belong in here. For starters it makes
+ # caching much more complicated. Or just let the UI logic handle it, it's
+ # what it's for.
+
+ # Is this cached already?
+ $opac = $opac ? 1 : 0; # normalise to be safe
+ my $branch_limit =
+ C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
+ my $selected_key = defined($selected) ? $selected : '';
+ my $cache_key =
+ "AuthorisedValues-$category-$selected_key-$opac-$branch_limit";
+ my $cache = Koha::Cache->get_instance();
+ my $result = $cache->get_from_cache($cache_key);
+ return $result if $result;
+
my @results;
my $dbh = C4::Context->dbh;
- my $query = "SELECT * FROM authorised_values";
- $query .= " WHERE category = '" . $category . "'" if $category;
- $query .= " ORDER BY category, lib, lib_opac";
+ my $query = qq{
+ SELECT *
+ FROM authorised_values
+ };
+ $query .= qq{
+ LEFT JOIN authorised_values_branches ON ( id = av_id )
+ } if $branch_limit;
+ my @where_strings;
+ my @where_args;
+ if($category) {
+ push @where_strings, "category = ?";
+ push @where_args, $category;
+ }
+ if($branch_limit) {
+ push @where_strings, "( branchcode = ? OR branchcode IS NULL )";
+ push @where_args, $branch_limit;
+ }
+ if(@where_strings > 0) {
+ $query .= " WHERE " . join(" AND ", @where_strings);
+ }
+ $query .= " GROUP BY lib";
+ $query .= ' ORDER BY category, ' . (
+ $opac ? 'COALESCE(lib_opac, lib)'
+ : 'lib, lib_opac'
+ );
+
my $sth = $dbh->prepare($query);
- $sth->execute;
+
+ $sth->execute( @where_args );
while (my $data=$sth->fetchrow_hashref) {
- if ( (defined($selected)) && ($selected eq $data->{'authorised_value'}) ) {
- $data->{'selected'} = 1;
+ if ( defined $selected and $selected eq $data->{authorised_value} ) {
+ $data->{selected} = 1;
}
else {
- $data->{'selected'} = 0;
+ $data->{selected} = 0;
}
- if ($opac && $data->{'lib_opac'}) {
- $data->{'lib'} = $data->{'lib_opac'};
+
+ if ($opac && $data->{lib_opac}) {
+ $data->{lib} = $data->{lib_opac};
}
push @results, $data;
}
- #my $data = $sth->fetchall_arrayref({});
- return \@results; #$data;
+ $sth->finish;
+
+ # We can't cache for long because of that "selected" thing which
+ # makes it impossible to clear the cache without iterating through every
+ # value, which sucks. This'll cover this request, and not a whole lot more.
+ $cache->set_in_cache( $cache_key, \@results, { deepcopy => 1, expiry => 5 } );
+ return \@results;
}
=head2 GetAuthorisedValueCategories
@@ -1057,9 +1223,31 @@ sub GetAuthorisedValueCategories {
return \@results;
}
+=head2 IsAuthorisedValueCategory
+
+ $is_auth_val_category = IsAuthorisedValueCategory($category);
+
+Returns whether a given category name is a valid one
+
+=cut
+
+sub IsAuthorisedValueCategory {
+ my $category = shift;
+ my $query = '
+ SELECT category
+ FROM authorised_values
+ WHERE BINARY category=?
+ LIMIT 1
+ ';
+ my $sth = C4::Context->dbh->prepare($query);
+ $sth->execute($category);
+ $sth->fetchrow ? return 1
+ : return 0;
+}
+
=head2 GetAuthorisedValueByCode
-$authhorised_value = GetAuthorisedValueByCode( $category, $authvalcode );
+$authorised_value = GetAuthorisedValueByCode( $category, $authvalcode, $opac );
Return the lib attribute from authorised_values from the row identified
by the passed category and code
@@ -1067,13 +1255,14 @@ by the passed category and code
=cut
sub GetAuthorisedValueByCode {
- my ( $category, $authvalcode ) = @_;
+ my ( $category, $authvalcode, $opac ) = @_;
+ my $field = $opac ? 'lib_opac' : 'lib';
my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("SELECT lib FROM authorised_values WHERE category=? AND authorised_value =?");
+ my $sth = $dbh->prepare("SELECT $field FROM authorised_values WHERE category=? AND authorised_value =?");
$sth->execute( $category, $authvalcode );
while ( my $data = $sth->fetchrow_hashref ) {
- return $data->{'lib'};
+ return $data->{ $field };
}
}
@@ -1103,7 +1292,7 @@ sub GetKohaAuthorisedValues {
}
return \%values;
} else {
- return undef;
+ return;
}
}
@@ -1134,7 +1323,7 @@ sub GetKohaAuthorisedValuesFromField {
}
return \%values;
} else {
- return undef;
+ return;
}
}
@@ -1178,6 +1367,26 @@ sub GetKohaAuthorisedValueLib {
return $value;
}
+=head2 AddAuthorisedValue
+
+ AddAuthorisedValue($category, $authorised_value, $lib, $lib_opac, $imageurl);
+
+Create a new authorised value.
+
+=cut
+
+sub AddAuthorisedValue {
+ my ($category, $authorised_value, $lib, $lib_opac, $imageurl) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ INSERT INTO authorised_values (category, authorised_value, lib, lib_opac, imageurl)
+ VALUES (?,?,?,?,?)
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute($category, $authorised_value, $lib, $lib_opac, $imageurl);
+}
+
=head2 display_marc_indicators
my $display_form = C4::Koha::display_marc_indicators($field);
@@ -1226,7 +1435,7 @@ sub GetNormalizedUPC {
}
# Normalizes and returns the first valid ISBN found in the record
-# ISBN13 are converted into ISBN10. This is required to get Amazon cover book.
+# ISBN13 are converted into ISBN10. This is required to get some book cover images.
sub GetNormalizedISBN {
my ($isbn,$record,$marcflavour) = @_;
my @fields;
@@ -1236,7 +1445,7 @@ sub GetNormalizedISBN {
$isbn =~ s/(.*)( \| )(.*)/$1/;
return _isbn_cleanup($isbn);
}
- return undef unless $record;
+ return unless $record;
if ($marcflavour eq 'UNIMARC') {
@fields = $record->field('010');
@@ -1245,7 +1454,7 @@ sub GetNormalizedISBN {
if ($isbn) {
return _isbn_cleanup($isbn);
} else {
- return undef;
+ return;
}
}
}
@@ -1256,7 +1465,7 @@ sub GetNormalizedISBN {
if ($isbn) {
return _isbn_cleanup($isbn);
} else {
- return undef;
+ return;
}
}
}
@@ -1301,12 +1510,50 @@ sub GetNormalizedOCLCNumber {
$oclc =~ s/\(OCoLC\)//;
return $oclc;
} else {
- return undef;
+ return;
}
}
}
}
+sub GetAuthvalueDropbox {
+ my ( $authcat, $default ) = @_;
+ my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
+ my $dbh = C4::Context->dbh;
+
+ my $query = qq{
+ SELECT *
+ FROM authorised_values
+ };
+ $query .= qq{
+ LEFT JOIN authorised_values_branches ON ( id = av_id )
+ } if $branch_limit;
+ $query .= qq{
+ WHERE category = ?
+ };
+ $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
+ $query .= " GROUP BY lib ORDER BY category, lib, lib_opac";
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $authcat, $branch_limit ? $branch_limit : () );
+
+
+ my $option_list = [];
+ my @authorised_values = ( q{} );
+ while (my $av = $sth->fetchrow_hashref) {
+ push @{$option_list}, {
+ value => $av->{authorised_value},
+ label => $av->{lib},
+ default => ($default eq $av->{authorised_value}),
+ };
+ }
+
+ if ( @{$option_list} ) {
+ return $option_list;
+ }
+ return;
+}
+
+
=head2 GetDailyQuote($opts)
Takes a hashref of options
@@ -1357,24 +1604,23 @@ sub GetDailyQuote {
$sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
$sth->execute;
my $range = ($sth->fetchrow_array)[0];
- if ($range > 1) {
- # chose a random id within that range if there is more than one quote
- my $id = int(rand($range));
- # grab it
- $query = 'SELECT * FROM quotes WHERE id = ?;';
- $sth = C4::Context->dbh->prepare($query);
- $sth->execute($id);
- }
- else {
- $query = 'SELECT * FROM quotes;';
- $sth = C4::Context->dbh->prepare($query);
- $sth->execute();
- }
+ # chose a random id within that range if there is more than one quote
+ my $offset = int(rand($range));
+ # grab it
+ $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
+ $sth = C4::Context->dbh->prepare($query);
+ # see http://www.perlmonks.org/?node_id=837422 for why
+ # we're being verbose and using bind_param
+ $sth->bind_param(1, $offset, SQL_INTEGER);
+ $sth->execute();
$quote = $sth->fetchrow_hashref();
# update the timestamp for that quote
$query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
$sth = C4::Context->dbh->prepare($query);
- $sth->execute(DateTime::Format::MySQL->format_datetime(DateTime->now), $quote->{'id'});
+ $sth->execute(
+ DateTime::Format::MySQL->format_datetime( dt_from_string() ),
+ $quote->{'id'}
+ );
}
return $quote;
}
@@ -1388,15 +1634,140 @@ sub _normalize_match_point {
}
sub _isbn_cleanup {
- require Business::ISBN;
- my $isbn = Business::ISBN->new( $_[0] );
- if ( $isbn ) {
- $isbn = $isbn->as_isbn10 if $isbn->type eq 'ISBN13';
- if (defined $isbn) {
- return $isbn->as_string([]);
+ my ($isbn) = @_;
+ return NormalizeISBN(
+ {
+ isbn => $isbn,
+ format => 'ISBN-10',
+ strip_hyphens => 1,
}
+ ) if $isbn;
+}
+
+=head2 NormalizedISBN
+
+ my $isbns = NormalizedISBN({
+ isbn => $isbn,
+ strip_hyphens => [0,1],
+ format => ['ISBN-10', 'ISBN-13']
+ });
+
+ Returns an isbn validated by Business::ISBN.
+ Optionally strips hyphens and/or forces the isbn
+ to be of the specified format.
+
+ If the string cannot be validated as an isbn,
+ it returns nothing.
+
+=cut
+
+sub NormalizeISBN {
+ my ($params) = @_;
+
+ my $string = $params->{isbn};
+ my $strip_hyphens = $params->{strip_hyphens};
+ my $format = $params->{format};
+
+ return unless $string;
+
+ my $isbn = Business::ISBN->new($string);
+
+ if ( $isbn && $isbn->is_valid() ) {
+
+ if ( $format eq 'ISBN-10' ) {
+ $isbn = $isbn->as_isbn10();
+ }
+ elsif ( $format eq 'ISBN-13' ) {
+ $isbn = $isbn->as_isbn13();
+ }
+ return unless $isbn;
+
+ if ($strip_hyphens) {
+ $string = $isbn->as_string( [] );
+ } else {
+ $string = $isbn->as_string();
+ }
+
+ return $string;
}
- return;
+}
+
+=head2 GetVariationsOfISBN
+
+ my @isbns = GetVariationsOfISBN( $isbn );
+
+ Returns a list of varations of the given isbn in
+ both ISBN-10 and ISBN-13 formats, with and without
+ hyphens.
+
+ In a scalar context, the isbns are returned as a
+ string delimited by ' | '.
+
+=cut
+
+sub GetVariationsOfISBN {
+ my ($isbn) = @_;
+
+ return unless $isbn;
+
+ my @isbns;
+
+ push( @isbns, NormalizeISBN({ isbn => $isbn }) );
+ push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10' }) );
+ push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13' }) );
+ push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-10', strip_hyphens => 1 }) );
+ push( @isbns, NormalizeISBN({ isbn => $isbn, format => 'ISBN-13', strip_hyphens => 1 }) );
+
+ # Strip out any "empty" strings from the array
+ @isbns = grep { defined($_) && $_ =~ /\S/ } @isbns;
+
+ return wantarray ? @isbns : join( " | ", @isbns );
+}
+
+=head2 GetVariationsOfISBNs
+
+ my @isbns = GetVariationsOfISBNs( @isbns );
+
+ Returns a list of varations of the given isbns in
+ both ISBN-10 and ISBN-13 formats, with and without
+ hyphens.
+
+ In a scalar context, the isbns are returned as a
+ string delimited by ' | '.
+
+=cut
+
+sub GetVariationsOfISBNs {
+ my (@isbns) = @_;
+
+ @isbns = map { GetVariationsOfISBN( $_ ) } @isbns;
+
+ return wantarray ? @isbns : join( " | ", @isbns );
+}
+
+=head2 IsKohaFieldLinked
+
+ my $is_linked = IsKohaFieldLinked({
+ kohafield => $kohafield,
+ frameworkcode => $frameworkcode,
+ });
+
+ Return 1 if the field is linked
+
+=cut
+
+sub IsKohaFieldLinked {
+ my ( $params ) = @_;
+ my $kohafield = $params->{kohafield};
+ my $frameworkcode = $params->{frameworkcode} || '';
+ my $dbh = C4::Context->dbh;
+ my $is_linked = $dbh->selectcol_arrayref( q|
+ SELECT COUNT(*)
+ FROM marc_subfield_structure
+ WHERE frameworkcode = ?
+ AND kohafield = ?
+ |,{}, $frameworkcode, $kohafield );
+ return $is_linked->[0];
}
1;