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

(-)a/Koha/Object/Mixin/AdditionalFields.pm (-19 / +21 lines)
Lines 43-93 Koha::Object::Mixin::AdditionalFields Link Here
43
=cut
43
=cut
44
44
45
sub set_additional_fields {
45
sub set_additional_fields {
46
    my ($self, $additional_fields) = @_;
46
    my ( $self, $additional_fields ) = @_;
47
47
48
    $self->additional_field_values->delete;
48
    $self->additional_field_values->delete;
49
49
50
    my $biblionumber;
50
    my $biblionumber;
51
    my $record;
51
    my $record;
52
    my $record_updated;
52
    my $record_updated;
53
    if ($self->_result->has_column('biblionumber')) {
53
    if ( $self->_result->has_column('biblionumber') ) {
54
        $biblionumber = $self->biblionumber;
54
        $biblionumber = $self->biblionumber;
55
    }
55
    }
56
56
57
    foreach my $additional_field (@$additional_fields) {
57
    foreach my $additional_field (@$additional_fields) {
58
        my $field = Koha::AdditionalFields->find($additional_field->{id});
58
        my $field = Koha::AdditionalFields->find( $additional_field->{id} );
59
        my $value = $additional_field->{value};
59
        my $value = $additional_field->{value};
60
60
61
        if ($biblionumber and $field->marcfield) {
61
        if ( $biblionumber and $field->marcfield ) {
62
            require Koha::Biblios;
62
            require Koha::Biblios;
63
            $record //= Koha::Biblios->find($biblionumber)->metadata->record;
63
            $record //= Koha::Biblios->find($biblionumber)->metadata->record;
64
64
65
            my ($tag, $subfield) = split /\$/, $field->marcfield;
65
            my ( $tag, $subfield ) = split /\$/, $field->marcfield;
66
            my $marc_field = $record->field($tag);
66
            my $marc_field = $record->field($tag);
67
            if ($field->marcfield_mode eq 'get') {
67
            if ( $field->marcfield_mode eq 'get' ) {
68
                $value = $marc_field ? $marc_field->subfield($subfield) : '';
68
                $value = $marc_field ? $marc_field->subfield($subfield) : '';
69
            } elsif ($field->marcfield_mode eq 'set') {
69
            } elsif ( $field->marcfield_mode eq 'set' ) {
70
                if ($marc_field) {
70
                if ($marc_field) {
71
                    $marc_field->update($subfield => $value);
71
                    $marc_field->update( $subfield => $value );
72
                } else {
72
                } else {
73
                    $marc_field = MARC::Field->new($tag, '', '', $subfield => $value);
73
                    $marc_field = MARC::Field->new( $tag, '', '', $subfield => $value );
74
                    $record->append_fields($marc_field);
74
                    $record->append_fields($marc_field);
75
                }
75
                }
76
                $record_updated = 1;
76
                $record_updated = 1;
77
            }
77
            }
78
        }
78
        }
79
79
80
        if (defined $value) {
80
        if ( defined $value ) {
81
            my $field_value = Koha::AdditionalFieldValue->new({
81
            my $field_value = Koha::AdditionalFieldValue->new(
82
                field_id => $additional_field->{id},
82
                {
83
                record_id => $self->id,
83
                    field_id  => $additional_field->{id},
84
                value => $value,
84
                    record_id => $self->id,
85
            })->store;
85
                    value     => $value,
86
                }
87
            )->store;
86
        }
88
        }
87
    }
89
    }
88
90
89
    if ($record_updated) {
91
    if ($record_updated) {
90
        C4::Biblio::ModBiblio($record, $biblionumber);
92
        C4::Biblio::ModBiblio( $record, $biblionumber );
91
    }
93
    }
92
}
94
}
93
95
Lines 193-199 sub additional_field_values { Link Here
193
    my ($self) = @_;
195
    my ($self) = @_;
194
196
195
    my $afv_rs = $self->_result->additional_field_values;
197
    my $afv_rs = $self->_result->additional_field_values;
196
    return Koha::AdditionalFieldValues->_new_from_dbic( $afv_rs );
198
    return Koha::AdditionalFieldValues->_new_from_dbic($afv_rs);
197
}
199
}
198
200
199
=head3 extended_attributes
201
=head3 extended_attributes
Lines 203-209 REST API embed of additional_field_values Link Here
203
=cut
205
=cut
204
206
205
sub extended_attributes {
207
sub extended_attributes {
206
    my ($self, $extended_attributes) = @_;
208
    my ( $self, $extended_attributes ) = @_;
207
209
208
    if ($extended_attributes) {
210
    if ($extended_attributes) {
209
        $self->set_additional_fields($extended_attributes);
211
        $self->set_additional_fields($extended_attributes);
Lines 268-274 sub strings_map { Link Here
268
        );
270
        );
269
    }
271
    }
270
272
271
    my @sorted = sort { $a->{field_id} <=> $b->{field_id} } @{ $strings->{additional_field_values} };
273
    my @sorted    = sort { $a->{field_id} <=> $b->{field_id} } @{ $strings->{additional_field_values} };
272
    my @non_empty = grep { $_->{value_str} ne "" } @sorted;
274
    my @non_empty = grep { $_->{value_str} ne "" } @sorted;
273
    $strings->{additional_field_values} = \@non_empty;
275
    $strings->{additional_field_values} = \@non_empty;
274
276
(-)a/Koha/REST/V1/ERM/Licenses.pm (-50 / +26 lines)
Lines 22-28 use Mojo::Base 'Mojolicious::Controller'; Link Here
22
use Koha::ERM::Licenses;
22
use Koha::ERM::Licenses;
23
23
24
use Scalar::Util qw( blessed );
24
use Scalar::Util qw( blessed );
25
use Try::Tiny qw( catch try );
25
use Try::Tiny    qw( catch try );
26
26
27
=head1 API
27
=head1 API
28
28
Lines 38-45 sub list { Link Here
38
    return try {
38
    return try {
39
        my $licenses = $c->objects->search( Koha::ERM::Licenses->new );
39
        my $licenses = $c->objects->search( Koha::ERM::Licenses->new );
40
        return $c->render( status => 200, openapi => $licenses );
40
        return $c->render( status => 200, openapi => $licenses );
41
    }
41
    } catch {
42
    catch {
43
        $c->unhandled_exception($_);
42
        $c->unhandled_exception($_);
44
    };
43
    };
45
44
Lines 68-75 sub get { Link Here
68
            status  => 200,
67
            status  => 200,
69
            openapi => $license
68
            openapi => $license
70
        );
69
        );
71
    }
