C4::Biblio::AddBiblioAndItems - added duplicate barcode check
[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     my $query = "
330      SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
331      FROM   opac_news
332      WHERE   (
333         expirationdate > CURRENT_DATE()
334         OR    expirationdate IS NULL
335         OR    expirationdate = '00-00-0000'
336       )
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         push @results, $row;
345     }
346     return \@results;
347 }
348
349 ### get electronic databases
350
351 sub add_opac_electronic {
352     my ($title, $edata, $lang,$image,$href,$section) = @_;
353     my $dbh = C4::Context->dbh;
354     my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
355     $sth->execute($title, $edata, $lang,$image,$href,$section);
356     $sth->finish;
357     return 1;
358 }
359
360 sub upd_opac_electronic {
361     my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
362     my $dbh = C4::Context->dbh;
363     my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
364     $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
365     $sth->finish;
366     return 1;
367 }
368
369 sub del_opac_electronic {
370     my ($ids) = @_;
371     if ($ids) {
372         my $dbh = C4::Context->dbh;
373         my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
374         $sth->execute();
375         $sth->finish;
376         return 1;
377     } else {
378         return 0;
379     }
380 }
381
382 sub get_opac_electronic {
383     my ($idelectronic) = @_;
384     my $dbh = C4::Context->dbh;
385     my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
386     $sth->execute($idelectronic);
387     my $data = $sth->fetchrow_hashref;
388     $data->{$data->{'lang'}} = 1;
389     $data->{$data->{'section'}} = 1;
390     $sth->finish;
391     return $data;
392 }
393
394 sub get_opac_electronics {
395     my ($section, $lang) = @_;
396     my $dbh = C4::Context->dbh;
397     my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";
398     if ($lang) {
399         $query.= " WHERE lang = '" .$lang ."' ";
400     }
401     if ($section) {
402         $query.= " and section= '" . $section."' ";
403     }
404     $query.= " ORDER BY title ";
405     
406     my $sth = $dbh->prepare($query);
407     $sth->execute();
408     my @opac_electronic;
409     my $count = 0;
410     while (my $row = $sth->fetchrow_hashref) {
411             push @opac_electronic, $row;
412
413     
414         $count++;
415     }
416
417     return ($count,\@opac_electronic);
418 }
419 END { }    # module clean-up code here (global destructor)
420
421 =back
422
423 =head1 AUTHOR
424
425 TG
426
427 =cut