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