Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / catalogue / search.pl
index 8f460de..6876499 100755 (executable)
@@ -211,6 +211,14 @@ if($cgi->cookie("holdforclub")){
     );
 }
 
+if($cgi->cookie("searchToOrder")){
+    my ( $basketno, $vendorid ) = split( /\//, $cgi->cookie("searchToOrder") );
+    $template->param(
+        searchtoorder_basketno => $basketno,
+        searchtoorder_vendorid => $vendorid
+    );
+}
+
 # get biblionumbers stored in the cart
 my @cart_list;
 
@@ -229,56 +237,12 @@ $template->param(
 );
 
 # load the Type stuff
-my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
-# the index parameter is different for item-level itemtypes
-my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
-my @advancedsearchesloop;
-my $cnt;
-my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes") || "itemtypes";
-my @advanced_search_types = split(/\|/, $advanced_search_types);
-
-foreach my $advanced_srch_type (@advanced_search_types) {
-    $advanced_srch_type =~ s/^\s*//;
-    $advanced_srch_type =~ s/\s*$//;
-   if ($advanced_srch_type eq 'itemtypes') {
-   # itemtype is a special case, since it's not defined in authorized values
-        my @itypesloop;
-        foreach my $thisitemtype ( sort {$itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description} } keys %$itemtypes ) {
-           my %row =(  number=>$cnt++,
-               ccl => "$itype_or_itemtype,phr",
-                code => $thisitemtype,
-                description => $itemtypes->{$thisitemtype}->{translated_description},
-                imageurl=> getitemtypeimagelocation( 'intranet', $itemtypes->{$thisitemtype}->{'imageurl'} ),
-            );
-           push @itypesloop, \%row;
-       }
-        my %search_code = (  advanced_search_type => $advanced_srch_type,
-                             code_loop => \@itypesloop );
-        push @advancedsearchesloop, \%search_code;
-    } else {
-    # covers all the other cases: non-itemtype authorized values
-       my $advsearchtypes = GetAuthorisedValues($advanced_srch_type);
-        my @authvalueloop;
-       for my $thisitemtype (@$advsearchtypes) {
-               my %row =(
-                               number=>$cnt++,
-                               ccl => $advanced_srch_type,
-                code => $thisitemtype->{authorised_value},
-                description => $thisitemtype->{'lib'},
-                imageurl => getitemtypeimagelocation( 'intranet', $thisitemtype->{'imageurl'} ),
-                );
-               push @authvalueloop, \%row;
-       }
-        my %search_code = (  advanced_search_type => $advanced_srch_type,
-                             code_loop => \@authvalueloop );
-        push @advancedsearchesloop, \%search_code;
-    }
-}
-$template->param(advancedsearchesloop => \@advancedsearchesloop);
 my $types = C4::Context->preference("AdvancedSearchTypes") || "itemtypes";
 my $advancedsearchesloop = prepare_adv_search_types($types);
 $template->param(advancedsearchesloop => $advancedsearchesloop);
 
+$template->param( searchid => scalar $cgi->param('searchid'), );
+
 # The following should only be loaded if we're bringing up the advanced search template
 if ( $template_type eq 'advsearch' ) {
 
@@ -354,7 +318,6 @@ if ( $template_type eq 'advsearch' ) {
 #  * we can edit the values by changing the key
 #  * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
 my $params = $cgi->Vars;
-
 # Params that can have more than one value
 # sort by is used to sort the query
 # in theory can have more than one but generally there's just one
@@ -416,7 +379,7 @@ my %is_nolimit = map { $_ => 1 } @nolimits;
 if($params->{'multibranchlimit'}) {
     my $search_group = Koha::Library::Groups->find( $params->{multibranchlimit} );
     my @libraries = $search_group->all_libraries;
-    my $multibranch = '('.join( " or ", map { 'branch: ' . $_->branchcode } @libraries ) .')';
+    my $multibranch = '('.join( " OR ", map { '(homebranch: ' . $_->branchcode .')' } @libraries ) .')';
     push @limits, $multibranch if ($multibranch ne  '()');
 }
 
@@ -464,6 +427,7 @@ my $scan = $params->{'scan'};
 my $count = C4::Context->preference('numSearchResults') || 20;
 my $results_per_page = $params->{'count'} || $count;
 my $offset = $params->{'offset'} || 0;
+my $whole_record = $params->{'whole_record'} || 0;
 $offset = 0 if $offset < 0;
 my $page = $cgi->param('page') || 1;
 #my $offset = ($page-1)*$results_per_page;
@@ -471,11 +435,6 @@ my $page = $cgi->param('page') || 1;
 # Define some global variables
 my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type);
 
-my $build_params;
-unless ( $cgi->param('advsearch') ) {
-    $build_params->{weighted_fields} = 1;
-}
-
 my $builder = Koha::SearchEngine::QueryBuilder->new(
     { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
 my $searcher = Koha::SearchEngine::Search->new(
@@ -488,7 +447,9 @@ my $searcher = Koha::SearchEngine::Search->new(
     $query_type
   )
   = $builder->build_query_compat( \@operators, \@operands, \@indexes, \@limits,
-    \@sort_by, $scan, $lang, $build_params );
+    \@sort_by, $scan, $lang, { weighted_fields => !$cgi->param('advsearch'), whole_record => $whole_record });
+
+$template->param( search_query => $query ) if C4::Context->preference('DumpSearchQueryTemplate');
 
 ## parse the query_cgi string and put it into a form suitable for <input>s
 my @query_inputs;
@@ -503,9 +464,10 @@ if ($query_cgi) {
         my $input_value = $2;
         push @query_inputs, { input_name => $input_name, input_value => Encode::decode_utf8( uri_unescape( $input_value ) ) };
         if ($input_name eq 'idx') {
-            $scan_index_to_use = $input_value; # unless $scan_index_to_use;
+            # The form contains multiple fields, so take the first value as the scan index
+            $scan_index_to_use = $input_value unless $scan_index_to_use;
         }
-        if ($input_name eq 'q') {
+        if (!defined $scan_search_term_to_use && $input_name eq 'q') {
             $scan_search_term_to_use = Encode::decode_utf8( uri_unescape( $input_value ));
         }
     }
@@ -589,8 +551,8 @@ for (my $i=0;$i<@servers;$i++) {
             $template->param( EnableSearchHistory => 1 );
         }
 
-        ## If there's just one result, redirect to the detail page
-        if ($total == 1) {         
+        ## If there's just one result, redirect to the detail page unless doing an index scan
+        if ($total == 1 && !$scan) {
             my $biblionumber = $newresults[0]->{biblionumber};
             my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
             my $views = { C4::Search::enabled_staff_search_views };
@@ -731,8 +693,6 @@ for (my $i=0;$i<@servers;$i++) {
 } #/end of the for loop
 #$template->param(FEDERATED_RESULTS => \@results_array);
 
-$template->{'VARS'}->{'searchid'} = $cgi->param('searchid');
-
 my $gotonumber = $cgi->param('gotoNumber');
 if ( $gotonumber && ( $gotonumber eq 'last' || $gotonumber eq 'first' ) ) {
     $template->{'VARS'}->{'gotoNumber'} = $gotonumber;