Bug 29691: Use template to display news on opac homepage

This patch moves the fetching of news to the template and
adds a p[lugin method to get news by id

TO test:
1 - Define some general and library specific news items
2 - Define in various languages
3 - Define some 'Additional contents' as well
4 - Apply patch
5 - Confirm onlly 'all libraries' news show if not loigged in to opac
6 - Confirm correct library news show when logged in
7 - View specific news items:
    http://localhost:8080/cgi-bin/koha/opac-main.pl?news_id=12
8 - Confirm that non-existent ids show " This news item does not exist. "
9 - Confirm if you enter ID for additional contents you get 'does not exist'

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Nick Clemens 2021-12-14 13:35:45 +00:00 committed by Tomas Cohen Arazi
parent de49b8cd3a
commit c1f628fbc9
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
3 changed files with 92 additions and 81 deletions

View File

@ -55,6 +55,26 @@ sub get {
}
}
sub get_opac_news_by_id {
my ( $self, $params ) = @_;
my $news_id = $params->{news_id};
my $content = Koha::AdditionalContents->search(
{
location => ['opac_only', 'staff_and_opac'],
idnew => $news_id,
category => 'news',
}
);
if ( $content->count ) {
return {
content => $content,
};
}
}
1;
=head1 NAME
@ -71,8 +91,22 @@ Koha::Template::Plugin::AdditionalContents - TT Plugin for displaying additional
=head2 get
In a template, you can get the all categories with
the following TT code: [% AdditionalContents.get() %]
In a template, you can get the news categories with
the following TT code: [% AdditionalContents.get( category => 'news', location => ['opac_only', 'staff_and_opac'], lang => lang, library => branchcode ) %]
The function returns a hashref with keys:
contents: a Koha::AdditionalContents object
location: the passed in location param returned
blocktitle: the passed in blocktitle param returned
=head2 get_opac_news_by_id
In a template, you can get a specific news item by passing
the news id. E.g.:
[% SET news_item = AdditionalContents.get_news_by_id( news_id => news_id ) %]
The function returns a hashref with a Koha::AdditionalContents object in the 'contents' key
=head1 AUTHOR

View File

