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