From 39f1832111a690690e9418e8daa7298708ffacbe Mon Sep 17 00:00:00 2001
From: Ian Walls <ian.walls@bywatersolutions.com>
Date: Thu, 16 Feb 2012 12:09:44 -0500
Subject: [PATCH] Bug 7401 - Shelving Location facet instead of Branch facet when only 1 branch configured

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Enable for either
A) SingleBranchMode is enabled
or
B) There is only one branch in the branches table

Signed-off-by: Nicole C. Engard <nengard@bywatersolutions.com>

Tested with one branch and with multiple branches, tested with
SingleBranchMode on and off. All tests passed.
---
 C4/Branch.pm                                       |   10 ++++
 C4/Koha.pm                                         |   58 ++++++++++++++------
 C4/Search.pm                                       |    7 ++-
 .../intranet-tmpl/prog/en/includes/facets.inc      |    1 +
 .../opac-tmpl/prog/en/includes/opac-facets.inc     |    1 +
 opac/opac-search.pl                                |    2 +-
 6 files changed, 60 insertions(+), 19 deletions(-)

diff --git a/C4/Branch.pm b/C4/Branch.pm
index 2153028..4b7d11f 100644
--- a/C4/Branch.pm
+++ b/C4/Branch.pm
@@ -46,6 +46,7 @@ BEGIN {
 		&DelBranchCategory
 	        &CheckCategoryUnique
 		&mybranch
+		&GetBranchesCount
 	);
 	@EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
 }
@@ -580,6 +581,15 @@ sub get_branch_code_from_name {
    return $sth->fetchrow_array;
 }
 
+sub GetBranchesCount {
+    my $dbh = C4::Context->dbh();
+    my $query = "SELECT COUNT(*) AS branches_count FROM branches";
+    my $sth = $dbh->prepare( $query );
+    $sth->execute();
+    my $row = $sth->fetchrow_hashref();
+    return $row->{'branches_count'};
+}
+
 1;
 __END__
 
diff --git a/C4/Koha.pm b/C4/Koha.pm
index ebb3eed..9c36849 100644
--- a/C4/Koha.pm
+++ b/C4/Koha.pm
@@ -24,7 +24,7 @@ use strict;
 #use warnings; FIXME - Bug 2505
 
 use C4::Context;
-
+use C4::Branch qw(GetBranchesCount);
 use Memoize;
 use DateTime;
 use DateTime::Format::MySQL;
@@ -713,14 +713,27 @@ sub getFacets {
                 tags  => [ qw/ 225a / ],
                 sep   => ', ',
             },
-        ];
-        my $library_facet = {
-            idx   => 'branch',
-            label => 'Libraries',
-            tags  => [ qw/ 995b / ],
-            expanded => '1',
-        };
-        push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
+            ];
+
+            my $library_facet;
+            unless ( C4::Context->preference("singleBranchMode") || GetBranchesCount() == 1 ) {
+                $library_facet = {
+                    link_value  => 'branch',
+                    label_value => 'Libraries',
+                    tags        => [ '995', ],
+                    subfield    => 'b',
+                    expanded    => '1',
+                };
+            } else {
+                $library_facet = {
+                    link_value  => 'location',
+                    label_value => 'Location',
+                    tags        => [ '995' ],
+                    subfield    => 'c',
+                    expanded    => '1',
+                };
+            }
+            push( @$facets, $library_facet );
     }
     else {
         $facets = [
@@ -761,15 +774,26 @@ sub getFacets {
                 sep   => ', ',
             },
             ];
+            
             my $library_facet;
-            $library_facet = {
-                idx   => 'branch',
-                label => 'Libraries',
-                tags  => [ qw/ 952b / ],
-                sep   => ', ',
-                expanded    => '1',
-            };
-            push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
+            unless ( C4::Context->preference("singleBranchMode") || GetBranchesCount() == 1 ) {
+                $library_facet = {
+                    link_value  => 'branch',
+                    label_value => 'Libraries',
+                    tags        => [ '952', ],
+                    subfield    => 'b',
+                    expanded    => '1',
+                };
+            } else {
+                $library_facet = {
+                    link_value => 'location',
+                    label_value => 'Location',
+                    tags => [ '952' ],
+                    subfield => 'c',
+                    expanded => '1',
+                };
+            }
+            push( @$facets, $library_facet );
     }
     return $facets;
 }