@ -72,7 +72,7 @@
<h1 class="sr-only">Koha home</h1>
[% IF Koha.Preference( 'OpacNewsLibrarySelect' ) %]
[% UNLESS news_item %]
[% UNLESS news_id %]
<form id="news-branch-select" class="form-inline" name="news-branch-select" method="get" action="/cgi-bin/koha/opac-main.pl">
<legend class="sr-only">News</legend>
<label for="news-branch">Display news for: </label>
@ -88,67 +88,62 @@
[% END %]
[% END %]
[% IF koha_news.count %]
[% IF single_news_error %]
<div class="alert alert-error">
This news item does not exist.
</div>
[% ELSE %]
<div id="news" class="newscontainer">
[% SET show_author = Koha.Preference('NewsAuthorDisplay') == 'opac' || Koha.Preference('NewsAuthorDisplay') == 'both' %]
[% FOREACH koha_new IN koha_news %]
[% IF koha_new.category == 'news' %]
<div class="newsitem">
<h4 class="newsheader">
[% IF ( news_item ) %]
[% koha_new.title | html %]
[% ELSE %]
<a name="newsitem[% koha_new.idnew | html %]" href="/cgi-bin/koha/opac-main.pl?news_id=[% koha_new.idnew | uri %]">[% koha_new.title | html %]</a>
[% END %]
</h4>
<div class="newsbody">[% koha_new.content | $raw %]</div>
<div class="newsfooter">
Published on [% koha_new.published_on | $KohaDates %]
[% IF ( show_author && koha_new.author ) %]
by <span class="newsauthor">[% INCLUDE 'patron-title.inc' patron=koha_new.author %]</span>
[% END %]
[% IF ( news_item ) %]
&bull; <a href="/cgi-bin/koha/opac-main.pl">Show all news</a>
[% END %]
</div>
[% IF news_id %]
[% SET koha_news = AdditionalContents.get_opac_news_by_id( news_id => news_id ) %]
[% ELSE %]
[% SET koha_news = AdditionalContents.get( category => 'news', location => ['opac_only', 'staff_and_opac'], lang => lang, library => branchcode ) %]
[% END %]
[% IF koha_news.content.count %]
<div id="news" class="newscontainer">
[% SET show_author = Koha.Preference('NewsAuthorDisplay') == 'opac' || Koha.Preference('NewsAuthorDisplay') == 'both' %]
[% FOREACH koha_new IN koha_news.content %]
<div class="newsitem">
<h4 class="newsheader">
[% IF ( news_item ) %]
[% koha_new.title | html %]
[% ELSE %]
<a name="newsitem[% koha_new.idnew | html %]" href="/cgi-bin/koha/opac-main.pl?news_id=[% koha_new.idnew | uri %]">[% koha_new.title | html %]</a>
[% END %]
</h4>
<div class="newsbody">[% koha_new.content | $raw %]</div>
<div class="newsfooter">
Published on [% koha_new.published_on | $KohaDates %]
[% IF ( show_author && koha_new.author ) %]
by <span class="newsauthor">[% INCLUDE 'patron-title.inc' patron=koha_new.author %]</span>
[% END %]
[% IF ( news_id ) %]
&bull; <a href="/cgi-bin/koha/opac-main.pl">Show all news</a>
[% END %]
</div>
[% END %]
[% END %]
</div>
[% END %]
[% UNLESS news_item %]
<div id="rssnews-container">
<!-- Logged in users have a branch code or it could be explicitly set -->
<a href="[% OPACBaseURL | url %]/cgi-bin/koha/opac-news-rss.pl?branchcode=[% branchcode | uri %]">
<i class="fa fa-rss" aria-hidden="true"></i>
[% IF Branches.all.size == 1 %]
[% IF branchcode %]
<span>RSS feed for [% Branches.GetName( branchcode ) | html %] library news</span>
[% ELSE %]
<span>RSS feed for library news</span>
[% END %]
[% UNLESS news_id %]
<div id="rssnews-container">
<!-- Logged in users have a branch code or it could be explicitly set -->
<a href="[% OPACBaseURL | url %]/cgi-bin/koha/opac-news-rss.pl?branchcode=[% branchcode | uri %]">
<i class="fa fa-rss" aria-hidden="true"></i>
[% IF Branches.all.size == 1 %]
[% IF branchcode %]
<span>RSS feed for [% Branches.GetName( branchcode ) | html %] library news</span>
[% ELSE %]
[% IF branchcode %]
<span>RSS feed for [% Branches.GetName( branchcode ) | html %] and system-wide library news</span>
[% ELSE %]
<span>RSS feed for system-wide library news</span>
[% END %]
<span>RSS feed for library news</span>
[% END %]
</a>
</div>
[% END %]
</div>
[% END # /IF single_news_error %]
[% ELSE %]
[% IF branchcode %]
<span>RSS feed for [% Branches.GetName( branchcode ) | html %] and system-wide library news</span>
[% ELSE %]
<span>RSS feed for system-wide library news</span>
[% END %]
[% END %]
</a>
</div>
[% END %]
</div>
[% ELSIF news_id %] <!-- news_id but no koha_news.content -->
<div class="alert alert-error">
This news item does not exist.
</div>
[% ELSE %] <!-- koha news -->
[% IF Koha.Preference( 'OpacNewsLibrarySelect' ) %]
<div id="news" class="newscontainer">

View File

@ -58,26 +58,9 @@ elsif (C4::Context->userenv and defined $input->param('branch') and length $inpu
$homebranch = "";
}
my $news_id = $input->param('news_id');
my $koha_news;
if (defined $news_id){
$koha_news = Koha::AdditionalContents->search({ idnew => $news_id, location => ['opac_only', 'staff_and_opac'] }); # 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 {
$koha_news = Koha::AdditionalContents->search_for_display(
{
category => 'news',
location => ['opac_only', 'staff_and_opac'],
lang => $template->lang,
library_id => $homebranch,
}
);
}
my $news_id = $input->param('news_id');
$template->param( news_id => $news_id );
# For dashboard
my $patron = Koha::Patrons->find( $borrowernumber );
@ -114,9 +97,8 @@ if ( $patron ) {
}
}
$template->param( branchcode => $homebranch ) if $homebranch;
$template->param(
koha_news => $koha_news,
branchcode => $homebranch,
daily_quote => Koha::Quotes->get_daily_quote(),
);