X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=tools%2FbatchMod.pl;h=d3c8d5060b88cfcfb41c384c282c9d1413cedd62;hb=b9bbc4b32aa01dcac353faf8f6dcea8aad987bc4;hp=ca10f6180c9b1de5ec6d505ff1d9a8165b8e10b1;hpb=cd0b2e33b3d5e36a3f789856cc5a7a9eaa27f30d;p=koha.git diff --git a/tools/batchMod.pl b/tools/batchMod.pl index ca10f6180c..d3c8d5060b 100755 --- a/tools/batchMod.pl +++ b/tools/batchMod.pl @@ -5,42 +5,45 @@ # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -use CGI; -use strict; -#use warnings; FIXME - Bug 2505 +use CGI qw ( -utf8 ); +use Modern::Perl; use C4::Auth; use C4::Output; use C4::Biblio; use C4::Items; use C4::Circulation; use C4::Context; -use C4::Koha; # XXX subfield_is_koha_internal_p -use C4::Branch; # XXX subfield_is_koha_internal_p +use C4::Koha; use C4::BackgroundJob; use C4::ClassSource; -use C4::Dates; use C4::Debug; use C4::Members; use MARC::File::XML; use List::MoreUtils qw/uniq/; +use Koha::Biblios; +use Koha::DateUtils; +use Koha::Items; +use Koha::ItemTypes; +use Koha::Patrons; + my $input = new CGI; my $dbh = C4::Context->dbh; my $error = $input->param('error'); -my @itemnumbers = $input->param('itemnumber'); +my @itemnumbers = $input->multi_param('itemnumber'); my $biblionumber = $input->param('biblionumber'); my $op = $input->param('op'); my $del = $input->param('del'); @@ -70,21 +73,19 @@ my ($template, $loggedinuser, $cookie) flagsrequired => $template_flag, }); -# Does the user have a limited item edition permission? -my $uid = GetMember( borrowernumber => $loggedinuser )->{userid} if ($loggedinuser) ; -my $limitededition = haspermission($uid, {'tools' => 'items_limited_batchmod'}) if ($uid); -# In case user is a superlibrarian, edition is not limited -$limitededition = 0 if ($limitededition != 0 && $limitededition->{'superlibrarian'} eq 1); +# Does the user have a restricted item edition permission? +my $uid = $loggedinuser ? Koha::Patrons->find( $loggedinuser )->userid : undef; +my $restrictededition = $uid ? haspermission($uid, {'tools' => 'items_batchmod_restricted'}) : undef; +# In case user is a superlibrarian, edition is not restricted +$restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian()); -my $today_iso = C4::Dates->today('iso'); -$template->param(today_iso => $today_iso); $template->param(del => $del); my $itemrecord; my $nextop=""; my @errors; # store errors found while checking data BEFORE saving item. my $items_display_hashref; -my $tagslib = &GetMarcStructure(1); +our $tagslib = &GetMarcStructure(1); my $deleted_items = 0; # Number of deleted items my $deleted_records = 0; # Number of deleted records ( with no items attached ) @@ -100,13 +101,13 @@ my $sessionID = $cookies{'CGISESSID'}->value; #--- ---------------------------------------------------------------------------- if ($op eq "action") { #------------------------------------------------------------------------------- - my @tags = $input->param('tag'); - my @subfields = $input->param('subfield'); - my @values = $input->param('field_value'); - my @disabled = $input->param('disable_input'); + my @tags = $input->multi_param('tag'); + my @subfields = $input->multi_param('subfield'); + my @values = $input->multi_param('field_value'); + my @disabled = $input->multi_param('disable_input'); # build indicator hash. - my @ind_tag = $input->param('ind_tag'); - my @indicator = $input->param('indicator'); + my @ind_tag = $input->multi_param('ind_tag'); + my @indicator = $input->multi_param('indicator'); # Is there something to modify ? # TODO : We shall use this var to warn the user in case no modification was done to the items @@ -117,11 +118,19 @@ if ($op eq "action") { # Once the job is done if ($completedJobID) { # If we have a reasonable amount of items, we display them - if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsForBatch") // 1000 ) ) { + if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsToDisplayForBatchDel") // 1000 ) ) { $items_display_hashref=BuildItemsData(@itemnumbers); } else { # Else, we only display the barcode - my @simple_items_display = map {{ itemnumber => $_, barcode => (GetBarcodeFromItemnumber($_) or ""), biblionumber => (GetBiblionumberFromItemnumber($_) or "") }} @itemnumbers; + my @simple_items_display = map { + my $itemnumber = $_; + my $item = Koha::Items->find($itemnumber); + { + itemnumber => $itemnumber, + barcode => $item ? ( $item->barcode // q{} ) : q{}, + biblionumber => $item ? $item->biblio->biblionumber : q{}, + }; + } @itemnumbers; $template->param("simple_items_display" => \@simple_items_display); } @@ -168,7 +177,7 @@ if ($op eq "action") { $job->progress($i) if $runinbackground; my $itemdata = GetItem($itemnumber); if ( $del ){ - my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'}); + my $return = DelItemCheck( $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'}); if ($return == 1) { $deleted_items++; } else { @@ -184,7 +193,7 @@ if ($op eq "action") { # If there are no items left, delete the biblio if ( $del_records ) { - my $itemscount = GetItemsCount($itemdata->{'biblionumber'}); + my $itemscount = Koha::Biblios->find( $itemdata->{'biblionumber'} )->items->count; if ( $itemscount == 0 ) { my $error = DelBiblio($itemdata->{'biblionumber'}); $deleted_records++ unless ( $error ); @@ -198,7 +207,7 @@ if ($op eq "action") { if ( $modified ) { eval { if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) { - LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost}; + LostItem($itemnumber, 'batchmod') if $item->{itemlost} and not $itemdata->{itemlost}; } }; } @@ -224,28 +233,27 @@ if ($op eq "action") { if ($op eq "show"){ my $filefh = $input->upload('uploadfile'); my $filecontent = $input->param('filecontent'); - my @notfoundbarcodes; + my ( @notfoundbarcodes, @notfounditemnumbers); my @contentlist; if ($filefh){ + binmode $filefh, ':encoding(UTF-8)'; while (my $content=<$filefh>){ $content =~ s/[\r\n]*$//; push @contentlist, $content if $content; } + @contentlist = uniq @contentlist; if ($filecontent eq 'barcode_file') { - foreach my $barcode (@contentlist) { - - my $itemnumber = GetItemnumberFromBarcode($barcode); - if ($itemnumber) { - push @itemnumbers,$itemnumber; - } else { - push @notfoundbarcodes, $barcode; - } - } + my $existing_items = Koha::Items->search({ itemnumber => \@contentlist }); + @itemnumbers = $existing_items->get_column('itemnumber'); + my %exists = map {$_=>1} @{$existing_items->get_column('barcode')}; + @notfoundbarcodes = grep { !$exists{$_} } @contentlist; } elsif ( $filecontent eq 'itemid_file') { - @itemnumbers = @contentlist; + @itemnumbers = Koha::Items->search({ itemnumber => \@contentlist })->get_column('itemnumber'); + my %exists = map {$_=>1} @itemnumbers; + @notfounditemnumbers = grep { !$exists{$_} } @contentlist; } } else { if (defined $biblionumber){ @@ -257,23 +265,21 @@ if ($op eq "show"){ if ( my $list=$input->param('barcodelist')){ push my @barcodelist, uniq( split(/\s\n/, $list) ); - foreach my $barcode (@barcodelist) { - - my $itemnumber = GetItemnumberFromBarcode($barcode); - if ($itemnumber) { - push @itemnumbers,$itemnumber; - } else { - push @notfoundbarcodes, $barcode; - } - } - + my $existing_items = Koha::Items->search({ barcode => \@barcodelist }); + @itemnumbers = $existing_items->get_column('itemnumber'); + my @barcodes = $existing_items->get_column('barcode'); + my %exists = map {$_=>1} @barcodes; + @notfoundbarcodes = grep { !$exists{$_} } @barcodelist; } } # Flag to tell the template there are valid results, hidden or not if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); } - # Only display the items if there are no more than pref MaxItemsForBatch - if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsForBatch") // 1000 ) ) { + # Only display the items if there are no more than pref MaxItemsToProcessForBatchMod or MaxItemsToDisplayForBatchDel + my $max_items = $del + ? C4::Context->preference("MaxItemsToDisplayForBatchDel") + : C4::Context->preference("MaxItemsToProcessForBatchMod"); + if (scalar(@itemnumbers) <= ( $max_items // 1000 ) ) { $items_display_hashref=BuildItemsData(@itemnumbers); } else { $template->param("too_many_items" => scalar(@itemnumbers)); @@ -291,23 +297,27 @@ $query .= qq{ AND ( branchcode = ? OR branchcode IS NULL ) } if $branch_limit; $query .= qq{ GROUP BY lib ORDER BY lib, lib_opac}; my $authorised_values_sth = $dbh->prepare( $query ); -my $branches = GetBranchesLoop(); # build once ahead of time, instead of multiple times later. +my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later. # Adding a default choice, in case the user does not want to modify the branch my $nochange_branch = { branchname => '', value => '', selected => 1 }; -unshift (@$branches, $nochange_branch); +unshift (@$libraries, $nochange_branch); my $pref_itemcallnumber = C4::Context->preference('itemcallnumber'); -# Getting list of subfields to keep when limited batchmod edit is enabled -my $subfieldsToAllowForBatchmod = C4::Context->preference('SubfieldsToAllowForLimitedBatchmod'); +# Getting list of subfields to keep when restricted batchmod edit is enabled +my $subfieldsToAllowForBatchmod = C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod'); +my $allowAllSubfields = ( + not defined $subfieldsToAllowForBatchmod + or $subfieldsToAllowForBatchmod eq q|| +) ? 1 : 0; my @subfieldsToAllow = split(/ /, $subfieldsToAllowForBatchmod); foreach my $tag (sort keys %{$tagslib}) { # loop through each subfield foreach my $subfield (sort keys %{$tagslib->{$tag}}) { - next if subfield_is_koha_internal_p($subfield); - next if ($limitededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow ); + next if IsMarcStructureInternal( $tagslib->{$tag}{$subfield} ); + next if (not $allowAllSubfields and $restrictededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow ); next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10"); # barcode and stocknumber are not meant to be batch-modified next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode'; @@ -322,8 +332,6 @@ foreach my $tag (sort keys %{$tagslib}) { } $subfield_data{tag} = $tag; $subfield_data{subfield} = $subfield; - $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms? - # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib}; $subfield_data{marc_lib} ="{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}.""; $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory}; $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable}; @@ -332,10 +340,13 @@ foreach my $tag (sort keys %{$tagslib}) { if ( !$value && $use_default_values) { $value = $tagslib->{$tag}->{$subfield}->{defaultvalue}; # get today date & replace YYYY, MM, DD if provided in the default value - my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas! - $value =~ s/YYYY/$year/g; - $value =~ s/MM/$month/g; - $value =~ s/DD/$day/g; + my $today = dt_from_string; + my $year = $today->year; + my $month = $today->month; + my $day = $today->day; + $value =~ s/YYYY/$year/g; + $value =~ s/MM/$month/g; + $value =~ s/DD/$day/g; } $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4)); # testing branch value if IndependentBranches. @@ -344,22 +355,21 @@ foreach my $tag (sort keys %{$tagslib}) { my @authorised_values; my %authorised_lib; # builds list, depending on authorised value... - - if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) { - foreach my $thisbranch (@$branches) { - push @authorised_values, $thisbranch->{value}; - $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname}; - } + + if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) { + foreach my $library (@$libraries) { + push @authorised_values, $library->{branchcode}; + $authorised_lib{$library->{branchcode}} = $library->{branchname}; + } $value = ""; - } - elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { - push @authorised_values, ""; - my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description"); - $sth->execute; - while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype} = $description; - } + } + elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { + push @authorised_values, ""; + my $itemtypes = Koha::ItemTypes->search_with_localization; + while ( my $itemtype = $itemtypes->next ) { + push @authorised_values, $itemtype->itemtype; + $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description; + } $value = ""; #---- class_sources @@ -407,26 +417,28 @@ foreach my $tag (sort keys %{$tagslib}) { value => $value, authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode}, } - # it's a plugin field } - elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) { - # opening plugin - my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'}; - if (do $plugin) { - my $temp; - my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data ); - my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data ); + elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) { # plugin + require Koha::FrameworkPlugin; + my $plugin = Koha::FrameworkPlugin->new( { + name => $tagslib->{$tag}->{$subfield}->{'value_builder'}, + item_style => 1, + }); + my $temp; + my $pars= { dbh => $dbh, record => $temp, tagslib => $tagslib, + id => $subfield_data{id}, tabloop => \@loop_data }; + $plugin->build( $pars ); + if( !$plugin->errstr ) { $subfield_data{marc_value} = { type => 'text2', id => $subfield_data{id}, value => $value, - function => $function_name, - random => $subfield_data{random}, - javascript => $javascript, + javascript => $plugin->javascript, + noclick => $plugin->noclick, }; } else { - warn "Plugin Failed: $plugin"; - $subfield_data{marc_value} = { # supply default input form + warn $plugin->errstr; + $subfield_data{marc_value} = { # supply default input form type => 'text', id => $subfield_data{id}, value => $value, @@ -477,11 +489,11 @@ $authorised_values_sth->finish; # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit. - $template->param(item => \@loop_data); - if (@notfoundbarcodes) { - my @notfoundbarcodesloop = map{{barcode=>$_}}@notfoundbarcodes; - $template->param(notfoundbarcodes => \@notfoundbarcodesloop); - } + $template->param( + item => \@loop_data, + notfoundbarcodes => \@notfoundbarcodes, + notfounditemnumbers => \@notfounditemnumbers + ); $nextop="action" } # -- End action="show" @@ -557,11 +569,11 @@ sub BuildItemsData{ # grab title, author, and ISBN to identify bib that the item # belongs to in the display - my $biblio=GetBiblioData($$itemdata{biblionumber}); - $this_row{title} = $biblio->{title}; - $this_row{author} = $biblio->{author}; - $this_row{isbn} = $biblio->{isbn}; - $this_row{biblionumber} = $biblio->{biblionumber}; + my $biblio = Koha::Biblios->find( $itemdata->{biblionumber} ); + $this_row{title} = $biblio->title; + $this_row{author} = $biblio->author; + $this_row{isbn} = $biblio->biblioitem->isbn; + $this_row{biblionumber} = $biblio->biblionumber; if (%this_row) { push(@big_array, \%this_row); @@ -666,7 +678,7 @@ sub add_saved_job_results_to_template { sub put_in_background { my $job_size = shift; - my $job = C4::BackgroundJob->new($sessionID, "test", $ENV{'SCRIPT_NAME'}, $job_size); + my $job = C4::BackgroundJob->new($sessionID, "test", '/cgi-bin/koha/tools/batchMod.pl', $job_size); my $jobID = $job->id(); # fork off @@ -691,7 +703,7 @@ sub put_in_background { close STDERR; } else { # fork failed, so exit immediately - warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job"; + warn "fork failed while attempting to run tools/batchMod.pl as a background job"; exit 0; } return $job;