From fa3b27a7703e6201fd785fc256b2aff2dde7bdf9 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 --- C4/Members.pm | 23 ++++-- C4/NewsChannels.pm | 96 ---------------------- Koha/Template/Plugin/KohaNews.pm | 18 ++-- .../intranet-tmpl/prog/en/modules/intranet-main.tt | 9 +- .../bootstrap/en/includes/html_helpers.inc | 2 +- .../opac-tmpl/bootstrap/en/modules/opac-main.tt | 6 +- .../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, 78 insertions(+), 203 deletions(-) delete mode 100644 C4/NewsChannels.pm delete mode 100644 t/db_dependent/NewsChannels.t diff --git a/C4/Members.pm b/C4/Members.pm index 205f45bd7b..ecd3ba69d7 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -35,7 +35,6 @@ use C4::Accounts; use C4::Biblio; use C4::Letters; use C4::Members::Attributes qw(SearchIdMatchingAttribute UpdateBorrowerAttribute); -use C4::NewsChannels; #get slip news use DateTime; use Koha::Database; use Koha::DateUtils; @@ -43,6 +42,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; @@ -580,11 +580,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 372d8b9efb..0000000000 --- a/C4/NewsChannels.pm +++ /dev/null @@ -1,96 +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 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; - # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate - my $query = q{ - SELECT opac_news.*,timestamp 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 - OR expirationdate = '00-00-0000' - ) - AND DATE(timestamp) < DATE_ADD(CURDATE(), INTERVAL 1 DAY) - AND (opac_news.lang = '' OR opac_news.lang = ?) - AND (opac_news.branchcode IS NULL OR opac_news.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,$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 49299f6e3d..0d6e7681de 100644 --- a/Koha/Template/Plugin/KohaNews.pm +++ b/Koha/Template/Plugin/KohaNews.pm @@ -26,14 +26,14 @@ use base qw( Template::Plugin ); use C4::Koha; use C4::Context; -use C4::NewsChannels; # GetNewsToDisplay +use Koha::News; sub get { my ( $self, $params ) = @_; my $display_location = $params->{location}; my $lang = $params->{lang}; - my $library = $params->{library} || ""; + my $library = $params->{library}; my $news_lang; if( !$display_location ){ @@ -42,8 +42,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' + }); return $content; } @@ -70,4 +78,4 @@ the following TT code: [% KohaNews.get() %] Owen Leonard -=cut \ No newline at end of file +=cut 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 ab04abbcd9..d1faedf428 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt @@ -1,6 +1,7 @@ [% USE raw %] [% USE Asset %] [% USE Koha %] +[% USE KohaDates %] [% SET footerjs = 1 %] [% INCLUDE 'doc-head-open.inc' %] Koha staff client @@ -16,14 +17,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 8cb9d2f674..a161d8cc28 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.size > 0 ) %] + [% IF ( news.count > 0 ) %] [% FOREACH n IN news %]

[% IF ( n.title ) %] 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 8947e27512..b4b8fe5ec9 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt @@ -75,12 +75,14 @@ [% ELSE %]
- [% SET newsdisp = ( Koha.Preference('NewsAuthorDisplay') ) %] + [% SET show_author = + Koha.Preference('NewsAuthorDisplay') == 'opac' + || Koha.Preference('NewsAuthorDisplay') == 'both' %] [% FOREACH koha_new IN koha_news %]

[% koha_new.title | html %]

[% koha_new.content | $raw %]
-
(published on [% koha_new.timestamp | $KohaDates with_hours = 1 %][% IF ( (newsdisp == 'opac' || newsdisp == 'both') && koha_new.borrowernumber ) %] by [% koha_new.author_title | html %] [% koha_new.author_firstname | html %] [% koha_new.author_surname | html %][% END %])
+
(published on [% koha_new.timestamp | $KohaDates with_hours = 1 %][% IF ( show_author && koha_new.borrowernumber ) %] by [% koha_new.author.title | html %] [% koha_new.author.firstname | html %] [% koha_new.author.surname | html %][% END %])
[% END %]
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 49f46fc272..85559afc07 100755 --- a/mainpage.pl +++ b/mainpage.pl @@ -24,9 +24,9 @@ use CGI qw ( -utf8 ); use C4::Output; use C4::Auth; use C4::Koha; -use C4::NewsChannels; # GetNewsToDisplay use C4::Suggestions qw/CountSuggestion/; use C4::Tags qw/get_count_by_tag_status/; +use Koha::News; use Koha::Patron::Modifications; use Koha::Patron::Discharge; use Koha::Reviews; @@ -48,13 +48,15 @@ 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 -); +$template->param( koha_news => $koha_news ); my $branch = ( C4::Context->preference("IndependentBranchesPatronModifications") diff --git a/opac/opac-main.pl b/opac/opac-main.pl index 4af6f1a818..3de41c4832 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 C4::Koha qw( GetDailyQuote ); use C4::Members; @@ -64,17 +63,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 (scalar @all_koha_news > 0){ - $template->param( news_item => @all_koha_news ); + $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($news_lang,$homebranch); + my $params; + $params->{lang} = [ $news_lang, '' ]; + $params->{branchcode} = [ $homebranch, undef ] if $homebranch; + $params->{-or} = [ expirationdate => { '>=' => \'NOW()' }, + expirationdate => undef ]; + $koha_news = Koha::News->search( + $params, + { + order_by => 'number' + }); } my $quote = GetDailyQuote(); # other options are to pass in an exact quote id or select a random quote each pass... see perldoc C4::Koha @@ -103,7 +111,7 @@ if ( $patron ) { } $template->param( - koha_news => @all_koha_news, + koha_news => $koha_news, news_lang => $news_lang, branchcode => $homebranch, display_daily_quote => C4::Context->preference('QuoteOfTheDay'), diff --git a/opac/opac-news-rss.pl b/opac/opac-news-rss.pl index 95cbd6da73..ea9893bfaa 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 = new CGI; 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 100644 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 aa386e0a58..4e92373a9f 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.11.0