Bug 14697: DBRev 19.06.00.047
[koha.git] / Koha / SharedContent.pm
1 package Koha::SharedContent;
2
3 # Copyright 2016 BibLibre Morgane Alonso
4 #
5 # This file is part of Koha.
6 #
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 3 of the License, or (at your option) any later
10 # version.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use JSON;
22 use HTTP::Request;
23 use LWP::UserAgent;
24
25 use Koha::Serials;
26 use Koha::Reports;
27 use C4::Context;
28
29 =head1 NAME
30
31 Koha::SharedContent - Set of methods for querying Mana KB server
32
33 =head1 DESCRIPTION
34
35 Package for accessing shared content via Mana KB. Methods here are intended
36 to build and process queries for requesting from Mana KB server.
37
38 =cut
39
40 =head2 process_request
41
42 Koha::SharedContent::process_request($request);
43
44 Send a request to Mana KB server. URL is defined in koha-conf.xml in mana_config
45 tag. $request parameter must be a HTTP::Request object. See build_request method.
46
47 =cut
48
49 sub process_request {
50     my $mana_request = shift;
51     my $result;
52     $mana_request->content_type('application/json');
53     my $userAgent = LWP::UserAgent->new;
54     if ( $mana_request->method eq "POST" ){
55         my $content;
56         if ($mana_request->content) {$content = from_json( $mana_request->content )};
57         $content->{securitytoken} = C4::Context->preference("ManaToken");
58         $mana_request->content( to_json($content) );
59     }
60
61     my $response = $userAgent->request($mana_request);
62
63     eval { $result = from_json( $response->decoded_content, { utf8 => 1} ); };
64     $result->{code} = $response->code;
65     if ( $@ ){
66         $result->{msg} = $@;
67     }
68     if ($response->is_error){
69         $result->{msg} = "An error occurred, mana server returned: " . $response->message;
70     }
71     return $result ;
72 }
73
74 =head2 increment_entity_value
75
76 Koha::SharedContent::increment_entity_value($entity_type, $mana_entity_id, $field);
77
78 Increment by 1 the field $field of a Mana entity. I.e, this is used to count the number
79 of Koha instances using a specific entity.
80
81 =cut
82
83 sub increment_entity_value {
84     return process_request(build_request('increment', @_));
85 }
86
87 =head2 send_entity
88
89 my $result = Koha::SharedContent::send_entity($language, $borrowernumber, $mana_entity_id, $entity_type);
90
91 Share a Koha entity (i.e subscription or report) to Mana KB.
92
93 =cut
94
95 sub send_entity {
96     my ($lang, $loggedinuser, $resourceid, $resourcetype) = @_;
97
98     my $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
99
100     my $result = process_request(build_request('post', $resourcetype, $content));
101
102     if ( $result and ($result->{code} eq "200" or $result->{code} eq "201") ) {
103         my $packages = "Koha::".ucfirst($resourcetype)."s";
104         my $resource = $packages->find($resourceid);
105         eval { $resource->set( { mana_id => $result->{id} } )->store };
106     }
107     return $result;
108 }
109
110 =head3 comment_entity
111
112 my $result = Koha::SharedContent::comment_entity($resource_id, $resource_type, $comment);
113
114 Send a comment about a Mana entity.
115
116 =cut
117
118 sub comment_entity {
119     my ($resourceid, $resourcetype, $comment) = @_;
120
121     my $result = process_request(build_request('post', 'resource_comment',
122             { resource_id => $resourceid, resource_type => $resourcetype, message => $comment }));
123
124     return $result;
125 }
126
127 =head2 prepare_entity_data
128
129 $data = prepare_entity_data($language, $borrowernumber, $mana_entity_id, $entity_type);
130
131 Prepare Koha entity data to be sent to Mana KB.
132
133 =cut
134
135 sub prepare_entity_data {
136     my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_;
137     $lang ||= C4::Context->preference('language');
138
139     my $mana_email;
140     if ( $loggedinuser ne 0 ) {
141         my $borrower = Koha::Patrons->find($loggedinuser);
142         $mana_email = $borrower->first_valid_email_address
143             || Koha::Libraries->find( C4::Context->userenv->{'branch'} )->branchemail
144     }
145     $mana_email = C4::Context->preference('KohaAdminEmailAddress')
146       if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) );
147
148     my %versions = C4::Context::get_versions();
149
150     my $mana_info = {
151         language    => $lang,
152         kohaversion => $versions{'kohaVersion'},
153         exportemail => $mana_email
154     };
155
156     my $ressource_mana_info;
157     my $packages = "Koha::".ucfirst($ressourcetype)."s";
158     my $package = "Koha::".ucfirst($ressourcetype);
159     $ressource_mana_info = $package->get_sharable_info($ressourceid);
160     $ressource_mana_info = { %$ressource_mana_info, %$mana_info };
161
162     return $ressource_mana_info;
163 }
164
165 =head2 get_entity_by_id
166
167 my $entity = Koha::SharedContent::get_entity_by_id($entity_type, $mana_entity_id, [{usecomments => 1}]);
168
169 Retrieve a Mana entity to be imported into Koha. Add {usecomments => 1} to tell Mana to
170 embed all user reviews.
171
172 =cut
173
174 sub get_entity_by_id {
175     return process_request(build_request('getwithid', @_));
176 }
177
178 =head2 search_entities
179
180 my $result = Koha::SharedContent::search_entities( $entity_type, $search_params );
181 my $entities = $result->{data};
182
183 Search entities on ManaKB.
184
185 =cut
186
187 sub search_entities {
188     return process_request(build_request('get', @_));
189 }
190
191 =head2 build_request
192
193 $request = build_request($mana_method, [$param1, $param2, ...]);
194
195 Create a HTTP::Request object to be passed to process_request.
196
197 =cut
198
199 sub build_request {
200     my $type = shift;
201     my $resource = shift;
202     my $mana_url = get_sharing_url();
203
204     if ( $type eq 'get' ) {
205         my $params = shift;
206         $params = join '&',
207             map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
208             keys %$params;
209         my $url = "$mana_url/$resource.json?$params";
210         return HTTP::Request->new( GET => $url );
211     }
212
213     if ( $type eq 'getwithid' ) {
214         my $id = shift;
215         my $params = shift;
216         $params = join '&',
217             map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
218             keys %$params;
219
220         my $url = "$mana_url/$resource/$id.json?$params";
221         return HTTP::Request->new( GET => $url );
222     }
223
224     if ( $type eq 'post' ) {
225         my $content  = shift;
226
227         my $url = "$mana_url/$resource.json";
228         my $request = HTTP::Request->new( POST => $url );
229
230         my $json = to_json( $content, { utf8 => 1 } );
231         $request->content($json);
232
233         return $request;
234     }
235
236     if ( $type eq 'increment' ) {
237         my $id       = shift;
238         my $field    = shift;
239         my $step     = shift;
240         my $param;
241
242         $param->{step} = $step || 1;
243         $param->{id} = $id;
244         $param->{resource} = $resource;
245         $param = join '&',
246            map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () }
247                keys %$param;
248         my $url = "$mana_url/$resource/$id.json/increment/$field?$param";
249         my $request = HTTP::Request->new( POST => $url );
250
251     }
252 }
253
254 =head2 get_sharing_url
255
256 my $mana_url = get_sharing_url();
257
258 Get the Mana KB server URL set in koha config file.
259
260 =cut
261
262 sub get_sharing_url {
263     return C4::Context->config('mana_config');
264 }
265
266 1;