1 package Koha::REST::V1::AdvancedEditorMacro;
3 # This file is part of Koha.
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
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.
14 # You should have received a copy of the GNU General Public License
15 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Mojo::Base 'Mojolicious::Controller';
20 use Koha::AdvancedEditorMacros;
22 use Try::Tiny qw( catch try );
26 Koha::REST::V1::AdvancedEditorMacro
34 Controller function that handles listing Koha::AdvancedEditorMacro objects
39 my $c = shift->openapi->valid_input or return;
40 my $patron = $c->stash('koha.user');
42 my $macros_set = Koha::AdvancedEditorMacros->search(
45 { shared => 1, borrowernumber => $patron->borrowernumber }
48 my $macros = $c->objects->search( $macros_set );
55 $c->unhandled_exception($_);
62 Controller function that handles retrieving a single Koha::AdvancedEditorMacro
67 my $c = shift->openapi->valid_input or return;
68 my $patron = $c->stash('koha.user');
69 my $macro = Koha::AdvancedEditorMacros->find({
70 id => $c->validation->param('advancededitormacro_id'),
73 return $c->render( status => 404,
74 openapi => { error => "Macro not found" } );
77 return $c->render( status => 403, openapi => {
78 error => "This macro is shared, you must access it via advanced_editor/macros/shared"
81 if( $macro->borrowernumber != $patron->borrowernumber ){
82 return $c->render( status => 403, openapi => {
83 error => "You do not have permission to access this macro"
87 return $c->render( status => 200, openapi => $macro->to_api );
92 Controller function that handles retrieving a single Koha::AdvancedEditorMacro
97 my $c = shift->openapi->valid_input or return;
98 my $patron = $c->stash('koha.user');
99 my $macro = Koha::AdvancedEditorMacros->find({
100 id => $c->validation->param('advancededitormacro_id'),
103 return $c->render( status => 404,
104 openapi => { error => "Macro not found" } );
106 unless( $macro->shared ){
107 return $c->render( status => 403, openapi => {
108 error => "This macro is not shared, you must access it via advanced_editor/macros"
111 return $c->render( status => 200, openapi => $macro->to_api );
116 Controller function that handles adding a new Koha::AdvancedEditorMacro object
121 my $c = shift->openapi->valid_input or return;
123 if( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
124 return $c->render( status => 403,
125 openapi => { error => "To create shared macros you must use advancededitor/shared" } );
129 my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') );
130 $macro->store->discard_changes;
131 $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
134 openapi => $macro->to_api
138 $c->unhandled_exception($_);
144 Controller function that handles adding a new shared Koha::AdvancedEditorMacro object
149 my $c = shift->openapi->valid_input or return;
151 unless( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
152 return $c->render( status => 403,
153 openapi => { error => "To create private macros you must use advancededitor" } );
156 my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') );
157 $macro->store->discard_changes;
158 $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
161 openapi => $macro->to_api
165 $c->unhandled_exception($_);
171 Controller function that handles updating a Koha::AdvancedEditorMacro object
176 my $c = shift->openapi->valid_input or return;
178 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
180 if ( not defined $macro ) {
181 return $c->render( status => 404,
182 openapi => { error => "Object not found" } );
184 my $patron = $c->stash('koha.user');
186 if( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
187 return $c->render( status => 403,
188 openapi => { error => "To update a macro as shared you must use the advanced_editor/macros/shared endpoint" } );
190 unless ( $macro->borrowernumber == $patron->borrowernumber ){
191 return $c->render( status => 403,
192 openapi => { error => "You can only edit macros you own" } );
197 my $params = $c->req->json;
198 $macro->set_from_api( $params );
199 $macro->store->discard_changes;
200 return $c->render( status => 200, openapi => $macro->to_api );
203 $c->unhandled_exception($_);
209 Controller function that handles updating a shared Koha::AdvancedEditorMacro object
214 my $c = shift->openapi->valid_input or return;
216 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
218 if ( not defined $macro ) {
219 return $c->render( status => 404,
220 openapi => { error => "Object not found" } );
223 unless( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
224 return $c->render( status => 403,
225 openapi => { error => "You can only update shared macros using this endpoint" } );
229 my $params = $c->req->json;
230 $macro->set_from_api( $params );
231 $macro->store->discard_changes;
232 return $c->render( status => 200, openapi => $macro->to_api );
235 $c->unhandled_exception($_);
241 Controller function that handles deleting a Koha::AdvancedEditorMacro object
246 my $c = shift->openapi->valid_input or return;
248 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
249 if ( not defined $macro ) {
250 return $c->render( status => 404,
251 openapi => { error => "Object not found" } );
254 my $patron = $c->stash('koha.user');
255 if( $macro->shared == 1 ){
256 return $c->render( status => 403,
257 openapi => { error => "You cannot delete shared macros using this endpoint" } );
259 unless ( $macro->borrowernumber == $patron->borrowernumber ){
260 return $c->render( status => 403,
261 openapi => { error => "You can only delete macros you own" } );
267 return $c->render( status => 204, openapi => q{} );
270 $c->unhandled_exception($_);
276 Controller function that handles deleting a shared Koha::AdvancedEditorMacro object
281 my $c = shift->openapi->valid_input or return;
283 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
284 if ( not defined $macro ) {
285 return $c->render( status => 404,
286 openapi => { error => "Object not found" } );
289 unless( $macro->shared == 1 ){
290 return $c->render( status => 403,
291 openapi => { error => "You can only delete shared macros using this endpoint" } );
296 return $c->render( status => 204, openapi => q{} );
299 $c->unhandled_exception($_);