From 141200794da9f27e0231e1c2dad4bcb994d650f3 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 3 Dec 2015 14:35:05 +0000 Subject: [PATCH] Bug 15295: Koha::Libraries - Remove GetBranchCategories MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Test plan 1/ enable OpacAddMastheadLibraryPulldown 2/ Defined a group of libraries as searchdomain and tick 'show in pull down' 3/ At the OPAC, go on the advanced search form, limit by the group of libraries you have just created. 4/ The group should be selected by default in the dropdown list Signed-off-by: Marc Véron Signed-off-by: Martin Renvoize http://bugs.koha-community.org/show_bug.cgi?id=15294 Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com --- C4/Auth.pm | 5 +- C4/Branch.pm | 50 ++----------------- catalogue/search.pl | 3 +- .../bootstrap/en/includes/masthead.inc | 2 +- opac/opac-search.pl | 7 +-- t/db_dependent/Branch.t | 20 +++----- 6 files changed, 21 insertions(+), 66 deletions(-) diff --git a/C4/Auth.pm b/C4/Auth.pm index d7be1d8b53..cda7ae0725 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -32,6 +32,7 @@ use C4::Branch; # GetBranches use C4::Search::History; use Koha; use Koha::AuthUtils qw(hash_password); +use Koha::LibraryCategories; use POSIX qw/strftime/; use List::MoreUtils qw/ any /; use Encode qw( encode is_utf8); @@ -501,12 +502,14 @@ sub get_template_and_user { $opac_name = C4::Context->userenv->{'branch'}; } + my $library_categories = Koha::LibraryCategories->search({categorytype => 'searchdomain', show_in_pulldown => 1}, { order_by => ['categorytype', 'categorycode']}); $template->param( OpacAdditionalStylesheet => C4::Context->preference("OpacAdditionalStylesheet"), AnonSuggestions => "" . C4::Context->preference("AnonSuggestions"), AuthorisedValueImages => C4::Context->preference("AuthorisedValueImages"), BranchesLoop => GetBranchesLoop($opac_name), - BranchCategoriesLoop => GetBranchCategories( 'searchdomain', 1, $opac_name ), + BranchCategoriesLoop => $library_categories, + opac_name => $opac_name, LibraryName => "" . C4::Context->preference("LibraryName"), LibraryNameTitle => "" . $LibraryNameTitle, LoginBranchname => C4::Context->userenv ? C4::Context->userenv->{"branchname"} : "", diff --git a/C4/Branch.pm b/C4/Branch.pm index 2397fd0fb3..2e9a9c67de 100644 --- a/C4/Branch.pm +++ b/C4/Branch.pm @@ -20,6 +20,7 @@ use strict; #use warnings; FIXME - Bug 2505 require Exporter; use C4::Context; +use Koha::LibraryCategories; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @@ -37,7 +38,6 @@ BEGIN { &ModBranch &GetBranchInfo &GetCategoryTypes - &GetBranchCategories &GetBranchesInCategory &ModBranchCategoryInfo &mybranch @@ -243,9 +243,9 @@ sub ModBranch { } # sort out the categories.... my @checkedcats; - my $cats = GetBranchCategories(); - foreach my $cat (@$cats) { - my $code = $cat->{'categorycode'}; + my @cats = Koha::LibraryCategories->search; + foreach my $cat (@cats) { + my $code = $cat->categorycode; if ( $data->{$code} ) { push( @checkedcats, $code ); } @@ -283,48 +283,6 @@ sub ModBranch { } } -=head2 GetBranchCategories - - my $categories = GetBranchCategories($categorytype,$show_in_pulldown,$selected_in_pulldown); - -Returns a list ref of anon hashrefs with keys eq columns of branchcategories table, -i.e. categorydescription, categorytype, categoryname. - -=cut - -sub GetBranchCategories { - my ( $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_; - my $dbh = C4::Context->dbh(); - - my $query = "SELECT * FROM branchcategories "; - - my ( @where, @bind ); - if ( $categorytype ) { - push @where, " categorytype = ? "; - push @bind, $categorytype; - } - - if ( defined( $show_in_pulldown ) ) { - push( @where, " show_in_pulldown = ? " ); - push( @bind, $show_in_pulldown ); - } - - $query .= " WHERE " . join(" AND ", @where) if(@where); - $query .= " ORDER BY categorytype, categorycode"; - my $sth=$dbh->prepare( $query); - $sth->execute(@bind); - - my $branchcats = $sth->fetchall_arrayref({}); - - if ( $selected_in_pulldown ) { - foreach my $bc ( @$branchcats ) { - $bc->{selected} = 1 if $bc->{categorycode} eq $selected_in_pulldown; - } - } - - return $branchcats; -} - =head2 GetCategoryTypes $categorytypes = GetCategoryTypes; diff --git a/catalogue/search.pl b/catalogue/search.pl index 69f6ac443e..25b9855a91 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -152,6 +152,7 @@ use POSIX qw(ceil floor); use C4::Branch; # GetBranches use C4::Search::History; +use Koha::LibraryCategories; use Koha::Virtualshelves; use URI::Escape; @@ -243,7 +244,7 @@ my @branch_loop = map { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches; -my $categories = GetBranchCategories('searchdomain'); +my $categories = Koha::LibraryCategories->search( { categorytype => 'searchdomain' }, { order_by => [ 'categorytype', 'categorycode' ] } ); $template->param(branchloop => \@branch_loop, searchdomainloop => $categories); diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc index 5a2e011a39..1afeae67b5 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc @@ -234,7 +234,7 @@ [% FOREACH bc IN BranchCategoriesLoop %] - [% IF ( bc.selected ) %] + [% IF bc.categorycode == opac_name %] [% ELSE %] diff --git a/opac/opac-search.pl b/opac/opac-search.pl index bc703b3a0a..84524ef2b0 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -43,6 +43,8 @@ use C4::SocialData; use C4::Ratings; use C4::External::OverDrive; +use Koha::LibraryCategories; + use POSIX qw(ceil floor strftime); use URI::Escape; use JSON qw/decode_json encode_json/; @@ -201,9 +203,8 @@ if ($cgi->cookie("search_path_code")) { } my $branches = GetBranches(); # used later in *getRecords, probably should be internalized by those functions after caching in C4::Branch is established -$template->param( - searchdomainloop => GetBranchCategories('searchdomain'), -); +my $library_categories = Koha::LibraryCategories->search( { categorytype => 'searchdomain' }, { order_by => [ 'categorytype', 'categorycode' ] } ); +$template->param( searchdomainloop => $library_categories ); # load the language limits (for search) my $languages_limit_loop = getLanguages($lang, 1); diff --git a/t/db_dependent/Branch.t b/t/db_dependent/Branch.t index 3ca302f5ac..afac90e63d 100644 --- a/t/db_dependent/Branch.t +++ b/t/db_dependent/Branch.t @@ -21,7 +21,7 @@ use Modern::Perl; use C4::Context; use Data::Dumper; -use Test::More tests => 26; +use Test::More tests => 24; use C4::Branch; use Koha::Libraries; @@ -43,7 +43,6 @@ can_ok( ModBranch GetBranchInfo GetCategoryTypes - GetBranchCategories GetBranchesInCategory ModBranchCategoryInfo mybranch @@ -165,8 +164,7 @@ $b1->{issuing} = undef; is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA"); #Test categories -my $categories = GetBranchCategories(); -my $count_cat = scalar( @$categories ); +my $count_cat = Koha::LibraryCategories->search->count; my $cat1 = { add => 1, @@ -201,18 +199,16 @@ ModBranchCategoryInfo({ ModBranchCategoryInfo($cat1); ModBranchCategoryInfo($cat2); -$categories = GetBranchCategories(); -is( scalar( @$categories ), $count_cat + 3, "Two categories added" ); +my $categories = Koha::LibraryCategories->search; +is( $categories->count, $count_cat + 3, "Two categories added" ); delete $cat1->{add}; delete $cat2->{add}; delete $new_category{add}; -is_deeply($categories, [ $cat1,$cat2,\%new_category ], 'retrieve all expected library categories (bug 10515)'); my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete; is( $del, 1, 'One row affected' ); -$categories = GetBranchCategories(); -is( scalar( @$categories ), $count_cat + 2, "Category CAT2 deleted" ); +is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" ); $b2->{CAT1} = 1; ModBranch($b2); @@ -232,8 +228,7 @@ $b2->{categories} = \@cat; is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' ); ModBranchCategoryInfo({add => 1,%$cat2}); -$categories = GetBranchCategories(); -is( scalar( @$categories ), $count_cat + 3, "Two categories added" ); +is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two catgories added" ); $b2 = { branchcode => 'BRB', branchname => 'BranchB', @@ -302,9 +297,6 @@ is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' ); my @category_types = GetCategoryTypes(); is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types'); -$categories = GetBranchCategories(undef, undef, 'LIBCATCODE'); -is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)'); - #TODO later: test mybranchine and onlymine # Actually we cannot mock C4::Context->userenv in unit tests -- 2.39.5