Skip to content

Commit

Permalink
Add some JS checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ricofreak committed Dec 18, 2024
1 parent a716a73 commit 000702e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ sub tool {
my ( $self, $args ) = @_;

my $cgi = $self->{'cgi'};
if ( $cgi->param('submitted2') || $cgi->param('submitted3') ) {
if ( $cgi->param('update') || $cgi->param('delete') ) {
$self->tool_step3();
}
elsif ( $cgi->param('submitted') ) {
Expand Down Expand Up @@ -238,12 +238,12 @@ sub tool_step3 {
my $template = $self->get_template({ file => 'tool-step3.tt' });

my @itemnumbers = $cgi->param('itemnumber');
my $action = $cgi->param('submitted2');
my $action = $cgi->param('action');
my $type = $cgi->param('type');
my $dbh = C4::Context->dbh;
my @updated_items;

if ( $cgi->param('submitted2') ) {
if ( $action eq 'update' ) {
my $new_message = $cgi->param('new_message');

unless ($type) {
Expand Down Expand Up @@ -289,7 +289,7 @@ sub tool_step3 {

$select_sth->finish;

} elsif ( $cgi->param('submitted3') ) {
} elsif ( $action eq 'delete' ) {
unless (@itemnumbers) {
warn "No itemnumbers provided!";
return;
Expand Down
80 changes: 69 additions & 11 deletions Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step2.tt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<div id="doc3">
<h1>Update item messages</h1>
<div class="page-section">
<h3>Scanned items:</h3>
[% INCLUDE 'csrf-token.inc' %]
<table>
<thead>
Expand All @@ -52,15 +53,15 @@
[% FOREACH item IN scanned_items %]
[% IF item.messages.size > 0 %]
[% FOREACH message IN item.messages %]
<tr>
<tr class="[% message.type | html %]">
<td>[% item.title | html %]</td>
<td>[% item.barcode | html %]</td>
<td>[% message.message | html %]</td>
<td><span class="badge text-bg-success">[% message.type | html %]</span></td>
</tr>
[% END %]
[% ELSE %]
<tr>
<tr class="notype nomessage">
<td>[% item.title | html %]</td>
<td>[% item.barcode | html %]</td>
<td></td>
Expand All @@ -75,7 +76,7 @@
<h3>Bulk update item messages by type:</h3>
<ul class="nav nav-tabs" role="tablist">
[% FOREACH av IN AuthorisedValues.Get('ITEM_MESSAGE_TYPE' ) %]
<li class="nav-item" role="presentation">
<li class="nav-item[% IF loop.count == 1 %] active[% END %]" role="presentation" data-type="[% av.authorised_value | html %]">
<a id="[% av.authorised_value | html %]-tab" class="nav-link[% IF loop.count == 1 %] active[% END %]" href="#[% av.authorised_value | html %]_panel" data-bs-toggle="tab" dat-bs-target="#[av.authorised_value | html]" role="tab" data-toggle="tab">[% av.authorised_value | html %]</a>
</li>
[% END %]
Expand All @@ -95,7 +96,8 @@
[% END %]
<h3>Actions for the item message type: [% av.authorised_value | html %]</h3>
<fieldset class="action">
<label for="new_message_[% av.authorised_value | html %]">Update ALL item messages of the type [% av.authorised_value | html %] to:</label>
<input type="radio" name="action" value="update">
<label for="new_message_[% av.authorised_value | html %]">Update ALL item messages of the type <span class="badge text-bg-success">[% av.authorised_value | html %]</span> to:</label>
[% IF type_options.size > 1 %]
<select id="new_message_[% av.authorised_value | html %]" name="new_message">
[% FOREACH option_type IN type_options %]
Expand All @@ -106,16 +108,12 @@
<input id="new_message_[% av.authorised_value | html %]" name="new_message" type="text" />
[% END %]
<input type="hidden" name="type" value="[% av.authorised_value | html %]">
<button type="submit" name="submitted2" value="update_type" class="btn btn-primary">Submit</button>
</fieldset>
<fieldset class="action">
<label for="delete_message">Delete selected item messages</label>
<select id="select_delete" name="select_delete">
<option value="0">No, don't delete.</option>
<option value="1">Yes, delete.</option>
</select>
<input type="submit" name="submitted3" class="btn btn-primary" value="Submit">
<input type="radio" name="action" value="delete">
<label for="delete_message">Delete all messages of the type <span class="badge text-bg-success">[% av.authorised_value | html %]</span>.</label>
</fieldset>
<button type="submit" name="update" value="update_type" class="btn btn-primary">Submit</button>
</form>
</div>
[% END %]
Expand All @@ -124,6 +122,66 @@
</div>
</div>
<br/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function () {

$('form').on('submit', function (e) {
const action = $(this).find('input[name="action"]:checked').val();
if ( !action ) {
alert('Please select an action (update or delete) before submitting.');
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.');
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 ( current_count == 0 ) {
alert('Please select at least one item message to update or delete.');
e.preventDefault();
return;
}
if ( !confirm(`Are you sure you want to ${action} ${current_count} item messages?`) ) {
e.preventDefault();
return;
}
});

let first_tab_type = $('.nav-item.active').data('type');
$('table tbody tr').each( function() {
let row_type = $(this).attr('class');
if ( row_type == first_tab_type ) {
$(this).find('td').css('color' , 'red');
} else {
$(this).find('td').css('color' , 'unset');
}
});
$('.nav-item').on('click' , function() {
let tab_type = $(this).data('type');
$('table tbody tr').each( function() {
let row_type = $(this).attr('class');
if ( row_type == tab_type ) {
$(this).find('td').css('color' , 'red');
} else {
$(this).find('td').css('color' , 'unset');
}
});
});
});
</script>

[% INCLUDE 'intranet-bottom.inc' %]
5 changes: 3 additions & 2 deletions Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step3.tt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
[% END #/ WRAPPER sub-header.inc %]

<div id="doc3" class="page-section">
[% IF action == 'update_type' %]
[% IF action == 'update' %]
<h3>[% updated_count %] item messages were successfully updated!</h3>
[% ELSE %]
<h3>[% updated_count %] item messages were successfully deleted!</h3>
Expand All @@ -54,7 +54,7 @@
<tr>
<td>[% item.title | html %]</td>
<td>[% item.barcode | html %]</td>
[% IF action == 'update_type' %]
[% IF action == 'update' %]
<td>[% item.message | html %]</td>
[% ELSE %]
<td>deleted</td>
Expand All @@ -64,6 +64,7 @@
[% END %]
</tbody>
</table>
<a class="btn btn-primary" href="/cgi-bin/koha/plugins/run.pl?class=Koha::Plugin::Com::ByWaterSolutions::ItemMessages&method=tool">Return to tool</a>
</div>
<br/>

Expand Down

0 comments on commit 000702e

Please sign in to comment.