View | Details | Raw Unified | Return to bug 17268
Collapse All | Expand All

(-)a/Koha/AdvancedEditorMacro.pm (+61 lines)
Line 0 Link Here
1
package Koha::AdvancedEditorMacro;
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 Carp;
21
22
use Koha::Database;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::AdvancedEditorMacro - Koha Advanced Editor Macro Object class
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 to_api_mapping
35
36
This method returns the mapping for representing a Koha::AdvancedEditorMacro object
37
on the API.
38
39
=cut
40
41
sub to_api_mapping {
42
    return {
43
        id              => 'macro_id',
44
        name            => 'name',
45
        macro           => 'macro_text',
46
        borrowernumber  => 'borrowernumber',
47
        public          => 'public'
48
    };
49
}
50
51
=head2 Internal methods
52
53
=head3 _type
54
55
=cut
56
57
sub _type {
58
    return 'AdvancedEditorMacro';
59
}
60
61
1;
(-)a/Koha/AdvancedEditorMacros.pm (+50 lines)
Line 0 Link Here
1
package Koha::AdvancedEditorMacros;
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 Carp;
21
22
use Koha::Database;
23
24
use Koha::AdvancedEditorMacro;
25
26
use base qw(Koha::Objects);
27
28
=head1 NAME
29
30
Koha::AdvancedEditorMacros - Koha Advanced Editor Macro Object set class
31
32
=head1 API
33
34
=head2 Class Methods
35
36
=cut
37
38
=head3 type
39
40
=cut
41
42
sub _type {
43
    return 'AdvancedEditorMacro';
44
}
45
46
sub object_class {
47
    return 'Koha::AdvancedEditorMacro';
48
}
49
50
1;
(-)a/Koha/REST/V1/AdvancedEditorMacros.pm (+249 lines)
Line 0 Link Here
1
package Koha::REST::V1::AdvancedEditorMacros;
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
22
use Koha::AdvancedEditorMacros;
23
24
use Try::Tiny;
25
26
=head1 API
27
28
=head2 Class Methods
29
30
=head3 list
31
32
=cut
33
34
sub list {
35
    my $c = shift->openapi->valid_input or return;
36
37
    return try {
38
        my $macros_set = Koha::AdvancedEditorMacros->new;
39
        my $macros = $c->objects->search( $macros_set, \&_to_model, \&_to_api );
40
        return $c->render( status => 200, openapi => $macros );
41
    }
42
    catch {
43
        if ( $_->isa('DBIx::Class::Exception') ) {
44
            return $c->render( status  => 500,
45
                               openapi => { error => $_->{msg} } );
46
        }
47
        else {
48
            return $c->render( status => 500,
49
                openapi => { error => "Something went wrong, check the logs. $_"} );
50
        }
51
    };
52
53
}
54
55
=head3 get
56
57
=cut
58
59
sub get {
60
    my $c = shift->openapi->valid_input or return;
61
62
    my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('macro_id') );
63
    unless ($macro) {
64
        return $c->render( status  => 404,
65
                           openapi => { error => "Macro not found" } );
66
    }
67
68
    return $c->render( status => 200, openapi => $macro->to_api );
69
}
70
71
=head3 add
72
73
=cut
74
75
sub add {
76
    my $c = shift->openapi->valid_input or return;
77
78
    return try {
79
        my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) );
80
        $macro->store;
81
        $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
82
        return $c->render(
83
            status  => 201,
84
            openapi => $macro->to_api
85
        );
86
    }
87
    catch {
88
        if ( $_->isa('DBIx::Class::Exception') ) {
89
            return $c->render(
90
                status  => 500,
91
                openapi => { error => $_->{msg} }
92
            );
93
        }
94
        else {
95
            return $c->render(
96
                status  => 500,
97
                openapi => { error => "Something went wrong, check the logs." }
98
            );
99
        }
100
    };
