From ff05c249f52a215e2928b83752986d24aa845b8d Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 16 Oct 2023 16:14:53 +0000 Subject: [PATCH] Bug 35044: Update: Serial subscriptions batch edit Test plan, k-t-d: Preparation: Create additional fields for table 'subscription', visit: /cgi-bin/koha/admin/additional-fields.pl?tablename=subscription 2 text fields, one repeatable, one not-repeatable 2 AV fields, one repeatable, one not-repeatable 2 MARC fields, one 'get' and one 'set', both non-repeatable, MARC field 942$c Attempt to create a repeatable MARC field (get or set). Notice you're unable to. 1) Add a new serial subscription, visit: /cgi-bin/koha/serials/subscription-add.pl 2) Set the mandatory "Record" input (e.g. '112'). Click the 'Next' and press 'Ok' on the alert box. 3) Fill in all required fields and press "Test prediction pattern" 4) At the bottom, fill in all additional fields, click the '+New' and 'Clear' links, hit 'Save' 5) Notice the fields are shown, repeated fields are comma separated 6) Repeat steps 1-4 to create a second subscription 7) Visit serials home and hit "Search": /cgi-bin/koha/serials/serials-home.pl 8) Check the checkboxes next to the 2 subscriptions and click "Edit selected serials" 9) Input some values in the additional fields section, click the '+New' and 'Clear' links, hit 'Save' 10) Verify that both subscriptions now have the new values form the batch edit. --- Koha/Object/Mixin/AdditionalFields.pm | 47 +++++++++++++++++++ .../en/includes/additional-fields-entry.inc | 12 ++++- .../modules/serials/subscription-batchedit.tt | 19 ++------ serials/subscription-batchedit.pl | 42 ++++++----------- 4 files changed, 76 insertions(+), 44 deletions(-) diff --git a/Koha/Object/Mixin/AdditionalFields.pm b/Koha/Object/Mixin/AdditionalFields.pm index 77aff5bbc4..9f7a39728b 100644 --- a/Koha/Object/Mixin/AdditionalFields.pm +++ b/Koha/Object/Mixin/AdditionalFields.pm @@ -91,6 +91,53 @@ sub set_additional_fields { } } +=head3 add_additional_fields + +Similar to set_additional_fields, but instead of overwriting existing fields, only adds new ones + + $foo->add_additional_fields( + { + '2' => [ + 'first value for field 2', + 'second value for field 2' + ], + '1' => ['first value for field 1'] + }, + 'subscription' + ); + +=cut + +sub add_additional_fields { + my ( $self, $new_additional_fields, $tablename ) = @_; + + my @additional_fields; + + my $table_fields = Koha::AdditionalFields->search( { tablename => $tablename } ); + while ( my $field = $table_fields->next ) { + my $new_additional_field_values = $new_additional_fields->{ $field->id }; + + if ( $new_additional_field_values && scalar @{$new_additional_field_values} ) { + foreach my $value ( @{$new_additional_field_values} ) { + push @additional_fields, { + id => $field->id, + value => $value, + } if $value; + } + } else { + my $existing_additional_field_values = $self->additional_field_values->search( { field_id => $field->id } ); + while ( my $existing = $existing_additional_field_values->next ) { + push @additional_fields, { + id => $field->id, + value => $existing->value, + } if $existing && $existing->value; + } + } + } + + $self->set_additional_fields( \@additional_fields ); +} + =head3 get_additional_field_values_for_template Returns additional field values in the format expected by the .tt file diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc index dd3db99683..4c42e905d3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc @@ -20,7 +20,11 @@ [% END %] [% END %] [% IF !field.repeatable %] - + [% IF batch_edit %] + + [% ELSE %] + + [% END %] [% END %] [% IF authorised_value_category == 'branches' %] [% FOREACH branch IN Branches.all() %] @@ -108,7 +112,11 @@ [% IF !text_field_value_rendered %]
  • - + [% IF batch_edit %] + + [% ELSE %] + + [% END %] [% UNLESS search_form == 1 %] Clear [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-batchedit.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-batchedit.tt index 1f9050e60f..5b9e6ba3c4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-batchedit.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-batchedit.tt @@ -1,4 +1,5 @@ [% USE raw %] +[% USE Asset %] [% USE AuthorisedValues %] [% USE Branches %] [% USE ItemTypes %] @@ -141,20 +142,8 @@
  • - [% FOREACH field IN additional_fields %] -
  • - - [% IF field.authorised_value_category %] - - [% ELSE %] - - [% END %] -
  • + [% IF available_additional_fields.count %] + [% INCLUDE 'additional-fields-entry.inc' available=available_additional_fields values=additional_field_values batch_edit=1 %] [% END %] @@ -175,5 +164,5 @@ - + [% Asset.js("js/additional-fields-entry.js") | $raw %] [% INCLUDE 'intranet-bottom.inc' %] diff --git a/serials/subscription-batchedit.pl b/serials/subscription-batchedit.pl index 274bcd4f23..ffdf6d9b8b 100755 --- a/serials/subscription-batchedit.pl +++ b/serials/subscription-batchedit.pl @@ -49,7 +49,7 @@ foreach my $subscriptionid (@subscriptionids) { push @subscriptions, $subscription if $subscription; } -my @available_additional_fields = Koha::AdditionalFields->search( { tablename => 'subscription' } )->as_list; +my $available_additional_fields = Koha::AdditionalFields->search( { tablename => 'subscription' } ); my $batchedit = $cgi->param('batchedit'); if ($batchedit) { @@ -65,9 +65,15 @@ if ($batchedit) { ); my $field_values = {}; - foreach my $field (@available_additional_fields) { - my $value = $cgi->param( 'field_' . $field->id ); - $field_values->{ $field->id } = $value; + + while ( my $available_field = $available_additional_fields->next ) { + my @submitted_fields = $cgi->param( 'additional_field_' . $available_field->id ); + + my @submitted_fields_array; + foreach my $submitted_field (@submitted_fields) { + push @submitted_fields_array, $submitted_field if $submitted_field; + } + $field_values->{ $available_field->id } = \@submitted_fields_array; } foreach my $subscription (@subscriptions) { @@ -80,25 +86,7 @@ if ($batchedit) { } } - my @additional_field_values; - foreach my $field (@available_additional_fields) { - my $value = $field_values->{ $field->id }; - if ( defined $value and $value ne '' ) { - push @additional_field_values, { - id => $field->id, - value => $value, - }; - } else { - my $existing = $subscription->additional_field_values->search( { field_id => $field->id } )->last; - if ( $existing && $existing->value ) { - push @additional_field_values, { - id => $field->id, - value => $existing->value, - }; - } - } - } - $subscription->set_additional_fields( \@additional_field_values ); + $subscription->add_additional_fields($field_values, 'subscription'); $subscription->store; } @@ -109,10 +97,10 @@ if ($batchedit) { } $template->param( - subscriptions => \@subscriptions, - booksellers => [ Koha::Acquisition::Booksellers->search->as_list ], - additional_fields => \@available_additional_fields, - referrer => scalar $cgi->param('referrer'), + subscriptions => \@subscriptions, + booksellers => [ Koha::Acquisition::Booksellers->search->as_list ], + available_additional_fields => Koha::AdditionalFields->search( { tablename => 'subscription' } ), + referrer => scalar $cgi->param('referrer'), ); output_html_with_http_headers $cgi, $cookie, $template->output; -- 2.30.2