From 0b0e072b395add4979a21faf3b62332557636592 Mon Sep 17 00:00:00 2001 From: Josef Moravec Date: Tue, 19 Mar 2019 10:17:31 +0000 Subject: [PATCH] Bug 22544: Move get_opac_news to Koha namespace Signed-off-by: David Nind --- C4/NewsChannels.pm | 42 +----------------- Koha/NewsItem.pm | 51 ++++++++++++++++++++++ .../prog/en/modules/tools/koha-news.tt | 16 +++---- t/db_dependent/NewsChannels.t | 41 ++--------------- tools/koha-news.pl | 22 ++++------ 5 files changed, 70 insertions(+), 102 deletions(-) diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm index 5ed50817a1..1dcee7698b 100644 --- a/C4/NewsChannels.pm +++ b/C4/NewsChannels.pm @@ -28,7 +28,7 @@ BEGIN { @ISA = qw(Exporter); @EXPORT = qw( &GetNewsToDisplay - &add_opac_new &upd_opac_new &del_opac_new &get_opac_news + &add_opac_new &upd_opac_new &del_opac_new ); } @@ -121,46 +121,6 @@ sub del_opac_new { } } -sub get_opac_news { - my ($limit, $lang, $branchcode) = @_; - my @values; - my $dbh = C4::Context->dbh; - my $query = q{ - SELECT opac_news.*, branches.branchname, - timestamp AS newdate, - borrowers.title AS author_title, - borrowers.firstname AS author_firstname, - borrowers.surname AS author_surname - FROM opac_news LEFT JOIN branches - ON opac_news.branchcode=branches.branchcode - LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber - }; - $query .= ' WHERE 1'; - if ($lang) { - $query .= " AND (opac_news.lang='' OR opac_news.lang=?)"; - push @values,$lang; - } - if ($branchcode) { - $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)'; - push @values,$branchcode; - } - $query.= ' ORDER BY timestamp DESC '; - #if ($limit) { - # $query.= 'LIMIT 0, ' . $limit; - #} - my $sth = $dbh->prepare($query); - $sth->execute(@values); - my @opac_news; - my $count = 0; - while (my $row = $sth->fetchrow_hashref) { - if ((($limit) && ($count < $limit)) || (!$limit)) { - push @opac_news, $row; - } - $count++; - } - return ($count, \@opac_news); -} - =head2 GetNewsToDisplay $news = &GetNewsToDisplay($lang,$branch); diff --git a/Koha/NewsItem.pm b/Koha/NewsItem.pm index 9d0dd63f9a..4cf875b659 100644 --- a/Koha/NewsItem.pm +++ b/Koha/NewsItem.pm @@ -22,6 +22,9 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::DateUtils; +use Koha::Libraries; +use Koha::Patrons; use base qw(Koha::Object); @@ -37,6 +40,54 @@ Koha::NewsItem represents a single piece of news from the opac_news table =cut +=head3 is_expired + +my $is_expired = $news_item->is_expired; + +Returns 1 if the news item is expired or 0; + +=cut + +sub is_expired { + my ( $self ) = @_; + + return 0 unless $self->expirationdate; + return 1 if dt_from_string( $self->expirationdate ) < dt_from_string->truncate( to => 'day' ); + return 0; +} + +=head3 library + +my $library = $news_item->library; + +Returns Koha::Library object or undef + +=cut + +sub library { + my ( $self ) = @_; + + my $library_rs = $self->_result->branchcode; + return unless $library_rs; + return Koha::Library->_new_from_dbic( $library_rs ); +} + +=head3 author + +my $patron = $news_item->author; + +Returns Koha::Patron object or undef + +=cut + +sub author { + my ( $self ) = @_; + + my $author_rs = $self->_result->borrowernumber; + return unless $author_rs; + return Koha::Patron->_new_from_dbic( $author_rs ); +} + =head3 type =cut 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 6621e0c878..1d5b86f368 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 @@ -6,8 +6,6 @@ [% INCLUDE 'doc-head-open.inc' %] Koha › Tools › News [% INCLUDE 'doc-head-close.inc' %] -[% IF ( opac_news_count ) %] -[% END %] @@ -100,7 +98,7 @@ Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]
Cancel
[% ELSE %] - [% IF ( opac_news_count ) %] + [% IF ( opac_news.count ) %]
@@ -116,7 +114,7 @@ Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %][% FOREACH opac_new IN opac_news %] - [% IF ( opac_new.expired ) %] + [% IF ( opac_new.is_expired ) %] [% ELSE %] @@ -137,13 +135,13 @@ Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %] - - + + - + @@ -206,7 +204,7 @@ Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %] [% MACRO jsinclude BLOCK %] [% INCLUDE 'calendar.inc' %] [% Asset.js("js/tools-menu.js") | $raw %] - [% IF ( opac_news_count ) %] + [% IF ( opac_news.count ) %] [% INCLUDE 'datatables.inc' %]
Actions
[% IF ( opac_new.branchcode == "" ) -%] All libraries - [% ELSE %][% opac_new.branchname | html %] + [% ELSE %][% opac_new.library.branchname | html %] [% END %] [% opac_new.number | html %][% opac_new.newdate | $KohaDates %][% opac_new.expirationdate | $KohaDates %] [% IF ( opac_new.expired ) %](expired)[% END %][% opac_new.timestamp | $KohaDates %][% opac_new.expirationdate | $KohaDates %] [% IF ( opac_new.is_expired ) %](expired)[% END %] [% opac_new.title | html %][% opac_new.author_title | html %] [% opac_new.author_firstname | html %] [% opac_new.author_surname | html %][% IF ( opac_new.author) %][% INCLUDE 'patron-title.inc' patron=opac_new.author hide_patron_infos_if_needed=1 %][% END %] [% opac_new.content | $raw %]