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