Bug 36498: Tidy cataloguing/additem.pl

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Kyle Hall 2024-05-17 10:29:39 +00:00 committed by Katrin Fischer
parent 49c55a03e7
commit 0877dfd16d
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834

View file

@ -69,8 +69,7 @@ sub add_item_to_item_group {
)->store();
$item_group_id = $item_group->id;
}
else {
} else {
$item_group_id = $item_group;
}
@ -104,7 +103,9 @@ sub get_item_from_cookie {
};
if ($@) {
$lastitemcookie ||= 'undef';
warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '".encode_base64url($lastitemcookie)."'. Caught error follows: '$@'";
warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '"
. encode_base64url($lastitemcookie)
. "'. Caught error follows: '$@'";
}
}
return $item_from_cookie;
@ -129,6 +130,7 @@ my $op = $input->param('op') || q{};
my $hostitemnumber = $input->param('hostitemnumber');
my $marcflavour = C4::Context->preference("marcflavour");
my $searchid = $input->param('searchid');
# fast cataloguing datas
my $fa_circborrowernumber = $input->param('circborrowernumber');
my $fa_barcode = $input->param('barcode');
@ -151,12 +153,14 @@ if (not defined $userflags) {
$userflags = ( $frameworkcode eq 'FA' ) ? "fast_cataloging" : "edit_items";
}
my ($template, $loggedinuser, $cookie)
= get_template_and_user({template_name => "cataloguing/additem.tt",
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "cataloguing/additem.tt",
query => $input,
type => "intranet",
flagsrequired => { editcatalogue => $userflags },
});
}
);
if ( $op eq 'edititem' || $op eq 'dupeitem' ) {
my $item = Koha::Items->find($itemnumber);
@ -177,16 +181,23 @@ if ( $item && !$patron->can_edit_items_from( $item->homebranch ) ) {
my $uid = $patron->userid;
my $restrictededition = $uid ? haspermission( $uid, { 'editcatalogue' => 'edit_items_restricted' } ) : undef;
# In case user is a superlibrarian, editing is not restricted
$restrictededition = 0 if ( $restrictededition != 0 && C4::Context->IsSuperLibrarian() );
# In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
$restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'}));
$restrictededition = 0
if ( $restrictededition != 0
&& $frameworkcode eq 'FA'
&& haspermission( $uid, { 'editcatalogue' => 'fast_cataloging' } ) );
our $tagslib = &GetMarcStructure( 1, $frameworkcode );
my $record = $biblio->metadata->record;
output_and_exit_if_error( $input, $cookie, $template,
{ module => 'cataloguing', record => $record } );
output_and_exit_if_error(
$input, $cookie, $template,
{ module => 'cataloguing', record => $record }
);
my $current_item;
my $nextop = "cud-additem";
@ -198,11 +209,15 @@ my $prefillitem = C4::Context->preference('PrefillItem');
my $load_template_submit = $input->param('load_template_submit');
my $delete_template_submit = $input->param('delete_template_submit');
my $unload_template_submit = $input->param('unload_template_submit');
my $use_template_for_session = $input->param('use_template_for_session') || $input->cookie('ItemEditorSessionTemplateId');
my $use_template_for_session =
$input->param('use_template_for_session') || $input->cookie('ItemEditorSessionTemplateId');
my $template_id = $input->param('template_id') || $input->cookie('ItemEditorSessionTemplateId');
if ($delete_template_submit) {
my $t = Koha::Item::Templates->find($template_id);
$t->delete if $t && ( $t->patron_id eq $loggedinuser || haspermission( $uid, { 'editcatalogue' => 'manage_item_editor_templates' } ) );
$t->delete
if $t
&& ( $t->patron_id eq $loggedinuser
|| haspermission( $uid, { 'editcatalogue' => 'manage_item_editor_templates' } ) );
$template_id = undef;
$use_template_for_session = undef;
}
@ -255,6 +270,7 @@ if ($op eq "cud-additem") {
}
if (@unlinked_item_subfields) {
my $marc = MARC::Record->new();
# use of tag 999 is arbitrary, and doesn't need to match the item tag
# used in the framework
$marc->append_fields( MARC::Field->new( '999', ' ', ' ', @unlinked_item_subfields ) );
@ -265,8 +281,7 @@ if ($op eq "cud-additem") {
$item->more_subfields_xml(undef);
} else {
next if $c eq 'itemnumber';
my @v = grep { $_ ne "" }
uniq $input->multi_param( "items." . $c );
my @v = grep { $_ ne "" } uniq $input->multi_param( "items." . $c );
next unless @v;
@ -303,13 +318,11 @@ if ($op eq "cud-additem") {
is_shared => $template_is_shared ? 1 : 0,
contents => $contents,
}
) if $template && (
$template->patron_id eq $loggedinuser
||
haspermission( $uid, { 'editcatalogue' => 'manage_item_editor_templates' } )
);
}
else {
)
if $template
&& ( $template->patron_id eq $loggedinuser
|| haspermission( $uid, { 'editcatalogue' => 'manage_item_editor_templates' } ) );
} else {
my $template = Koha::Item::Template->new(
{
name => $template_name,
@ -320,6 +333,7 @@ if ($op eq "cud-additem") {
)->store();
}
}
# If we have to add or add & duplicate, we add the item
elsif ( $add_submit || $add_duplicate_submit || $prefillitem ) {
@ -334,7 +348,10 @@ if ($op eq "cud-additem") {
}
unless (@errors) {
$item->store->discard_changes;
add_item_to_item_group( $item->biblionumber, $item->itemnumber, $item_group, $item_group_description, $display_order );
add_item_to_item_group(
$item->biblionumber, $item->itemnumber, $item_group, $item_group_description,
$display_order
);
# This is a bit tricky : if there is a cookie for the last created item and
# we just added an item, the cookie value is not correct yet (it will be updated
@ -346,6 +363,7 @@ if ($op eq "cud-additem") {
if ($prefillitem) {
my $last_created_item_cookie = $input->cookie(
-name => 'LastCreatedItem',
# We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems
-value => encode_base64url( freeze( { %{ $item->unblessed }, itemnumber => undef } ) ),
-HttpOnly => 1,
@ -369,8 +387,8 @@ if ($op eq "cud-additem") {
if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
my ($barcode) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
$current_item->{barcode} = $barcode;
}
else {
} else {
# we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
$current_item->{barcode} = undef; # FIXME or delete?
}
@ -396,8 +414,8 @@ if ($op eq "cud-additem") {
push @errors, "no_next_barcode";
}
else {
} else {
# We add each item
# For the first iteration
@ -415,8 +433,7 @@ if ($op eq "cud-additem") {
# Putting it into the record
if ($barcodevalue) {
if ( C4::Context->preference("autoBarcode") eq
'hbyymmincr' && $i > 0 )
if ( C4::Context->preference("autoBarcode") eq 'hbyymmincr' && $i > 0 )
{ # The first copy already contains the homebranch prefix
# This is terribly hacky but the easiest way to fix the way hbyymmincr is working
# Contrary to what one might think, the barcode plugin does not prefix the returned string with the homebranch
@ -441,8 +458,7 @@ if ($op eq "cud-additem") {
# Adding the item
if ( !$exist_itemnumber ) {
delete $current_item->{itemnumber};
$current_item = Koha::Item->new($current_item)->store(
{ skip_record_index => 1 } );
$current_item = Koha::Item->new($current_item)->store( { skip_record_index => 1 } );
$current_item->discard_changes; # Cannot chain discard_changes
$current_item = $current_item->unblessed;
add_item_to_item_group(
@ -463,48 +479,54 @@ if ($op eq "cud-additem") {
$oldbarcode = $barcodevalue;
}
my $indexer = Koha::SearchEngine::Indexer->new(
{ index => $Koha::SearchEngine::BIBLIOS_INDEX } );
$indexer->index_records( $biblionumber, "specialUpdate",
"biblioserver" );
my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
$indexer->index_records(
$biblionumber, "specialUpdate",
"biblioserver"
);
undef($current_item);
}
}
if ( $frameworkcode eq 'FA' && $fa_circborrowernumber ) {
print $input->redirect(
'/cgi-bin/koha/circ/circulation.pl?'
.'borrowernumber='.$fa_circborrowernumber
.'&barcode='.uri_escape_utf8($fa_barcode)
.'&duedatespec='.$fa_duedatespec
.'&stickyduedate='.$fa_stickyduedate
);
print $input->redirect( '/cgi-bin/koha/circ/circulation.pl?'
. 'borrowernumber='
. $fa_circborrowernumber
. '&barcode='
. uri_escape_utf8($fa_barcode)
. '&duedatespec='
. $fa_duedatespec
. '&stickyduedate='
. $fa_stickyduedate );
exit;
}
#-------------------------------------------------------------------------------
} elsif ( $op eq "edititem" ) {
#-------------------------------------------------------------------------------
# retrieve item if exist => then, it's a modif
$current_item = Koha::Items->find($itemnumber)->unblessed;
$nextop = "cud-saveitem";
#-------------------------------------------------------------------------------
} elsif ( $op eq "dupeitem" ) {
#-------------------------------------------------------------------------------
# retrieve item if exist => then, it's a modif
$current_item = Koha::Items->find($itemnumber)->unblessed;
if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
my ($barcode) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
$current_item->{barcode} = $barcode;
}
else {
} else {
$current_item->{barcode} = undef; # Don't save it!
}
$nextop = "cud-additem";
#-------------------------------------------------------------------------------
} elsif ( $op eq "cud-delitem" ) {
#-------------------------------------------------------------------------------
# check that there is no issue on this item before deletion.
my $item = Koha::Items->find($itemnumber);
@ -517,13 +539,14 @@ if ($op eq "cud-additem") {
if ($deleted) {
print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
exit;
}
else {
} else {
push @errors, @{ $deleted->messages }[0]->message;
$nextop = "cud-additem";
}
#-------------------------------------------------------------------------------
} elsif ( $op eq "cud-delallitems" ) {
#-------------------------------------------------------------------------------
my $items = Koha::Items->search( { biblionumber => $biblionumber } );
while ( my $item = $items->next ) {
@ -538,18 +561,23 @@ if ($op eq "cud-additem") {
my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
my $views = {C4::Search::enabled_staff_search_views};
if ( $defaultview eq 'isbd' && $views->{can_view_ISBD} ) {
print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
print $input->redirect(
"/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
} elsif ( $defaultview eq 'marc' && $views->{can_view_MARC} ) {
print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
print $input->redirect(
"/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
} elsif ( $defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC} ) {
print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
print $input->redirect(
"/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
} else {
print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
}
exit;
}
#-------------------------------------------------------------------------------
} elsif ( $op eq "cud-saveitem" ) {
#-------------------------------------------------------------------------------
my $itemnumber = $input->param('itemnumber');
@ -571,6 +599,7 @@ if ($op eq "cud-additem") {
}
if (@unlinked_item_subfields) {
my $marc = MARC::Record->new();
# use of tag 999 is arbitrary, and doesn't need to match the item tag
# used in the framework
$marc->append_fields( MARC::Field->new( '999', ' ', ' ', @unlinked_item_subfields ) );
@ -691,24 +720,24 @@ my @header_value_loop = map {
} sort keys %$subfieldcode_attribute_mappings;
# Using last created item if it exists
if (
$op ne "cud-additem"
if ( $op ne "cud-additem"
&& $op ne "edititem"
&& $op ne "dupeitem" )
{
if ($template_id) {
my $item_from_template = get_item_from_template($template_id);
$current_item = $item_from_template if $item_from_template;
}
elsif ( $prefillitem ) {
} elsif ($prefillitem) {
my $item_from_cookie = get_item_from_cookie($input);
$current_item = $item_from_cookie if $item_from_cookie;
}
}
if ( $current_item->{more_subfields_xml} ) {
# FIXME Use Maybe MARC::Record::new_from_xml if encoding issues on subfield (??)
$current_item->{marc_more_subfields_xml} = MARC::Record->new_from_xml($current_item->{more_subfields_xml}, 'UTF-8');
$current_item->{marc_more_subfields_xml} =
MARC::Record->new_from_xml( $current_item->{more_subfields_xml}, 'UTF-8' );
}
my $branchcode = $input->param('branch') || C4::Context->userenv->{branch};
@ -722,11 +751,13 @@ if ( $nextop eq 'cud-additem' && $op ne 'dupeitem' && $prefillitem ) {
}
# Getting list of subfields to keep when restricted editing is enabled
my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedEditing') : ();
my @subfields_to_allow =
$restrictededition
? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedEditing')
: ();
my $subfields =
Koha::UI::Form::Builder::Item->new(
{ biblionumber => $biblionumber, item => $current_item } )->edit_form(
Koha::UI::Form::Builder::Item->new( { biblionumber => $biblionumber, item => $current_item } )->edit_form(
{
branchcode => $branchcode,
restricted_editition => $restrictededition,
@ -761,8 +792,10 @@ if( my $default_location = C4::Context->preference('NewItemsDefaultLocation') )
}
my @ig = Koha::Biblio::ItemGroups->search( { biblio_id => $biblionumber } )->as_list();
#sort by display order
my @sorted_ig = sort { $a->display_order <=> $b->display_order } @ig;
# what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
$template->param(
biblio => $biblio,
@ -779,6 +812,7 @@ $template->param(
$template->{'VARS'}->{'searchid'} = $searchid;
if ( $frameworkcode eq 'FA' ) {
# fast cataloguing datas
$template->param(
'circborrowernumber' => $fa_circborrowernumber,