Skip to content

Commit

Permalink
Merge pull request #5 from ricofreak/main
Browse files Browse the repository at this point in the history
Improvements to the bulk item message update/delete feature
  • Loading branch information
ricofreak authored Dec 20, 2024
2 parents 962424f + a0c9480 commit d31e82e
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 128 deletions.
190 changes: 150 additions & 40 deletions Koha/Plugin/Com/ByWaterSolutions/ItemMessages.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -192,7 +193,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
};
}
Expand Down Expand Up @@ -240,40 +242,140 @@ 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;
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');

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 $query = qq{
UPDATE item_messages
SET message = ?

my $fetch_query = qq{
SELECT itemnumber, message, type
FROM item_messages
WHERE type = ?
AND item_message_id IN ( $placeholders )
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 type = ?
AND itemnumber IN ( $placeholders )
};

my $delete_sth = $dbh->prepare($delete_query);
$delete_sth->execute($new_type, @itemnumbers);

my $query = qq{
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;

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.item_message_id IN ($placeholders)
AND im.itemnumber IN ($placeholders)
};

my $select_sth = $dbh->prepare($select_query);
$select_sth->execute($type, @itemnumbers);
$select_sth->execute($new_type, @itemnumbers);

while (my $row = $select_sth->fetchrow_hashref) {
next unless $row;
Expand All @@ -286,7 +388,6 @@ sub tool_step3 {
type => $row->{type},
};
}

$select_sth->finish;

} elsif ( $action eq 'delete' ) {
Expand All @@ -296,41 +397,50 @@ 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,
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},
my $select_query = qq{
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
LEFT JOIN biblio b ON i.biblionumber = b.biblionumber
WHERE im.type = ?
AND i.itemnumber IN ($placeholders)
};
}
$select_sth->finish;

my $select_sth = $dbh->prepare($select_query);
$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},
barcode => $row->{barcode},
title => $row->{title},
old_message => $row->{old_message},
old_type => $row->{old_type},
};
}

$select_sth->finish;

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);
}

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;
Expand Down
9 changes: 9 additions & 0 deletions Koha/Plugin/Com/ByWaterSolutions/ItemMessages/tool-step1.tt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
</li>
</ol>
</fieldset>
<fieldset class="rows">
<legend>Or scan items one by one</legend>
<ol>
<li>
<label for="barcodelist">Barcode list (one barcode per line): </label>
<textarea rows="10" cols="30" id="barcodelist" name="barcodelist"></textarea>
</li>
</ol>
</fieldset>

<fieldset class="action">
<input type="submit" name="submitted" class="btn btn-primary" value="Submit">
Expand Down
Loading

0 comments on commit d31e82e

Please sign in to comment.