Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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 DESCRIPTION
30
31 Package for accessing shared content via Mana
32
33 =head2 Package Functions
34
35 =cut
36
37 =head3 process_request
38
39 =cut
40
41 sub process_request {
42     my $mana_request = shift;
43     my $result;
44     $mana_request->content_type('application/json');
45     my $userAgent = LWP::UserAgent->new;
46     if ( $mana_request->method eq "POST" ){
47         my $content;
48         if ($mana_request->content) {$content = from_json( $mana_request->content )};
49         $content->{securitytoken} = C4::Context->preference("ManaToken");
50         $mana_request->content( to_json($content) );
51     }
52
53     my $response = $userAgent->request($mana_request);
54
55     eval { $result = from_json( $response->decoded_content, { utf8 => 1} ); };
56     $result->{code} = $response->code;
57     if ( $@ ){
58         $result->{msg} = $@;
59     }
60     if ($response->is_error){
61         $result->{msg} = "An error occurred, mana server returned: " . $response->message;
62     }
63     return $result ;
64 }
65
66 =head3 increment_entity_value
67
68 =cut
69
70 sub increment_entity_value {
71     return process_request(build_request('increment', @_));
72 }
73
74 =head3 send_entity
75
76 =cut
77
78 sub send_entity {
79     my ($lang, $loggedinuser, $resourceid, $resourcetype, $content) = @_;
80
81     unless ( $content ) {
82         $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
83     }
84
85     my $result = process_request(build_request('post', $resourcetype, $content));
86
87     if ( $result and ($result->{code} eq "200" or $result->{code} eq "201") ) {
88         my $packages = "Koha::".ucfirst($resourcetype)."s";
89         my $resource = $packages->find($resourceid);
90         eval { $resource->set( { mana_id => $result->{id} } )->store };
91     }
92     return $result;
93 }
94
95 =head3 prepare_entity_data
96
97 =cut
98
99 sub prepare_entity_data {
100     my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_;
101     $lang ||= C4::Context->preference('language');
102
103     my $mana_email;
104     if ( $loggedinuser ne 0 ) {
105         my $borrower = Koha::Patrons->find($loggedinuser);
106         $mana_email = $borrower->first_valid_email_address
107             || Koha::Libraries->find( C4::Context->userenv->{'branch'} )->branchemail
108     }
109     $mana_email = C4::Context->preference('KohaAdminEmailAddress')
110       if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) );
111
112     my %versions = C4::Context::get_versions();
113
114     my $mana_info = {
115         language    => $lang,
116         kohaversion => $versions{'kohaVersion'},
117         exportemail => $mana_email
118     };
119
120     my $ressource_mana_info;
121     my $packages = "Koha::".ucfirst($ressourcetype)."s";
122     my $package = "Koha::".ucfirst($ressourcetype);
123     $ressource_mana_info = $package->get_sharable_info($ressourceid);
124     $ressource_mana_info = { %$ressource_mana_info, %$mana_info };
125
126     return $ressource_mana_info;
127 }
128
129 =head3 get_entity_by_id
130
131 =cut
132
133 sub get_entity_by_id {
134     return process_request(build_request('getwithid', @_));
135 }
136
137 =head3 search_entities
138
139 =cut
140
141 sub search_entities {
142     return process_request(build_request('get', @_));
143 }
144
145 =head3 build_request
146
147 =cut
148
149 sub build_request {
150     my $type = shift;
151     my $resource = shift;
152     my $mana_url = get_sharing_url();
153
154     if ( $type eq 'get' ) {
155         my $params = shift;
156         $params = join '&',
157             map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
158             keys %$params;
159         my $url = "$mana_url/$resource.json?$params";
160         return HTTP::Request->new( GET => $url );
161     }
162
163     if ( $type eq 'getwithid' ) {
164         my $id = shift;
165         my $params = shift;
166         $params = join '&',
167             map { defined $params->{$_} && $params->{$_} ne '' ? $_ . "=" . $params->{$_} : () }
168             keys %$params;
169
170         my $url = "$mana_url/$resource/$id.json?$params";
171         return HTTP::Request->new( GET => $url );
172     }
173
174     if ( $type eq 'post' ) {
175         my $content  = shift;
176
177         my $url = "$mana_url/$resource.json";
178         my $request = HTTP::Request->new( POST => $url );
179
180         my $json = to_json( $content, { utf8 => 1 } );
181         $request->content($json);
182
183         return $request;
184     }
185
186     if ( $type eq 'increment' ) {
187         my $id       = shift;
188         my $field    = shift;
189         my $step     = shift;
190         my $param;
191
192         $param->{step} = $step || 1;
193         $param->{id} = $id;
194         $param->{resource} = $resource;
195         $param = join '&',
196            map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () }
197                keys %$param;
198         my $url = "$mana_url/$resource/$id.json/increment/$field?$param";
199         my $request = HTTP::Request->new( POST => $url );
200
201     }
202 }
203
204 =head3 get_sharing_url
205
206 =cut
207
208 sub get_sharing_url {
209     return C4::Context->config('mana_config');
210 }
211
212 1;