@@ -, +, @@ --- C4/Auth.pm | 5 ++- C4/Branch.pm | 50 ++-------------------- catalogue/search.pl | 3 +- .../opac-tmpl/bootstrap/en/includes/masthead.inc | 2 +- opac/opac-search.pl | 7 +-- t/db_dependent/Branch.t | 20 +++------ 6 files changed, 21 insertions(+), 66 deletions(-) --- a/C4/Auth.pm +++ a/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"} : "", --- a/C4/Branch.pm +++ a/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; --- a/catalogue/search.pl +++ a/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; @@ -244,7 +245,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); --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc +++ a/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 %] --- a/opac/opac-search.pl +++ a/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); --- a/t/db_dependent/Branch.t +++ a/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 --