From 5eb02a3d6d945e594cd41cf63be22dca4c6a52a8 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' Signed-off-by: Martin Renvoize --- Koha/Template/Plugin/AdditionalContents.pm | 38 +++++- .../bootstrap/en/modules/opac-main.tt | 109 +++++++++--------- opac/opac-main.pl | 24 +--- 3 files changed, 91 insertions(+), 80 deletions(-) diff --git a/Koha/Template/Plugin/AdditionalContents.pm b/Koha/Template/Plugin/AdditionalContents.pm index c37abece4e..455ba848e1 100644 --- a/Koha/Template/Plugin/AdditionalContents.pm +++ b/Koha/Template/Plugin/AdditionalContents.pm @@ -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 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 4ffbc8e024..820d5035cd 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt @@ -72,7 +72,7 @@

Koha home

[% IF Koha.Preference( 'OpacNewsLibrarySelect' ) %] - [% UNLESS news_item %] + [% UNLESS news_id %]
News @@ -88,67 +88,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_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 %] +
+ [% 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_id ) %] + • Show all news + [% END %] +
+
+ [% END %] -
- [% SET show_author = Koha.Preference('NewsAuthorDisplay') == 'opac' || Koha.Preference('NewsAuthorDisplay') == 'both' %] - [% FOREACH koha_new IN koha_news %] - [% IF koha_new.category == 'news' %] -
-

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

-
[% 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 %] - [% 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 78f7975a10..f90d98cdcf 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 ); @@ -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(), ); -- 2.40.0