Bug 6258 - Guided reports wizard 'Build new' brings up empty page
[koha.git] / catalogue / search.pl
index 791bba1..e1e9a98 100755 (executable)
@@ -3,6 +3,7 @@
 # For documentation try 'perldoc /path/to/search'
 #
 # Copyright 2006 LibLime
+# Copyright 2010 BibLibre
 #
 # This file is part of Koha
 #
@@ -218,16 +219,20 @@ if($cgi->cookie("holdfor")){
 
 # load the branches
 my $branches = GetBranches();
-my @branch_loop;
 
-# we need to know the borrower branch code to set a default branch
-my $borrowerbranchcode = C4::Context->userenv->{'branch'};
-
-for my $branch_hash (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
-    # if independantbranches is activated, set the default branch to the borrower branch
-    my $selected = (C4::Context->preference("independantbranches") and ($borrowerbranchcode eq $branch_hash)) ? 1 : undef;
-    push @branch_loop, {value => "$branch_hash" , branchname => $branches->{$branch_hash}->{'branchname'}, selected => $selected};
-}
+# Populate branch_loop with all branches sorted by their name.  If
+# independantbranches is activated, set the default branch to the borrower
+# branch, except for superlibrarian who need to search all libraries.
+my $user = C4::Context->userenv;
+my @branch_loop = map {
+     {
+        value      => $_,
+        branchname => $branches->{$_}->{branchname},
+        selected   => $user->{branch} eq $_ && C4::Branch::onlymine(),
+     }
+} sort {
+    $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}
+} keys %$branches;
 
 my $categories = GetBranchCategories(undef,'searchdomain');
 
@@ -245,7 +250,7 @@ my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
 
 if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {                                                                 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
     my %row =(  number=>$cnt++,
-                ccl => $itype_or_itemtype,
+                ccl => qq($itype_or_itemtype,phr),
                 code => $thisitemtype,
                 selected => $selected,
                 description => $itemtypes->{$thisitemtype}->{'description'},
@@ -378,8 +383,11 @@ my @indexes;
 @indexes = split("\0",$params->{'idx'});
 
 # if a simple index (only one)  display the index used in the top search box
-if ($indexes[0] && !$indexes[1]) {
-    $template->param("ms_".$indexes[0] => 1);}
+if ($indexes[0] && (!$indexes[1] || $params->{'scan'})) {
+    my $idx = "ms_".$indexes[0];
+    $idx =~ s/\,/comma/g;  # template toolkit doesnt like variables with a , in it
+    $template->param($idx => 1);
+}
 
 
 # an operand can be a single term, a phrase, or a complete ccl query
@@ -391,7 +399,7 @@ my @limits;
 @limits = split("\0",$params->{'limit'}) if $params->{'limit'};
 
 if($params->{'multibranchlimit'}) {
-push @limits, join(" or ", map { "branch: $_ "}  @{GetBranchesInCategory($params->{'multibranchlimit'})}) ;
+    push @limits, '('.join( " or ", map { "branch: $_ " } @{ GetBranchesInCategory( $params->{'multibranchlimit'} ) } ).')';
 }
 
 my $available;
@@ -463,10 +471,9 @@ my $scan_index_to_use;
 
 for my $this_cgi ( split('&',$query_cgi) ) {
     next unless $this_cgi;
-    $this_cgi =~ m/(.*=)(.*)/;
+    $this_cgi =~ m/(.?)=(.*)/;
     my $input_name = $1;
     my $input_value = $2;
-    $input_name =~ s/=$//;
     push @query_inputs, { input_name => $input_name, input_value => $input_value };
        if ($input_name eq 'idx') {
        $scan_index_to_use = $input_value; # unless $scan_index_to_use;
@@ -652,6 +659,7 @@ $template->param(
             total => $total,
             opacfacets => 1,
             facets_loop => $facets,
+           displayFacetCount=> C4::Context->preference('displayFacetCount')||0,
             scan => $scan,
             search_error => $error,
 );
@@ -668,17 +676,14 @@ my $row_count = 10; # FIXME:This probably should be a syspref
 my ($pubshelves, $total) = GetRecentShelves(2, $row_count, undef);
 my ($barshelves, $total) = GetRecentShelves(1, $row_count, $borrowernumber);
 
-my @pubshelves = @{$pubshelves};
-my @barshelves = @{$barshelves};
-
-if (@pubshelves) {
-        $template->param( addpubshelves     => scalar (@pubshelves));
-        $template->param( addpubshelvesloop => @pubshelves);
+if (@{$pubshelves}) {
+        $template->param( addpubshelves     => scalar @{$pubshelves});
+        $template->param( addpubshelvesloop => $pubshelves);
 }
 
-if (@barshelves) {
-        $template->param( addbarshelves     => scalar (@barshelves));
-        $template->param( addbarshelvesloop => @barshelves);
+if (@{$barshelves}) {
+        $template->param( addbarshelves     => scalar @{$barshelves});
+        $template->param( addbarshelvesloop => $barshelves);
 }