From f1700e7430e64313d12e6e7482f737be4579654a Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Tue, 19 Mar 2019 09:14:47 +0000
Subject: [PATCH] Bug 22544: Move get_opac_new to Koha namespace

Signed-off-by: David Nind <david@davidnind.com>
---
 C4/NewsChannels.pm            | 19 +---------------
 t/db_dependent/NewsChannels.t | 51 +------------------------------------------
 tools/koha-news.pl            |  9 ++++----
 3 files changed, 7 insertions(+), 72 deletions(-)

diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm
index 7c9df5b2e5..071f054c22 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_new &get_opac_news
+        &add_opac_new &upd_opac_new &del_opac_new &get_opac_news
     );
 }
 
@@ -123,23 +123,6 @@ sub del_opac_new {
     }
 }
 
-sub get_opac_new {
-    my ($idnew) = @_;
-    my $dbh = C4::Context->dbh;
-    my $query = q{
-                  SELECT opac_news.*,branches.branchname
-                  FROM opac_news LEFT JOIN branches
-                      ON opac_news.branchcode=branches.branchcode
-                  WHERE opac_news.idnew = ?;
-                };
-    my $sth = $dbh->prepare($query);
-    $sth->execute($idnew);
-    my $data = $sth->fetchrow_hashref;
-    $data->{expirationdate} = output_pref({ dt => dt_from_string( $data->{expirationdate} ), dateonly => 1 }) if ( $data->{expirationdate} );
-    $data->{published_on} = output_pref({ dt => dt_from_string( $data->{published_on} ), dateonly => 1 });
-    return $data;
-}
-
 sub get_opac_news {
     my ($limit, $lang, $branchcode) = @_;
     my @values;
diff --git a/t/db_dependent/NewsChannels.t b/t/db_dependent/NewsChannels.t
index 9eb1489f3d..3f3c8bdea4 100755
--- a/t/db_dependent/NewsChannels.t
+++ b/t/db_dependent/NewsChannels.t
@@ -5,7 +5,7 @@ use Koha::Database;
 use Koha::DateUtils;
 use Koha::Libraries;
 
-use Test::More tests => 14;
+use Test::More tests => 11;
 
 BEGIN {
     use_ok('C4::NewsChannels');
@@ -134,55 +134,6 @@ $href_entry2->{idnew} = $idnew2;
 $rv                   = upd_opac_new($href_entry2);
 is( $rv, 1, 'Successfully updated second dummy news item!' );
 
-# Test get_opac_new (single news item)
-$timestamp1      = output_pref( { dt => dt_from_string( $timestamp1 ), dateonly => 1 } );
-$expirationdate1 = output_pref( { dt => dt_from_string( $expirationdate1 ), dateonly => 1 } );
-$timestamp2      = output_pref( { dt => dt_from_string( $timestamp2 ), dateonly => 1 } );
-$expirationdate2 = output_pref( { dt => dt_from_string( $expirationdate2) , dateonly => 1 } );
-
-my $updated_on = %{get_opac_new($idnew1)}{updated_on};
-is_deeply(
-    get_opac_new($idnew1),
-    {
-        title          => $title1,
-        content        => $new1,
-        lang           => $lang1,
-        expirationdate => $expirationdate1,
-        published_on=> $timestamp1,
-        number         => $number1,
-        borrowernumber => undef,
-        idnew          => $idnew1,
-        branchname     => "$addbra branch",
-        branchcode     => $addbra,
-        updated_on     => $updated_on,
-    },
-    'got back expected news item via get_opac_new - ID 1'
-);
-
-# Test get_opac_new (single news item)
-$updated_on = %{get_opac_new($idnew2)}{updated_on};
-is_deeply(
-    get_opac_new($idnew2),
-    {  
-        title          => $title2,
-        content        => $new2,
-        lang           => $lang2,
-        expirationdate => $expirationdate2,
-        published_on=> $timestamp2,
-        number         => $number2,
-        borrowernumber => $brwrnmbr,
-        idnew          => $idnew2,
-        branchname     => "$addbra branch",
-        branchcode     => $addbra,
-        updated_on     => $updated_on,
-    },
-    'got back expected news item via get_opac_new - ID 2'
-);
-
-# Test get_opac_new (single news item without expiration date)
-my $news3 = get_opac_new($idnew3);
-is($news3->{ expirationdate }, undef, "Expiration date should be empty");
-
 # Test get_opac_news (multiple news items)
 my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
 
diff --git a/tools/koha-news.pl b/tools/koha-news.pl
index 821cd4bb15..3ba9544e31 100755
--- a/tools/koha-news.pl
+++ b/tools/koha-news.pl
@@ -32,6 +32,7 @@ use C4::NewsChannels;
 use C4::Languages qw(getTranslatedLanguages);
 use Date::Calc qw/Date_to_Days Today/;
 use Koha::DateUtils;
+use Koha::News;
 
 my $cgi = new CGI;
 
@@ -58,7 +59,7 @@ if( $cgi->param('editmode') ){
 # NULL = All branches.
 $branchcode = undef if (defined($branchcode) && $branchcode eq '');
 
-my $new_detail = get_opac_new($id);
+my $new_detail = Koha::News->find( $id );
 
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {
@@ -82,7 +83,7 @@ foreach my $language ( @$tlangs ) {
         push @lang_list,
         {
             language => $sublanguage->{'rfc4646_subtag'},
-            selected => ( $new_detail->{lang} eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
+            selected => ( $new_detail && $new_detail->lang eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
         };
     }
 }
@@ -95,10 +96,10 @@ my $op = $cgi->param('op') // '';
 if ( $op eq 'add_form' ) {
     $template->param( add_form => 1 );
     if ($id) {
-        if($new_detail->{lang} eq "slip"){ $template->param( slip => 1); }
+        if($new_detail->lang eq "slip"){ $template->param( slip => 1); }
         $template->param( 
             op => 'edit',
-            id => $new_detail->{'idnew'}
+            id => $new_detail->idnew
         );
         $template->{VARS}->{'new_detail'} = $new_detail;
     }
-- 
2.11.0