diff --git a/C4/Search.pm b/C4/Search.pm
index c786080..2762c41 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -302,7 +302,7 @@ sub getRecords {
     my (
         $koha_query,       $simple_query, $sort_by_ref,    $servers_ref,
         $results_per_page, $offset,       $expanded_facet, $branches,
-        $query_type,       $scan
+        $query_type,       $scan,         $opac
     ) = @_;
 
     my @servers = @$servers_ref;
@@ -556,6 +556,11 @@ sub getRecords {
 								}
                             }
 
+                            # also, if it's a location code, use the name instead of the code
+                            if ( $link_value =~ /location/ ) {
+                                $facet_label_value = GetKohaAuthorisedValueLib('LOC', $one_facet, $opac) || '*';
+                            }
+
                             # but we're down with the whole label being in the link's title.
                             push @this_facets_array, {
                                 facet_count       => $facets_counter->{$link_value}->{$one_facet},
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc
index e74bced..4b719c4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc
@@ -16,6 +16,7 @@
 [% IF ( facets_loo.type_label_Places ) %]Places[% END %]
 [% IF ( facets_loo.type_label_Series ) %]Series[% END %]
 [% IF ( facets_loo.type_label_Libraries ) %]Libraries[% END %]
+[% IF ( facets_loo.type_label_Location ) %]Locations[% END %]
 <ul>
         [% FOREACH facet IN facets_loo.facets %]<li><a href="/cgi-bin/koha/catalogue/search.pl?[% query_cgi |html %][% limit_cgi |html %][% IF ( sort_by ) %]&amp;sort_by=[% sort_by %][% END %]&amp;limit=[% facet.type_link_value %]:[% facet.facet_link_value %]" title="[% facet.facet_title_value %]">[% facet.facet_label_value %]</a> [% IF ( displayFacetCount ) %]([% facet.facet_count %])[% END %]</li>[% END %][% IF ( facets_loo.expandable ) %]
         <li class="showmore"><a href="/cgi-bin/koha/catalogue/search.pl?[% query_cgi |html %][% limit_cgi |html %][% IF ( sort_by ) %]&amp;sort_by=[% sort_by %][% END %][% IF ( offset ) %]&amp;offset=[% offset %][% END %]&amp;expand=[% facets_loo.expand %]#[% facets_loo.type_id %]">Show more</a></li>
diff --git a/koha-tmpl/opac-tmpl/prog/en/includes/opac-facets.inc b/koha-tmpl/opac-tmpl/prog/en/includes/opac-facets.inc
index b3c1bc3..661c525 100644
--- a/koha-tmpl/opac-tmpl/prog/en/includes/opac-facets.inc
+++ b/koha-tmpl/opac-tmpl/prog/en/includes/opac-facets.inc
@@ -17,6 +17,7 @@
 [% UNLESS ( singleBranchMode ) %]
 [% IF ( facets_loo.type_label_Libraries ) %]Libraries[% END %]
 [% END %]
+[% IF ( facets_loo.type_label_Location ) %]Locations[% END %]
 <ul>
         [% FOREACH facet IN facets_loo.facets %]<li><a href="/cgi-bin/koha/opac-search.pl?[% query_cgi |html %][% limit_cgi |html %][% IF ( sort_by ) %]&amp;sort_by=[% sort_by %][% END %]&amp;limit=[% facet.type_link_value %]:[% facet.facet_link_value %]" title="[% facet.facet_title_value |html %]">[% facet.facet_label_value %]</a> [% IF ( displayFacetCount ) %]([% facet.facet_count %])[% END %]</li>[% END %][% IF ( facets_loo.expandable ) %]
         <li class="showmore"><a href="/cgi-bin/koha/opac-search.pl?[% query_cgi |html %][% limit_cgi |html %][% IF ( sort_by ) %]&amp;sort_by=[% sort_by %][% END %][% IF ( offset ) %]&amp;offset=[% offset %][% END %]&amp;expand=[% facets_loo.expand %]#[% facets_loo.type_id %]">Show more</a></li>
diff --git a/opac/opac-search.pl b/opac/opac-search.pl
index 5f3ad86..2665a91 100755
--- a/opac/opac-search.pl
+++ b/opac/opac-search.pl
@@ -475,7 +475,7 @@ elsif (C4::Context->preference('NoZebra')) {
     $pasarParams .= '&amp;simple_query=' . $simple_query;
     $pasarParams .= '&amp;query_type=' . $query_type if ($query_type);
     eval {
-        ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
+        ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan,1);
     };
 }
 # This sorts the facets into alphabetical order
-- 
1.7.2.5