Line 0
Link Here
|
|
|
1 |
package Koha::Service::Item::Message; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright (C) 2015 ByWater Solutions |
6 |
# |
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. |
11 |
# |
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. |
16 |
# |
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>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use JSON; |
23 |
|
24 |
use Koha::Database; |
25 |
use C4::Koha; |
26 |
|
27 |
use base 'Koha::Service'; |
28 |
|
29 |
sub new { |
30 |
my ($class) = @_; |
31 |
|
32 |
# Authentication is handled manually below |
33 |
return $class->SUPER::new( |
34 |
{ |
35 |
authnotrequired => 0, |
36 |
needed_flags => { editcatalogue => 'edit_catalogue' }, |
37 |
routes => [ |
38 |
[ qr'GET /(\d+)', 'get_messages' ], |
39 |
[ qr'POST /(\d+)', 'add_message' ], |
40 |
[ qr'PUT /(\d+)', 'update_message' ], |
41 |
[ qr'DELETE /(\d+)', 'delete_message' ], |
42 |
] |
43 |
} |
44 |
); |
45 |
} |
46 |
|
47 |
sub run { |
48 |
my ($self) = @_; |
49 |
|
50 |
$self->authenticate; |
51 |
|
52 |
unless ( $self->auth_status eq "ok" ) { |
53 |
$self->output( |
54 |
XMLout( { auth_status => $self->auth_status }, NoAttr => 1, RootName => 'response', XMLDecl => 1 ), |
55 |
{ type => 'xml', status => '403 Forbidden' } ); |
56 |
exit; |
57 |
} |
58 |
|
59 |
$self->dispatch; |
60 |
} |
61 |
|
62 |
sub get_messages { |
63 |
my ( $self, $itemnumber ) = @_; |
64 |
|
65 |
my $rs = Koha::Database->new->schema()->resultset('ItemMessage') |
66 |
->search( { itemnumber => $itemnumber }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } ); |
67 |
|
68 |
my @messages = map { $_ } $rs->all(); |
69 |
map { $_->{authorised_value} = GetAuthorisedValueByCode( 'ITEM_MESSAGE', $_->{type} ) } @messages; |
70 |
|
71 |
$self->output( \@messages ); |
72 |
} |
73 |
|
74 |
sub add_message { |
75 |
my ( $self, $itemnumber ) = @_; |
76 |
|
77 |
my $query = $self->query; |
78 |
|
79 |
my $json = $query->param('POSTDATA'); |
80 |
my $data = from_json( $json, { utf8 => 1 } ); |
81 |
|
82 |
my $type = $data->{type}; |
83 |
my $message = $data->{message}; |
84 |
|
85 |
my $msg = Koha::Database->new->schema()->resultset('ItemMessage')->create( |
86 |
{ |
87 |
itemnumber => $itemnumber, |
88 |
type => $type, |
89 |
message => $message, |
90 |
} |
91 |
); |
92 |
$msg->discard_changes(); |
93 |
|
94 |
$self->output( |
95 |
{ |
96 |
item_message_id => $msg->id(), |
97 |
itemnumber => $itemnumber, |
98 |
type => $type, |
99 |
message => $message, |
100 |
created_on => $msg->created_on(), |
101 |
authorised_value => GetAuthorisedValueByCode( 'ITEM_MESSAGE', $type ), |
102 |
} |
103 |
); |
104 |
} |
105 |
|
106 |
sub update_message { |
107 |
my ( $self, $id ) = @_; |
108 |
|
109 |
my $query = $self->query; |
110 |
|
111 |
my $json = $query->param('PUTDATA'); |
112 |
my $data = from_json( $json, { utf8 => 1 } ); |
113 |
|
114 |
my $type = $data->{type}; |
115 |
my $message = $data->{message}; |
116 |
|
117 |
my $msg = Koha::Database->new->schema()->resultset('ItemMessage')->find($id); |
118 |
|
119 |
croak("no item message") unless $msg; |
120 |
|
121 |
$msg->update( |
122 |
{ |
123 |
type => $type, |
124 |
message => $message, |
125 |
} |
126 |
); |
127 |
|
128 |
$self->output( |
129 |
{ |
130 |
item_message_id => $msg->id(), |
131 |
itemnumber => $msg->get_column('itemnumber'), |
132 |
type => $type, |
133 |
message => $message, |
134 |
created_on => $msg->created_on(), |
135 |
authorised_value => GetAuthorisedValueByCode( 'ITEM_MESSAGE', $type ), |
136 |
} |
137 |
); |
138 |
} |
139 |
|
140 |
sub delete_message { |
141 |
my ( $self, $id ) = @_; |
142 |
|
143 |
my $msg = Koha::Database->new->schema()->resultset('ItemMessage')->find($id); |
144 |
|
145 |
craok("no item message") unless $msg; |
146 |
|
147 |
$msg->delete(); |
148 |
|
149 |
$self->output( |
150 |
{ |
151 |
item_message_id => $msg->id(), |
152 |
itemnumber => $msg->get_column('itemnumber'), |
153 |
type => $msg->type(), |
154 |
message => $msg->message(), |
155 |
created_on => $msg->created_on(), |
156 |
} |
157 |
); |
158 |
} |
159 |
|
160 |
1; |