From 4c07a5887e7ffbcafaadbf1d30916197db5b299e Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Fri, 18 Jul 2014 20:37:28 +0300 Subject: [PATCH] Bug 11677 - Limit to Only items currently available for loan or reference not working! Worklog entries FYI: (A proper patch follows (unless somebody does it first)) Identified this issue. Looking for a solution. Browsing the Zebra manual for answers on regexp, regexps not supported :( Made it possible to accurately and quickly filter results by holdingbranch and onloan. So "Selecting available" now works when a library is selected as well. This causes normal "Selecting available"-feature to not work in it's mutant form. Need to refactor the default value for items.onloan from NULL to '0000-00-00' to fix it. This row: $availability_limit .= "( ( allrecords,AlwaysMatches='' not onloan,AlwaysMatches='') and (lost,st-numeric=0) )"; #or ( allrecords,AlwaysMatches='' not lost,AlwaysMatches='')) )"; Causes all records in Zebra to be selected and removes records with even one item onloan (even if items would be available)! This is obviously wrong, but because Zebra (and not many other text indexers) cannot deal with (If undef)-search terms, a fix might be to set 952$q (onloan) datetime to 0000-00-00 instead of NULL in the DB. Then we could find Records with items where onloan is 0000-00-00. --- C4/Search.pm | 45 +++++++++++++++++++++++++++++++++++++++++---- catalogue/search.pl | 15 +++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 20ea1e4..395af92 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -329,7 +329,7 @@ sub getRecords { my ( $koha_query, $simple_query, $sort_by_ref, $servers_ref, $results_per_page, $offset, $expanded_facet, $branches, - $itemtypes, $query_type, $scan, $opac + $itemtypes, $query_type, $scan, $opac, $availableLimit ) = @_; my @servers = @$servers_ref; @@ -348,6 +348,14 @@ sub getRecords { my $facets_maxrecs = C4::Context->preference('maxRecordsForFacets')||20; my @facets_loop; # stores the ref to array of hashes for template facets loop + + #Get the required marc tags for determining branch-based availability. + my ( $holdingbranchField, $holdingbranchSubfield, $onloanField, $onloanSubfield ); + if ($availableLimit) { + ( $holdingbranchField, $holdingbranchSubfield ) = GetMarcFromKohaField( "items.holdingbranch" ); + ( $onloanField, $onloanSubfield ) = GetMarcFromKohaField( "items.onloan" ); + } + ### LOOP THROUGH THE SERVERS for ( my $i = 0 ; $i < @servers ; $i++ ) { @@ -493,8 +501,17 @@ sub getRecords { # not an index scan else { - $record = $results[ $i - 1 ]->record($j)->raw(); + $record = $results[ $i - 1 ]->record($j); + last() unless $record; + $record = $record->raw(); # warn "RECORD $j:".$record; + + if ( $availableLimit && $availableLimit != 1 && #Availablelimit is 1 if there is no limit=branch:FTL given + not(isAvailableFromMARCXML( $record, $availableLimit, $holdingbranchField, $holdingbranchSubfield, $onloanField, $onloanSubfield )) ) { + $times++; #Skip this record, but make sure we still get the $results_per_page results to display. + next(); + } + $results_hash->{'RECORDS'}[$j] = $record; } @@ -1535,8 +1552,8 @@ sub buildQuery { ## 'available' is defined as (items.onloan is NULL) and (items.itemlost = 0) ## In English: ## all records not indexed in the onloan register (zebra) and all records with a value of lost equal to 0 - $availability_limit .= -"( ( allrecords,AlwaysMatches='' not onloan,AlwaysMatches='') and (lost,st-numeric=0) )"; #or ( allrecords,AlwaysMatches='' not lost,AlwaysMatches='')) )"; +# $availability_limit .= +#"( ( allrecords,AlwaysMatches='' not onloan,AlwaysMatches='') and (lost,st-numeric=0) )"; #or ( allrecords,AlwaysMatches='' not lost,AlwaysMatches='')) )"; $limit_cgi .= "&limit=available"; $limit_desc .= ""; } @@ -2434,6 +2451,26 @@ sub new_record_from_zebra { } +sub isAvailableFromMARCXML { + my ( $recordXML, $limitHoldingbranch, $holdingbranchField, $holdingbranchSubfield, $onloanField, $onloanSubfield ) = @_; + + my @itemsFields = $recordXML =~ /(.*?)<\/datafield>/sg; + + my ($itemHoldingbranch, $itemOnloan); + foreach my $fieldStr (@itemsFields) { + if ($fieldStr =~ /(.*?)<\/subfield>/s) { + $itemHoldingbranch = $1; + } + if ($fieldStr =~ /(.*?)<\/subfield>/s) { + $itemOnloan = $1; + } + if ($itemHoldingbranch && not($itemOnloan) && $itemHoldingbranch eq $limitHoldingbranch) { + return 1; #Available ! wooee! + } + } + return 0; +} + END { } # module clean-up code here (global destructor) 1; diff --git a/catalogue/search.pl b/catalogue/search.pl index a391dfe..8cb31c0 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -412,17 +412,28 @@ my @operands = map uri_unescape($_), $cgi->param('q'); # limits are use to limit to results to a pre-defined category such as branch or language my @limits = map uri_unescape($_), $cgi->param('limit'); + +##HACKMAN HERE +#push @limits, "branch:JOE_JOE"; +##HACKMAN if($params->{'multibranchlimit'}) { my $multibranch = '('.join( " or ", map { "branch: $_ " } @{ GetBranchesInCategory( $params->{'multibranchlimit'} ) } ).')'; push @limits, $multibranch if ($multibranch ne '()'); } -my $available; +my ($available, $availableBranch); foreach my $limit(@limits) { if ($limit =~/available/) { $available = 1; } + elsif ($limit =~ /^branch:(.*?)$/) { + $availableBranch = $1; + } +} +if ($availableBranch && $available) { + $available = $availableBranch; } +undef $availableBranch; #Don't pollute namespace! $template->param(available => $available); # append year limits if they exist @@ -520,7 +531,7 @@ my @results_array; my $results_hashref; eval { - ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$itemtypes,$query_type,$scan); + ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$itemtypes,$query_type,$scan,undef,$available); }; # This sorts the facets into alphabetical order -- 1.8.1.2