From 9ca8d08a51f823a55998dd8c36f4bf2aae7b71aa Mon Sep 17 00:00:00 2001 From: Josef Moravec Date: Wed, 20 Mar 2019 07:36:53 +0000 Subject: [PATCH] Bug 22544: Move GetNewsToDisplay to Koha namespace Signed-off-by: David Nind Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Bug 22544: fix count call - to squash Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- C4/Members.pm | 23 +++-- C4/NewsChannels.pm | 94 ------------------- Koha/Template/Plugin/KohaNews.pm | 15 ++- .../prog/en/modules/intranet-main.tt | 9 +- .../bootstrap/en/includes/html_helpers.inc | 2 +- .../bootstrap/en/modules/opac-main.tt | 17 ++-- .../bootstrap/en/modules/opac-news-rss.tt | 2 +- mainpage.pl | 16 ++-- opac/opac-main.pl | 22 +++-- opac/opac-news-rss.pl | 19 ++-- t/db_dependent/NewsChannels.t | 67 ------------- tools/koha-news.pl | 1 - 12 files changed, 81 insertions(+), 206 deletions(-) delete mode 100644 C4/NewsChannels.pm delete mode 100755 t/db_dependent/NewsChannels.t diff --git a/C4/Members.pm b/C4/Members.pm index c2ef801294..edb33bdcec 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -33,7 +33,6 @@ use C4::Reserves; use C4::Accounts; use C4::Biblio; use C4::Letters; -use C4::NewsChannels; #get slip news use DateTime; use Koha::Database; use Koha::DateUtils; @@ -41,6 +40,7 @@ use Koha::AuthUtils qw(hash_password); use Koha::Database; use Koha::Holds; use Koha::List::Patron; +use Koha::News; use Koha::Patrons; use Koha::Patron::Categories; @@ -588,11 +588,22 @@ sub IssueSlip { issues => $all, }; } - my $news = GetNewsToDisplay( "slip", $branch ); - my @news = map { - $_->{'timestamp'} = $_->{'newdate'}; - { opac_news => $_ } - } @$news; + my $news = Koha::News->search({ + lang => [ 'slip', '' ], + branchcode => [ $branch, undef ], + -or => [ expirationdate => { '>=' => \'NOW()' }, + expirationdate => undef ] + },{ + order_by => 'number' + } + ); + my @news; + while ( my $n = $news->next ) { + my $all = $n->unblessed_all_relateds; + push @news, { + opac_news => $all, + }; + } $letter_code = 'ISSUESLIP'; %repeat = ( checkedout => \@checkouts, diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm deleted file mode 100644 index 2a50a2a28c..0000000000 --- a/C4/NewsChannels.pm +++ /dev/null @@ -1,94 +0,0 @@ -package C4::NewsChannels; - -# This file is part of Koha. -# -# Copyright (C) 2000-2002 Katipo Communications -# Copyright (C) 2013 Mark Tompsett -# -# 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 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 Modern::Perl; -use C4::Context; -use Koha::DateUtils; -use C4::Log qw(logaction); -use Koha::News; - -use vars qw(@ISA @EXPORT); - -BEGIN { - @ISA = qw(Exporter); - @EXPORT = qw( - &GetNewsToDisplay - ); -} - -=head1 NAME - -C4::NewsChannels - Functions to manage OPAC and intranet news - -=head1 DESCRIPTION - -This module provides the functions needed to mange OPAC and intranet news. - -=head1 FUNCTIONS - -=cut - -=head2 GetNewsToDisplay - - $news = &GetNewsToDisplay($lang,$branch); - C<$news> is a ref to an array which contains - all news with expirationdate > today or expirationdate is null - that is applicable for a given branch. - -=cut - -sub GetNewsToDisplay { - my ($lang,$branch) = @_; - my $dbh = C4::Context->dbh; - my $query = q{ - SELECT opac_news.*,published_on AS newdate, - borrowers.title AS author_title, - borrowers.firstname AS author_firstname, - borrowers.surname AS author_surname - FROM opac_news - LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber - WHERE ( - expirationdate >= CURRENT_DATE() - OR expirationdate IS NULL - ) - AND published_on <= CURDATE() - AND (opac_news.lang = '' OR opac_news.lang = ?) - AND (opac_news.branchcode IS NULL OR opac_news.branchcode = ?) - ORDER BY number - }; - my $sth = $dbh->prepare($query); - $lang = $lang // q{}; - $sth->execute($lang,$branch); - my @results; - while ( my $row = $sth->fetchrow_hashref ){ - $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 }); - push @results, $row; - } - return \@results; -} - -1; -__END__ - -=head1 AUTHOR - -TG - -=cut diff --git a/Koha/Template/Plugin/KohaNews.pm b/Koha/Template/Plugin/KohaNews.pm index efd08dd52f..d602646234 100644 --- a/Koha/Template/Plugin/KohaNews.pm +++ b/Koha/Template/Plugin/KohaNews.pm @@ -26,7 +26,7 @@ use base qw( Template::Plugin ); use C4::Koha; use C4::Context; -use C4::NewsChannels; # GetNewsToDisplay +use Koha::News; sub get { my ( $self, $params ) = @_; @@ -34,7 +34,7 @@ sub get { my $display_location = $params->{location}; my $blocktitle = $params->{blocktitle}; my $lang = $params->{lang}; - my $library = $params->{library} || ""; + my $library = $params->{library}; my $news_lang; if( !$display_location ){ @@ -43,7 +43,16 @@ sub get { $news_lang = $display_location."_".$lang; } - my $content = &GetNewsToDisplay( $news_lang, $library ); + my $search_params; + $search_params->{lang} = $news_lang; + $search_params->{branchcode} = [ $library, undef ] if $library; + $search_params->{-or} = [ expirationdate => { '>=' => \'NOW()' }, + expirationdate => undef ]; + my $content = Koha::News->search( + $search_params, + { + order_by => 'number' + }); if( @$content ){ return { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt index c65c30cd25..b13d4660f4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt @@ -2,6 +2,7 @@ [% USE Asset %] [% USE Koha %] [% USE Branches %] +[% USE KohaDates %] [% SET footerjs = 1 %] [% INCLUDE 'doc-head-open.inc' %] Koha staff interface @@ -23,14 +24,16 @@
- [% IF ( koha_news_count ) %] + [% IF ( koha_news.count ) %]

