|
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 |
warn Data::Dumper::Dumper( \@messages ); |
| 72 |
|
| 73 |
$self->output( \@messages ); |
| 74 |
} |
| 75 |
|
| 76 |
sub add_message { |
| 77 |
my ( $self, $itemnumber ) = @_; |
| 78 |
|
| 79 |
my $query = $self->query; |
| 80 |
|
| 81 |
my $json = $query->param('POSTDATA'); |
| 82 |
my $data = from_json( $json, { utf8 => 1 } ); |
| 83 |
|
| 84 |
my $type = $data->{type}; |
| 85 |
my $message = $data->{message}; |
| 86 |
|
| 87 |
my $msg = Koha::Database->new->schema()->resultset('ItemMessage')->create( |
| 88 |
{ |
| 89 |
itemnumber => $itemnumber, |
| 90 |
type => $type, |
| 91 |
message => $message, |
| 92 |
} |
| 93 |
); |
| 94 |
$msg->discard_changes(); |
| 95 |
|
| 96 |
$self->output( |
| 97 |
{ |
| 98 |
item_message_id => $msg->id(), |
| 99 |
itemnumber => $itemnumber, |
| 100 |
type => $type, |
| 101 |
message => $message, |
| 102 |
created_on => $msg->created_on(), |
| 103 |
authorised_value => GetAuthorisedValueByCode( 'ITEM_MESSAGE', $type ), |
| 104 |
} |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
sub update_message { |
| 109 |
my ( $self, $id ) = @_; |
| 110 |
|
| 111 |
my $query = $self->query; |
| 112 |
|
| 113 |
my $json = $query->param('PUTDATA'); |
| 114 |
my $data = from_json( $json, { utf8 => 1 } ); |
| 115 |
|
| 116 |
my $type = $data->{type}; |
| 117 |
my $message = $data->{message}; |
| 118 |
|
| 119 |
my $msg = Koha::Database->new->schema()->resultset('ItemMessage')->find($id); |
| 120 |
|
| 121 |
croak("no item message") unless $msg; |
| 122 |
|
| 123 |
$msg->update( |
| 124 |
{ |
| 125 |
type => $type, |
| 126 |
message => $message, |
| 127 |
} |
| 128 |
); |
| 129 |
|
| 130 |
$self->output( |
| 131 |
{ |
| 132 |
item_message_id => $msg->id(), |
| 133 |
itemnumber => $msg->get_column('itemnumber'), |
| 134 |
type => $type, |
| 135 |
message => $message, |
| 136 |
created_on => $msg->created_on(), |
| 137 |
authorised_value => GetAuthorisedValueByCode( 'ITEM_MESSAGE', $type ), |
| 138 |
} |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
sub delete_message { |
| 143 |
my ( $self, $id ) = @_; |
| 144 |
|
| 145 |
my $msg = Koha::Database->new->schema()->resultset('ItemMessage')->find($id); |
| 146 |
|
| 147 |
craok("no item message") unless $msg; |
| 148 |
|
| 149 |
$msg->delete(); |
| 150 |
|
| 151 |
$self->output( |
| 152 |
{ |
| 153 |
item_message_id => $msg->id(), |
| 154 |
itemnumber => $msg->get_column('itemnumber'), |
| 155 |
type => $msg->type(), |
| 156 |
message => $msg->message(), |
| 157 |
created_on => $msg->created_on(), |
| 158 |
} |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
1; |