Merge remote branch 'kc/new/enh/bug_3659' into kcmaster
[koha.git] / catalogue / search.pl
index adb3cc7..f3f6008 100755 (executable)
@@ -15,9 +15,9 @@
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 =head1 NAME
 
@@ -79,7 +79,7 @@ There are several additional secondary functions performed that I will
 not cover in detail.
 
 =head3 1. Building Query Strings
-    
+
 There are several types of queries needed in the process of search and retrieve:
 
 =over
@@ -132,6 +132,7 @@ Not yet completed...
 =cut
 
 use strict;            # always use
+#use warnings; FIXME - Bug 2505
 
 ## STEP 1. Load things that are used in both search page and
 # results page and decide which template to load, operations 
@@ -144,10 +145,12 @@ use C4::Auth qw(:DEFAULT get_session);
 use C4::Search;
 use C4::Languages qw(getAllLanguages);
 use C4::Koha;
+use C4::Members qw(GetMember);
 use C4::VirtualShelves qw(GetRecentShelves);
 use POSIX qw(ceil floor);
 use C4::Branch; # GetBranches
 
+my $DisplayMultiPlaceHold = C4::Context->preference("DisplayMultiPlaceHold");
 # create a new CGI object
 # FIXME: no_undef_params needs to be tested
 use CGI qw('-no_undef_params');
@@ -179,6 +182,16 @@ if (C4::Context->preference("marcflavour") eq "UNIMARC" ) {
     $template->param('UNIMARC' => 1);
 }
 
+if($cgi->cookie("holdfor")){ 
+    my $holdfor_patron = GetMember('borrowernumber' => $cgi->cookie("holdfor"));
+    $template->param(
+        holdfor => $cgi->cookie("holdfor"),
+        holdfor_surname => $holdfor_patron->{'surname'},
+        holdfor_firstname => $holdfor_patron->{'firstname'},
+        holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
+    );
+}
+
 ## URI Re-Writing
 # Deprecated, but preserved because it's interesting :-)
 # The same thing can be accomplished with mod_rewrite in
@@ -205,16 +218,20 @@ if (C4::Context->preference("marcflavour") eq "UNIMARC" ) {
 
 # 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');
 
@@ -497,6 +514,12 @@ if (C4::Context->preference('NoZebra')) {
         ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
     };
 }
+# This sorts the facets into alphabetical order
+if ($facets) {
+    foreach my $f (@$facets) {
+        $f->{facets} = [ sort { uc($a->{facet_title_value}) cmp uc($b->{facet_title_value}) } @{ $f->{facets} } ];
+    }
+}
 if ($@ || $error) {
     $template->param(query_error => $error.$@);
     output_html_with_http_headers $cgi, $cookie, $template->output;
@@ -511,7 +534,8 @@ for (my $i=0;$i<@servers;$i++) {
     if ($server =~/biblioserver/) { # this is the local bibliographic server
         $hits = $results_hashref->{$server}->{"hits"};
         my $page = $cgi->param('page') || 0;
-        my @newresults = searchResults( $query_desc,$hits,$results_per_page,$offset,$scan,@{$results_hashref->{$server}->{"RECORDS"}});
+        my @newresults = searchResults('intranet', $query_desc, $hits, $results_per_page, $offset, $scan,
+                                       @{$results_hashref->{$server}->{"RECORDS"}});
         $total = $total + $results_hashref->{$server}->{"hits"};
         ## If there's just one result, redirect to the detail page
         if ($total == 1) {         
@@ -540,6 +564,8 @@ for (my $i=0;$i<@servers;$i++) {
             $template->param(query_cgi => $query_cgi);
             $template->param(query_desc => $query_desc);
             $template->param(limit_desc => $limit_desc);
+            $template->param(offset     => $offset);
+            $template->param(DisplayMultiPlaceHold => $DisplayMultiPlaceHold);
                        $template->param (z3950_search_params => C4::Search::z3950_search_args($query_desc));
             if ($query_desc || $limit_desc) {
                 $template->param(searchdesc => 1);