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