Bug 7847 - OPAC search dies with plack
[koha.git] / C4 / Search.pm
index 04ce112..a301b79 100644 (file)
@@ -71,6 +71,7 @@ This module provides searching functions for Koha's bibliographic databases
   &AddSearchHistory
   &GetDistinctValues
   &enabled_staff_search_views
+  &SimpleSearch
 );
 
 # make all your functions, whether exported or not;
@@ -469,46 +470,39 @@ sub getRecords {
                 if ( !$scan && $servers[ $i - 1 ] =~ /biblioserver/ ) {
 
                     my $jmax = $size>$facets_maxrecs? $facets_maxrecs: $size;
-
-                    for ( my $k = 0 ; $k <= @$facets ; $k++ ) {
-                        ($facets->[$k]) or next;
-                        my @fcodes = @{$facets->[$k]->{'tags'}};
-                        my $sfcode = $facets->[$k]->{'subfield'};
-
+                    for my $facet ( @$facets ) {
                                for ( my $j = 0 ; $j < $jmax ; $j++ ) {
                                    my $render_record = $results[ $i - 1 ]->record($j)->render();
                             my @used_datas = ();
-
-                            foreach my $fcode (@fcodes) {
-
+                            foreach my $tag ( @{$facet->{tags}} ) {
                                 # avoid first line
-                                my $field_pattern = '\n'.$fcode.' ([^\n]+)';
+                                my $tag_num = substr($tag, 0, 3);
+                                my $letters = substr($tag, 3);
+                                my $field_pattern = '\n' . $tag_num . ' ([^\n]+)';
                                 my @field_tokens = ( $render_record =~ /$field_pattern/g ) ;
-
                                 foreach my $field_token (@field_tokens) {
-                                    my $subfield_pattern = '\$'.$sfcode.' ([^\$]+)';
-                                    my @subfield_values = ( $field_token =~ /$subfield_pattern/g );
-
-                                    foreach my $subfield_value (@subfield_values) {
-
-                                        my $data = $subfield_value;
-                                        $data =~ s/^\s+//; # trim left
-                                        $data =~ s/\s+$//; # trim right
-
-                                        unless ( $data ~~ @used_datas ) {
-                                            $facets_counter->{ $facets->[$k]->{'link_value'} }->{$data}++;
-                                            push @used_datas, $data;
+                                    my @subf = ( $field_token =~ /\$([a-zA-Z0-9]) ([^\$]+)/g );
+                                    my @values;
+                                    for (my $i = 0; $i < @subf; $i += 2) {
+                                        if ( $letters =~ $subf[$i] ) {
+                                             my $value = $subf[$i+1];
+                                             $value =~ s/^ *//;
+                                             $value =~ s/ *$//;
+                                             push @values, $value;
                                         }
-                                    } # subfields
+                                    }
+                                    my $data = join($facet->{sep}, @values);
+                                    unless ( $data ~~ @used_datas ) {
+                                        $facets_counter->{ $facet->{idx} }->{$data}++;
+                                        push @used_datas, $data;
+                                    }
                                 } # fields
                             } # field codes
                         } # records
-
-                        $facets_info->{ $facets->[$k]->{'link_value'} }->{'label_value'} = $facets->[$k]->{'label_value'};
-                        $facets_info->{ $facets->[$k]->{'link_value'} }->{'expanded'} = $facets->[$k]->{'expanded'};
+                        $facets_info->{ $facet->{idx} }->{label_value} = $facet->{label};
+                        $facets_info->{ $facet->{idx} }->{expanded} = $facet->{expanded};
                     } # facets
                 }
-                # End PROGILONE
             }
 
             # warn "connection ", $i-1, ": $size hits";
@@ -739,6 +733,13 @@ sub _build_stemmed_operand {
 
 # FIXME: the locale should be set based on the user's language and/or search choice
     #warn "$lang";
+    # Make sure we only use the first two letters from the language code
+    $lang = lc(substr($lang, 0, 2));
+    # The language codes for the two variants of Norwegian will now be "nb" and "nn",
+    # none of which Lingua::Stem::Snowball can use, so we need to "translate" them
+    if ($lang eq 'nb' || $lang eq 'nn') {
+      $lang = 'no';
+    }
     my $stemmer = Lingua::Stem::Snowball->new( lang => $lang,
                                                encoding => "UTF-8" );
 
@@ -1303,7 +1304,7 @@ sub buildQuery {
     warn "QUERY BEFORE LIMITS: >$query<" if $DEBUG;
 
     # add limits
-    my $group_OR_limits;
+    my %group_OR_limits;
     my $availability_limit;
     foreach my $this_limit (@limits) {
         if ( $this_limit =~ /available/ ) {
@@ -1320,17 +1321,16 @@ sub buildQuery {
         # group_OR_limits, prefixed by mc-
         # OR every member of the group
         elsif ( $this_limit =~ /mc/ ) {
-        
-            if ( $this_limit =~ /mc-ccode:/ ) {
+            my ($k,$v) = split(/:/, $this_limit,2);
+            if ( $k !~ /mc-i(tem)?type/ ) {
                 # in case the mc-ccode value has complicating chars like ()'s inside it we wrap in quotes
                 $this_limit =~ tr/"//d;
-                my ($k,$v) = split(/:/, $this_limit,2);
                 $this_limit = $k.":\"".$v."\"";
             }
 
-            $group_OR_limits .= " or " if $group_OR_limits;
-            $limit_desc      .= " or " if $group_OR_limits;
-            $group_OR_limits .= "$this_limit";
+            $group_OR_limits{$k} .= " or " if $group_OR_limits{$k};
+            $limit_desc      .= " or " if $group_OR_limits{$k};
+            $group_OR_limits{$k} .= "$this_limit";
             $limit_cgi       .= "&limit=$this_limit";
             $limit_desc      .= " $this_limit";
         }
@@ -1353,9 +1353,9 @@ sub buildQuery {
             }
         }
     }
-    if ($group_OR_limits) {
+    foreach my $k (keys (%group_OR_limits)) {
         $limit .= " and " if ( $query || $limit );
-        $limit .= "($group_OR_limits)";
+        $limit .= "($group_OR_limits{$k})";
     }
     if ($availability_limit) {
         $limit .= " and " if ( $query || $limit );
@@ -1627,10 +1627,11 @@ sub searchResults {
             foreach my $code ( keys %subfieldstosearch ) {
                 $item->{$code} = $field->subfield( $subfieldstosearch{$code} );
             }
+            $item->{description} = $itemtypes{ $item->{itype} }{description};
 
                # Hidden items
             if ($is_opac) {
-                   my @hi = GetHiddenItemnumbers($item);
+                   my @hi = C4::Items::GetHiddenItemnumbers($item);
                $item->{'hideatopac'} = @hi;
               push @hiddenitems, @hi;
             }
@@ -1657,6 +1658,7 @@ sub searchResults {
                                $onloan_items->{$key}->{branchname} = $item->{branchname};
                                $onloan_items->{$key}->{location} = $shelflocations->{ $item->{location} };
                                $onloan_items->{$key}->{itemcallnumber} = $item->{itemcallnumber};
+                               $onloan_items->{$key}->{description} = $item->{description};
                                $onloan_items->{$key}->{imageurl} = getitemtypeimagelocation( $search_context, $itemtypes{ $item->{itype} }->{imageurl} );
                 # if something's checked out and lost, mark it as 'long overdue'
                 if ( $item->{itemlost} ) {
@@ -1738,9 +1740,10 @@ sub searchResults {
                     }
                     $other_items->{$key}->{intransit} = ( $transfertwhen ne '' ) ? 1 : 0;
                     $other_items->{$key}->{onhold} = ($reservestatus) ? 1 : 0;
-                                       $other_items->{$key}->{notforloan} = GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value) if $notforloan_authorised_value;
+                    $other_items->{$key}->{notforloan} = GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value) if $notforloan_authorised_value and $item->{notforloan};
                                        $other_items->{$key}->{count}++ if $item->{$hbranch};
                                        $other_items->{$key}->{location} = $shelflocations->{ $item->{location} };
+                                       $other_items->{$key}->{description} = $item->{description};
                                        $other_items->{$key}->{imageurl} = getitemtypeimagelocation( $search_context, $itemtypes{ $item->{itype} }->{imageurl} );
                 }
                 # item is available
@@ -1748,7 +1751,7 @@ sub searchResults {
                     $can_place_holds = 1;
                     $available_count++;
                                        $available_items->{$prefix}->{count}++ if $item->{$hbranch};
-                                       foreach (qw(branchname itemcallnumber hideatopac)) {
+                                       foreach (qw(branchname itemcallnumber hideatopac description)) {
                        $available_items->{$prefix}->{$_} = $item->{$_};
                                        }
                                        $available_items->{$prefix}->{location} = $shelflocations->{ $item->{location} };
@@ -1780,8 +1783,7 @@ sub searchResults {
        $debug && warn $marcrecord->as_formatted;
        my $interface = $search_context eq 'opac' ? 'OPAC' : '';
        if (!$scan && C4::Context->preference($interface . "XSLTResultsDisplay")) {
-            $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display($oldbiblio->{biblionumber}, $marcrecord, 'Results',
-                                                                $search_context, 1, \@hiddenitems);
+            $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display($oldbiblio->{biblionumber}, $marcrecord, $interface."XSLTResultsDisplay", 1, \@hiddenitems);
            # the last parameter tells Koha to clean up the problematic ampersand entities that Zebra outputs
         }