70
    } catch {
72
    catch {
73
        $c->unhandled_exception($_);
71
        $c->unhandled_exception($_);
74
    };
72
    };
75
}
73
}
Lines 89-114 sub add { Link Here
89
87
90
                my $body = $c->req->json;
88
                my $body = $c->req->json;
91
89
92
                my $user_roles = delete $body->{user_roles} // [];
90
                my $user_roles          = delete $body->{user_roles}          // [];
93
                my $documents = delete $body->{documents} // [];
91
                my $documents           = delete $body->{documents}           // [];
94
                my $extended_attributes = delete $body->{extended_attributes} // [];
92
                my $extended_attributes = delete $body->{extended_attributes} // [];
95
93
96
                my $license = Koha::ERM::License->new_from_api($body)->store;
94
                my $license = Koha::ERM::License->new_from_api($body)->store;
97
                $license->user_roles($user_roles);
95
                $license->user_roles($user_roles);
98
                $license->documents($documents);
96
                $license->documents($documents);
99
97
100
                my @extended_attributes = map { {'id' => $_->{field_id}, 'value' => $_->{value}} } @{$extended_attributes};
98
                my @extended_attributes =
99
                    map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes};
101
                $license->extended_attributes( \@extended_attributes );
100
                $license->extended_attributes( \@extended_attributes );
102
101
103
                $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id);
102
                $c->res->headers->location( $c->req->url->to_string . '/' . $license->license_id );
104
                return $c->render(
103
                return $c->render(
105
                    status  => 201,
104
                    status  => 201,
106
                    openapi => $license->to_api
105
                    openapi => $license->to_api
107
                );
106
                );
108
            }
107
            }
109
        );
108
        );
110
    }
