Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2017 BibLibre Baptiste Wojtkowski |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
# |
20 |
|
21 |
use Modern::Perl; |
22 |
use Koha::SharedContent; |
23 |
use Koha::Biblioitems; |
24 |
use Koha::Biblioitem; |
25 |
use Koha::Reading_suggestions; |
26 |
use Koha::Reading_suggestion; |
27 |
|
28 |
use Koha; |
29 |
use C4::Koha; |
30 |
|
31 |
use C4::Auth qw(check_cookie_auth), qw(get_template_and_user); |
32 |
|
33 |
use CGI; |
34 |
use JSON; |
35 |
|
36 |
use DateTime; |
37 |
use DateTime::Format::MySQL; |
38 |
use DateTime::Duration; |
39 |
|
40 |
use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( months => 3 ); |
41 |
|
42 |
use C4::Output qw( output_with_http_headers ); |
43 |
my $input = new CGI; |
44 |
binmode STDOUT, ":encoding(UTF-8)"; |
45 |
my $biblionumber = $input->param( "biblionumber" ); |
46 |
my $biblioitem = Koha::Biblioitems->find( $biblionumber ); |
47 |
|
48 |
my $local_suggestions; |
49 |
|
50 |
my $now; |
51 |
my $timestamp; |
52 |
my $temp; |
53 |
#check it doesn't already have the informations localy stored |
54 |
eval { |
55 |
$local_suggestions = Koha::Reading_suggestions->find( $biblionumber )->unblessed(); |
56 |
$now = DateTime->now(); |
57 |
$timestamp = DateTime::Format::MySQL->parse_timestamp( $local_suggestions->{timestamp} ); |
58 |
$temp = $now - $timestamp; |
59 |
}; |
60 |
my @biblios; |
61 |
if ( $local_suggestions and DateTime::Duration->compare( ($now - $timestamp), DURATION_BEFORE_REFRESH ) == -1 ){ |
62 |
delete $local_suggestions->{timestamp}; |
63 |
delete $local_suggestions->{biblionumber}; |
64 |
foreach my $key ( sort keys %{ $local_suggestions }){ |
65 |
push @biblios, Koha::Biblios->find( $local_suggestions->{ $key } ); |
66 |
} |
67 |
} |
68 |
else{ |
69 |
# get all informations to ask mana |
70 |
my $idtype; |
71 |
my $documentid; |
72 |
if ( $biblioitem->isbn ){ |
73 |
$idtype= "isbn"; |
74 |
$documentid= NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 }); |
75 |
} |
76 |
elsif ( $biblioitem->issn ){ |
77 |
$idtype= "issn"; |
78 |
$documentid= $biblioitem->issn; |
79 |
} |
80 |
elsif ( $biblioitem->ean ){ |
81 |
$idtype= "ean"; |
82 |
$documentid= $biblioitem->ean; |
83 |
} |
84 |
else{ |
85 |
die "error: no propper identifier"; |
86 |
} |
87 |
|
88 |
#request mana |
89 |
my $mana_ip = C4::Context->config('mana_config'); |
90 |
my $url = "$mana_ip/getsuggestion/$documentid/$idtype"; |
91 |
my $request = HTTP::Request->new( GET => $url ); |
92 |
$request->content_type('aplication/json'); |
93 |
my $response = Koha::SharedContent::manaRequest( $request ); |
94 |
|
95 |
#error handling |
96 |
my $resources = $response->{data}; |
97 |
unless ( $resources ){ |
98 |
my $msg; |
99 |
$msg = $response->{msg}; |
100 |
if ( $msg ){ |
101 |
die $msg; |
102 |
} |
103 |
else{ |
104 |
die "Unknown error"; |
105 |
} |
106 |
} |
107 |
#create the list of owned suggested resources |
108 |
my $ownedbiblioitem; |
109 |
my $ownedbiblio; |
110 |
foreach my $resource ( @{ $resources } ){ |
111 |
|
112 |
|
113 |
# isbn processsing |
114 |
if ( $resource->{idtype} eq "isbn" ){ |
115 |
$documentid= NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); |
116 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
117 |
|
118 |
#if we don't have such a biblioitem, we try to format else the isbn |
119 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
120 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); |
121 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
122 |
} |
123 |
|
124 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
125 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); |
126 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
127 |
} |
128 |
|
129 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
130 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); |
131 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
132 |
} |
133 |
|
134 |
} |
135 |
#others ids do not need any processing |
136 |
else{ |
137 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
138 |
} |
139 |
if (scalar @{ $ownedbiblioitem->unblessed() } ){ |
140 |
$ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); |
141 |
push @biblios, $ownedbiblio; |
142 |
} |
143 |
} |
144 |
|
145 |
#preparing new suggestion |
146 |
my $newSuggestion; |
147 |
my $cter = scalar @biblios; |
148 |
$cter = 10 unless ($cter < 10); |
149 |
$newSuggestion->{ biblionumber } = $biblionumber; |
150 |
if ( scalar @biblios < 11 ){ |
151 |
while ( $cter > 1 ){ |
152 |
$cter--; |
153 |
$newSuggestion->{ "biblionumber".$cter }=$biblios[ $cter ]->biblionumber; |
154 |
} |
155 |
} |
156 |
if ( $local_suggestions ){ |
157 |
Koha::Reading_suggestions->find( $biblionumber )->delete(); |
158 |
} |
159 |
Koha::Reading_suggestion->new( $newSuggestion )->store; |
160 |
|
161 |
} |
162 |
|
163 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
164 |
{ |
165 |
template_name => "mana/mana_suggestions.tt", |
166 |
query => $input, |
167 |
type => "opac", |
168 |
authnotrequired => 1, |
169 |
debug => 1, |
170 |
} |
171 |
); |
172 |
|
173 |
|
174 |
$template->param( JacketImage => 1); |
175 |
$template->param( suggestions => \@biblios ); |
176 |
|
177 |
|
178 |
output_with_http_headers $input, $cookie, $template->output, 'json'; |