Bug 22358: (RM follow-up) Typo fix
[koha.git] / Koha / SharedContent.pm
blob80b43094a7efdf414822b2654f035775d1440a8b
1 package Koha::SharedContent;
3 # Copyright 2016 BibLibre Morgane Alonso
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
21 use JSON;
22 use HTTP::Request;
23 use LWP::UserAgent;
25 use Koha::Serials;
26 use Koha::Reports;
27 use C4::Context;
29 =head1 NAME
31 Koha::SharedContent - Set of methods for querying Mana KB server
33 =head1 DESCRIPTION
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.
38 =cut
40 =head2 process_request
42 Koha::SharedContent::process_request($request);
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.
47 =cut
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) );
61 my $response = $userAgent->request($mana_request);
63 if ( $response->code != 200 ) {
64 return {
65 code => $response->code,
66 msg => $response->message,
70 eval { $result = from_json( $response->decoded_content, { utf8 => 1} ); };
71 $result->{code} = $response->code;
72 if ( $@ ){
73 $result->{msg} = $@;
75 if ($response->is_error){
76 $result->{msg} = "An error occurred, mana server returned: " . $response->message;
78 return $result ;
81 =head2 increment_entity_value
83 Koha::SharedContent::increment_entity_value($entity_type, $mana_entity_id, $field);
85 Increment by 1 the field $field of a Mana entity. I.e, this is used to count the number
86 of Koha instances using a specific entity.
88 =cut
90 sub increment_entity_value {
91 return process_request(build_request('increment', @_));
94 =head2 send_entity
96 my $result = Koha::SharedContent::send_entity($language, $borrowernumber, $mana_entity_id, $entity_type);
98 Share a Koha entity (i.e subscription or report) to Mana KB.
100 =cut
102 sub send_entity {
103 my ($lang, $loggedinuser, $resourceid, $resourcetype, $content) = @_;
105 unless ( $content ) {
106 $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
109 my $result = process_request(build_request('post', $resourcetype, $content));
111 if ( $result and ($result->{code} eq "200" or $result->{code} eq "201") ) {
112 my $packages = "Koha::".ucfirst($resourcetype)."s";
113 my $resource = $packages->find($resourceid);
114 eval { $resource->set( { mana_id => $result->{id} } )->store };
116 return $result;
119 =head2 prepare_entity_data
121 $data = prepare_entity_data($language, $borrowernumber, $mana_entity_id, $entity_type);
123 Prepare Koha entity data to be sent to Mana KB.
125 =cut
127 sub prepare_entity_data {
128 my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_;
129 $lang ||= C4::Context->preference('language');
131 my $mana_email;
132 if ( $loggedinuser ne 0 ) {
133 my $borrower = Koha::Patrons->find($loggedinuser);
134 $mana_email = $borrower->first_valid_email_address
135 || Koha::Libraries->find( C4::Context->userenv->{'branch'} )->branchemail
137 $mana_email = C4::Context->preference('KohaAdminEmailAddress')
138 if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) );
140 my %versions = C4::Context::get_versions();
142 my $mana_info = {
143 language => $lang,
144 kohaversion => $versions{'kohaVersion'},
145 exportemail => $mana_email
148 my $ressource_mana_info;
149 my $packages = "Koha::".ucfirst($ressourcetype)."s";
150 my $package = "Koha::".ucfirst($ressourcetype);
151 $ressource_mana_info = $package->get_sharable_info($ressourceid);
152 $ressource_mana_info = { %$ressource_mana_info, %$mana_info };
154 return $ressource_mana_info;
157 =head2 get_entity_by_id
159 my $entity = Koha::SharedContent::get_entity_by_id($entity_type, $mana_entity_id, [{usecomments => 1}]);
161 Retrieve a Mana entity to be imported into Koha. Add {usecomments => 1} to tell Mana to
162 embed all user reviews.
164 =cut
166 sub get_entity_by_id {
167 return process_request(build_request('getwithid', @_));
170 =head2 search_entities
172 my $result = Koha::SharedContent::search_entities( $entity_type, $search_params );
173 my $entities = $result->{data};
175 Search entities on ManaKB.
177 =cut
179 sub search_entities {
180 return process_request(build_request('get', @_));
183 =head2 build_request
185 $request = build_request($mana_method, [$param1, $param2, ...]);
187 Create a HTTP::Request object to be passed to process_request.
189 =cut
191 sub build_request {
192 my $type = shift;
193 my $resource = shift;
194 my $mana_url = get_sharing_url();
196 if ( $type eq 'get' ) {
197 my $params = shift;
198 $params = join '&',
199 map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
200 keys %$params;
201 my $url = "$mana_url/$resource.json?$params";
202 return HTTP::Request->new( GET => $url );
205 if ( $type eq 'getwithid' ) {
206 my $id = shift;
207 my $params = shift;
208 $params = join '&',
209 map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
210 keys %$params;
212 my $url = "$mana_url/$resource/$id.json?$params";
213 return HTTP::Request->new( GET => $url );
216 if ( $type eq 'post' ) {
217 my $content = shift;
219 my $url = "$mana_url/$resource.json";
220 my $request = HTTP::Request->new( POST => $url );
222 my $json = to_json( $content, { utf8 => 1 } );
223 $request->content($json);
225 return $request;
228 if ( $type eq 'increment' ) {
229 my $id = shift;
230 my $field = shift;
231 my $step = shift;
232 my $param;
234 $param->{step} = $step || 1;
235 $param->{id} = $id;
236 $param->{resource} = $resource;
237 $param = join '&',
238 map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () }
239 keys %$param;
240 my $url = "$mana_url/$resource/$id.json/increment/$field?$param";
241 my $request = HTTP::Request->new( POST => $url );
246 =head2 get_sharing_url
248 my $mana_url = get_sharing_url();
250 Get the Mana KB server URL set in koha config file.
252 =cut
254 sub get_sharing_url {
255 return C4::Context->config('mana_config');