109
    } catch {
111
    catch {
112
110
113
        my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
111
        my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
114
112
Lines 118-145 sub add { Link Here
118
                    status  => 409,
116
                    status  => 409,
119
                    openapi => { error => $_->error, conflict => $_->duplicate_id }
117
                    openapi => { error => $_->error, conflict => $_->duplicate_id }
120
                );
118
                );
121
            }
119
            } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
122
            elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
123
                return $c->render(
120
                return $c->render(
124
                    status  => 400,
121
                    status  => 400,
125
                    openapi => {
122
                    openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" }
126
                            error => "Given "
127
                            . $to_api_mapping->{ $_->broken_fk }
128
                            . " does not exist"
129
                    }
130
                );
123
                );
131
            }
124
            } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
132
            elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
133
                return $c->render(
125
                return $c->render(
134
                    status  => 400,
126
                    status  => 400,
135
                    openapi => {
127
                    openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" }
136
                            error => "Given "
137
                            . $to_api_mapping->{ $_->parameter }
138
                            . " does not exist"
139
                    }
140
                );
128
                );
141
            }
129
            } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
142
            elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
143
                return $c->render(
130
                return $c->render(
144
                    status  => 413,
131
                    status  => 413,
145
                    openapi => { error => $_->error }
132
                    openapi => { error => $_->error }
Lines 175-224 sub update { Link Here
175
162
176
                my $body = $c->req->json;
163
                my $body = $c->req->json;
177
164
178
                my $user_roles = delete $body->{user_roles} // [];
165
                my $user_roles          = delete $body->{user_roles}          // [];
179
                my $documents = delete $body->{documents} // [];
166
                my $documents           = delete $body->{documents}           // [];
180
                my $extended_attributes = delete $body->{extended_attributes} // [];
167
                my $extended_attributes = delete $body->{extended_attributes} // [];
181
168
182
                $license->set_from_api($body)->store;
169
                $license->set_from_api($body)->store;
183
                $license->user_roles($user_roles);
170
                $license->user_roles($user_roles);
184
                $license->documents($documents);
171
                $license->documents($documents);
185
172
186
                my @extended_attributes = map { {'id' => $_->{field_id}, 'value' => $_->{value}} } @{$extended_attributes};
173
                my @extended_attributes =
174
                    map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes};
187
                $license->extended_attributes( \@extended_attributes );
175
                $license->extended_attributes( \@extended_attributes );
188
176
189
                $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id);
177
                $c->res->headers->location( $c->req->url->to_string . '/' . $license->license_id );
190
                return $c->render(
178
                return $c->render(
191
                    status  => 200,
179
                    status  => 200,
192
                    openapi => $license->to_api
180
                    openapi => $license->to_api
193
                );
181
                );
194
            }
182
            }
195
        );
183
        );
196
    }
184
    } catch {
197
    catch {
198
        my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
185
        my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
199
186
200
        if ( blessed $_ ) {
187
        if ( blessed $_ ) {
201
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
188
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
202
                return $c->render(
189
                return $c->render(
203
                    status  => 400,
190
                    status  => 400,
204
                    openapi => {
191
                    openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" }
205
                            error => "Given "
206
                            . $to_api_mapping->{ $_->broken_fk }
207
                            . " does not exist"
208
                    }
209
                );
192
                );
210
            }
193
            } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
211
            elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
212
                return $c->render(
194
                return $c->render(
213
                    status  => 400,
195
                    status  => 400,
214
                    openapi => {
196
                    openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" }
215
                            error => "Given "
216
                            . $to_api_mapping->{ $_->parameter }
217
                            . " does not exist"
218
                    }
219
                );
197
                );
220
            }
198
            } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
221
            elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
222
                return $c->render(
199
                return $c->render(
223
                    status  => 413,
200
                    status  => 413,
224
                    openapi => { error => $_->error }
201
                    openapi => { error => $_->error }
Lines 228-234 sub update { Link Here
228
205
229
        $c->unhandled_exception($_);
206
        $c->unhandled_exception($_);
230
    };
207
    };
231
};
208
}
232
209
233
=head3 delete
210
=head3 delete
234
211
Lines 251-258 sub delete { Link Here
251
            status  => 204,
228
            status  => 204,
252
            openapi => q{}
229
            openapi => q{}
253
        );
230
        );
254
    }
