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
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
24 use C4::Dates qw(format_date);
26 use vars qw($VERSION @ISA @EXPORT);
29 $VERSION = 3.01; # set the version for version checking
33 &news_channels &get_new_channel &del_channels &add_channel &update_channel
34 &news_channels_categories &get_new_channel_category &del_channels_categories
35 &add_channel_category &update_channel_category &news_channels_by_category
36 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
37 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
43 C4::NewsChannels - Functions to manage the news channels and its categories
47 This module provides the functions needed to admin the news channels and its categories
53 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
55 Looks up news channels by name or category.
57 C<$channel_name> is the channel name to search.
59 C<$id_category> is the channel category code to search.
61 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
63 if none of the params are set C<&news_channels> returns all the news channels.
65 C<&news_channels> returns two values: an integer giving the number of
66 news channels found and a reference to an array
67 of references to hash, which has the news_channels and news_channels_categories fields.
72 my ($channel_name, $id_category, $unclassified) = @_;
73 my $dbh = C4::Context->dbh;
75 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
76 if ( ($channel_name ne '') && ($id_category ne '') ) {
77 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
78 } elsif ($channel_name ne '') {
79 $query.= " WHERE channel_name like '" . $channel_name . "%'";
80 } elsif ($id_category ne '') {
81 $query.= " WHERE news_channels.id_category = " . $id_category;
82 } elsif ($unclassified) {
83 $query.= " WHERE news_channels.id_category IS NULL ";
85 my $sth = $dbh->prepare($query);
87 while (my $row = $sth->fetchrow_hashref) {
91 return (scalar(@channels), @channels);
94 =head2 news_channels_by_category
96 ($count, @results) = &news_channels_by_category();
98 Looks up news channels grouped by category.
100 C<&news_channels_by_category> returns two values: an integer giving the number of
101 categories found and a reference to an array
102 of references to hash, which the following keys:
106 =item C<channels_count>
108 The number of news channels in that category
112 A reference to an array of references to hash which keys are the new_channels fields.
114 Additionally the last index of results has a reference to all the news channels which don't have a category
120 sub news_channels_by_category {
122 my ($categories_count, @results) = &news_channels_categories();
123 foreach my $row (@results) {
125 my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
126 $row->{'channels_count'} = $channels_count;
127 $row->{'channels'} = \@channels;
130 my ($channels_count, @channels) = &news_channels('', '', 1);
132 $row{'id_category'} = -1;
133 $row{'unclassified'} = 1;
134 $row{'channels_count'} = $channels_count;
135 $row{'channels'} = \@channels;
136 push @results, \%row;
138 return (scalar(@results), @results);
141 sub get_new_channel {
143 my $dbh = C4::Context->dbh;
144 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
146 my $channel = $sth->fetchrow_hashref;
154 my $dbh = C4::Context->dbh;
155 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
164 my ($name, $url, $id_category, $notes) = @_;
165 my $dbh = C4::Context->dbh;
166 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
167 $sth->execute($name, $url, $id_category, $notes);
173 my ($id, $name, $url, $id_category, $notes) = @_;
174 my $dbh = C4::Context->dbh;
175 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
176 $sth->execute($name, $url, $id_category, $notes, $id);
181 sub news_channels_categories {
182 my $dbh = C4::Context->dbh;
184 my $query = "SELECT * FROM news_channels_categories";
185 my $sth = $dbh->prepare($query);
187 while (my $row = $sth->fetchrow_hashref) {
188 push @categories, $row;
191 return (scalar(@categories), @categories);
195 sub get_new_channel_category {
197 my $dbh = C4::Context->dbh;
198 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
200 my $category = $sth->fetchrow_hashref;
205 sub del_channels_categories {
208 my $dbh = C4::Context->dbh;
209 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
211 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
219 sub add_channel_category {
221 my $dbh = C4::Context->dbh;
222 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
223 $sth->execute($name);
228 sub update_channel_category {
229 my ($id, $name) = @_;
230 my $dbh = C4::Context->dbh;
231 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
232 $sth->execute($name, $id);
238 my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_;
239 my $dbh = C4::Context->dbh;
240 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)");
241 $sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number);
247 my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_;
248 my $dbh = C4::Context->dbh;
249 my $sth = $dbh->prepare("
259 $sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew);
267 my $dbh = C4::Context->dbh;
268 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
279 my $dbh = C4::Context->dbh;
280 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
281 $sth->execute($idnew);
282 my $data = $sth->fetchrow_hashref;
283 $data->{$data->{'lang'}} = 1 if defined $data->{lang};
284 $data->{expirationdate} = format_date($data->{expirationdate});
285 $data->{timestamp} = format_date($data->{timestamp});
291 my ($limit, $lang) = @_;
292 my $dbh = C4::Context->dbh;
293 my $query = "SELECT *, timestamp AS newdate FROM opac_news";
295 $query.= " WHERE lang = '" .$lang ."' ";
297 $query.= " ORDER BY timestamp DESC ";
299 # $query.= "LIMIT 0, " . $limit;
301 my $sth = $dbh->prepare($query);
305 while (my $row = $sth->fetchrow_hashref) {
306 if ((($limit) && ($count < $limit)) || (!$limit)) {
307 $row->{'newdate'} = format_date($row->{'newdate'});
308 $row->{'expirationdate'} = format_date($row->{'expirationdate'});
309 push @opac_news, $row;
313 return ($count, \@opac_news);
316 =head2 GetNewsToDisplay
318 $news = &GetNewsToDisplay($lang);
319 C<$news> is a ref to an array which containts
320 all news with expirationdate > today or expirationdate is null.
324 sub GetNewsToDisplay {
326 my $dbh = C4::Context->dbh;
327 # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
329 SELECT *,timestamp AS newdate
332 expirationdate >= CURRENT_DATE()
333 OR expirationdate IS NULL
334 OR expirationdate = '00-00-0000'
336 AND `timestamp` <= CURRENT_DATE()
339 "; # expirationdate field is NOT in ISO format?
340 my $sth = $dbh->prepare($query);
341 $sth->execute($lang);
343 while ( my $row = $sth->fetchrow_hashref ){
344 $row->{newdate} = format_date($row->{newdate});
350 ### get electronic databases
352 sub add_opac_electronic {
353 my ($title, $edata, $lang,$image,$href,$section) = @_;
354 my $dbh = C4::Context->dbh;
355 my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
356 $sth->execute($title, $edata, $lang,$image,$href,$section);
361 sub upd_opac_electronic {
362 my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
363 my $dbh = C4::Context->dbh;
364 my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
365 $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
370 sub del_opac_electronic {
373 my $dbh = C4::Context->dbh;
374 my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
383 sub get_opac_electronic {
384 my ($idelectronic) = @_;
385 my $dbh = C4::Context->dbh;
386 my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
387 $sth->execute($idelectronic);
388 my $data = $sth->fetchrow_hashref;
389 $data->{$data->{'lang'}} = 1;
390 $data->{$data->{'section'}} = 1;
395 sub get_opac_electronics {
396 my ($section, $lang) = @_;
397 my $dbh = C4::Context->dbh;
398 my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";
400 $query.= " WHERE lang = '" .$lang ."' ";
403 $query.= " and section= '" . $section."' ";
405 $query.= " ORDER BY title ";
407 my $sth = $dbh->prepare($query);
411 while (my $row = $sth->fetchrow_hashref) {
412 push @opac_electronic, $row;
416 return ($count,\@opac_electronic);