Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Items; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Mojo::Base 'Mojolicious::Controller'; |
21 |
use Mojo::JSON; |
22 |
|
23 |
use C4::Auth qw( haspermission ); |
24 |
use C4::Items qw( GetHiddenItemnumbers ); |
25 |
|
26 |
use Koha::Items; |
27 |
|
28 |
use Try::Tiny; |
29 |
|
30 |
sub get { |
31 |
my $c = shift->openapi->valid_input or return; |
32 |
|
33 |
my $item; |
34 |
try { |
35 |
$item = Koha::Items->find($c->validation->param('item_id')); |
36 |
|
37 |
# Hide non-public itemnotes if user has no staff access |
38 |
my $user = $c->stash('koha.user'); |
39 |
unless ($user && haspermission($user->userid, 'catalogue')) { |
40 |
push my @items, $item->unblessed; |
41 |
my @hiddenitems = C4::Items::GetHiddenItemnumbers( ({items => (\@items)}) ); |
42 |
my %hiddenitems = map { $_ => 1 } @hiddenitems; |
43 |
|
44 |
# Pretend it was not found as it's hidden from OPAC to regular users |
45 |
if ($hiddenitems{$item->itemnumber}) { |
46 |
return $c->render( status => 404, |
47 |
openapi => { error => 'Item not found'} ) ; |
48 |
} |
49 |
|
50 |
$item->set({ itemnotes_nonpublic => undef }); |
51 |
} |
52 |
|
53 |
return $c->render( status => 200, openapi => _to_api( $item->TO_JSON ) ); |
54 |
} |
55 |
catch { |
56 |
unless ( defined $item ) { |
57 |
return $c->render( status => 404, |
58 |
openapi => { error => 'Item not found'} ); |
59 |
} |
60 |
if ( $_->isa('DBIx::Class::Exception') ) { |
61 |
return $c->render( status => 500, |
62 |
openapi => { error => $_->{msg} } ); |
63 |
} |
64 |
else { |
65 |
return $c->render( status => 500, |
66 |
openapi => { error => "Something went wrong, check the logs."} ); |
67 |
} |
68 |
}; |
69 |
} |
70 |
|
71 |
=head3 _to_api |
72 |
|
73 |
Helper function that maps unblessed Koha::Hold objects into REST api |
74 |
attribute names. |
75 |
|
76 |
=cut |
77 |
|
78 |
sub _to_api { |
79 |
my $item = shift; |
80 |
|
81 |
# Rename attributes |
82 |
foreach my $column ( keys %{ $Koha::REST::V1::Items::to_api_mapping } ) { |
83 |
my $mapped_column = $Koha::REST::V1::Items::to_api_mapping->{$column}; |
84 |
if ( exists $item->{ $column } |
85 |
&& defined $mapped_column ) |
86 |
{ |
87 |
# key != undef |
88 |
$item->{ $mapped_column } = delete $item->{ $column }; |
89 |
} |
90 |
elsif ( exists $item->{ $column } |
91 |
&& !defined $mapped_column ) |
92 |
{ |
93 |
# key == undef |
94 |
delete $item->{ $column }; |
95 |
} |
96 |
} |
97 |
|
98 |
return $item; |
99 |
} |
100 |
|
101 |
=head3 _to_model |
102 |
|
103 |
Helper function that maps REST api objects into Koha::Hold |
104 |
attribute names. |
105 |
|
106 |
=cut |
107 |
|
108 |
sub _to_model { |
109 |
my $item = shift; |
110 |
|
111 |
foreach my $attribute ( keys %{ $Koha::REST::V1::Items::to_model_mapping } ) { |
112 |
my $mapped_attribute = $Koha::REST::V1::Items::to_model_mapping->{$attribute}; |
113 |
if ( exists $item->{ $attribute } |
114 |
&& defined $mapped_attribute ) |
115 |
{ |
116 |
# key => !undef |
117 |
$item->{ $mapped_attribute } = delete $item->{ $attribute }; |
118 |
} |
119 |
elsif ( exists $item->{ $attribute } |
120 |
&& !defined $mapped_attribute ) |
121 |
{ |
122 |
# key => undef / to be deleted |
123 |
delete $item->{ $attribute }; |
124 |
} |
125 |
} |
126 |
|
127 |
return $item; |
128 |
} |
129 |
|
130 |
=head2 Global variables |
131 |
|
132 |
=head3 $to_api_mapping |
133 |
|
134 |
=cut |
135 |
|
136 |
our $to_api_mapping = { |
137 |
itemnumber => 'item_id', |
138 |
biblionumber => 'biblio_id', |
139 |
biblioitemnumber => 'biblioitem_id', |
140 |
barcode => 'barcode', |
141 |
dateaccessioned => 'dateaccessioned', |
142 |
booksellerid => 'booksellerid', |
143 |
homebranch => 'homebranch', |
144 |
price => 'price', |
145 |
replacementprice => 'replacementprice', |
146 |
replacementpricedate => 'replacementpricedate', |
147 |
datelastborrowed => 'datelastborrowed', |
148 |
datelastseen => 'datelastseen', |
149 |
stack => 'stack', |
150 |
notforloan => 'notforloan', |
151 |
damaged => 'damaged', |
152 |
damaged_on => 'damaged_on', |
153 |
itemlost => 'itemlost', |
154 |
itemlost_on => 'itemlost_on', |
155 |
withdrawn => 'withdrawn', |
156 |
withdrawn_on => 'withdrawn_on', |
157 |
itemcallnumber => 'itemcallnumber', |
158 |
coded_location_qualifier => 'coded_location_qualifier', |
159 |
issues => 'issues', |
160 |
renewals => 'renewals', |
161 |
reserves => 'reserves', |
162 |
restricted => 'restricted', |
163 |
itemnotes => 'itemnotes', |
164 |
itemnotes_nonpublic => 'itemnotes_nonpublic', |
165 |
holdingbranch => 'holdingbranch', |
166 |
paidfor => 'paidfor', |
167 |
timestamp => 'timestamp', |
168 |
location => 'location', |
169 |
permanent_location => 'permanent_location', |
170 |
onloan => 'onloan', |
171 |
cn_source => 'cn_source', |
172 |
cn_sort => 'cn_sort', |
173 |
ccode => 'ccode', |
174 |
materials => 'materials', |
175 |
uri => 'uri', |
176 |
itype => 'itype', |
177 |
more_subfields_xml => 'more_subfields_xml', |
178 |
enumchron => 'enumchron', |
179 |
copynumber => 'copynumber', |
180 |
stocknumber => 'stocknumber', |
181 |
new_status => 'new_status' |
182 |
}; |
183 |
|
184 |
=head3 $to_model_mapping |
185 |
|
186 |
=cut |
187 |
|
188 |
our $to_model_mapping = { |
189 |
item_id => 'itemnumber', |
190 |
biblio_id => 'biblionumber', |
191 |
biblioitem_id => 'biblioitemnumber', |
192 |
barcode => 'barcode', |
193 |
dateaccessioned => 'dateaccessioned', |
194 |
booksellerid => 'booksellerid', |
195 |
homebranch => 'homebranch', |
196 |
price => 'price', |
197 |
replacementprice => 'replacementprice', |
198 |
replacementpricedate => 'replacementpricedate', |
199 |
datelastborrowed => 'datelastborrowed', |
200 |
datelastseen => 'datelastseen', |
201 |
stack => 'stack', |
202 |
notforloan => 'notforloan', |
203 |
damaged => 'damaged', |
204 |
damaged_on => 'damaged_on', |
205 |
itemlost => 'itemlost', |
206 |
itemlost_on => 'itemlost_on', |
207 |
withdrawn => 'withdrawn', |
208 |
withdrawn_on => 'withdrawn_on', |
209 |
itemcallnumber => 'itemcallnumber', |
210 |
coded_location_qualifier => 'coded_location_qualifier', |
211 |
issues => 'issues', |
212 |
renewals => 'renewals', |
213 |
reserves => 'reserves', |
214 |
restricted => 'restricted', |
215 |
itemnotes => 'itemnotes', |
216 |
itemnotes_nonpublic => 'itemnotes_nonpublic', |
217 |
holdingbranch => 'holdingbranch', |
218 |
paidfor => 'paidfor', |
219 |
timestamp => 'timestamp', |
220 |
location => 'location', |
221 |
permanent_location => 'permanent_location', |
222 |
onloan => 'onloan', |
223 |
cn_source => 'cn_source', |
224 |
cn_sort => 'cn_sort', |
225 |
ccode => 'ccode', |
226 |
materials => 'materials', |
227 |
uri => 'uri', |
228 |
itype => 'itype', |
229 |
more_subfields_xml => 'more_subfields_xml', |
230 |
enumchron => 'enumchron', |
231 |
copynumber => 'copynumber', |
232 |
stocknumber => 'stocknumber', |
233 |
new_status => 'new_status' |
234 |
}; |
235 |
|
236 |
1; |