News

- [% SET newsdisp = Koha.Preference('NewsAuthorDisplay') %] + [% SET show_author = + Koha.Preference('NewsAuthorDisplay') == 'staff' + || Koha.Preference('NewsAuthorDisplay') == 'both' %] [% FOREACH koha_new IN koha_news %]

[% koha_new.title | html %]

[% koha_new.content | $raw %]
-

Posted on [% koha_new.newdate | html %][% IF( ( newsdisp == 'staff' || newsdisp == 'both' ) && koha_new.borrowernumber ) %] by [% koha_new.author_title | html %] [% koha_new.author_firstname | html %] [% koha_new.author_surname | html %]
[% END %] +

Posted on [% koha_new.timestamp | $KohaDates %][% IF( show_author && koha_new.borrowernumber ) %] by [% koha_new.author.title | html %] [% koha_new.author.firstname | html %] [% koha_new.author.surname | html %]
[% END %] [% IF ( CAN_user_tools ) %] Edit | Delete diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/html_helpers.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/html_helpers.inc index d872c73f4d..f292178697 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/html_helpers.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/html_helpers.inc @@ -9,7 +9,7 @@ [% END %] [% BLOCK koha_news_block %] - [% IF ( news.content.size > 0 ) %] + [% IF ( news.content.count > 0 ) %]

[% FOREACH n IN news.content %]
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt index 2c50b3ce84..c805585844 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt @@ -85,7 +85,7 @@ [% END %] [% END %] - [% IF ( koha_news ) %] + [% IF koha_news.count %] [% IF single_news_error %] @@ -96,7 +96,9 @@ [% ELSE %]
- [% SET newsdisp = ( Koha.Preference('NewsAuthorDisplay') ) %] + [% SET show_author = + Koha.Preference('NewsAuthorDisplay') == 'opac' + || Koha.Preference('NewsAuthorDisplay') == 'both' %] [% FOREACH koha_new IN koha_news %]

