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