Bug 3289 - Fixed bad conditional check that made news items not show up until the...
[koha.git] / C4 / NewsChannels.pm
1 package C4::NewsChannels;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21
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.01;        # set the version for version checking
29         @ISA = qw(Exporter);
30         @EXPORT = qw(
31                 &GetNewsToDisplay
32                 &news_channels &get_new_channel &del_channels &add_channel &update_channel
33                 &news_channels_categories &get_new_channel_category &del_channels_categories
34                 &add_channel_category &update_channel_category &news_channels_by_category
35                 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
36                 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
37         );
38 }
39
40 =head1 NAME
41
42 C4::NewsChannels - Functions to manage the news channels and its categories
43
44 =head1 DESCRIPTION
45
46 This module provides the functions needed to admin the news channels and its categories
47
48 =head1 FUNCTIONS
49
50 =head2 news_channels
51
52   ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
53
54 Looks up news channels by name or category.
55
56 C<$channel_name> is the channel name to search.
57
58 C<$id_category> is the channel category code to search.
59
60 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
61
62 if none of the params are set C<&news_channels> returns all the news channels.
63
64 C<&news_channels> returns two values: an integer giving the number of
65 news channels found and a reference to an array
66 of references to hash, which has the news_channels and news_channels_categories fields.
67
68 =cut
69
70 sub news_channels {
71     my ($channel_name, $id_category, $unclassified) = @_;
72     my $dbh = C4::Context->dbh;
73     my @channels;
74     my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
75     if ( ($channel_name ne '') && ($id_category ne '') ) {
76         $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
77     } elsif ($channel_name ne '')  {
78         $query.= " WHERE channel_name like '" . $channel_name . "%'";
79     } elsif ($id_category ne '') {
80         $query.= " WHERE news_channels.id_category = " . $id_category;
81     } elsif ($unclassified) {
82         $query.= " WHERE news_channels.id_category IS NULL ";
83     }
84     my $sth = $dbh->prepare($query);
85     $sth->execute();
86     while (my $row = $sth->fetchrow_hashref) {
87         push @channels, $row;
88     }
89     $sth->finish;
90     return (scalar(@channels), @channels);
91 }
92
93 =head2 news_channels_by_category
94
95   ($count, @results) = &news_channels_by_category();
96
97 Looks up news channels grouped by category.
98
99 C<&news_channels_by_category> returns two values: an integer giving the number of
100 categories found and a reference to an array
101 of references to hash, which the following keys: 
102
103 =over 4
104
105 =item C<channels_count>
106
107 The number of news channels in that category
108
109 =item C<channels>
110
111 A reference to an array of references to hash which keys are the new_channels fields. 
112
113 Additionally the last index of results has a reference to all the news channels which don't have a category 
114
115 =back
116
117 =cut
118
119 sub news_channels_by_category {
120     
121     my ($categories_count, @results) = &news_channels_categories();
122     foreach my $row (@results) {
123
124         my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
125         $row->{'channels_count'} = $channels_count;
126         $row->{'channels'} = \@channels;
127     }
128
129     my ($channels_count, @channels) = &news_channels('', '', 1);
130     my %row;
131     $row{'id_category'} = -1;
132     $row{'unclassified'} = 1;
133     $row{'channels_count'} = $channels_count;
134     $row{'channels'} = \@channels;
135     push @results, \%row;
136
137     return (scalar(@results), @results);
138 }
139
140 sub get_new_channel {
141     my ($id) = @_;
142     my $dbh = C4::Context->dbh;
143     my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
144     $sth->execute($id);
145     my $channel = $sth->fetchrow_hashref;
146     $sth->finish;
147     return $channel;
148 }
149
150 sub del_channels {
151     my ($ids) = @_;
152     if ($ids ne '') {
153         my $dbh = C4::Context->dbh;
154         my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
155         $sth->execute();
156         $sth->finish;
157         return $ids;
158     }
159     return 0;
160 }
161
162 sub add_channel {
163     my ($name, $url, $id_category, $notes) = @_;
164     my $dbh = C4::Context->dbh;
165     my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
166     $sth->execute($name, $url, $id_category, $notes);
167     $sth->finish;
168     return 1;
169 }
170
171 sub update_channel {
172     my ($id, $name, $url, $id_category, $notes) = @_;
173     my $dbh = C4::Context->dbh;
174     my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?,  url = ?, id_category = ?, notes = ? WHERE id = ?");
175     $sth->execute($name, $url, $id_category, $notes, $id);
176     $sth->finish;
177     return 1;
178 }
179
180 sub news_channels_categories {
181     my $dbh = C4::Context->dbh;
182     my @categories;
183     my $query = "SELECT * FROM news_channels_categories";
184     my $sth = $dbh->prepare($query);
185     $sth->execute();
186     while (my $row = $sth->fetchrow_hashref) {
187         push @categories, $row;
188     }
189     $sth->finish;
190     return (scalar(@categories), @categories);
191
192 }
193
194 sub get_new_channel_category {
195     my ($id) = @_;
196     my $dbh = C4::Context->dbh;
197     my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
198     $sth->execute($id);
199     my $category = $sth->fetchrow_hashref;
200     $sth->finish;
201     return $category;
202 }
203
204 sub del_channels_categories {
205     my ($ids) = @_;
206     if ($ids ne '') {
207         my $dbh = C4::Context->dbh;
208         my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
209         $sth->execute();
210         $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
211         $sth->execute();
212         $sth->finish;
213         return $ids;
214     }
215     return 0;
216 }
217
218 sub add_channel_category {
219     my ($name) = @_;
220     my $dbh = C4::Context->dbh;
221     my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
222     $sth->execute($name);
223     $sth->finish;
224     return 1;
225 }
226
227 sub update_channel_category {
228     my ($id, $name) = @_;
229     my $dbh = C4::Context->dbh;
230     my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
231     $sth->execute($name, $id);
232     $sth->finish;
233     return 1;
234 }
235
236 sub add_opac_new {
237     my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_;
238     my $dbh = C4::Context->dbh;
239     my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)");
240     $sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number);
241     $sth->finish;
242     return 1;
243 }
244
245 sub upd_opac_new {
246     my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_;
247     my $dbh = C4::Context->dbh;
248     my $sth = $dbh->prepare("
249         UPDATE opac_news SET 
250             title = ?,
251             new = ?,
252             lang = ?,
253             expirationdate = ?,
254             timestamp = ?,
255             number = ?
256         WHERE idnew = ?
257     ");
258     $sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew);
259     $sth->finish;
260     return 1;
261 }
262
263 sub del_opac_new {
264     my ($ids) = @_;
265     if ($ids) {
266         my $dbh = C4::Context->dbh;
267         my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
268         $sth->execute();
269         $sth->finish;
270         return 1;
271     } else {
272         return 0;
273     }
274 }
275
276 sub get_opac_new {
277     my ($idnew) = @_;
278     my $dbh = C4::Context->dbh;
279     my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
280     $sth->execute($idnew);
281     my $data = $sth->fetchrow_hashref;
282     $data->{$data->{'lang'}} = 1;
283     $data->{expirationdate} = format_date($data->{expirationdate});
284     $data->{timestamp}      = format_date($data->{timestamp});
285     $sth->finish;
286     return $data;
287 }
288
289 sub get_opac_news {
290     my ($limit, $lang) = @_;
291     my $dbh = C4::Context->dbh;
292     my $query = "SELECT *, timestamp AS newdate FROM opac_news";
293     if ($lang) {
294         $query.= " WHERE lang = '" .$lang ."' ";
295     }
296     $query.= " ORDER BY timestamp DESC ";
297     #if ($limit) {
298     #    $query.= "LIMIT 0, " . $limit;
299     #}
300     my $sth = $dbh->prepare($query);
301     $sth->execute();
302     my @opac_news;
303     my $count = 0;
304     while (my $row = $sth->fetchrow_hashref) {
305         if ((($limit) && ($count < $limit)) || (!$limit)) {
306             $row->{'newdate'} = format_date($row->{'newdate'});
307             $row->{'expirationdate'} = format_date($row->{'expirationdate'});
308             push @opac_news, $row;
309         }
310         $count++;
311     }
312     return ($count, \@opac_news);
313 }
314
315 =head2 GetNewsToDisplay
316
317     $news = &GetNewsToDisplay($lang);
318     C<$news> is a ref to an array which containts
319     all news with expirationdate > today or expirationdate is null.
320
321 =cut
322
323 sub GetNewsToDisplay {
324     my $lang = shift;
325     my $dbh = C4::Context->dbh;
326     # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
327     my $query = "
328      SELECT *,timestamp AS newdate
329      FROM   opac_news
330      WHERE   (
331         expirationdate >= CURRENT_DATE()
332         OR    expirationdate IS NULL
333         OR    expirationdate = '00-00-0000'
334       )
335       AND   `timestamp` <= CURRENT_DATE()
336       AND   lang = ?
337       ORDER BY number
338     ";                          # expirationdate field is NOT in ISO format?
339     my $sth = $dbh->prepare($query);
340     $sth->execute($lang);
341     my @results;
342     while ( my $row = $sth->fetchrow_hashref ){
343                 $row->{newdate} = format_date($row->{newdate});
344         push @results, $row;
345     }
346     return \@results;
347 }
348
349 ### get electronic databases
350
351 sub add_opac_electronic {
352     my ($title, $edata, $lang,$image,$href,$section) = @_;
353     my $dbh = C4::Context->dbh;
354     my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
355     $sth->execute($title, $edata, $lang,$image,$href,$section);
356     $sth->finish;
357     return 1;
358 }
359
360 sub upd_opac_electronic {
361     my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
362     my $dbh = C4::Context->dbh;
363     my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
364     $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
365     $sth->finish;
366     return 1;
367 }
368
369 sub del_opac_electronic {
370     my ($ids) = @_;
371     if ($ids) {
372         my $dbh = C4::Context->dbh;
373         my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
374         $sth->execute();
375         $sth->finish;
376         return 1;
377     } else {
378         return 0;
379     }
380 }
381
382 sub get_opac_electronic {
383     my ($idelectronic) = @_;
384     my $dbh = C4::Context->dbh;
385     my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
386     $sth->execute($idelectronic);
387     my $data = $sth->fetchrow_hashref;
388     $data->{$data->{'lang'}} = 1;
389     $data->{$data->{'section'}} = 1;
390     $sth->finish;
391     return $data;
392 }
393
394 sub get_opac_electronics {
395     my ($section, $lang) = @_;
396     my $dbh = C4::Context->dbh;
397     my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";
398     if ($lang) {
399         $query.= " WHERE lang = '" .$lang ."' ";
400     }
401     if ($section) {
402         $query.= " and section= '" . $section."' ";
403     }
404     $query.= " ORDER BY title ";
405     
406     my $sth = $dbh->prepare($query);
407     $sth->execute();
408     my @opac_electronic;
409     my $count = 0;
410     while (my $row = $sth->fetchrow_hashref) {
411             push @opac_electronic, $row;
412         $count++;
413     }
414
415     return ($count,\@opac_electronic);
416 }
417
418 1;
419 __END__
420
421 =head1 AUTHOR
422
423 TG
424
425 =cut