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
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.
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->request($mana_request);
63 eval { $result = from_json
( $response->decoded_content, { utf8
=> 1} ); };
64 $result->{code
} = $response->code;
68 if ($response->is_error){
69 $result->{msg
} = "An error occurred, mana server returned: " . $response->message;
74 =head2 increment_entity_value
76 Koha::SharedContent::increment_entity_value($entity_type, $mana_entity_id, $field);
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.
83 sub increment_entity_value
{
84 return process_request
(build_request
('increment', @_));
89 my $result = Koha::SharedContent::send_entity($language, $borrowernumber, $mana_entity_id, $entity_type);
91 Share a Koha entity (i.e subscription or report) to Mana KB.
96 my ($lang, $loggedinuser, $resourceid, $resourcetype, $content) = @_;
99 $content = prepare_entity_data
($lang, $loggedinuser, $resourceid, $resourcetype);
102 my $result = process_request
(build_request
('post', $resourcetype, $content));
104 if ( $result and ($result->{code
} eq "200" or $result->{code
} eq "201") ) {
105 my $packages = "Koha::".ucfirst($resourcetype)."s";
106 my $resource = $packages->find($resourceid);
107 eval { $resource->set( { mana_id
=> $result->{id
} } )->store };
112 =head2 prepare_entity_data
114 $data = prepare_entity_data($language, $borrowernumber, $mana_entity_id, $entity_type);
116 Prepare Koha entity data to be sent to Mana KB.
120 sub prepare_entity_data
{
121 my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_;
122 $lang ||= C4
::Context
->preference('language');
125 if ( $loggedinuser ne 0 ) {
126 my $borrower = Koha
::Patrons
->find($loggedinuser);
127 $mana_email = $borrower->first_valid_email_address
128 || Koha
::Libraries
->find( C4
::Context
->userenv->{'branch'} )->branchemail
130 $mana_email = C4
::Context
->preference('KohaAdminEmailAddress')
131 if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) );
133 my %versions = C4
::Context
::get_versions
();
137 kohaversion
=> $versions{'kohaVersion'},
138 exportemail
=> $mana_email
141 my $ressource_mana_info;
142 my $packages = "Koha::".ucfirst($ressourcetype)."s";
143 my $package = "Koha::".ucfirst($ressourcetype);
144 $ressource_mana_info = $package->get_sharable_info($ressourceid);
145 $ressource_mana_info = { %$ressource_mana_info, %$mana_info };
147 return $ressource_mana_info;
150 =head2 get_entity_by_id
152 my $entity = Koha::SharedContent::get_entity_by_id($entity_type, $mana_entity_id, [{usecomments => 1}]);
154 Retrieve a Mana entity to be imported into Koha. Add {usecomments => 1} to tell Mana to
155 embed all user reviews.
159 sub get_entity_by_id
{
160 return process_request
(build_request
('getwithid', @_));
163 =head2 search_entities
165 my $result = Koha::SharedContent::search_entities( $entity_type, $search_params );
166 my $entities = $result->{data};
168 Search entities on ManaKB.
172 sub search_entities
{
173 return process_request
(build_request
('get', @_));
178 $request = build_request($mana_method, [$param1, $param2, ...]);
180 Create a HTTP::Request object to be passed to process_request.
186 my $resource = shift;
187 my $mana_url = get_sharing_url
();
189 if ( $type eq 'get' ) {
192 map { defined $params->{$_} && $params->{$_} ne '' ?
$_ . "=" . $params->{$_} : () }
194 my $url = "$mana_url/$resource.json?$params";
195 return HTTP
::Request
->new( GET
=> $url );
198 if ( $type eq 'getwithid' ) {
202 map { defined $params->{$_} && $params->{$_} ne '' ?
$_ . "=" . $params->{$_} : () }
205 my $url = "$mana_url/$resource/$id.json?$params";
206 return HTTP
::Request
->new( GET
=> $url );
209 if ( $type eq 'post' ) {
212 my $url = "$mana_url/$resource.json";
213 my $request = HTTP
::Request
->new( POST
=> $url );
215 my $json = to_json
( $content, { utf8
=> 1 } );
216 $request->content($json);
221 if ( $type eq 'increment' ) {
227 $param->{step
} = $step || 1;
229 $param->{resource
} = $resource;
231 map { defined $param->{$_} ?
$_ . "=" . $param->{$_} : () }
233 my $url = "$mana_url/$resource/$id.json/increment/$field?$param";
234 my $request = HTTP
::Request
->new( POST
=> $url );
239 =head2 get_sharing_url
241 my $mana_url = get_sharing_url();
243 Get the Mana KB server URL set in koha config file.
247 sub get_sharing_url
{
248 return C4
::Context
->config('mana_config');