231
    } catch {
255
    catch {
256
        $c->unhandled_exception($_);
232
        $c->unhandled_exception($_);
257
    };
233
    };
258
}
234
}
(-)a/t/db_dependent/Koha/Object/Mixin/AdditionalFields.t (-29 / +21 lines)
Lines 12-18 use Koha::ERM::License; Link Here
12
use C4::Context;
12
use C4::Context;
13
13
14
my $builder = t::lib::TestBuilder->new;
14
my $builder = t::lib::TestBuilder->new;
15
my $schema = Koha::Database->schema;
15
my $schema  = Koha::Database->schema;
16
16
17
subtest 'set_additional_fields with marcfield_mode = "get"' => sub {
17
subtest 'set_additional_fields with marcfield_mode = "get"' => sub {
18
    plan tests => 1;
18
    plan tests => 1;
Lines 22-30 subtest 'set_additional_fields with marcfield_mode = "get"' => sub { Link Here
22
    my $biblio = $builder->build_sample_biblio();
22
    my $biblio = $builder->build_sample_biblio();
23
    my $record = $biblio->record;
23
    my $record = $biblio->record;
24
    $record->append_fields(
24
    $record->append_fields(
25
        MARC::Field->new('998', '', '', 'Z' => 'some value'),
25
        MARC::Field->new( '998', '', '', 'Z' => 'some value' ),
26
    );
26
    );
27
    $biblio->metadata->metadata($record->as_xml_record(C4::Context->preference('marcflavour')));
27
    $biblio->metadata->metadata( $record->as_xml_record( C4::Context->preference('marcflavour') ) );
28
    $biblio->metadata->store()->discard_changes();
28
    $biblio->metadata->store()->discard_changes();
29
    my $subscription = Koha::Subscription->new(
29
    my $subscription = Koha::Subscription->new(
30
        {
30
        {
Lines 35-43 subtest 'set_additional_fields with marcfield_mode = "get"' => sub { Link Here
35
35
36
    my $field = Koha::AdditionalField->new(
36
    my $field = Koha::AdditionalField->new(
37
        {
37
        {
38
            tablename => 'subscription',
38
            tablename      => 'subscription',
39
            name => random_string('c' x 100),
39
            name           => random_string( 'c' x 100 ),
40
            marcfield => '998$Z',
40
            marcfield      => '998$Z',
41
            marcfield_mode => 'get',
41
            marcfield_mode => 'get',
42
        }
42
        }
43
    );
43
    );
Lines 50-56 subtest 'set_additional_fields with marcfield_mode = "get"' => sub { Link Here
50
50
51
    my $values = $subscription->additional_field_values()->as_list();
51
    my $values = $subscription->additional_field_values()->as_list();
52
52
53
    is($values->[0]->value, 'some value', 'value was copied from the biblio record to the field');
53
    is( $values->[0]->value, 'some value', 'value was copied from the biblio record to the field' );
54
54
55
    $schema->txn_rollback;
55
    $schema->txn_rollback;
56
};
56
};
Lines 60-66 subtest 'set_additional_fields with marcfield_mode = "set"' => sub { Link Here
60
60
61
    $schema->txn_begin;
61
    $schema->txn_begin;
62
62
63
    my $biblio = $builder->build_sample_biblio();
63
    my $biblio       = $builder->build_sample_biblio();
64
    my $subscription = Koha::Subscription->new(
64
    my $subscription = Koha::Subscription->new(
65
        {
65
        {
66
            biblionumber => $biblio->biblionumber,
66
            biblionumber => $biblio->biblionumber,
Lines 70-78 subtest 'set_additional_fields with marcfield_mode = "set"' => sub { Link Here
70
70
71
    my $field = Koha::AdditionalField->new(
71
    my $field = Koha::AdditionalField->new(
72
        {
72
        {
73
            tablename => 'subscription',
73
            tablename      => 'subscription',
74
            name => random_string('c' x 100),
74
            name           => random_string( 'c' x 100 ),
75
            marcfield => '999$Z',
75
            marcfield      => '999$Z',
76
            marcfield_mode => 'set',
76
            marcfield_mode => 'set',
77
        }
77
        }
78
    );
78
    );
Lines 80-93 subtest 'set_additional_fields with marcfield_mode = "set"' => sub { Link Here
80
    $subscription->set_additional_fields(
80
    $subscription->set_additional_fields(
81
        [
81
        [
82
            {
82
            {
83
                id => $field->id,
83
                id    => $field->id,
84
                value => 'some value',
84
                value => 'some value',
85
            },
85
            },
86
        ]
86
        ]
87
    );
87
    );
88
88
89
    my $record = $biblio->record;
89
    my $record = $biblio->record;
90
    is($record->subfield('999', 'Z'), 'some value', 'value was copied from the field to the biblio record');
90
    is( $record->subfield( '999', 'Z' ), 'some value', 'value was copied from the field to the biblio record' );
91
91
92
    $schema->txn_rollback;
92
    $schema->txn_rollback;
93
};
93
};
Lines 107-122 subtest 'get_additional_field_values_for_template' => sub { Link Here
107
107
108
    my $field = Koha::AdditionalField->new(
108
    my $field = Koha::AdditionalField->new(
109
        {
109
        {
110
            tablename  => 'subscription',
110
            tablename => 'subscription',
111
            name       => random_string( 'c' x 100 )
111
            name      => random_string( 'c' x 100 )
112
        }
112
        }
113
    );
113
    );
114
    $field->store()->discard_changes();
114
    $field->store()->discard_changes();
115
115
116
    my $field2 = Koha::AdditionalField->new(
116
    my $field2 = Koha::AdditionalField->new(
117
        {
117
        {
118
            tablename  => 'subscription',
118
            tablename => 'subscription',
119
            name       => random_string( 'c' x 100 )
119
            name      => random_string( 'c' x 100 )
120
        }
120
        }
121
    );
121
    );
122
    $field2->store()->discard_changes();
122
    $field2->store()->discard_changes();
Lines 201-217 subtest 'add_additional_fields' => sub { Link Here
201
201
202
    $subscription->add_additional_fields(
202
    $subscription->add_additional_fields(
203
        {
203
        {
204
            $field2->id => [
204
            $field2->id => ['second field'],
205
                'second field'
206
            ],
207
        },
205
        },
208
        'subscription'
206
        'subscription'
209
    );
207
    );
210
208
211
    my $template_additional_field_values = $subscription->get_additional_field_values_for_template;
209
    my $template_additional_field_values = $subscription->get_additional_field_values_for_template;
212
210
213
214
215
    is_deeply(
211
    is_deeply(
216
        $template_additional_field_values,
212
        $template_additional_field_values,
217
        { $field->id => [ $value . ' 1', $value . ' 2' ], $field2->id => ['second field'] },
213
        { $field->id => [ $value . ' 1', $value . ' 2' ], $field2->id => ['second field'] },
Lines 274-286 subtest 'strings_map() tests' => sub { Link Here
274
        'erm_licenses'
270
        'erm_licenses'
275
    );
271
    );
276
272
277
273
    my $av_category = Koha::AuthorisedValueCategory->new( { category_name => "AV_CAT_NAME" } );
278
    my $av_category      = Koha::AuthorisedValueCategory->new( { category_name => "AV_CAT_NAME" } );
279
    $av_category->store()->discard_changes();
274
    $av_category->store()->discard_changes();
280
275
281
    my $av_value = Koha::AuthorisedValue->new(
276
    my $av_value = Koha::AuthorisedValue->new(
282
        {
277
        {
283
            category => $av_category->category_name,
278
            category         => $av_category->category_name,
284
            authorised_value => 'BOB',
279
            authorised_value => 'BOB',
285
            lib              => "Robert"
280
            lib              => "Robert"
286
        }
281
        }
Lines 295-305 subtest 'strings_map() tests' => sub { Link Here
295
    );
290
    );
296
    $av_field->store()->discard_changes();
291
    $av_field->store()->discard_changes();
297
292
298
299
300
    $license->add_additional_fields(
293
    $license->add_additional_fields(
301
        {
294
        {
302
            $av_field->id => [$av_value->authorised_value],
295
            $av_field->id => [ $av_value->authorised_value ],
303
        },
296
        },
304
        'erm_licenses'
297
        'erm_licenses'
305
    );
298
    );
306
- 

Return to bug 35287