@@ -109,21 +111,16 @@
[% koha_new.content | $raw %]
Published on [% koha_new.published_on | $KohaDates %] - [% IF ( (newsdisp == 'opac' || newsdisp == 'both') && koha_new.borrowernumber ) %] - [% IF news_item %] - [% SET author = koha_new.author %] - by [% author.title | html %] [% author.firstname | html %] [% author.surname | html %] - [% ELSE %] - by [% koha_new.author_title | html %] [% koha_new.author_firstname | html %] [% koha_new.author_surname | html %] - [% END %] + [% IF ( show_author && koha_new.borrowernumber ) %] + by [% koha_new.author.title | html %] [% koha_new.author.firstname | html %] [% koha_new.author.surname | html %] [% END %] [% IF ( news_item ) %] • Show all news [% END %]

- [% END %] + [% UNLESS news_item %]
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-news-rss.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-news-rss.tt index d3935e6d2f..645ba47e0f 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-news-rss.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-news-rss.tt @@ -1,4 +1,4 @@ -[% USE raw %] +[% USE raw -%] diff --git a/mainpage.pl b/mainpage.pl index 183b5e0fcd..6d2c79783c 100755 --- a/mainpage.pl +++ b/mainpage.pl @@ -24,8 +24,8 @@ use CGI qw ( -utf8 ); use C4::Output; use C4::Auth; use C4::Koha; -use C4::NewsChannels; # GetNewsToDisplay use C4::Tags qw/get_count_by_tag_status/; +use Koha::News; use Koha::Patron::Modifications; use Koha::Patron::Discharge; use Koha::Reviews; @@ -49,13 +49,17 @@ 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; +my $koha_news = Koha::News->search({ + lang => 'koha', + branchcode => [ $homebranch, undef ] +}, +{ + order_by => 'number' +}); $template->param( - koha_news => $all_koha_news, - koha_news_count => $koha_news_count, - daily_quote => Koha::Quotes->get_daily_quote(), + koha_news => $koha_news, + daily_quote => Koha::Quotes->get_daily_quote(), ); my $branch = diff --git a/opac/opac-main.pl b/opac/opac-main.pl index af0377f483..f11aecdf01 100755 --- a/opac/opac-main.pl +++ b/opac/opac-main.pl @@ -22,7 +22,6 @@ use Modern::Perl; use CGI qw ( -utf8 ); use C4::Auth; # get_template_and_user use C4::Output; -use C4::NewsChannels; # GetNewsToDisplay use C4::Languages qw(getTranslatedLanguages accept_language); use Koha::Quotes; use C4::Members; @@ -61,17 +60,26 @@ elsif (C4::Context->userenv and defined $input->param('branch') and length $inpu } my $news_id = $input->param('news_id'); -my $all_koha_news; +my $koha_news; if (defined $news_id){ - $all_koha_news = Koha::News->search({ idnew => $news_id, lang => { '!=', 'koha' } }); # get news that is not staff-only news - if( $all_koha_news->count ) { # we only expect one btw - $template->param( news_item => $all_koha_news->next ); + $koha_news = Koha::News->search({ idnew => $news_id, lang => { '!=', 'koha' } }); # get news that is not staff-only news + if ( $koha_news->count > 0){ + $template->param( news_item => $koha_news->next ); } else { $template->param( single_news_error => 1 ); } } else { - $all_koha_news = &GetNewsToDisplay( $template->lang, $homebranch); + my $params; + $params->{lang} = [ $template->lang, '' ]; + $params->{branchcode} = [ $homebranch, undef ] if $homebranch; + $params->{-or} = [ expirationdate => { '>=' => \'NOW()' }, + expirationdate => undef ]; + $koha_news = Koha::News->search( + $params, + { + order_by => 'number' + }); } # For dashboard @@ -104,7 +112,7 @@ if ( $patron ) { } $template->param( - koha_news => $all_koha_news, + koha_news => $koha_news, branchcode => $homebranch, daily_quote => Koha::Quotes->get_daily_quote(), ); diff --git a/opac/opac-news-rss.pl b/opac/opac-news-rss.pl index f8c2b0e485..ff00cdfe24 100755 --- a/opac/opac-news-rss.pl +++ b/opac/opac-news-rss.pl @@ -22,8 +22,8 @@ use Modern::Perl; use CGI; use C4::Auth; # get_template_and_user use C4::Output; -use C4::NewsChannels; # GetNewsToDisplay use C4::Languages qw(getTranslatedLanguages accept_language); +use Koha::News; my $input = CGI->new; my $dbh = C4::Context->dbh; @@ -43,12 +43,17 @@ my ($theme, $news_lang, $availablethemes) = C4::Templates::themelanguage(C4::Con my $branchcode = $input->param('branchcode'); -my $all_koha_news = GetNewsToDisplay( $news_lang, $branchcode ); -my $koha_news_count = scalar @$all_koha_news; +my $params; +$params->{lang} = [ $news_lang, '' ]; +$params->{branchcode} = [ $branchcode, undef ] if $branchcode; +$params->{-or} = [ expirationdate => { '>=' => \'NOW()' }, + expirationdate => undef ]; +my $koha_news = Koha::News->search( + $params, + { + order_by => 'number' + }); -$template->param( - koha_news => $all_koha_news, - koha_news_count => $koha_news_count, -); +$template->param( koha_news => $koha_news ); output_html_with_http_headers $input, $cookie, $template->output; diff --git a/t/db_dependent/NewsChannels.t b/t/db_dependent/NewsChannels.t deleted file mode 100755 index 20e8ae12e9..0000000000 --- a/t/db_dependent/NewsChannels.t +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Koha::Database; -use Koha::DateUtils; -use Koha::Libraries; -use Koha::News; - -use Test::More tests => 4; - -BEGIN { - use_ok('C4::NewsChannels'); -} - -my $schema = Koha::Database->new->schema; -$schema->storage->txn_begin; -my $dbh = C4::Context->dbh; - -# Add LIB1, if it doesn't exist. -my $addbra = 'LIB1'; -unless ( Koha::Libraries->find($addbra) ) { - $dbh->do( q{ INSERT INTO branches (branchcode,branchname) VALUES (?,?) }, - undef, ( $addbra, "$addbra branch" ) ); -} - -# Add CAT1, if it doesn't exist. -my $addcat = 'CAT1'; -{ - my $sth = $dbh->prepare( q{ SELECT categorycode FROM categories WHERE categorycode = ? } ); - $sth->execute ( $addcat ); - if ( not defined $sth->fetchrow () ) { - $dbh->do( q{ INSERT INTO categories (categorycode,description) VALUES (?,?) }, - undef, ( $addcat, "$addcat description") ); - } -} - -# Add a test user if not already present. -my $addbrwr = 'BRWR1'; -my $brwrnmbr; -{ - my $query = - q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? }; - my $sth = $dbh->prepare( $query ); - $sth->execute( ($addbrwr, $addbra, $addcat) ); - $brwrnmbr = $sth->fetchrow; - - # Not found, let us insert it. - if ( not defined $brwrnmbr ) { - $dbh->do( q{ INSERT INTO borrowers (surname, address, city, branchcode, categorycode) VALUES (?, ?, ?, ?, ?) }, - undef, ($addbrwr, '(test) address', '(test) city', $addbra, $addcat) ); - - # Retrieve the njew borrower number. - $query = - q{ SELECT borrowernumber from borrowers WHERE surname = ? AND branchcode = ? AND categorycode = ? }; - my $sth = $dbh->prepare( $query ); - $sth->execute( ($addbrwr, $addbra, $addcat) ); - $brwrnmbr = $sth->fetchrow; - } -} - -# Must have valid borrower number, or tests are meaningless. -ok ( defined $brwrnmbr ); - -# Test GetNewsToDisplay -my ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' ); -ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' ); - diff --git a/tools/koha-news.pl b/tools/koha-news.pl index e63030995f..46c6574cdb 100755 --- a/tools/koha-news.pl +++ b/tools/koha-news.pl @@ -28,7 +28,6 @@ use C4::Auth; use C4::Koha; use C4::Context; use C4::Output; -use C4::NewsChannels; use C4::Languages qw(getTranslatedLanguages); use Date::Calc qw/Date_to_Days Today/; use Koha::DateUtils; -- 2.39.5