Bug 22544: Move get_opac_new 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         &add_opac_new &upd_opac_new &del_opac_new &get_opac_news
34     );
35 }
36
37 =head1 NAME
38
39 C4::NewsChannels - Functions to manage OPAC and intranet news
40
41 =head1 DESCRIPTION
42
43 This module provides the functions needed to mange OPAC and intranet news.
44
45 =head1 FUNCTIONS
46
47 =cut
48
49 =head2 add_opac_new
50
51     $retval = add_opac_new($hashref);
52
53     $hashref should contains all the fields found in opac_news,
54     except idnew. The idnew field is auto-generated.
55
56 =cut
57
58 sub add_opac_new {
59     my ($href_entry) = @_;
60     my $retval = 0;
61
62     if ($href_entry) {
63         $href_entry->{number} = 0 if $href_entry->{number} !~ /^\d+$/;
64         my @fields = keys %{$href_entry};
65         my @values = values %{$href_entry};
66         my $field_string = join ',', @fields;
67         $field_string = $field_string // q{};
68         my $values_string = join(',', map { '?' } @fields);
69         my $dbh = C4::Context->dbh;
70         my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string )");
71         $sth->execute(@values);
72         $retval = 1;
73
74         #Log new news entry
75         if (C4::Context->preference("NewsLog")) {
76                 logaction('NEWS', 'ADD' , undef, $href_entry->{lang} . ' | ' . $href_entry->{content});
77         }
78     }
79     return $retval;
80 }
81
82 =head2 upd_opac_new
83
84     $retval = upd_opac_new($hashref);
85
86     $hashref should contains all the fields found in opac_news,
87     including idnew, since it is the key for the SQL UPDATE.
88
89 =cut
90
91 sub upd_opac_new {
92     my ($href_entry) = @_;
93     my $retval = 0;
94
95     if ($href_entry) {
96         $href_entry->{number} = 0 if $href_entry->{number} !~ /^\d+$/;
97         # take the keys of hash entry and make a list, but...
98         my @fields = keys %{$href_entry};
99         my @values;
100         $#values = -1;
101         my $field_string = q{};
102         foreach my $field_name (@fields) {
103             # exclude idnew
104             if ( $field_name ne 'idnew' ) {
105                 $field_string = $field_string . "$field_name = ?,";
106                 push @values,$href_entry->{$field_name};
107             }
108         }
109         # put idnew at the end, so we know which record to update
110         push @values,$href_entry->{'idnew'};
111         chop $field_string; # remove that excess ,
112
113         my $dbh = C4::Context->dbh;
114         my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;");
115         $sth->execute(@values);
116         $retval = 1;
117     }
118
119     #Log news entry modification
120     if (C4::Context->preference("NewsLog")) {
121             logaction('NEWS', 'MODIFY' , undef, $href_entry->{lang} . ' | ' . $href_entry->{content});
122     }
123     return $retval;
124 }
125
126 sub del_opac_new {
127     my ($ids) = @_;
128     if ($ids) {
129
130         #Log news deletion
131         if (C4::Context->preference("NewsLog")) {
132             foreach my $newsid ( split(/,/, $ids )) {
133                 my $n = Koha::News->find( $newsid );
134                 logaction('NEWS', 'DELETE', undef, $n->unblessed->{lang} . ' | ' . $n->unblessed->{content} );
135             }
136         }
137
138         my $dbh = C4::Context->dbh;
139         my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
140         $sth->execute();
141         return 1;
142     } else {
143         return 0;
144     }
145
146 }
147
148 sub get_opac_news {
149     my ($limit, $lang, $branchcode) = @_;
150     my @values;
151     my $dbh = C4::Context->dbh;
152     my $query = q{
153                   SELECT opac_news.*, branches.branchname,
154                          published_on AS newdate,
155                          borrowers.title AS author_title,
156                          borrowers.firstname AS author_firstname,
157                          borrowers.surname AS author_surname
158                   FROM opac_news LEFT JOIN branches
159                       ON opac_news.branchcode=branches.branchcode
160                   LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
161                 };
162     $query .= ' WHERE 1';
163     if ($lang) {
164         $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
165         push @values,$lang;
166     }
167     if ($branchcode) {
168         $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
169         push @values,$branchcode;
170     }
171     $query.= ' ORDER BY published_on DESC ';
172     #if ($limit) {
173     #    $query.= 'LIMIT 0, ' . $limit;
174     #}
175     my $sth = $dbh->prepare($query);
176     $sth->execute(@values);
177     my @opac_news;
178     my $count = 0;
179     while (my $row = $sth->fetchrow_hashref) {
180         if ((($limit) && ($count < $limit)) || (!$limit)) {
181             push @opac_news, $row;
182         }
183         $count++;
184     }
185     return ($count, \@opac_news);
186 }
187
188 =head2 GetNewsToDisplay
189
190     $news = &GetNewsToDisplay($lang,$branch);
191     C<$news> is a ref to an array which contains
192     all news with expirationdate > today or expirationdate is null
193     that is applicable for a given branch.
194
195 =cut
196
197 sub GetNewsToDisplay {
198     my ($lang,$branch) = @_;
199     my $dbh = C4::Context->dbh;
200     my $query = q{
201      SELECT opac_news.*,published_on AS newdate,
202      borrowers.title AS author_title,
203      borrowers.firstname AS author_firstname,
204      borrowers.surname AS author_surname
205      FROM   opac_news
206      LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
207      WHERE   (
208         expirationdate >= CURRENT_DATE()
209         OR    expirationdate IS NULL
210      )
211      AND   published_on <= CURDATE()
212      AND   (opac_news.lang = '' OR opac_news.lang = ?)
213      AND   (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
214      ORDER BY number
215     };
216     my $sth = $dbh->prepare($query);
217     $lang = $lang // q{};
218     $sth->execute($lang,$branch);
219     my @results;
220     while ( my $row = $sth->fetchrow_hashref ){
221         $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 });
222         push @results, $row;
223     }
224     return \@results;
225 }
226
227 1;
228 __END__
229
230 =head1 AUTHOR
231
232 TG
233
234 =cut