Bug 10741: reformat test for displaying place hold link in OPAC search results
[koha.git] / C4 / Search.pm
index 8261e0a..753d5ae 100644 (file)
@@ -36,7 +36,7 @@ use URI::Escape;
 use Business::ISBN;
 use MARC::Record;
 use MARC::Field;
-
+use utf8;
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
 
 # set the version for version checking
@@ -112,6 +112,7 @@ sub FindDuplicate {
             $titleindex = 'title|exact';
             $authorindex = 'author|exact';
             $op = '&&';
+            $QParser->custom_data->{'QueryAutoTruncate'} = C4::Context->preference('QueryAutoTruncate');
         } else {
             $titleindex = 'ti,ext';
             $authorindex = 'au,ext';
@@ -233,6 +234,9 @@ sub SimpleSearch {
 
     my $QParser;
     $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser') && ! ($query =~ m/\w,\w|\w=\w/));
+    if ($QParser) {
+        $QParser->custom_data->{'QueryAutoTruncate'} = C4::Context->preference('QueryAutoTruncate');
+    }
 
     # Initialize & Search Zebra
     for ( my $i = 0 ; $i < @servers ; $i++ ) {
@@ -484,7 +488,6 @@ sub getRecords {
                     # not an index scan
                     else {
                         $record = $results[ $i - 1 ]->record($j)->raw();
-                        utf8::decode( $record );
 
                         # warn "RECORD $j:".$record;
                         $results_hash->{'RECORDS'}[$j] = $record;
@@ -502,7 +505,6 @@ sub getRecords {
                         for ( my $j = 0 ; $j < $jmax ; $j++ ) {
                             my $render_record =
                               $results[ $i - 1 ]->record($j)->render();
-                            utf8::decode($render_record);
                             my @used_datas = ();
                             foreach my $tag ( @{ $facet->{tags} } ) {
 
@@ -716,7 +718,6 @@ sub pazGetRecords {
         for (my $i = 0; $i < $count; $i++) {
             # FIXME -- may need to worry about diacritics here
             my $rec = $paz->record($recid, $i);
-            utf8::decode( $rec );
             push @{ $result_group->{'RECORDS'} }, $rec;
         }
 
@@ -1180,6 +1181,7 @@ sub parseQuery {
 
     if ($QParser)
     {
+        $QParser->custom_data->{'QueryAutoTruncate'} = C4::Context->preference('QueryAutoTruncate');
         $query = '';
         for ( my $ii = 0 ; $ii <= @operands ; $ii++ ) {
             next unless $operands[$ii];
@@ -1296,17 +1298,17 @@ sub buildQuery {
         if ( @limits ) {
             $q .= ' and '.join(' and ', @limits);
         }
-        return ( undef, $q, $q, "q=ccl=".uri_escape_utf8($q), $q, '', '', '', '', 'ccl' );
+        return ( undef, $q, $q, "q=ccl=".uri_escape($q), $q, '', '', '', '', 'ccl' );
     }
     if ( $query =~ /^cql=/ ) {
-        return ( undef, $', $', "q=cql=".uri_escape_utf8($'), $', '', '', '', '', 'cql' );
+        return ( undef, $', $', "q=cql=".uri_escape($'), $', '', '', '', '', 'cql' );
     }
     if ( $query =~ /^pqf=/ ) {
         if ($query_desc) {
-            $query_cgi = "q=".uri_escape_utf8($query_desc);
+            $query_cgi = "q=".uri_escape($query_desc);
         } else {
             $query_desc = $';
-            $query_cgi = "q=pqf=".uri_escape_utf8($');
+            $query_cgi = "q=pqf=".uri_escape($');
         }
         return ( undef, $', $', $query_cgi, $query_desc, '', '', '', '', 'pqf' );
     }
@@ -1478,9 +1480,9 @@ sub buildQuery {
                         $query     .= " $operators[$i-1] ";
                         $query     .= " $index_plus " unless $indexes_set;
                         $query     .= " $operand";
-                        $query_cgi .= "&op=".uri_escape_utf8($operators[$i-1]);
-                        $query_cgi .= "&idx=".uri_escape_utf8($index) if $index;
-                        $query_cgi .= "&q=".uri_escape_utf8($operands[$i]) if $operands[$i];
+                        $query_cgi .= "&op=".uri_escape($operators[$i-1]);
+                        $query_cgi .= "&idx=".uri_escape($index) if $index;
+                        $query_cgi .= "&q=".uri_escape($operands[$i]) if $operands[$i];
                         $query_desc .=
                           " $operators[$i-1] $index_plus $operands[$i]";
                     }
@@ -1490,8 +1492,8 @@ sub buildQuery {
                         $query      .= " and ";
                         $query      .= "$index_plus " unless $indexes_set;
                         $query      .= "$operand";
-                        $query_cgi  .= "&op=and&idx=".uri_escape_utf8($index) if $index;
-                        $query_cgi  .= "&q=".uri_escape_utf8($operands[$i]) if $operands[$i];
+                        $query_cgi  .= "&op=and&idx=".uri_escape($index) if $index;
+                        $query_cgi  .= "&q=".uri_escape($operands[$i]) if $operands[$i];
                         $query_desc .= " and $index_plus $operands[$i]";
                     }
                 }
@@ -1503,8 +1505,8 @@ sub buildQuery {
                     $query .= " $index_plus " unless $indexes_set;
                     $query .= $operand;
                     $query_desc .= " $index_plus $operands[$i]";
-                    $query_cgi  .= "&idx=".uri_escape_utf8($index) if $index;
-                    $query_cgi  .= "&q=".uri_escape_utf8($operands[$i]) if $operands[$i];
+                    $query_cgi  .= "&idx=".uri_escape($index) if $index;
+                    $query_cgi  .= "&q=".uri_escape($operands[$i]) if $operands[$i];
                     $previous_operand = 1;
                 }
             }    #/if $operands
@@ -1937,23 +1939,17 @@ sub searchResults {
                     $item_onhold_count++     if $reservestatus eq 'Waiting';
                     $item->{status} = $item->{wthdrawn} . "-" . $item->{itemlost} . "-" . $item->{damaged} . "-" . $item->{notforloan};
 
-                    # can place hold on item ?
-                    if ( !$item->{itemlost} ) {
-                        if ( !$item->{wthdrawn} ){
-                            if ( $item->{damaged} ){
-                                if ( C4::Context->preference('AllowHoldsOnDamagedItems') ){
-                                    # can place a hold on a damaged item if AllowHoldsOnDamagedItems is true
-                                    if ( ( !$item->{notforloan} || $item->{notforloan} < 0 ) ){
-                                        # item is either for loan or has notforloan < 0
-                                        $can_place_holds = 1;
-                                    }
-                                }
-                            } elsif ( $item->{notforloan} < 0 ) {
-                                # item is not damaged and notforloan is < 0
-                                $can_place_holds = 1;
-                            }
-                        }
-                    }
+                    # can place a hold on a item if
+                    # not lost nor withdrawn
+                    # not damaged unless AllowHoldsOnDamagedItems is true
+                    # item is either for loan or on order (notforloan < 0)
+                    $can_place_holds = 1
+                      if (
+                           !$item->{itemlost}
+                        && !$item->{wthdrawn}
+                        && ( !$item->{damaged} || C4::Context->preference('AllowHoldsOnDamagedItems') )
+                        && ( !$item->{notforloan} || $item->{notforloan} < 0 )
+                      );
 
                     $other_count++;