Bug 22544: Move add_opac_item to Koha namespace
[koha.git] / C4 / NewsChannels.pm
1 package C4::NewsChannels;
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2000-2002  Katipo Communications
6 # Copyright (C) 2013       Mark Tompsett
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Context;
23 use Koha::DateUtils;
24 use C4::Log qw(logaction);
25 use Koha::News;
26
27 use vars qw(@ISA @EXPORT);
28
29 BEGIN { 
30     @ISA = qw(Exporter);
31     @EXPORT = qw(
32         &GetNewsToDisplay
33     );
34 }
35
36 =head1 NAME
37
38 C4::NewsChannels - Functions to manage OPAC and intranet news
39
40 =head1 DESCRIPTION
41
42 This module provides the functions needed to mange OPAC and intranet news.
43
44 =head1 FUNCTIONS
45
46 =cut
47
48 =head2 GetNewsToDisplay
49
50     $news = &GetNewsToDisplay($lang,$branch);
51     C<$news> is a ref to an array which contains
52     all news with expirationdate > today or expirationdate is null
53     that is applicable for a given branch.
54
55 =cut
56
57 sub GetNewsToDisplay {
58     my ($lang,$branch) = @_;
59     my $dbh = C4::Context->dbh;
60     my $query = q{
61      SELECT opac_news.*,published_on AS newdate,
62      borrowers.title AS author_title,
63      borrowers.firstname AS author_firstname,
64      borrowers.surname AS author_surname
65      FROM   opac_news
66      LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
67      WHERE   (
68         expirationdate >= CURRENT_DATE()
69         OR    expirationdate IS NULL
70      )
71      AND   published_on <= CURDATE()
72      AND   (opac_news.lang = '' OR opac_news.lang = ?)
73      AND   (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
74      ORDER BY number
75     };
76     my $sth = $dbh->prepare($query);
77     $lang = $lang // q{};
78     $sth->execute($lang,$branch);
79     my @results;
80     while ( my $row = $sth->fetchrow_hashref ){
81         $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 });
82         push @results, $row;
83     }
84     return \@results;
85 }
86
87 1;
88 __END__
89
90 =head1 AUTHOR
91
92 TG
93
94 =cut