From 1d6b20a142a4baee584c21f0c0353e804774c110 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Thu, 19 Dec 2013 22:59:28 -0500 Subject: [PATCH] Bug 7567 - Use, Display, Filter News by Branch With the branch parameter added to key functions in C4/NewsChannels.pm, function calls in C4/Members.pm, mainpage.pl, opac/opac-main.pl, tools/koha-news.pl, and t/db_dependent/NewsChannels.t were needed. Some license texts were updated. Templates were modified to display, allow for entry and editing of the branches selected. TEST PLAN --------- 1) Having logged into the staff client, is the news displaying correctly? Have you entered a news item which should not display for this branch of logged in user? 2) Find a patron (with some items checked out?) 3) Print a slip - News which is labelled 'All Branches' or for the same branch as the one printing the slip should display on the slip. - THIS DOES NOT AFFECT QUICK SLIPS 4) Home -> Tools -> News - Can you edit a news item? - Does the change save correctly? - Can you filter based on location and branch correctly? - Can you add a new entry correctly? - Can you delete an entry correctly? 5) Open an OPAC client. - Does only the news for all branches display? 6) Log into the OPAC client. - Does the news for all branches and the specific branch display? 7) prove -v t/db_dependent/NewsChannels.t - Does it run and all succeed? - Does the code seem to catch the required cases? 8) Comparing the patched and unpatched versions of files affected, are the license changes missing anything? --- C4/Members.pm | 2 +- C4/NewsChannels.pm | 43 ++++++++++++---- .../prog/en/modules/tools/koha-news.tt | 52 +++++++++++++++++--- koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt | 4 +- mainpage.pl | 36 ++++++++------ opac/opac-main.pl | 33 +++++++------ t/db_dependent/NewsChannels.t | 19 +++++-- tools/koha-news.pl | 17 +++++-- 8 files changed, 151 insertions(+), 55 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index b04cc64..20d8ed2 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -2425,7 +2425,7 @@ sub IssueSlip { 'news' => [ map { $_->{'timestamp'} = $_->{'newdate'}; { opac_news => $_ } - } @{ GetNewsToDisplay("slip") } ], + } @{ GetNewsToDisplay("slip",$branch) } ], ); } diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm index 1e060ff..3e21285 100644 --- a/C4/NewsChannels.pm +++ b/C4/NewsChannels.pm @@ -125,7 +125,12 @@ sub del_opac_new { sub get_opac_new { my ($idnew) = @_; my $dbh = C4::Context->dbh; - my $query = q{ SELECT * FROM opac_news WHERE idnew = ? }; + my $query = q{ + SELECT opac_news.*,branches.branchname + FROM opac_news LEFT JOIN branches + ON opac_news.branchcode=branches.branchcode + WHERE opac_news.idnew = ?; + }; my $sth = $dbh->prepare($query); $sth->execute($idnew); my $data = $sth->fetchrow_hashref; @@ -136,14 +141,32 @@ sub get_opac_new { } sub get_opac_news { - my ($limit, $lang) = @_; + my ($limit, $lang, $branchcode) = @_; my @values; my $dbh = C4::Context->dbh; - my $query = q{ SELECT *, timestamp AS newdate FROM opac_news }; - if ($lang) { - $query.= " WHERE (lang='' OR lang=?)"; + my $query = q{ + SELECT opac_news.*, branches.branchname, + timestamp AS newdate + FROM opac_news LEFT JOIN branches + ON opac_news.branchcode=branches.branchcode + }; + if ($lang && $branchcode) { + $query .= " WHERE (opac_news.lang='' OR opac_news.lang=?)"; + $query .= " AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)"; + push @values,$lang; + push @values,$branchcode; + } + elsif ($lang) { + $query .= " WHERE (opac_news.lang='' OR opac_news.lang=?)"; push @values,$lang; } + elsif ($branchcode) { + $query .= " WHERE (opac_news.branchcode IS NULL OR opac_news.branchcode=?)"; + push @values,$branchcode; + } + else { + # nothing + } $query.= ' ORDER BY timestamp DESC '; #if ($limit) { # $query.= 'LIMIT 0, ' . $limit; @@ -165,14 +188,15 @@ sub get_opac_news { =head2 GetNewsToDisplay - $news = &GetNewsToDisplay($lang); + $news = &GetNewsToDisplay($lang,$branch); C<$news> is a ref to an array which containts - all news with expirationdate > today or expirationdate is null. + all news with expirationdate > today or expirationdate is null + that is applicable for a given branch. =cut sub GetNewsToDisplay { - my ($lang) = @_; + my ($lang,$branch) = @_; my $dbh = C4::Context->dbh; # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate my $query = q{ @@ -185,13 +209,14 @@ sub GetNewsToDisplay { ) AND `timestamp` < CURRENT_DATE()+1 AND (lang = '' OR lang = ?) + AND (branchcode IS NULL OR branchcode = ?) ORDER BY number }; # expirationdate field is NOT in ISO format? # timestamp has HH:mm:ss, CURRENT_DATE generates 00:00:00 # by adding 1, that captures today correctly. my $sth = $dbh->prepare($query); $lang = $lang // q{}; - $sth->execute($lang); + $sth->execute($lang,$branch); my @results; while ( my $row = $sth->fetchrow_hashref ){ $row->{newdate} = format_date($row->{newdate}); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt index 48f6025..834a2cc 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt @@ -75,7 +75,7 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %]
OPAC and Koha news
  1. - +
  2. + + +
  3. +
  4. @@ -154,7 +171,26 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %] [% IF ( lang_lis.language == lang ) %] [% ELSE %] - + + [% END %] + [% END %] + + + @@ -167,6 +203,7 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %]   Location + Branch Number Creation date Expiration date @@ -184,17 +221,20 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %] [% SWITCH opac_new.lang %] - [% CASE "koha" %] + [% CASE 'koha' %] Librarian interface - [% CASE "slip" %] + [% CASE 'slip' %] Slip - [% CASE "" %] + [% CASE '' %] All [% CASE %] OPAC ([% opac_new.lang %]) [% END %] - + [% IF ( opac_new.branchcode == "" ) -%] + All Branches + [% ELSE %][% opac_new.branchname %] + [% END %] [% opac_new.number %] [% opac_new.newdate %] [% opac_new.expirationdate %] [% IF ( opac_new.expired ) %](expired)[% END %] diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt index eb1e600..df368ba 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt @@ -21,8 +21,8 @@
    [% END %]
    - [% IF ( koha_news_count ) %]
    + [% IF ( koha_news_count ) %] [% FOREACH koha_new IN koha_news %] @@ -30,8 +30,8 @@

    (published on [% koha_new.newdate %])

    [% END %]
    [% koha_new.title %]
    -
    [% END %] +
    [% IF ( display_daily_quote && daily_quote ) %]

    Quote of the Day

    [% daily_quote.text %] ~ [% daily_quote.source %]
    diff --git a/mainpage.pl b/mainpage.pl index 4159618..a0de153 100755 --- a/mainpage.pl +++ b/mainpage.pl @@ -1,30 +1,30 @@ #!/usr/bin/perl +# This file is part of Koha. +# # Copyright Paul Poulain 2002 # Parts Copyright Liblime 2007 +# Copyright (C) 2013 Mark Tompsett # -# This file is part of Koha. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# 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., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -use strict; -use warnings; +use Modern::Perl; use CGI; use C4::Output; use C4::Auth; use C4::Koha; -use C4::NewsChannels; +use C4::NewsChannels; # GetNewsToDisplay use C4::Review qw/numberofreviews/; use C4::Suggestions qw/CountSuggestion/; use C4::Tags qw/get_count_by_tag_status/; @@ -42,7 +42,11 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( } ); -my $all_koha_news = &GetNewsToDisplay("koha"); +my $homebranch; +if (C4::Context->userenv) { + $homebranch = C4::Context->userenv->{'branch'}; +} +my $all_koha_news = &GetNewsToDisplay("koha",$homebranch); my $koha_news_count = scalar @$all_koha_news; $template->param( diff --git a/opac/opac-main.pl b/opac/opac-main.pl index 7c6ee36..a58659c 100755 --- a/opac/opac-main.pl +++ b/opac/opac-main.pl @@ -2,26 +2,27 @@ # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Parts Copyright (C) 2013 Mark Tompsett # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# 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. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY 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, see . -use strict; -use warnings; +use Modern::Perl; use CGI; use C4::Auth; # get_template_and_user use C4::Output; -use C4::NewsChannels; # get_opac_news +use C4::NewsChannels; # GetNewsToDisplay use C4::Languages qw(getTranslatedLanguages accept_language); use C4::Koha qw( GetDailyQuote ); @@ -48,7 +49,11 @@ $template->param( # use cookie setting for language, bug default to syspref if it's not set my ($theme, $news_lang, $availablethemes) = C4::Templates::themelanguage(C4::Context->config('opachtdocs'),'opac-main.tt','opac',$input); -my $all_koha_news = &GetNewsToDisplay($news_lang); +my $homebranch; +if (C4::Context->userenv) { + $homebranch = C4::Context->userenv->{'branch'}; +} +my $all_koha_news = &GetNewsToDisplay($news_lang,$homebranch); my $koha_news_count = scalar @$all_koha_news; my $quote = GetDailyQuote(); # other options are to pass in an exact quote id or select a random quote each pass... see perldoc C4::Koha diff --git a/t/db_dependent/NewsChannels.t b/t/db_dependent/NewsChannels.t index 0e34cb0..f2a29e4 100644 --- a/t/db_dependent/NewsChannels.t +++ b/t/db_dependent/NewsChannels.t @@ -23,6 +23,13 @@ my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; +# Add LIB1, if it doesn't exist. +my $addbra = 'LIB1'; +if ( !GetBranchName($addbra) ) { + $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) }, + undef, ( $addbra, "$addbra branch" ) ); +} + # Test add_opac_new my $rv = add_opac_new(); # intentionally bad ok( $rv == 0, 'Correctly failed on no parameter!' ); @@ -38,6 +45,7 @@ my $href_entry1 = { expirationdate => $expirationdate1, timestamp => $timestamp1, number => $number1, + branchcode => 'LIB1', }; $rv = add_opac_new($href_entry1); @@ -52,6 +60,7 @@ my $href_entry2 = { expirationdate => $expirationdate2, timestamp => $timestamp2, number => $number2, + branchcode => 'LIB1', }; $rv = add_opac_new($href_entry2); ok( $rv == 1, 'Successfully added the second dummy news item!' ); @@ -107,11 +116,13 @@ if ( $hashref_check->{number} ne $number2 ) { $failure = $F6; } ok( $failure == 0, "Successfully tested get_opac_new id2 ($failure)!" ); # Test get_opac_news (multiple news items) -my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{} ); -ok( $opac_news_count >= 2, 'Successfully tested get_opac_news!' ); +my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' ); + +# using >= 2, because someone may have LIB1 news already. +ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' ); # Test GetNewsToDisplay -( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay(q{}); -ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay!' ); +( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' ); +ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' ); $dbh->rollback; diff --git a/tools/koha-news.pl b/tools/koha-news.pl index 5170433..f5ef7be 100755 --- a/tools/koha-news.pl +++ b/tools/koha-news.pl @@ -33,6 +33,7 @@ use C4::Output; use C4::NewsChannels; use C4::Languages qw(getTranslatedLanguages); use Date::Calc qw/Date_to_Days Today/; +use C4::Branch qw/GetBranches/; my $cgi = new CGI; @@ -43,6 +44,10 @@ my $expirationdate = format_date_in_iso($cgi->param('expirationdate')); my $timestamp = format_date_in_iso($cgi->param('timestamp')); my $number = $cgi->param('number'); my $lang = $cgi->param('lang'); +my $branchcode = $cgi->param('branch'); +# Foreign Key constraints work with NULL, not '' +# NULL = All branches. +$branchcode = undef if (defined($branchcode) && $branchcode eq ''); my $new_detail = get_opac_new($id); @@ -68,9 +73,13 @@ foreach my $language ( @$tlangs ) { }; } -$template->param( lang_list => \@lang_list ); +my $branches = GetBranches; -my $op = $cgi->param('op'); +$template->param( lang_list => \@lang_list, + branch_list => $branches, + branchcode => $branchcode ); + +my $op = $cgi->param('op') // ''; if ( $op eq 'add_form' ) { $template->param( add_form => 1 ); @@ -94,6 +103,7 @@ elsif ( $op eq 'add' ) { expirationdate => $expirationdate, timestamp => $timestamp, number => $number, + branchcode => $branchcode, }; add_opac_new( $hashref ); print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl"); @@ -107,6 +117,7 @@ elsif ( $op eq 'edit' ) { expirationdate => $expirationdate, timestamp => $timestamp, number => $number, + branchcode => $branchcode, }; upd_opac_new( $hashref ); print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl"); @@ -119,7 +130,7 @@ elsif ( $op eq 'del' ) { else { - my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang ); + my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang, $branchcode ); foreach my $new ( @$opac_news ) { next unless $new->{'expirationdate'}; -- 1.7.9.5