Bug 29218: Rename .hidden class for DT visibility
[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_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_new {
149     my ($idnew) = @_;
150     my $dbh = C4::Context->dbh;
151     my $query = q{
152                   SELECT opac_news.*,branches.branchname
153                   FROM opac_news LEFT JOIN branches
154                       ON opac_news.branchcode=branches.branchcode
155                   WHERE opac_news.idnew = ?;
156                 };
157     my $sth = $dbh->prepare($query);
158     $sth->execute($idnew);
159     my $data = $sth->fetchrow_hashref;
160     $data->{expirationdate} = output_pref({ dt => dt_from_string( $data->{expirationdate} ), dateonly => 1 }) if ( $data->{expirationdate} );
161     $data->{published_on} = output_pref({ dt => dt_from_string( $data->{published_on} ), dateonly => 1 });
162     return $data;
163 }
164
165 sub get_opac_news {
166     my ($limit, $lang, $branchcode) = @_;
167     my @values;
168     my $dbh = C4::Context->dbh;
169     my $query = q{
170                   SELECT opac_news.*, branches.branchname,
171                          published_on AS newdate,
172                          borrowers.title AS author_title,
173                          borrowers.firstname AS author_firstname,
174                          borrowers.surname AS author_surname
175                   FROM opac_news LEFT JOIN branches
176                       ON opac_news.branchcode=branches.branchcode
177                   LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
178                 };
179     $query .= ' WHERE 1';
180     if ($lang) {
181         $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
182         push @values,$lang;
183     }
184     if ($branchcode) {
185         $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
186         push @values,$branchcode;
187     }
188     $query.= ' ORDER BY published_on DESC ';
189     #if ($limit) {
190     #    $query.= 'LIMIT 0, ' . $limit;
191     #}
192     my $sth = $dbh->prepare($query);
193     $sth->execute(@values);
194     my @opac_news;
195     my $count = 0;
196     while (my $row = $sth->fetchrow_hashref) {
197         if ((($limit) && ($count < $limit)) || (!$limit)) {
198             push @opac_news, $row;
199         }
200         $count++;
201     }
202     return ($count, \@opac_news);
203 }
204
205 =head2 GetNewsToDisplay
206
207     $news = &GetNewsToDisplay($lang,$branch);
208     C<$news> is a ref to an array which contains
209     all news with expirationdate > today or expirationdate is null
210     that is applicable for a given branch.
211
212 =cut
213
214 sub GetNewsToDisplay {
215     my ($lang,$branch) = @_;
216     my $dbh = C4::Context->dbh;
217     my $query = q{
218      SELECT opac_news.*,published_on AS newdate,
219      borrowers.title AS author_title,
220      borrowers.firstname AS author_firstname,
221      borrowers.surname AS author_surname
222      FROM   opac_news
223      LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
224      WHERE   (
225         expirationdate >= CURRENT_DATE()
226         OR    expirationdate IS NULL
227      )
228      AND   published_on <= CURDATE()
229      AND   (opac_news.lang = '' OR opac_news.lang = ?)
230      AND   (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
231      ORDER BY number
232     };
233     my $sth = $dbh->prepare($query);
234     $lang = $lang // q{};
235     $sth->execute($lang,$branch);
236     my @results;
237     while ( my $row = $sth->fetchrow_hashref ){
238         $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 });
239         push @results, $row;
240     }
241     return \@results;
242 }
243
244 1;
245 __END__
246
247 =head1 AUTHOR
248
249 TG
250
251 =cut