From 72b077c9e724ae9fa88888c7d142aae4896ed43a Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Thu, 19 Dec 2024 18:57:49 +0000 Subject: [PATCH 1/3] Add ability to add/update item messages without message or type --- .../Com/ByWaterSolutions/ItemMessages.pm | 95 ++++++-------- .../ItemMessages/tool-step2.tt | 124 ++++++++---------- 2 files changed, 95 insertions(+), 124 deletions(-) diff --git a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm index c1bbc0c..08f0800 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm +++ b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm @@ -192,7 +192,8 @@ sub tool_step2 { itemnumber => $item->itemnumber, barcode => $item->barcode, title => $biblio->title, - messages => [], # Placeholder for item_messages.message + biblio => $biblio, + messages => [], # Placeholder for item_messages.message type => '', # Placeholder for item_messages.type }; } @@ -240,43 +241,64 @@ sub tool_step3 { my @itemnumbers = $cgi->param('itemnumber'); my $action = $cgi->param('action'); my $type = $cgi->param('type'); + my $delete_type = $cgi->param('delete_type'); my $dbh = C4::Context->dbh; my @updated_items; if ( $action eq 'update' ) { my $new_message = $cgi->param('new_message'); + my $new_type = $cgi->param('new_type'); - unless ($type) { - warn "No type provided!"; + unless ($new_type && $new_message ) { + warn "No message or message type provided!"; return; } my $placeholders = join(',', ('?') x scalar(@itemnumbers)); + + my $delete_query = qq{ + DELETE FROM item_messages + WHERE itemnumber IN ( $placeholders ) + }; + + my $delete_sth = $dbh->prepare($delete_query); + $delete_sth->execute(@itemnumbers); + my $query = qq{ - UPDATE item_messages - SET message = ? - WHERE type = ? - AND item_message_id IN ( $placeholders ) + INSERT INTO item_messages (itemnumber, message, type) + VALUES (?, ?, ?) }; my $sth = $dbh->prepare($query); - $sth->execute($new_message, $type, @itemnumbers); + foreach my $itemnumber (@itemnumbers) { + $sth->execute($itemnumber, $new_message, $new_type); + } + $sth->finish; + + } elsif ( $action eq 'delete' ) { + unless (@itemnumbers) { + warn "No itemnumbers provided!"; + return; + } + + my $placeholders = join(',', ('?') x scalar(@itemnumbers)); my $select_query = qq{ - SELECT im.item_message_id, im.itemnumber, im.message AS message, im.type, + SELECT im.item_message_id, im.itemnumber, im.message AS old_message, im.type, i.barcode, b.title FROM item_messages im JOIN items i ON im.itemnumber = i.itemnumber LEFT JOIN biblio b ON i.biblionumber = b.biblionumber WHERE im.type = ? - AND im.item_message_id IN ($placeholders) + AND i.itemnumber IN ($placeholders) }; my $select_sth = $dbh->prepare($select_query); - $select_sth->execute($type, @itemnumbers); + $select_sth->execute($delete_type, @itemnumbers); while (my $row = $select_sth->fetchrow_hashref) { next unless $row; + push @updated_items, { item_message_id => $row->{item_message_id}, itemnumber => $row->{itemnumber}, @@ -288,58 +310,23 @@ sub tool_step3 { } $select_sth->finish; - - } elsif ( $action eq 'delete' ) { - unless (@itemnumbers) { - warn "No itemnumbers provided!"; - return; - } - - my $placeholders = join(',', ('?') x scalar(@itemnumbers)); - - my $select_query = qq{ - SELECT im.item_message_id, im.itemnumber, im.message AS old_message, im.type, - i.barcode, b.title - FROM item_messages im - JOIN items i ON im.itemnumber = i.itemnumber - LEFT JOIN biblio b ON i.biblionumber = b.biblionumber - WHERE im.type = ? - AND im.item_message_id IN ($placeholders) - }; - my $select_sth = $dbh->prepare($select_query); - $select_sth->execute($type, @itemnumbers); - - while (my $row = $select_sth->fetchrow_hashref) { - next unless $row; - - push @updated_items, { - item_message_id => $row->{item_message_id}, - itemnumber => $row->{itemnumber}, - barcode => $row->{barcode}, - title => $row->{title}, - message => $row->{message}, - type => $row->{type}, - }; - } - $select_sth->finish; + my $count = scalar @updated_items; my $query = qq{ DELETE FROM item_messages WHERE type = ? - AND item_message_id IN ( $placeholders ) + AND itemnumber IN ( $placeholders ) }; my $sth = $dbh->prepare($query); - $sth->execute($type, @itemnumbers); + $sth->execute($delete_type, @itemnumbers); + $template->param( + action => $action, + updated_count => $count, + updated_items => \@updated_items, + ); } - my $count = scalar @updated_items; - $template->param( - action => $action, - updated_count => $count, - updated_items => \@updated_items, - ); - $self->output_html( $template->output() ); } diff --git a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt index 2d18ed9..99cbbf0 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt +++ b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt @@ -53,8 +53,9 @@ [% FOREACH item IN scanned_items %] [% IF item.messages.size > 0 %] [% FOREACH message IN item.messages %] + [% item.biblionumber %] - [% item.title | html %] + [% INCLUDE 'biblio-title.inc' biblio=item.biblio link=1 %] [% item.barcode | html %] [% message.message | html %] [% message.type | html %] @@ -73,50 +74,38 @@
-

Bulk update item messages by type:

- -
- [% FOREACH av IN AuthorisedValues.Get('ITEM_MESSAGE_TYPE' ) %] - [% SET type_options = av.lib_opac.split('\|') %] -
-
- [% INCLUDE 'csrf-token.inc' %] - - - [% FOREACH item IN scanned_items %] - [% FOREACH message IN item.messages %] - - [% END %] - [% END %] -

Actions for the item message type: [% av.authorised_value | html %]

-
- - - [% IF type_options.size > 1 %] - - [% ELSE %] - - [% END %] - -
-
- - -
- -
-
- [% END %] +

Bulk update ALL uploaded/scanned item messages:

+
+ [% INCLUDE 'csrf-token.inc' %] + + + [% FOREACH item IN scanned_items %] + + [% END %] +
+ + + + + +
+
+ + + +
+ +
@@ -125,6 +114,26 @@ From 1c2ed3db0410f072ef22787e2456d29131391a68 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Thu, 19 Dec 2024 22:41:28 +0000 Subject: [PATCH 2/3] Update to templates and add reporting on old messages/old types --- .../Com/ByWaterSolutions/ItemMessages.pm | 149 ++++++++++++++++-- .../ItemMessages/tool-step1.tt | 9 ++ .../ItemMessages/tool-step2.tt | 143 ++++++++++++----- .../ItemMessages/tool-step3.tt | 16 +- 4 files changed, 261 insertions(+), 56 deletions(-) diff --git a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm index 08f0800..37f85eb 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm +++ b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm @@ -154,6 +154,7 @@ sub tool_step2 { my $template = $self->get_template({ file => 'tool-step2.tt' }); my $uploadbarcodes = $cgi->upload('uploadbarcodes'); + my $barcodelist = $cgi->param('barcodelist'); my @barcodes; my @uploadedbarcodes; @@ -165,7 +166,7 @@ sub tool_step2 { push @uploadedbarcodes, grep { /\S/ } split( /[$split_chars]/, $barcode ); } } else { - warn 'NO UPLOADED BARCODES'; + push @uploadedbarcodes, split(/\s\n/, scalar $cgi->param('barcodelist') ); } for my $barcode (@uploadedbarcodes) { @@ -244,8 +245,76 @@ sub tool_step3 { my $delete_type = $cgi->param('delete_type'); my $dbh = C4::Context->dbh; my @updated_items; + my @old_messages; - if ( $action eq 'update' ) { + if ( $action eq 'update_all') { + my $new_message = $cgi->param('new_message_all'); + my $new_type = $cgi->param('new_type_all'); + + my $placeholders = join(',', ('?') x scalar(@itemnumbers)); + + my $fetch_query = qq{ + SELECT itemnumber, message, type + FROM item_messages + WHERE itemnumber IN ($placeholders) + }; + my $fetch_sth = $dbh->prepare($fetch_query); + $fetch_sth->execute(@itemnumbers); + + while (my $row = $fetch_sth->fetchrow_hashref) { + push @old_messages, { + itemnumber => $row->{itemnumber}, + old_message => $row->{message}, + old_type => $row->{type}, + }; + } + $fetch_sth->finish; + + my $delete_query = qq{ + DELETE FROM item_messages + WHERE itemnumber IN ( $placeholders ) + }; + + my $delete_sth = $dbh->prepare($delete_query); + $delete_sth->execute(@itemnumbers); + + my $query = qq{ + INSERT INTO item_messages (itemnumber, message, type) + VALUES (?, ?, ?) + }; + + my $sth = $dbh->prepare($query); + foreach my $itemnumber (@itemnumbers) { + $sth->execute($itemnumber, $new_message, $new_type); + } + + $sth->finish; + + my $select_query = qq{ + SELECT im.item_message_id, im.itemnumber, im.message AS message, im.type, + i.barcode, b.title + FROM item_messages im + JOIN items i ON im.itemnumber = i.itemnumber + LEFT JOIN biblio b ON i.biblionumber = b.biblionumber + WHERE im.itemnumber IN ($placeholders) + }; + + my $select_sth = $dbh->prepare($select_query); + $select_sth->execute(@itemnumbers); + + while (my $row = $select_sth->fetchrow_hashref) { + next unless $row; + push @updated_items, { + item_message_id => $row->{item_message_id}, + itemnumber => $row->{itemnumber}, + barcode => $row->{barcode}, + title => $row->{title}, + message => $row->{message}, + type => $row->{type}, + }; + } + $select_sth->finish; + } elsif ( $action eq 'update' ) { my $new_message = $cgi->param('new_message'); my $new_type = $cgi->param('new_type'); @@ -256,13 +325,32 @@ sub tool_step3 { my $placeholders = join(',', ('?') x scalar(@itemnumbers)); + my $fetch_query = qq{ + SELECT itemnumber, message, type + FROM item_messages + WHERE type = ? + AND itemnumber IN ($placeholders) + }; + my $fetch_sth = $dbh->prepare($fetch_query); + $fetch_sth->execute($new_type, @itemnumbers); + + while (my $row = $fetch_sth->fetchrow_hashref) { + push @old_messages, { + itemnumber => $row->{itemnumber}, + old_message => $row->{message}, + old_type => $row->{type}, + }; + } + $fetch_sth->finish; + my $delete_query = qq{ DELETE FROM item_messages - WHERE itemnumber IN ( $placeholders ) + WHERE type = ? + AND itemnumber IN ( $placeholders ) }; my $delete_sth = $dbh->prepare($delete_query); - $delete_sth->execute(@itemnumbers); + $delete_sth->execute($new_type, @itemnumbers); my $query = qq{ INSERT INTO item_messages (itemnumber, message, type) @@ -276,6 +364,32 @@ sub tool_step3 { $sth->finish; + my $select_query = qq{ + SELECT im.item_message_id, im.itemnumber, im.message AS message, im.type, + i.barcode, b.title + FROM item_messages im + JOIN items i ON im.itemnumber = i.itemnumber + LEFT JOIN biblio b ON i.biblionumber = b.biblionumber + WHERE im.type = ? + AND im.itemnumber IN ($placeholders) + }; + + my $select_sth = $dbh->prepare($select_query); + $select_sth->execute($new_type, @itemnumbers); + + while (my $row = $select_sth->fetchrow_hashref) { + next unless $row; + push @updated_items, { + item_message_id => $row->{item_message_id}, + itemnumber => $row->{itemnumber}, + barcode => $row->{barcode}, + title => $row->{title}, + message => $row->{message}, + type => $row->{type}, + }; + } + $select_sth->finish; + } elsif ( $action eq 'delete' ) { unless (@itemnumbers) { warn "No itemnumbers provided!"; @@ -284,7 +398,7 @@ sub tool_step3 { my $placeholders = join(',', ('?') x scalar(@itemnumbers)); my $select_query = qq{ - SELECT im.item_message_id, im.itemnumber, im.message AS old_message, im.type, + SELECT im.item_message_id, im.itemnumber, im.message AS old_message, im.type AS old_type, i.barcode, b.title FROM item_messages im JOIN items i ON im.itemnumber = i.itemnumber @@ -304,13 +418,12 @@ sub tool_step3 { itemnumber => $row->{itemnumber}, barcode => $row->{barcode}, title => $row->{title}, - message => $row->{message}, - type => $row->{type}, + old_message => $row->{old_message}, + old_type => $row->{old_type}, }; } $select_sth->finish; - my $count = scalar @updated_items; my $query = qq{ DELETE FROM item_messages @@ -320,13 +433,23 @@ sub tool_step3 { my $sth = $dbh->prepare($query); $sth->execute($delete_type, @itemnumbers); - $template->param( - action => $action, - updated_count => $count, - updated_items => \@updated_items, - ); } + if ( $action eq 'update' || $action eq 'update_all' ) { + foreach my $updated_item (@updated_items) { + my ($old_data) = grep { $_->{itemnumber} == $updated_item->{itemnumber} } @old_messages; + $updated_item->{old_message} = $old_data->{old_message} // 'No Previous Message'; + $updated_item->{old_type} = $old_data->{old_type} // 'No Previous Type'; + } + } + + my $count = scalar @updated_items; + $template->param( + action => $action, + updated_count => $count, + updated_items => \@updated_items, + ); + $self->output_html( $template->output() ); } diff --git a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step1.tt b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step1.tt index f6ea166..7f2b7cf 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step1.tt +++ b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step1.tt @@ -53,6 +53,15 @@ +
+ Or scan items one by one +
    +
  1. + + +
  2. +
+
diff --git a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt index 99cbbf0..5fc6334 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt +++ b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt @@ -63,7 +63,7 @@ [% END %] [% ELSE %] - [% item.title | html %] + [% INCLUDE 'biblio-title.inc' biblio=item.biblio link=1 %] [% item.barcode | html %] @@ -73,8 +73,8 @@ -
-

Bulk update ALL uploaded/scanned item messages:

+
+

Choose an action:

[% INCLUDE 'csrf-token.inc' %] @@ -82,33 +82,47 @@ [% FOREACH item IN scanned_items %] [% END %] -
- - - - - -
-
- - - -
+
+
+ + + + + +
This will REPLACE all scanned item's messages and types regardless of current item message or type.
+
+
+ + + + + +
+
+ + + +
+
-
@@ -131,10 +145,29 @@ } else { html = ''; } - console.log(html); $('#new_message').replaceWith(html); }); + $('#new_type_all').on('change',function() { + let new_type = $(this).val(); + let new_options = ''; + [% FOREACH av IN AuthorisedValues.Get('ITEM_MESSAGE_TYPE' ) %] + [% SET type_options = av.lib_opac.split('\|') %] + if ( "[% av.authorised_value %]" == new_type ) { + [% FOREACH option_type IN type_options %] + new_options = new_options + '' + [% END %] + } + [% END %] + let html = ''; + if ( new_options ) { + html = ''; + } else { + html = ''; + } + $('#new_message_all').replaceWith(html); + }); + $('form').on('submit', function (e) { const action = $(this).find('input[name="action"]:checked').val(); if ( !action ) { @@ -142,24 +175,56 @@ e.preventDefault(); // Prevent form submission return; } + + if (action == 'update_all') { + let new_type = $('#new_type_all[name="new_type_all"]').val(); + let new_message = $('#new_message_all[name="new_message_all"]').val(); + if ( !new_type || !new_message ) { + alert('Please specify a message and an item message type for updating.'); + e.preventDefault(); // Prevent form submission + return; + } + } + if (action == 'update') { - let multivalue = $(this).find('select[name="new_message"]').val(); - let singlevalue = $(this).find('input[name="new_message"]').val(); - if ( !multivalue && !singlevalue ) { - alert('Please specify a message for updating.'); + let new_type = $('#new_type[name="new_type"]').val(); + let new_message = $('#new_message[name="new_message"]').val(); + if ( !new_type || !new_message ) { + alert('Please specify a message and an item message type for updating.'); e.preventDefault(); // Prevent form submission return; } } - let current_type = $('.nav-item.active').data('type'); - let current_count = 0; - $('table tbody tr').each( function() { - let row_type = $(this).attr('class'); - if ( current_type == row_type ) { - current_count++; + + if (action == 'delete') { + let delete_type = $('#delete_type[name="delete_type"]').val(); + if ( !delete_type ) { + alert('Please specify an item message type for deleting.'); + e.preventDefault(); // Prevent form submission + return; } - }); - if ( !confirm(`Are you sure you want to ${action} ${current_count} item messages?`) ) { + } + + let count = 0; + if ( action == 'delete' || action == 'update' ) { + $('table tbody tr').each( function() { + let row_type = $(this).attr('class'); + let this_type = $('#new_type[name="new_type"]').val() || $('#delete_type[name="delete_type"]').val(); + if ( row_type == this_type ) { + count++; + } + }); + + if ( count == 0 ) { + alert('Nothing to update or delete!'); + e.preventDefault(); // Prevent form submission + return; + } + } else { + count = $('table tbody tr').length; + } + + if ( !confirm(`Are you sure you want to ${action} ${count} item messages?`) ) { e.preventDefault(); return; } diff --git a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step3.tt b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step3.tt index 3d5c59b..9786187 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step3.tt +++ b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step3.tt @@ -35,7 +35,7 @@ [% END #/ WRAPPER sub-header.inc %]
- [% IF action == 'update' %] + [% IF action == 'update' || action == 'update_all' %]

[% updated_count %] item messages were successfully updated!

[% ELSE %]

[% updated_count %] item messages were successfully deleted!

@@ -45,8 +45,10 @@ Title Barcode + Old message New message - Type + Old type + New type @@ -54,12 +56,18 @@ [% item.title | html %] [% item.barcode | html %] - [% IF action == 'update' %] + [% item.old_message | html %] + [% IF action == 'update' || action == 'update_all' %] [% item.message | html %] [% ELSE %] deleted [% END %] - [% item.type | html %] + [% item.old_type | html %] + [% IF action == 'update' || action == 'update_all' %] + [% item.type | html %] + [% ELSE %] + deleted + [% END %] [% END %] From a0c94804a40343d0f0f626540195368d0a795293 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Thu, 19 Dec 2024 23:57:06 +0000 Subject: [PATCH 3/3] UI improvements --- .../ItemMessages/tool-step2.tt | 120 ++++++++++++------ 1 file changed, 78 insertions(+), 42 deletions(-) diff --git a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt index 5fc6334..3fc1b67 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt +++ b/Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt @@ -13,11 +13,18 @@ [% t("Koha") | html %] [% END %] [% INCLUDE 'doc-head-close.inc' %] + + [% INCLUDE 'header.inc' %] [% PROCESS 'about-team.inc' %] + [% WRAPPER 'sub-header.inc' %] [% WRAPPER breadcrumbs %] [% IF blocking_error %] @@ -35,7 +42,7 @@ [% END #/ WRAPPER breadcrumbs %] [% END #/ WRAPPER sub-header.inc %] -
+

Update item messages

Scanned items:

@@ -73,59 +80,83 @@
-
-

Choose an action:

-
- [% INCLUDE 'csrf-token.inc' %] - - - [% FOREACH item IN scanned_items %] - - [% END %] -
-
+
+

Choose an action:

+ + [% INCLUDE 'csrf-token.inc' %] + + + [% FOREACH item IN scanned_items %] + + [% END %] +
+
+
    +
  1. - + Replace all +
  2. +
  3. + - +
  4. +
  5. +
    This will REPLACE all scanned item's messages and types regardless of current item message or type.
    -
-
- - - - - -
-
- - - -
-
+ + +
+
+
+
+
    +
  1. + + Replace all with type of: + +
  2. +
  3. + + +
  4. +
+
+
+
+
+
    +
  1. + + Delete all with type of: + +
  2. +
+
+
+
- -
+
+

- +