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
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.
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.
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>.
21 use JSON qw( from_json to_json );
31 Koha::SharedContent - Set of methods for querying Mana KB server
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.
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.
50 my $mana_request = shift;
52 $mana_request->content_type('application/json');
53 my $userAgent = LWP::UserAgent->new;
54 if ( $mana_request->method eq "POST" ){
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->simple_request($mana_request);
62 eval { $result = from_json( $response->decoded_content, { utf8 => 1} ); };
63 $result->{code} = $response->code;
67 if ($response->is_error){
68 $result->{msg} = "An error occurred, mana server returned: " . $response->message;
73 =head2 increment_entity_value
75 Koha::SharedContent::increment_entity_value($entity_type, $mana_entity_id, $field);
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.
82 sub increment_entity_value {
83 return process_request(build_request('increment', @_));
88 my $result = Koha::SharedContent::send_entity($language, $borrowernumber, $mana_entity_id, $entity_type);
90 Share a Koha entity (i.e subscription or report) to Mana KB.
95 my ($lang, $loggedinuser, $resourceid, $resourcetype) = @_;
97 my $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
99 my $result = process_request(build_request('post', $resourcetype, $content));
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 };
109 =head3 comment_entity
111 my $result = Koha::SharedContent::comment_entity($resource_id, $resource_type, $comment);
113 Send a comment about a Mana entity.
118 my ($resourceid, $resourcetype, $comment) = @_;
120 my $result = process_request(build_request('post', 'resource_comment',
121 { resource_id => $resourceid, resource_type => $resourcetype, message => $comment }));
126 =head2 prepare_entity_data
128 $data = prepare_entity_data($language, $borrowernumber, $mana_entity_id, $entity_type);
130 Prepare Koha entity data to be sent to Mana KB.
134 sub prepare_entity_data {
135 my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_;
136 $lang ||= C4::Context->preference('language');
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
144 $mana_email = C4::Context->preference('KohaAdminEmailAddress')
145 if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) );
147 my %versions = C4::Context::get_versions();
151 kohaversion => $versions{'kohaVersion'},
152 exportemail => $mana_email
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 };
161 return $ressource_mana_info;
164 =head2 get_entity_by_id
166 my $entity = Koha::SharedContent::get_entity_by_id($entity_type, $mana_entity_id, [{usecomments => 1}]);
168 Retrieve a Mana entity to be imported into Koha. Add {usecomments => 1} to tell Mana to
169 embed all user reviews.
173 sub get_entity_by_id {
174 return process_request(build_request('getwithid', @_));
177 =head2 search_entities
179 my $result = Koha::SharedContent::search_entities( $entity_type, $search_params );
180 my $entities = $result->{data};
182 Search entities on ManaKB.
186 sub search_entities {
187 return process_request(build_request('get', @_));
192 $request = build_request($mana_method, [$param1, $param2, ...]);
194 Create a HTTP::Request object to be passed to process_request.
200 my $resource = shift;
201 my $mana_url = get_sharing_url();
203 if ( $type eq 'get' ) {
206 map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
208 my $url = "$mana_url/$resource.json?$params";
209 return HTTP::Request->new( GET => $url );
212 if ( $type eq 'getwithid' ) {
216 map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
219 my $url = "$mana_url/$resource/$id.json?$params";
220 return HTTP::Request->new( GET => $url );
223 if ( $type eq 'post' ) {
226 my $url = "$mana_url/$resource.json";
227 my $request = HTTP::Request->new( POST => $url );
229 my $json = to_json( $content, { utf8 => 1 } );
230 $request->content($json);
235 if ( $type eq 'increment' ) {
241 $param->{step} = $step || 1;
243 $param->{resource} = $resource;
245 map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () }
247 my $url = "$mana_url/$resource/$id.json/increment/$field?$param";
248 my $request = HTTP::Request->new( POST => $url );
253 =head2 get_sharing_url
255 my $mana_url = get_sharing_url();
257 Get the Mana KB server URL set in koha config file.
261 sub get_sharing_url {
262 return C4::Context->config('mana_config');