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 along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Controller';
21 use Koha::AdvancedEditorMacros;
33 Controller function that handles listing Koha::AdvancedEditorMacro objects
38 my $c = shift->openapi->valid_input or return;
39 my $patron = $c->stash('koha.user');
41 my $macros_set = Koha::AdvancedEditorMacros->search({ -or => { shared => 1, borrowernumber => $patron->borrowernumber } });
42 my $macros = $c->objects->search( $macros_set, \&_to_model, \&_to_api );
43 return $c->render( status => 200, openapi => $macros );
46 if ( $_->isa('DBIx::Class::Exception') ) {
47 return $c->render( status => 500,
48 openapi => { error => $_->{msg} } );
51 return $c->render( status => 500,
52 openapi => { error => "Something went wrong, check the logs. $_"} );
60 Controller function that handles retrieving a single Koha::AdvancedEditorMacro
65 my $c = shift->openapi->valid_input or return;
66 my $patron = $c->stash('koha.user');
67 my $macro = Koha::AdvancedEditorMacros->find({
68 id => $c->validation->param('advancededitormacro_id'),
71 return $c->render( status => 404,
72 openapi => { error => "Macro not found" } );
75 return $c->render( status => 403, openapi => {
76 error => "This macro is shared, you must access it via advancededitormacros/shared"
79 warn $macro->borrowernumber;
80 warn $patron->borrowernumber;
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 advancededitormacros"
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( _to_model( $c->validation->param('body') ) );
131 $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
134 openapi => $macro->to_api
137 catch { handle_error($_) };
142 Controller function that handles adding a new shared Koha::AdvancedEditorMacro object
147 my $c = shift->openapi->valid_input or return;
149 unless( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
150 return $c->render( status => 403,
151 openapi => { error => "To create private macros you must use advancededitor" } );
155 my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) );
157 $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
160 openapi => $macro->to_api
163 catch { handle_error($_) };
168 Controller function that handles updating a Koha::AdvancedEditorMacro object
173 my $c = shift->openapi->valid_input or return;
175 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
177 if ( not defined $macro ) {
178 return $c->render( status => 404,
179 openapi => { error => "Object not found" } );
181 my $patron = $c->stash('koha.user');
183 if( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
184 return $c->render( status => 403,
185 openapi => { error => "To update a macro as shared you must use the advancededitormacros/shared endpoint" } );
187 unless ( $macro->borrowernumber == $patron->borrowernumber ){
188 return $c->render( status => 403,
189 openapi => { error => "You can only edit macros you own" } );
194 my $params = $c->req->json;
195 $macro->set( _to_model($params) );
197 return $c->render( status => 200, openapi => $macro->to_api );
199 catch { handle_error($_) };
204 Controller function that handles updating a shared Koha::AdvancedEditorMacro object
209 my $c = shift->openapi->valid_input or return;
211 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
213 if ( not defined $macro ) {
214 return $c->render( status => 404,
215 openapi => { error => "Object not found" } );
218 unless( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
219 return $c->render( status => 403,
220 openapi => { error => "You can only update shared macros using this endpoint" } );
224 my $params = $c->req->json;
225 $macro->set( _to_model($params) );
227 return $c->render( status => 200, openapi => $macro->to_api );
229 catch { handle_error($_) };
234 Controller function that handles deleting a Koha::AdvancedEditorMacro object
239 my $c = shift->openapi->valid_input or return;
241 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
242 if ( not defined $macro ) {
243 return $c->render( status => 404,
244 openapi => { error => "Object not found" } );
247 my $patron = $c->stash('koha.user');
248 if( $macro->shared == 1 ){
249 return $c->render( status => 403,
250 openapi => { error => "You cannot delete shared macros using this endpoint" } );
252 unless ( $macro->borrowernumber == $patron->borrowernumber ){
253 return $c->render( status => 403,
254 openapi => { error => "You can only delete macros you own" } );
260 return $c->render( status => 200, openapi => "" );
262 catch { handle_error($_) };
267 Controller function that handles deleting a shared Koha::AdvancedEditorMacro object
272 my $c = shift->openapi->valid_input or return;
274 my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
275 if ( not defined $macro ) {
276 return $c->render( status => 404,
277 openapi => { error => "Object not found" } );
280 unless( $macro->shared == 1 ){
281 return $c->render( status => 403,
282 openapi => { error => "You can only delete shared macros using this endpoint" } );
287 return $c->render( status => 200, openapi => "" );
289 catch { handle_error($_,$c) };
294 Helper function that passes exception or error
300 if ( $err->isa('DBIx::Class::Exception') ) {
301 return $c->render( status => 500,
302 openapi => { error => $err->{msg} } );
305 return $c->render( status => 500,
306 openapi => { error => "Something went wrong, check the logs."} );
313 Helper function that maps a hashref of Koha::AdvancedEditorMacro attributes into REST api
322 foreach my $column ( keys %{ $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping } ) {
323 my $mapped_column = $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping->{$column};
324 if ( exists $macro->{ $column }
325 && defined $mapped_column )
328 $macro->{ $mapped_column } = delete $macro->{ $column };
330 elsif ( exists $macro->{ $column }
331 && !defined $mapped_column )
333 # key == undef => to be deleted
334 delete $macro->{ $column };
343 Helper function that maps REST api objects into Koha::AdvancedEditorMacros
351 foreach my $attribute ( keys %{ $Koha::REST::V1::AdvancedEditorMacro::to_model_mapping } ) {
352 my $mapped_attribute = $Koha::REST::V1::AdvancedEditorMacro::to_model_mapping->{$attribute};
353 if ( exists $macro->{ $attribute }
354 && defined $mapped_attribute )
357 $macro->{ $mapped_attribute } = delete $macro->{ $attribute };
359 elsif ( exists $macro->{ $attribute }
360 && !defined $mapped_attribute )
362 # key == undef => to be deleted
363 delete $macro->{ $attribute };
367 if ( exists $macro->{shared} ) {
368 $macro->{shared} = ($macro->{shared}) ? 1 : 0;
375 =head2 Global variables
377 =head3 $to_api_mapping
381 our $to_api_mapping = {
383 macro => 'macro_text',
384 borrowernumber => 'patron_id',
387 =head3 $to_model_mapping
391 our $to_model_mapping = {
393 macro_text => 'macro',
394 patron_id => 'borrowernumber',