Added issue date column to issues list
[koha.git] / C4 / NewsChannels.pm
blobec222baba6fb06e6879c8c16d8470e0564b13b32
1 package C4::NewsChannels;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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
20 use strict;
22 use C4::Context;
23 use C4::Dates qw(format_date);
25 use vars qw($VERSION @ISA @EXPORT);
27 BEGIN {
28 $VERSION = 3.01; # set the version for version checking
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 &GetNewsToDisplay
32 &news_channels &get_new_channel &del_channels &add_channel &update_channel
33 &news_channels_categories &get_new_channel_category &del_channels_categories
34 &add_channel_category &update_channel_category &news_channels_by_category
35 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
36 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
40 =head1 NAME
42 C4::NewsChannels - Functions to manage the news channels and its categories
44 =head1 DESCRIPTION
46 This module provides the functions needed to admin the news channels and its categories
48 =head1 FUNCTIONS
50 =over 2
52 =item news_channels
54 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
56 Looks up news channels by name or category.
58 C<$channel_name> is the channel name to search.
60 C<$id_category> is the channel category code to search.
62 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
64 if none of the params are set C<&news_channels> returns all the news channels.
66 C<&news_channels> returns two values: an integer giving the number of
67 news channels found and a reference to an array
68 of references to hash, which has the news_channels and news_channels_categories fields.
70 =cut
72 sub news_channels {
73 my ($channel_name, $id_category, $unclassified) = @_;
74 my $dbh = C4::Context->dbh;
75 my @channels;
76 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
77 if ( ($channel_name ne '') && ($id_category ne '') ) {
78 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
79 } elsif ($channel_name ne '') {
80 $query.= " WHERE channel_name like '" . $channel_name . "%'";
81 } elsif ($id_category ne '') {
82 $query.= " WHERE news_channels.id_category = " . $id_category;
83 } elsif ($unclassified) {
84 $query.= " WHERE news_channels.id_category IS NULL ";
86 my $sth = $dbh->prepare($query);
87 $sth->execute();
88 while (my $row = $sth->fetchrow_hashref) {
89 push @channels, $row;
91 $sth->finish;
92 return (scalar(@channels), @channels);
95 =item news_channels_by_category
97 ($count, @results) = &news_channels_by_category();
99 Looks up news channels grouped by category.
101 C<&news_channels_by_category> returns two values: an integer giving the number of
102 categories found and a reference to an array
103 of references to hash, which the following keys:
105 =over 4
107 =item C<channels_count>
109 The number of news channels in that category
111 =item C<channels>
113 A reference to an array of references to hash which keys are the new_channels fields.
115 Additionally the last index of results has a reference to all the news channels which don't have a category
117 =cut
119 sub news_channels_by_category {
121 my ($categories_count, @results) = &news_channels_categories();
122 foreach my $row (@results) {
124 my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
125 $row->{'channels_count'} = $channels_count;
126 $row->{'channels'} = \@channels;
129 my ($channels_count, @channels) = &news_channels('', '', 1);
130 my %row;
131 $row{'id_category'} = -1;
132 $row{'unclassified'} = 1;
133 $row{'channels_count'} = $channels_count;
134 $row{'channels'} = \@channels;
135 push @results, \%row;
137 return (scalar(@results), @results);
140 sub get_new_channel {
141 my ($id) = @_;
142 my $dbh = C4::Context->dbh;
143 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
144 $sth->execute($id);
145 my $channel = $sth->fetchrow_hashref;
146 $sth->finish;
147 return $channel;
150 sub del_channels {
151 my ($ids) = @_;
152 if ($ids ne '') {
153 my $dbh = C4::Context->dbh;
154 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
155 $sth->execute();
156 $sth->finish;
157 return $ids;
159 return 0;
162 sub add_channel {
163 my ($name, $url, $id_category, $notes) = @_;
164 my $dbh = C4::Context->dbh;
165 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
166 $sth->execute($name, $url, $id_category, $notes);
167 $sth->finish;
168 return 1;
171 sub update_channel {
172 my ($id, $name, $url, $id_category, $notes) = @_;
173 my $dbh = C4::Context->dbh;
174 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
175 $sth->execute($name, $url, $id_category, $notes, $id);
176 $sth->finish;
177 return 1;
180 sub news_channels_categories {
181 my $dbh = C4::Context->dbh;
182 my @categories;
183 my $query = "SELECT * FROM news_channels_categories";
184 my $sth = $dbh->prepare($query);
185 $sth->execute();
186 while (my $row = $sth->fetchrow_hashref) {
187 push @categories, $row;
189 $sth->finish;
190 return (scalar(@categories), @categories);
194 sub get_new_channel_category {
195 my ($id) = @_;
196 my $dbh = C4::Context->dbh;
197 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
198 $sth->execute($id);
199 my $category = $sth->fetchrow_hashref;
200 $sth->finish;
201 return $category;
204 sub del_channels_categories {
205 my ($ids) = @_;
206 if ($ids ne '') {
207 my $dbh = C4::Context->dbh;
208 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
209 $sth->execute();
210 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
211 $sth->execute();
212 $sth->finish;
213 return $ids;
215 return 0;
218 sub add_channel_category {
219 my ($name) = @_;
220 my $dbh = C4::Context->dbh;
221 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
222 $sth->execute($name);
223 $sth->finish;
224 return 1;
227 sub update_channel_category {
228 my ($id, $name) = @_;
229 my $dbh = C4::Context->dbh;
230 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
231 $sth->execute($name, $id);
232 $sth->finish;
233 return 1;
236 sub add_opac_new {
237 my ($title, $new, $lang, $expirationdate, $number) = @_;
238 my $dbh = C4::Context->dbh;
239 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, number) VALUES (?,?,?,?,?)");
240 $sth->execute($title, $new, $lang, $expirationdate, $number);
241 $sth->finish;
242 return 1;
245 sub upd_opac_new {
246 my ($idnew, $title, $new, $lang, $expirationdate, $number) = @_;
247 my $dbh = C4::Context->dbh;
248 my $sth = $dbh->prepare("
249 UPDATE opac_news SET
250 title = ?,
251 new = ?,
252 lang = ?,
253 expirationdate = ?,
254 number = ?
255 WHERE idnew = ?
257 $sth->execute($title, $new, $lang, $expirationdate,$number,$idnew);
258 $sth->finish;
259 return 1;
262 sub del_opac_new {
263 my ($ids) = @_;
264 if ($ids) {
265 my $dbh = C4::Context->dbh;
266 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
267 $sth->execute();
268 $sth->finish;
269 return 1;
270 } else {
271 return 0;
275 sub get_opac_new {
276 my ($idnew) = @_;
277 my $dbh = C4::Context->dbh;
278 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
279 $sth->execute($idnew);
280 my $data = $sth->fetchrow_hashref;
281 $data->{$data->{'lang'}} = 1;
282 $data->{expirationdate} = format_date($data->{expirationdate});
283 $sth->finish;
284 return $data;
287 sub get_opac_news {
288 my ($limit, $lang) = @_;
289 my $dbh = C4::Context->dbh;
290 my $query = "SELECT *, timestamp AS newdate FROM opac_news";
291 if ($lang) {
292 $query.= " WHERE lang = '" .$lang ."' ";
294 $query.= " ORDER BY timestamp DESC ";
295 #if ($limit) {
296 # $query.= "LIMIT 0, " . $limit;
298 my $sth = $dbh->prepare($query);
299 $sth->execute();
300 my @opac_news;
301 my $count = 0;
302 while (my $row = $sth->fetchrow_hashref) {
303 if ((($limit) && ($count < $limit)) || (!$limit)) {
304 $row->{'newdate'} = format_date($row->{'newdate'});
305 $row->{'expirationdate'} = format_date($row->{'expirationdate'});
306 push @opac_news, $row;
308 $count++;
310 return ($count, \@opac_news);
313 =head2 GetNewsToDisplay
315 $news = &GetNewsToDisplay($lang);
316 C<$news> is a ref to an array which containts
317 all news with expirationdate > today or expirationdate is null.
319 =cut
321 sub GetNewsToDisplay {
322 my $lang = shift;
323 my $dbh = C4::Context->dbh;
324 # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
325 my $query = "
326 SELECT *,timestamp AS newdate
327 FROM opac_news
328 WHERE (
329 expirationdate > CURRENT_DATE()
330 OR expirationdate IS NULL
331 OR expirationdate = '00-00-0000'
333 AND lang = ?
334 ORDER BY number
335 "; # expirationdate field is NOT in ISO format?
336 my $sth = $dbh->prepare($query);
337 $sth->execute($lang);
338 my @results;
339 while ( my $row = $sth->fetchrow_hashref ){
340 $row->{newdate} = format_date($row->{newdate});
341 push @results, $row;
343 return \@results;
346 ### get electronic databases
348 sub add_opac_electronic {
349 my ($title, $edata, $lang,$image,$href,$section) = @_;
350 my $dbh = C4::Context->dbh;
351 my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
352 $sth->execute($title, $edata, $lang,$image,$href,$section);
353 $sth->finish;
354 return 1;
357 sub upd_opac_electronic {
358 my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
359 my $dbh = C4::Context->dbh;
360 my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
361 $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
362 $sth->finish;
363 return 1;
366 sub del_opac_electronic {
367 my ($ids) = @_;
368 if ($ids) {
369 my $dbh = C4::Context->dbh;
370 my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
371 $sth->execute();
372 $sth->finish;
373 return 1;
374 } else {
375 return 0;
379 sub get_opac_electronic {
380 my ($idelectronic) = @_;
381 my $dbh = C4::Context->dbh;
382 my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
383 $sth->execute($idelectronic);
384 my $data = $sth->fetchrow_hashref;
385 $data->{$data->{'lang'}} = 1;
386 $data->{$data->{'section'}} = 1;
387 $sth->finish;
388 return $data;
391 sub get_opac_electronics {
392 my ($section, $lang) = @_;
393 my $dbh = C4::Context->dbh;
394 my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";
395 if ($lang) {
396 $query.= " WHERE lang = '" .$lang ."' ";
398 if ($section) {
399 $query.= " and section= '" . $section."' ";
401 $query.= " ORDER BY title ";
403 my $sth = $dbh->prepare($query);
404 $sth->execute();
405 my @opac_electronic;
406 my $count = 0;
407 while (my $row = $sth->fetchrow_hashref) {
408 push @opac_electronic, $row;
409 $count++;
412 return ($count,\@opac_electronic);
416 __END__
418 =back
420 =head1 AUTHOR
424 =cut