101
}
102
103
=head3 update
104
105
=cut
106
107
sub update {
108
    my $c = shift->openapi->valid_input or return;
109
110
    my $macro = Koha::AdvancedEditorMacro->find( $c->validation->param('macro_id') );
111
112
    if ( not defined $macro ) {
113
        return $c->render( status  => 404,
114
                           openapi => { error => "Object not found" } );
115
    }
116
117
    return try {
118
        my $params = $c->req->json;
119
        $macro->set( _to_model($params) );
120
        $macro->store();
121
        return $c->render( status => 200, openapi => $macro->to_api );
122
    }
123
    catch {
124
        if ( $_->isa('Koha::Exceptions::Object') ) {
125
            return $c->render( status  => 500,
126
                               openapi => { error => $_->message } );
127
        }
128
        else {
129
            return $c->render( status => 500,
130
                openapi => { error => "Something went wrong, check the logs."} );
131
        }
132
    };
133
}
134
135
=head3 delete
136
137
=cut
138
139
sub delete {
140
    my $c = shift->openapi->valid_input or return;
141
142
    my $macro = Koha::AdvancedEditorMacro->find( $c->validation->param('macro_id') );
143
    if ( not defined $macro ) {
144
        return $c->render( status  => 404,
145
                           openapi => { error => "Object not found" } );
146
    }
147
148
    return try {
149
        $macro->delete;
150
        return $c->render( status => 200, openapi => "" );
151
    }
152
    catch {
153
        if ( $_->isa('DBIx::Class::Exception') ) {
154
            return $c->render( status  => 500,
155
                               openapi => { error => $_->{msg} } );
156
        }
157
        else {
158
            return $c->render( status => 500,
159
                openapi => { error => "Something went wrong, check the logs. line 159"} );
160
        }
161
    };
162
}
163
164
=head3 _to_api
165
166
Helper function that maps a hashref of Koha::AdvancedEditorMacro attributes into REST api
167
attribute names.
168
169
=cut
170
171
sub _to_api {
172
    my $macro    = shift;
173
174
    # Rename attributes
175
    foreach my $column ( keys %{ $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping } ) {
176
        my $mapped_column = $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping->{$column};
177
        if (    exists $macro->{ $column }
178
             && defined $mapped_column )
179
        {
180
            # key /= undef
181
            $macro->{ $mapped_column } = delete $macro->{ $column };
182
        }
183
        elsif (    exists $macro->{ $column }
184
                && !defined $mapped_column )
185
        {
186
            # key == undef => to be deleted
187
            delete $macro->{ $column };
188
        }
189
    }
190
191
    return $macro;
192
}
193
194
=head3 _to_model
195
196
Helper function that maps REST api objects into Koha::AdvancedEditorMacros
197
attribute names.
198
199
=cut
200
201
sub _to_model {
202
    my $macro = shift;
203
204
    foreach my $attribute ( keys %{ $Koha::REST::V1::AdvancedEditorMacros::to_model_mapping } ) {
205
        my $mapped_attribute = $Koha::REST::V1::AdvancedEditorMacros::to_model_mapping->{$attribute};
206
        if (    exists $macro->{ $attribute }
207
             && defined $mapped_attribute )
208
        {
209
            # key /= undef
210
            $macro->{ $mapped_attribute } = delete $macro->{ $attribute };
211
        }
212
        elsif (    exists $macro->{ $attribute }
213
                && !defined $mapped_attribute )
214
        {
215
            # key == undef => to be deleted
216
            delete $macro->{ $attribute };
217
        }
218
    }
219
220
    return $macro;
221
}
222
223
=head2 Global variables
224
225
=head3 $to_api_mapping
226
227
=cut
228
229
our $to_api_mapping = {
230
    id                  => 'macro_id',
231
    name                => 'name',
232
    macro               => 'macro_text',
233
    borrrowernumber     => 'borrowernumber',
234
    public              => 'public'
235
};
236
237
=head3 $to_model_mapping
238
239
=cut
240
241
our $to_model_mapping = {
242
    macro_id         => 'id',
243
    name             => 'name',
244
    macro_text            => 'macro',
245
    borrowernumber   => 'borrowernumber',
246
    public           => 'public'
247
};
248
249
1;
(-)a/Koha/Schema/Result/Borrower.pm (-2 / +17 lines)
Lines 734-739 __PACKAGE__->has_many( Link Here
734
  { cascade_copy => 0, cascade_delete => 0 },
734
  { cascade_copy => 0, cascade_delete => 0 },
735
);
735
);
736
736
737
=head2 advanced_editor_macros
738
739
Type: has_many
740
741
Related object: L<Koha::Schema::Result::AdvancedEditorMacro>
742
743
=cut
744
745
__PACKAGE__->has_many(
746
  "advanced_editor_macros",
747
  "Koha::Schema::Result::AdvancedEditorMacro",
748
  { "foreign.borrowernumber" => "self.borrowernumber" },
749
  { cascade_copy => 0, cascade_delete => 0 },
750
);
751
737
=head2 api_keys
752
=head2 api_keys
738
753
739
Type: has_many
754
Type: has_many
Lines 1635-1642 Composing rels: L</aqorder_users> -> ordernumber Link Here
1635
__PACKAGE__->many_to_many("ordernumbers", "aqorder_users", "ordernumber");
1650
__PACKAGE__->many_to_many("ordernumbers", "aqorder_users", "ordernumber");
1636
1651
1637
1652
1638
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-10-10 14:31:00
1653
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-11-27 13:18:35
1639
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:GjJLIOViIFRm185Yjl9vYA
1654
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:RRiPuHSENnEJfBGhqakEAQ
1640
1655
1641
__PACKAGE__->add_columns(
1656
__PACKAGE__->add_columns(
1642
    '+anonymized'    => { is_boolean => 1 },
1657
    '+anonymized'    => { is_boolean => 1 },
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 26-31 Link Here
26
  "item": {
26
  "item": {
27
    "$ref": "definitions/item.json"
27
    "$ref": "definitions/item.json"
28
  },
28
  },
29
  "advancededitormacro": {
30
    "$ref": "definitions/advancededitormacro.json"
31
  },
29
  "patron": {
32
  "patron": {
30
    "$ref": "definitions/patron.json"
33
    "$ref": "definitions/patron.json"
31
  },
34
  },
(-)a/api/v1/swagger/definitions/advancededitormacro.json (+26 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "id": {
5
      "$ref": "../x-primitives.json#/advancededitormacro_id"
6
    },
7
    "name": {
8
      "description": "macro name",
9
      "type": "string"
10
    },
11
    "macro": {
12
      "description": "macro text",
13
      "type": ["string", "null"]
14
    },
15
    "borrowernumber": {
16
      "description": "borrower number",
17
      "type": ["integer", "null"]
18
    },
19
    "public": {
20
        "description": "is macro public",
21
        "type": ["string", "null"]
22
    }
23
  },
24
  "additionalProperties": false,
25
  "required": ["name", "macro", "borrowernumber", "public"]
26
}
(-)a/api/v1/swagger/parameters.json (+3 lines)
Lines 2-7 Link Here
2
  "biblio_id_pp": {
2
  "biblio_id_pp": {
3
    "$ref": "parameters/biblio.json#/biblio_id_pp"
3
    "$ref": "parameters/biblio.json#/biblio_id_pp"
4
  },
4
  },
5
  "advancededitormacro_id_pp": {
6
    "$ref": "parameters/advancededitormacro.json#/advancededitormacro_id_pp"
7
  },
5
  "patron_id_pp": {
8
  "patron_id_pp": {
6
    "$ref": "parameters/patron.json#/patron_id_pp"
9
    "$ref": "parameters/patron.json#/patron_id_pp"
7
  },
10
  },
(-)a/api/v1/swagger/parameters/advancededitormacro.json (+9 lines)
Line 0 Link Here
1
{
2
    "advancededitormacro_id_pp": {
3
      "name": "advancededitormacro_id",
4
      "in": "path",
5
      "description": "Advanced Editor Macro internal identifier",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 59-64 Link Here
59
  "/checkouts/{checkout_id}/allows_renewal": {
59
  "/checkouts/{checkout_id}/allows_renewal": {
60
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1allows_renewal"
60
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1allows_renewal"
61
  },
61
  },
62
  "/advancededitormacros": {
63
    "$ref": "paths/advancededitormacros.json#/~1advancededitormacros"
64
  },
65
  "/advancededitormacros/{advancededitormacro_id}": {
66
    "$ref": "paths/advancededitormacros.json#/~1advancededitormacros~1{advancededitormacro_id}"
67
  },
62
  "/patrons": {
68
  "/patrons": {
63
    "$ref": "paths/patrons.json#/~1patrons"
69
    "$ref": "paths/patrons.json#/~1patrons"
64
  },
70
  },
(-)a/api/v1/swagger/paths/advancededitormacros.json (+267 lines)
Line 0 Link Here
1
{
2
  "/advancededitormacros": {
3
    "get": {
4
      "x-mojo-to": "AdvancedEditorMacros#list",
5
      "operationId": "listMacros",
6
      "tags": ["advancededitormacros"],
7
      "produces": [
8
        "application/json"
9
      ],
10
      "parameters": [{
11
        "name": "name",
12
        "in": "query",
13
        "description": "Case insensative search on macro name",
14
        "required": false,
15
        "type": "string"
16
      }],
17
      "responses": {
18
        "200": {
19
          "description": "A list of macros",
20
          "schema": {
21
            "type": "array",
22
            "items": {
23
              "$ref": "../definitions.json#/advancededitormacro"
24
            }
25
          }
26
        },
27
        "403": {
28
          "description": "Access forbidden",
29
          "schema": {
30
            "$ref": "../definitions.json#/error"
31
          }
32
        },
33
        "500": {
34
          "description": "Internal error",
35
          "schema": {
36
            "$ref": "../definitions.json#/error"
37
          }
38
        },
39
        "503": {
40
          "description": "Under maintenance",
41
          "schema": {
42
            "$ref": "../definitions.json#/error"
43
          }
44
        }
45
      },
46
      "x-koha-authorization": {
47
        "permissions": {
48
            "catalogue": "1"
49
        }
50
      }
51
    },
52
    "post": {
53
      "x-mojo-to": "AdvancedEditorMacros#add",
54
      "operationId": "addAdvancedEditorMacro",
55
      "tags": ["advancededitormacros"],
56
      "parameters": [{
57
        "name": "body",
58
        "in": "body",
59
        "description": "A JSON object containing informations about the new macro",
60
        "required": true,
61
        "schema": {
62
          "$ref": "../definitions.json#/advancededitormacro"
63
        }
64
      }],
65
      "produces": [
66
        "application/json"
67
      ],
68
      "responses": {
69
        "201": {
70
          "description": "Macro added",
71
          "schema": {
72
            "$ref": "../definitions.json#/advancededitormacro"
73
          }
74
        },
75
        "401": {
76
          "description": "Authentication required",
77
          "schema": {
78
            "$ref": "../definitions.json#/error"
79
          }
80
        },
81
        "403": {
82
          "description": "Access forbidden",
83
          "schema": {
84
            "$ref": "../definitions.json#/error"
85
          }
86
        },
87
        "500": {
88
          "description": "Internal error",
89
          "schema": {
90
            "$ref": "../definitions.json#/error"
91
          }
92
        },
93
        "503": {
94
          "description": "Under maintenance",
95
          "schema": {
96
            "$ref": "../definitions.json#/error"
97
          }
98
        }
99
      },
100
      "x-koha-authorization": {
101
        "permissions": {
102
          "parameters": "manage_advancededitormacros"
103
        }
104
      }
105
    }
106
  },
107
  "/advancededitormacros/{advancededitormacro_id}": {
108
    "get": {
109
      "x-mojo-to": "AdvancedEditorMacros#get",
110
      "operationId": "getAdvancedEditorMacro",
111
      "tags": ["advancededitormacros"],
112
      "parameters": [{
113
        "$ref": "../parameters.json#/advancededitormacro_id_pp"
114
      }],
115
      "produces": [
116
        "application/json"
117
      ],
118
      "responses": {
119
        "200": {
120
          "description": "A macro",
121
          "schema": {
122
            "$ref": "../definitions.json#/advancededitormacro"
123
          }
124
        },
125
        "404": {
126
          "description": "AdvancedEditorMacro not found",
127
          "schema": {
128
            "$ref": "../definitions.json#/error"
129
          }
130
        },
131
        "500": {
132
          "description": "Internal error",
133
          "schema": {
134
            "$ref": "../definitions.json#/error"
135
          }
136
        },
137
        "503": {
138
          "description": "Under maintenance",
139
          "schema": {
140
            "$ref": "../definitions.json#/error"
141
          }
142
        }
143
      },
144
      "x-koha-authorization": {
145
        "permissions": {
146
            "catalogue": "1"
147
        }
148
      }
149
    },
150
    "put": {
151
      "x-mojo-to": "AdvancedEditorMacro#update",
152
      "operationId": "updateAdvancedEditorMacro",
153
      "tags": ["advancededitormacros"],
154
      "parameters": [{
155
        "$ref": "../parameters.json#/advancededitormacro_id_pp"
156
      }, {
157
        "name": "body",
158
        "in": "body",
159
        "description": "An advanced editor macro object",
160
        "required": true,
161
        "schema": {
162
          "$ref": "../definitions.json#/advancededitormacro"
163
        }
164
      }],
165
      "produces": [
166
        "application/json"
167
      ],
168
      "responses": {
169
        "200": {
170
          "description": "An advanced editor macro",
171
          "schema": {
172
            "$ref": "../definitions.json#/advancededitormacro"
173
          }
174
        },
175
        "401": {
176
          "description": "Authentication required",
177
          "schema": {
178
            "$ref": "../definitions.json#/error"
179
          }
180
        },
181
        "403": {
182
          "description": "Access forbidden",
183
          "schema": {
184
            "$ref": "../definitions.json#/error"
185
          }
186
        },
187
        "404": {
188
          "description": "Macro not found",
189
          "schema": {
190
            "$ref": "../definitions.json#/error"
191
          }
192
        },
193
        "500": {
194
          "description": "Internal error",
195
          "schema": {
196
            "$ref": "../definitions.json#/error"
197
          }
198
        },
199
        "503": {
200
          "description": "Under maintenance",
201
          "schema": {
202
            "$ref": "../definitions.json#/error"
203
          }
204
        }
205
      },
206
      "x-koha-authorization": {
207
        "permissions": {
208
          "parameters": "manage_advancededitormacro"
209
        }
210
      }
211
    },
212
    "delete": {
213
      "x-mojo-to": "AdvancedEditorMacros#delete",
214
      "operationId": "deleteAdvancedEditorMacro",
215
      "tags": ["advancededitormacros"],
216
      "parameters": [{
217
        "$ref": "../parameters.json#/advancededitormacro_id_pp"
218
      }],
219
      "produces": [
220
        "application/json"
221
      ],
222
      "responses": {
223
        "200": {
224
          "description": "Advanced editor macro deleted",
225
          "schema": {
226
            "type": "string"
227
          }
228
        },
229
        "401": {
230
          "description": "Authentication required",
231
          "schema": {
232
            "$ref": "../definitions.json#/error"
233
          }
234
        },
235
        "403": {
236
          "description": "Access forbidden",
237
          "schema": {
238
            "$ref": "../definitions.json#/error"
239
          }
240
        },
241
        "404": {
242
          "description": "Macro not found",
243
          "schema": {
244
            "$ref": "../definitions.json#/error"
245
          }
246
        },
247
        "500": {
248
          "description": "Internal error",
249
          "schema": {
250
            "$ref": "../definitions.json#/error"
251
          }
252
        },
253
        "503": {
254
          "description": "Under maintenance",
255
          "schema": {
256
            "$ref": "../definitions.json#/error"
257
          }
258
        }
259
      },
260
      "x-koha-authorization": {
261
        "permissions": {
262
          "parameters": "manage_advancededitormacros"
263
        }
264
      }
265
    }
266
  }
267
}
(-)a/api/v1/swagger/x-primitives.json (-1 / +17 lines)
Lines 3-8 Link Here
3
    "type": "integer",
3
    "type": "integer",
4
    "description": "Internal biblio identifier"
4
    "description": "Internal biblio identifier"
5
  },
5
  },
6
  "advancededitormacro_id": {
7
    "type": "integer",
8
    "description": "Internal advanced editor macro identifier",
9
    "readOnly": true
10
  },
11
  "public":{
12
    "type": "boolean",
13
    "description": "advanced editor macro is public"
14
  },
15
  "macro": {
16
    "type": "string",
17
    "description": "macro text"
18
  },
19
  "borrowernumber": {
20
    "type": "integer",
21
    "description": "Internal intranet user"
22
  },
6
  "patron_id": {
23
  "patron_id": {
7
    "type": "integer",
24
    "type": "integer",
8
    "description": "Internal patron identifier"
25
    "description": "Internal patron identifier"
9
- 

Return to bug 17268