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