From 05d29c0ff849e5c3096a821343aba9945935fcb7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 12 Oct 2015 11:36:31 +0100 Subject: [PATCH] Bug 14292: Simplify the category list Basically we have 3 lists of categories: 1/ The full one, in the DB 2/ The one we want and list with --category 3/ The one we don't want and list with --skip-category The list of categories to process is: * 1 if none --category or --skip-category options are provided * 2 if --category is provided * 1 - 3 if --skip-category is provided Signed-off-by: Barton Chittenden Signed-off-by: Jonathan Druart --- misc/cronjobs/longoverdue.pl | 60 +++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 40 deletions(-) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index aa9fc13..6c8b41e 100755 --- a/misc/cronjobs/longoverdue.pl +++ b/misc/cronjobs/longoverdue.pl @@ -251,51 +251,31 @@ sub longoverdue_sth { return C4::Context->dbh->prepare($query); } -sub check_user_categories { - - my $categories = shift; - my $test_categories = shift; - my $category_label = shift; - my %good = map { $_ => 1 } @$categories; - my @bad_categories = grep { ! $good{ uc $_ } } @$test_categories; - - if ( scalar @bad_categories ) { - my $error = "$category_label: category/categories '" - . join( ' ', @bad_categories ) - . "' not in the available borrower cateogories: [ " - . join( ' ', @$categories ) - . " ]"; - pod2usage( '-verbose' => 1 - , '-exitval' => 1 - , '-message' => $error - ); +my $dbh = C4::Context->dbh; +my @available_categories = map { uc $_->[0] } @{ $dbh->selectall_arrayref(q|SELECT categorycode FROM categories|) }; +my %category_to_process; +for my $cat ( @$borrower_category ) { + unless ( grep { /^$cat$/ } @available_categories ) { + pod2usage( + '-exitval' => 1, + '-message' => "The category $cat does not exist in the database", + ); } + $category_to_process{$cat} = 1; } - -# returns a hash of borrower category codes to test for long-overdue-ness. -# The key is the borrower category, upper case. The value is 1. This -# will make a fast look-up of valid borrower category codes. -sub get_borrower_categories { - my ( $borrower_category, $skip_borrower_category ) = ( @_ ); - - return () unless ( scalar @$borrower_category || scalar @$skip_borrower_category ); - - my $dbh = C4::Context->dbh; - my @categories = map { uc $_->[0] } @{ $dbh->selectall_arrayref(q|SELECT categorycode FROM categories|) }; - if( @$borrower_category ) { - check_user_categories( \@categories, $borrower_category, "--category" ); - my %borcat = map { ( uc($_) => 1 ) } @$borrower_category; - @categories = grep { $borcat{$_} } @categories; - } - if( @$skip_borrower_category ) { - check_user_categories( \@categories, $skip_borrower_category, "--skip-category" ); - my %skip_borcat = map { ( uc($_) => 1 ) } @$skip_borrower_category; - @categories = grep { ! $skip_borcat{$_} } @categories; +if ( @$skip_borrower_category ) { + for my $cat ( @$skip_borrower_category ) { + unless ( grep { /^$cat$/ } @available_categories ) { + pod2usage( + '-exitval' => 1, + '-message' => "The category $cat does not exist in the database", + ); + } } - return map { $_ => 1 } @categories; + %category_to_process = map { $_ => 1 } @available_categories; + %category_to_process = ( %category_to_process, map { $_ => 0 } @$skip_borrower_category ); } -my %category_to_process = get_borrower_categories( $borrower_category, $skip_borrower_category ); my $filter_borrower_categories = ( scalar @$borrower_category || scalar @$skip_borrower_category ); my $count; -- 2.1.0