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
25 use vars qw($VERSION @ISA @EXPORT);
27 # set the version for version checking
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
29 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
34 C4::NewsChannels - Functions to manage the news channels and its categories
38 This module provides the functions needed to admin the news channels and its categories
50 &news_channels &get_new_channel &del_channels &add_channel &update_channel
51 &news_channels_categories &get_new_channel_category &del_channels_categories
52 &add_channel_category &update_channel_category &news_channels_by_category
53 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
54 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
60 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
62 Looks up news channels by name or category.
64 C<$channel_name> is the channel name to search.
66 C<$id_category> is the channel category code to search.
68 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
70 if none of the params are set C<&news_channels> returns all the news channels.
72 C<&news_channels> returns two values: an integer giving the number of
73 news channels found and a reference to an array
74 of references to hash, which has the news_channels and news_channels_categories fields.
79 my ($channel_name, $id_category, $unclassified) = @_;
80 my $dbh = C4::Context->dbh;
82 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
83 if ( ($channel_name ne '') && ($id_category ne '') ) {
84 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
85 } elsif ($channel_name ne '') {
86 $query.= " WHERE channel_name like '" . $channel_name . "%'";
87 } elsif ($id_category ne '') {
88 $query.= " WHERE news_channels.id_category = " . $id_category;
89 } elsif ($unclassified) {
90 $query.= " WHERE news_channels.id_category IS NULL ";
92 my $sth = $dbh->prepare($query);
94 while (my $row = $sth->fetchrow_hashref) {
98 return (scalar(@channels), @channels);
101 =item news_channels_by_category
103 ($count, @results) = &news_channels_by_category();
105 Looks up news channels grouped by category.
107 C<&news_channels_by_category> returns two values: an integer giving the number of
108 categories found and a reference to an array
109 of references to hash, which the following keys:
113 =item C<channels_count>
115 The number of news channels in that category
119 A reference to an array of references to hash which keys are the new_channels fields.
121 Additionally the last index of results has a reference to all the news channels which don't have a category
125 sub news_channels_by_category {
127 my ($categories_count, @results) = &news_channels_categories();
128 foreach my $row (@results) {
130 my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
131 $row->{'channels_count'} = $channels_count;
132 $row->{'channels'} = \@channels;
135 my ($channels_count, @channels) = &news_channels('', '', 1);
137 $row{'id_category'} = -1;
138 $row{'unclassified'} = 1;
139 $row{'channels_count'} = $channels_count;
140 $row{'channels'} = \@channels;
141 push @results, \%row;
143 return (scalar(@results), @results);
146 sub get_new_channel {
148 my $dbh = C4::Context->dbh;
149 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
151 my $channel = $sth->fetchrow_hashref;
159 my $dbh = C4::Context->dbh;
160 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
169 my ($name, $url, $id_category, $notes) = @_;
170 my $dbh = C4::Context->dbh;
171 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
172 $sth->execute($name, $url, $id_category, $notes);
178 my ($id, $name, $url, $id_category, $notes) = @_;
179 my $dbh = C4::Context->dbh;
180 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
181 $sth->execute($name, $url, $id_category, $notes, $id);
186 sub news_channels_categories {
187 my $dbh = C4::Context->dbh;
189 my $query = "SELECT * FROM news_channels_categories";
190 my $sth = $dbh->prepare($query);
192 while (my $row = $sth->fetchrow_hashref) {
193 push @categories, $row;
196 return (scalar(@categories), @categories);
200 sub get_new_channel_category {
202 my $dbh = C4::Context->dbh;
203 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
205 my $category = $sth->fetchrow_hashref;
210 sub del_channels_categories {
213 my $dbh = C4::Context->dbh;
214 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
216 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
224 sub add_channel_category {
226 my $dbh = C4::Context->dbh;
227 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
228 $sth->execute($name);
233 sub update_channel_category {
234 my ($id, $name) = @_;
235 my $dbh = C4::Context->dbh;
236 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
237 $sth->execute($name, $id);
243 my ($title, $new, $lang, $expirationdate, $number) = @_;
244 my $dbh = C4::Context->dbh;
245 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, number) VALUES (?,?,?,?,?)");
246 $sth->execute($title, $new, $lang, $expirationdate, $number);
252 my ($idnew, $title, $new, $lang, $expirationdate, $number) = @_;
253 my $dbh = C4::Context->dbh;
254 my $sth = $dbh->prepare("
263 $sth->execute($title, $new, $lang, $expirationdate,$number,$idnew);
271 my $dbh = C4::Context->dbh;
272 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
283 my $dbh = C4::Context->dbh;
284 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
285 $sth->execute($idnew);
286 my $data = $sth->fetchrow_hashref;
287 $data->{$data->{'lang'}} = 1;
288 $data->{expirationdate} = format_date($data->{expirationdate});
294 my ($limit, $lang) = @_;
295 my $dbh = C4::Context->dbh;
296 my $query = "SELECT *, timestamp AS newdate FROM opac_news";
298 $query.= " WHERE lang = '" .$lang ."' ";
300 $query.= " ORDER BY timestamp DESC ";
302 # $query.= "LIMIT 0, " . $limit;
304 my $sth = $dbh->prepare($query);
308 while (my $row = $sth->fetchrow_hashref) {
309 if ((($limit) && ($count < $limit)) || (!$limit)) {
310 $row->{'newdate'} = format_date($row->{'newdate'});
311 $row->{'expirationdate'} = format_date($row->{'expirationdate'});
312 push @opac_news, $row;
316 return ($count, \@opac_news);
319 =head2 GetNewsToDisplay
321 $news = &GetNewsToDisplay($lang);
322 C<$news> is a ref to an array which containts
323 all news with expirationdate > today or expirationdate is null.
327 sub GetNewsToDisplay {
329 my $dbh = C4::Context->dbh;
331 SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
334 expirationdate > CURRENT_DATE()
335 OR expirationdate IS NULL
336 OR expirationdate = '00-00-0000'
341 my $sth = $dbh->prepare($query);
342 $sth->execute($lang);
344 while ( my $row = $sth->fetchrow_hashref ){
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;
418 return ($count,\@opac_electronic);
420 END { } # module clean-up code here (global destructor)