Bug 7567: convert news add/update routines to take hashref; fix bugs
[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 C4::Dates qw(format_date);
24
25 use vars qw($VERSION @ISA @EXPORT);
26
27 BEGIN { 
28     $VERSION = 3.07.00.049;    # set the version for version checking
29     @ISA = qw(Exporter);
30     @EXPORT = qw(
31         &GetNewsToDisplay
32         &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
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 add_opac_new
49
50     $retval = add_opac_new($hashref);
51
52     $hashref should contains all the fields found in opac_news,
53     except idnew. The idnew field is auto-generated.
54
55 =cut
56
57 sub add_opac_new {
58     my ($href_entry) = @_;
59     my $retval = 0;
60
61     if ($href_entry) {
62         my @fields = keys %{$href_entry};
63         my @values = values %{$href_entry};
64         my $field_string = join ',',@fields;
65         $field_string = $field_string // q{};
66         my $values_string = '?,' x ($#fields) . '?';
67         my $dbh = C4::Context->dbh;
68         my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string )");
69         $sth->execute(@values);
70         $retval = 1;
71     }
72     return $retval;
73 }
74
75 =head2 upd_opac_new
76
77     $retval = upd_opac_new($hashref);
78
79     $hashref should contains all the fields found in opac_news,
80     including idnew, since it is the key for the SQL UPDATE.
81
82 =cut
83
84 sub upd_opac_new {
85     my ($href_entry) = @_;
86     my $retval = 0;
87
88     if ($href_entry) {
89         # take the keys of hash entry and make a list, but...
90         my @fields = keys %{$href_entry};
91         my @values;
92         $#values = -1;
93         my $field_string = q{};
94         foreach my $field_name (@fields) {
95             # exclude idnew
96             if ( $field_name ne 'idnew' ) {
97                 $field_string = $field_string . "$field_name = ?,";
98                 push @values,$href_entry->{$field_name};
99             }
100         }
101         # put idnew at the end, so we know which record to update
102         push @values,$href_entry->{'idnew'};
103         chop $field_string; # remove that excess ,
104
105         my $dbh = C4::Context->dbh;
106         my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;");
107         $sth->execute(@values);
108         $retval = 1;
109     }
110     return $retval;
111 }
112
113 sub del_opac_new {
114     my ($ids) = @_;
115     if ($ids) {
116         my $dbh = C4::Context->dbh;
117         my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
118         $sth->execute();
119         return 1;
120     } else {
121         return 0;
122     }
123 }
124
125 sub get_opac_new {
126     my ($idnew) = @_;
127     my $dbh = C4::Context->dbh;
128     my $query = q{ SELECT * FROM opac_news WHERE idnew = ? };
129     my $sth = $dbh->prepare($query);
130     $sth->execute($idnew);
131     my $data = $sth->fetchrow_hashref;
132     $data->{$data->{'lang'}} = 1 if defined $data->{lang};
133     $data->{expirationdate} = format_date($data->{expirationdate});
134     $data->{timestamp}      = format_date($data->{timestamp});
135     return $data;
136 }
137
138 sub get_opac_news {
139     my ($limit, $lang) = @_;
140     my @values;
141     my $dbh = C4::Context->dbh;
142     my $query = q{ SELECT *, timestamp AS newdate FROM opac_news };
143     if ($lang) {
144         $query.= " WHERE (lang='' OR lang=?)";
145         push @values,$lang;
146     }
147     $query.= ' ORDER BY timestamp DESC ';
148     #if ($limit) {
149     #    $query.= 'LIMIT 0, ' . $limit;
150     #}
151     my $sth = $dbh->prepare($query);
152     $sth->execute(@values);
153     my @opac_news;
154     my $count = 0;
155     while (my $row = $sth->fetchrow_hashref) {
156         if ((($limit) && ($count < $limit)) || (!$limit)) {
157             push @opac_news, $row;
158         }
159         $count++;
160     }
161     return ($count, \@opac_news);
162 }
163
164 =head2 GetNewsToDisplay
165
166     $news = &GetNewsToDisplay($lang);
167     C<$news> is a ref to an array which containts
168     all news with expirationdate > today or expirationdate is null.
169
170 =cut
171
172 sub GetNewsToDisplay {
173     my ($lang) = @_;
174     my $dbh = C4::Context->dbh;
175     # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
176     my $query = q{
177      SELECT *,timestamp AS newdate
178      FROM   opac_news
179      WHERE   (
180         expirationdate >= CURRENT_DATE()
181         OR    expirationdate IS NULL
182         OR    expirationdate = '00-00-0000'
183      )
184      AND   `timestamp` < CURRENT_DATE()+1
185      AND   (lang = '' OR lang = ?)
186      ORDER BY number
187     }; # expirationdate field is NOT in ISO format?
188        # timestamp has HH:mm:ss, CURRENT_DATE generates 00:00:00
189        #           by adding 1, that captures today correctly.
190     my $sth = $dbh->prepare($query);
191     $lang = $lang // q{};
192     $sth->execute($lang);
193     my @results;
194     while ( my $row = $sth->fetchrow_hashref ){
195         $row->{newdate} = format_date($row->{newdate});
196         push @results, $row;
197     }
198     return \@results;
199 }
200
201 1;
202 __END__
203
204 =head1 AUTHOR
205
206 TG
207
208 =cut