FIX for Date calculation
[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(\r
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) = @_;\r
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("\r
255         UPDATE opac_news SET \r
256             title = ?,\r
257             new = ?,\r
258             lang = ?,\r
259             expirationdate = ?,\r
260             number = ?\r
261         WHERE idnew = ?\r
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     $data->{expirationdate} = format_date($data->{expirationdate});\r
289     $sth->finish;\r
290     return $data;\r
291 }\r
292 \r
293 sub get_opac_news {\r
294     my ($limit, $lang) = @_;\r
295     my $dbh = C4::Context->dbh;\r
296     my $query = "SELECT *, timestamp AS newdate FROM opac_news";\r
297     if ($lang) {\r
298         $query.= " WHERE lang = '" .$lang ."' ";\r
299     }\r
300     $query.= " ORDER BY timestamp DESC ";\r
301     #if ($limit) {\r
302     #    $query.= "LIMIT 0, " . $limit;\r
303     #}\r
304     my $sth = $dbh->prepare($query);\r
305     $sth->execute();\r
306     my @opac_news;\r
307     my $count = 0;\r
308     while (my $row = $sth->fetchrow_hashref) {\r
309         if ((($limit) && ($count < $limit)) || (!$limit)) {\r
310             $row->{'newdate'} = format_date($row->{'newdate'});\r
311             $row->{'expirationdate'} = format_date($row->{'expirationdate'});\r
312             push @opac_news, $row;\r
313         }\r
314         $count++;\r
315     }\r
316     return ($count, \@opac_news);\r
317 }\r
318 \r
319 =head2 GetNewsToDisplay\r
320     \r
321     $news = &GetNewsToDisplay($lang);\r
322     C<$news> is a ref to an array which containts\r
323     all news with expirationdate > today or expirationdate is null.\r
324     \r
325 =cut\r
326 \r
327 sub GetNewsToDisplay {\r
328     my $lang = shift;\r
329     my $dbh = C4::Context->dbh;\r
330     my $query = "\r
331      SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate\r
332      FROM   opac_news\r
333      WHERE   (\r
334         expirationdate > CURRENT_DATE()\r
335         OR    expirationdate IS NULL\r
336         OR    expirationdate = '00-00-0000'\r
337       )\r
338       AND   lang = ?\r
339       ORDER BY number\r
340     ";\r
341     my $sth = $dbh->prepare($query);\r
342     $sth->execute($lang);\r
343     my @results;\r
344     while ( my $row = $sth->fetchrow_hashref ){\r
345         push @results, $row;\r
346     }\r
347     return \@results;\r
348 }\r
349 \r
350 ### get electronic databases\r
351 \r
352 sub add_opac_electronic {\r
353     my ($title, $edata, $lang,$image,$href,$section) = @_;\r
354     my $dbh = C4::Context->dbh;\r
355     my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");\r
356     $sth->execute($title, $edata, $lang,$image,$href,$section);\r
357     $sth->finish;\r
358     return 1;\r
359 }\r
360 \r
361 sub upd_opac_electronic {\r
362     my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;\r
363     my $dbh = C4::Context->dbh;\r
364     my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");\r
365     $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);\r
366     $sth->finish;\r
367     return 1;\r
368 }\r
369 \r
370 sub del_opac_electronic {\r
371     my ($ids) = @_;\r
372     if ($ids) {\r
373         my $dbh = C4::Context->dbh;\r
374         my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");\r
375         $sth->execute();\r
376         $sth->finish;\r
377         return 1;\r
378     } else {\r
379         return 0;\r
380     }\r
381 }\r
382 \r
383 sub get_opac_electronic {\r
384     my ($idelectronic) = @_;\r
385     my $dbh = C4::Context->dbh;\r
386     my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");\r
387     $sth->execute($idelectronic);\r
388     my $data = $sth->fetchrow_hashref;\r
389     $data->{$data->{'lang'}} = 1;\r
390     $data->{$data->{'section'}} = 1;\r
391     $sth->finish;\r
392     return $data;\r
393 }\r
394 \r
395 sub get_opac_electronics {\r
396     my ($section, $lang) = @_;\r
397     my $dbh = C4::Context->dbh;\r
398     my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";\r
399     if ($lang) {\r
400         $query.= " WHERE lang = '" .$lang ."' ";\r
401     }\r
402     if ($section) {\r
403         $query.= " and section= '" . $section."' ";\r
404     }\r
405     $query.= " ORDER BY title ";\r
406     \r
407     my $sth = $dbh->prepare($query);\r
408     $sth->execute();\r
409     my @opac_electronic;\r
410     my $count = 0;\r
411     while (my $row = $sth->fetchrow_hashref) {\r
412             push @opac_electronic, $row;\r
413 \r
414     \r
415         $count++;\r
416     }\r
417 \r
418     return ($count,\@opac_electronic);\r
419 }\r
420 END { }    # module clean-up code here (global destructor)\r
421 \r
422 =back\r
423 \r
424 =head1 AUTHOR\r
425 \r
426 TG\r
427 \r
428 =cut\r
429 \r
430 \r