Bug 24545: Fix license statements
[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
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 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->simple_request($mana_request);
62     eval { $result = from_json( $response->decoded_content, { utf8 => 1} ); };
63     $result->{code} = $response->code;
64     if ( $@ ){
65         $result->{msg} = $@;
66     }
67     if ($response->is_error){
68         $result->{msg} = "An error occurred, mana server returned: " . $response->message;
69     }
70     return $result ;
71 }
72
73 =head2 increment_entity_value
74
75 Koha::SharedContent::increment_entity_value($entity_type, $mana_entity_id, $field);
76
77 Increment by 1 the field $field of a Mana entity. I.e, this is used to count the number
78 of Koha instances using a specific entity.
79
80 =cut
81
82 sub increment_entity_value {
83     return process_request(build_request('increment', @_));
84 }
85
86 =head2 send_entity
87
88 my $result = Koha::SharedContent::send_entity($language, $borrowernumber, $mana_entity_id, $entity_type);
89
90 Share a Koha entity (i.e subscription or report) to Mana KB.
91
92 =cut
93
94 sub send_entity {
95     my ($lang, $loggedinuser, $resourceid, $resourcetype) = @_;
96
97     my $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
98
99     my $result = process_request(build_request('post', $resourcetype, $content));
100
101     if ( $result and ($result->{code} eq "200" or $result->{code} eq "201") ) {
102         my $packages = "Koha::".ucfirst($resourcetype)."s";
103         my $resource = $packages->find($resourceid);
104         eval { $resource->set( { mana_id => $result->{id} } )->store };
105     }
106     return $result;
107 }
108
109 =head3 comment_entity
110
111 my $result = Koha::SharedContent::comment_entity($resource_id, $resource_type, $comment);
112
113 Send a comment about a Mana entity.
114
115 =cut
116
117 sub comment_entity {
118     my ($resourceid, $resourcetype, $comment) = @_;
119
120     my $result = process_request(build_request('post', 'resource_comment',
121             { resource_id => $resourceid, resource_type => $resourcetype, message => $comment }));
122
123     return $result;
124 }
125
126 =head2 prepare_entity_data
127
128 $data = prepare_entity_data($language, $borrowernumber, $mana_entity_id, $entity_type);
129
130 Prepare Koha entity data to be sent to Mana KB.
131
132 =cut
133
134 sub prepare_entity_data {
135     my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_;
136     $lang ||= C4::Context->preference('language');
137
138     my $mana_email;
139     if ( $loggedinuser ne 0 ) {
140         my $borrower = Koha::Patrons->find($loggedinuser);
141         $mana_email = $borrower->first_valid_email_address
142             || Koha::Libraries->find( C4::Context->userenv->{'branch'} )->branchemail
143     }
144     $mana_email = C4::Context->preference('KohaAdminEmailAddress')
145       if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) );
146
147     my %versions = C4::Context::get_versions();
148
149     my $mana_info = {
150         language    => $lang,
151         kohaversion => $versions{'kohaVersion'},
152         exportemail => $mana_email
153     };
154
155     my $ressource_mana_info;
156     my $packages = "Koha::".ucfirst($ressourcetype)."s";
157     my $package = "Koha::".ucfirst($ressourcetype);
158     $ressource_mana_info = $package->get_sharable_info($ressourceid);
159     $ressource_mana_info = { %$ressource_mana_info, %$mana_info };
160
161     return $ressource_mana_info;
162 }
163
164 =head2 get_entity_by_id
165
166 my $entity = Koha::SharedContent::get_entity_by_id($entity_type, $mana_entity_id, [{usecomments => 1}]);
167
168 Retrieve a Mana entity to be imported into Koha. Add {usecomments => 1} to tell Mana to
169 embed all user reviews.
170
171 =cut
172
173 sub get_entity_by_id {
174     return process_request(build_request('getwithid', @_));
175 }
176
177 =head2 search_entities
178
179 my $result = Koha::SharedContent::search_entities( $entity_type, $search_params );
180 my $entities = $result->{data};
181
182 Search entities on ManaKB.
183
184 =cut
185
186 sub search_entities {
187     return process_request(build_request('get', @_));
188 }
189
190 =head2 build_request
191
192 $request = build_request($mana_method, [$param1, $param2, ...]);
193
194 Create a HTTP::Request object to be passed to process_request.
195
196 =cut
197
198 sub build_request {
199     my $type = shift;
200     my $resource = shift;
201     my $mana_url = get_sharing_url();
202
203     if ( $type eq 'get' ) {
204         my $params = shift;
205         $params = join '&',
206             map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
207             keys %$params;
208         my $url = "$mana_url/$resource.json?$params";
209         return HTTP::Request->new( GET => $url );
210     }
211
212     if ( $type eq 'getwithid' ) {
213         my $id = shift;
214         my $params = shift;
215         $params = join '&',
216             map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
217             keys %$params;
218
219         my $url = "$mana_url/$resource/$id.json?$params";
220         return HTTP::Request->new( GET => $url );
221     }
222
223     if ( $type eq 'post' ) {
224         my $content  = shift;
225
226         my $url = "$mana_url/$resource.json";
227         my $request = HTTP::Request->new( POST => $url );
228
229         my $json = to_json( $content, { utf8 => 1 } );
230         $request->content($json);
231
232         return $request;
233     }
234
235     if ( $type eq 'increment' ) {
236         my $id       = shift;
237         my $field    = shift;
238         my $step     = shift;
239         my $param;
240
241         $param->{step} = $step || 1;
242         $param->{id} = $id;
243         $param->{resource} = $resource;
244         $param = join '&',
245            map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () }
246                keys %$param;
247         my $url = "$mana_url/$resource/$id.json/increment/$field?$param";
248         my $request = HTTP::Request->new( POST => $url );
249
250     }
251 }
252
253 =head2 get_sharing_url
254
255 my $mana_url = get_sharing_url();
256
257 Get the Mana KB server URL set in koha config file.
258
259 =cut
260
261 sub get_sharing_url {
262     return C4::Context->config('mana_config');
263 }
264
265 1;