From 903a29f7f58c6a14a4927560034d0baefaa6403d Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 14 Dec 2021 13:35:45 +0000 Subject: [PATCH] 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' --- Koha/Template/Plugin/AdditionalContents.pm | 23 ++++ .../bootstrap/en/modules/opac-main.tt | 107 +++++++++--------- opac/opac-main.pl | 24 +--- 3 files changed, 78 insertions(+), 76 deletions(-) diff --git a/Koha/Template/Plugin/AdditionalContents.pm b/Koha/Template/Plugin/AdditionalContents.pm index c37abece4e..e2262a18ff 100644 --- a/Koha/Template/Plugin/AdditionalContents.pm +++ b/Koha/Template/Plugin/AdditionalContents.pm @@ -55,6 +55,29 @@ sub get { } } +sub get_news_id { + my ( $self, $params ) = @_; + + my $idnew = $params->{idnew}; + my $location = ['opac_only', 'staff_and_opac']; + my $blocktitle = $params->{blocktitle}; + + my $content = Koha::AdditionalContents->search( + { + location => $location, + idnew => $idnew, + } + ); + + if ( $content->count ) { + return { + content => $content, + location => $location, + blocktitle => $blocktitle + }; + } +} + 1; =head1 NAME 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 12cb72e35c..fe558c4eae 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt @@ -71,7 +71,7 @@

Koha home

[% IF Koha.Preference( 'OpacNewsLibrarySelect' ) %] - [% UNLESS news_item %] + [% UNLESS news_id %]
News @@ -87,65 +87,62 @@ [% END %] [% END %] - [% IF koha_news.count %] - - [% IF single_news_error %] - -
- This news item does not exist. -
- - [% ELSE %] + [% IF news_id %] + [% SET koha_news = AdditionalContents.get_news_id( idnew => 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 %] +
+ [% SET show_author = Koha.Preference('NewsAuthorDisplay') == 'opac' || Koha.Preference('NewsAuthorDisplay') == 'both' %] + [% FOREACH koha_new IN koha_news.content %] +
+

+ [% IF ( news_item ) %] + [% koha_new.title | html %] + [% ELSE %] + [% koha_new.title | html %] + [% END %] +

+
[% koha_new.content | $raw %]
+
+ Published on [% koha_new.published_on | $KohaDates %] + [% IF ( show_author && koha_new.author ) %] + by [% INCLUDE 'patron-title.inc' patron=koha_new.author %] + [% END %] + [% IF ( news_item ) %] + • Show all news + [% END %] +
+
+ [% END %] -
- [% SET show_author = Koha.Preference('NewsAuthorDisplay') == 'opac' || Koha.Preference('NewsAuthorDisplay') == 'both' %] - [% FOREACH koha_new IN koha_news %] -
-

- [% IF ( news_item ) %] - [% koha_new.title | html %] + [% UNLESS news_item %] +

-
[% koha_new.content | $raw %]
-
- Published on [% koha_new.published_on | $KohaDates %] - [% IF ( show_author && koha_new.author ) %] - by [% INCLUDE 'patron-title.inc' patron=koha_new.author %] - [% END %] - [% IF ( news_item ) %] - • Show all news + RSS feed for library news [% END %] -
-
- [% END %] - - [% UNLESS news_item %] - - [% END %] -
- - [% END # /IF single_news_error %] - + [% END %] + +
+ [% END %] + + [% ELSIF news_id %] +
+ This news item does not exist. +
[% ELSE %] [% IF Koha.Preference( 'OpacNewsLibrarySelect' ) %]
diff --git a/opac/opac-main.pl b/opac/opac-main.pl index bf63962ae0..3e3123a003 100755 --- a/opac/opac-main.pl +++ b/opac/opac-main.pl @@ -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 ); @@ -108,9 +91,8 @@ if ( $patron ) { } } +$template->param( branchcode => $homebranch ) if $homebranch; $template->param( - koha_news => $koha_news, - branchcode => $homebranch, daily_quote => Koha::Quotes->get_daily_quote(), ); -- 2.20.1