From 6b000948bc80455e7703f97b99fce1abf7f652aa Mon Sep 17 00:00:00 2001 From: Jan Kissig Date: Thu, 19 Dec 2024 11:13:00 +0000 Subject: [PATCH] Bug 35380: Make record sources available in overlay rules This patch enhances the filters in record overlay rules when using the "Source" module. The available filter values are now system defined in Administration -> Record sources and can be extended by own record souces. The former hard coded values (f.e. batchmod, intranet) are added to the database and cannot be deleted. Signed-off-by: David Nind --- Koha/REST/V1/RecordSources.pm | 38 +++++++++++++++++++ Koha/RecordSource.pm | 11 ++++++ admin/marc-overlay-rules.pl | 5 +++ api/v1/swagger/definitions/record_source.yaml | 1 - api/v1/swagger/paths/record_sources.yaml | 15 ++++++++ .../en/modules/admin/marc-overlay-rules.tt | 25 +++++++----- 6 files changed, 84 insertions(+), 11 deletions(-) diff --git a/Koha/REST/V1/RecordSources.pm b/Koha/REST/V1/RecordSources.pm index 46ba7dbc6e..b4a91662cb 100644 --- a/Koha/REST/V1/RecordSources.pm +++ b/Koha/REST/V1/RecordSources.pm @@ -81,6 +81,17 @@ sub add { openapi => $c->objects->to_api($source) ); } catch { + if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) { + return $c->render( + status => 409, + openapi => { error => 'Duplicate name.' } + ); + } elsif ( blessed $_ and $_->isa('Koha::Exceptions::BadParameter') ) { + return $c->render( + status => 409, + openapi => { error => 'Name not allowed.' } + ); + } $c->unhandled_exception($_); }; } @@ -97,11 +108,29 @@ sub update { return $c->render_resource_not_found("Record source") unless $source; + if ( $source->is_system ) { + return $c->render( + status => 409, + openapi => { error => 'Cannot edit system defined value.' } + ); + } + return try { $source->set_from_api( $c->req->json )->store; $source->discard_changes; return $c->render( status => 200, openapi => $c->objects->to_api($source) ); } catch { + if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) { + return $c->render( + status => 409, + openapi => { error => 'Duplicate name.' } + ); + } elsif ( blessed $_ and $_->isa('Koha::Exceptions::BadParameter') ) { + return $c->render( + status => 409, + openapi => { error => 'Name not allowed.' } + ); + } $c->unhandled_exception($_); }; } @@ -131,6 +160,15 @@ sub delete { } ); } + elsif ( blessed $_ and $_->isa('Koha::Exceptions::CannotDeleteDefault') ) { + return $c->render( + status => 409, + openapi => { + error => 'Cannot delete system defined record source', + error_code => 'cannot_delete_is_system', + } + ); + } $c->unhandled_exception($_); }; } diff --git a/Koha/RecordSource.pm b/Koha/RecordSource.pm index 4e3da7c90c..de46ce2c42 100644 --- a/Koha/RecordSource.pm +++ b/Koha/RecordSource.pm @@ -46,6 +46,17 @@ sub usage_count { return $self->_result->biblio_metadatas->count(); } +=head3 store + +=cut + +sub store { + my ($self) = @_; + Koha::Exceptions::BadParameter->throw( '* not allowed' ) if $self->name eq '*'; + + return $self->SUPER::store; +} + =head3 delete Overridden delete method to prevent system default deletions diff --git a/admin/marc-overlay-rules.pl b/admin/marc-overlay-rules.pl index bebaa1d6ac..e2b38c3d7a 100755 --- a/admin/marc-overlay-rules.pl +++ b/admin/marc-overlay-rules.pl @@ -26,6 +26,7 @@ use C4::Output qw( output_html_with_http_headers ); use C4::ImportBatch; use Koha::MarcOverlayRules; use Koha::Patron::Categories; +use Koha::RecordSources; my $input = CGI->new; my $op = $input->param('op') || ''; @@ -124,9 +125,13 @@ my $categories = Koha::Patron::Categories->search_with_library_limits( { order_by => ['description'], columns => [ 'categorycode', 'description' ] } )->unblessed; +my @record_sources = + map { $_->{name} } @{ Koha::RecordSources->search->unblessed }; + $template->param( rules => $rules, categories => $categories, + record_sources => \@record_sources, messages => $errors ); diff --git a/api/v1/swagger/definitions/record_source.yaml b/api/v1/swagger/definitions/record_source.yaml index 6c1ff382eb..de52257e9b 100644 --- a/api/v1/swagger/definitions/record_source.yaml +++ b/api/v1/swagger/definitions/record_source.yaml @@ -17,7 +17,6 @@ properties: is_system: description: Is this record source system or not type: boolean - readOnly: true additionalProperties: false required: - name diff --git a/api/v1/swagger/paths/record_sources.yaml b/api/v1/swagger/paths/record_sources.yaml index 732368e2ae..dc197ac9f3 100644 --- a/api/v1/swagger/paths/record_sources.yaml +++ b/api/v1/swagger/paths/record_sources.yaml @@ -97,6 +97,13 @@ description: Not allowed schema: $ref: "../swagger.yaml#/definitions/error" + "409": + description: | + Conflict creating the resource. Possible `error_code` attribute values: + + * `duplicate` + schema: + $ref: "../swagger.yaml#/definitions/error" "500": description: | Internal server error. Possible `error_code` attribute values: @@ -198,6 +205,13 @@ description: Not found schema: $ref: "../swagger.yaml#/definitions/error" + "409": + description: | + Conflict creating the resource. Possible `error_code` attribute values: + + * `duplicate` + schema: + $ref: "../swagger.yaml#/definitions/error" "500": description: | Internal server error. Possible `error_code` attribute values: @@ -248,6 +262,7 @@ Conflict in deleting resource. Possible `error_code` attribute values: * `cannot_delete_used`: record source linked to a record cannot be deleted. + * `cannot_delete_is_system`: record source is system defined and cannot be deleted. schema: $ref: "../swagger.yaml#/definitions/error" "500": diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc-overlay-rules.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc-overlay-rules.tt index 016d892c0b..f84ec6fde3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc-overlay-rules.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc-overlay-rules.tt @@ -497,16 +497,21 @@ categories.unshift({ categorycode: '*', description: '*'}); categories = categories.reduce( ( a,c ) => ( { ...a, [c.categorycode]: c.description } ), {} ); - var module_filter_options = { - source: { - '*': '*', - batchmod: _("Batch record modification"), - intranet: _("Staff interface MARC editor"), - batchimport: _("Staged MARC import"), - z3950: _("Z39.50"), - bulkmarcimport: _("bulkmarcimport.pl"), - import_lexile: _("import_lexile.pl") - }, + var module_filter_options = { + source: { + '*': '*', + [% FOREACH record_source IN record_sources %] + [% SWITCH record_source %] + [% CASE 'batchmod' %]'[% record_source %]': _("Batch record modification"), + [% CASE 'intranet' %]'[% record_source %]':_("Staff interface MARC editor"), + [% CASE 'batchimport' %]'[% record_source %]': _("Staged MARC import"), + [% CASE 'z3950' %]'[% record_source %]': _("Z39.50"), + [% CASE 'bulkmarkimport' %]'[% record_source %]': _("bulkmarcimport.pl"), + [% CASE 'import_lexile' %]'[% record_source %]': _("import_lexile.pl"), + [% CASE %] '[% record_source %]' : '[% record_source | html %]', + [% END %] + [% END %] + }, categorycode: categories, }; -- 2.39.5