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
23 use C4::Dates qw(format_date);
25 use vars qw($VERSION @ISA @EXPORT);
28 $VERSION = 3.00; # set the version for version checking
33 C4::NewsChannels - Functions to manage the news channels and its categories
37 This module provides the functions needed to admin the news channels and its categories
49 &news_channels &get_new_channel &del_channels &add_channel &update_channel
50 &news_channels_categories &get_new_channel_category &del_channels_categories
51 &add_channel_category &update_channel_category &news_channels_by_category
52 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
53 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
59 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
61 Looks up news channels by name or category.
63 C<$channel_name> is the channel name to search.
65 C<$id_category> is the channel category code to search.
67 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
69 if none of the params are set C<&news_channels> returns all the news channels.
71 C<&news_channels> returns two values: an integer giving the number of
72 news channels found and a reference to an array
73 of references to hash, which has the news_channels and news_channels_categories fields.
78 my ($channel_name, $id_category, $unclassified) = @_;
79 my $dbh = C4::Context->dbh;
81 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
82 if ( ($channel_name ne '') && ($id_category ne '') ) {
83 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
84 } elsif ($channel_name ne '') {
85 $query.= " WHERE channel_name like '" . $channel_name . "%'";
86 } elsif ($id_category ne '') {
87 $query.= " WHERE news_channels.id_category = " . $id_category;
88 } elsif ($unclassified) {
89 $query.= " WHERE news_channels.id_category IS NULL ";
91 my $sth = $dbh->prepare($query);
93 while (my $row = $sth->fetchrow_hashref) {
97 return (scalar(@channels), @channels);
100 =item news_channels_by_category
102 ($count, @results) = &news_channels_by_category();
104 Looks up news channels grouped by category.
106 C<&news_channels_by_category> returns two values: an integer giving the number of
107 categories found and a reference to an array
108 of references to hash, which the following keys:
112 =item C<channels_count>
114 The number of news channels in that category
118 A reference to an array of references to hash which keys are the new_channels fields.
120 Additionally the last index of results has a reference to all the news channels which don't have a category
124 sub news_channels_by_category {
126 my ($categories_count, @results) = &news_channels_categories();
127 foreach my $row (@results) {
129 my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
130 $row->{'channels_count'} = $channels_count;
131 $row->{'channels'} = \@channels;
134 my ($channels_count, @channels) = &news_channels('', '', 1);
136 $row{'id_category'} = -1;
137 $row{'unclassified'} = 1;
138 $row{'channels_count'} = $channels_count;
139 $row{'channels'} = \@channels;
140 push @results, \%row;
142 return (scalar(@results), @results);
145 sub get_new_channel {
147 my $dbh = C4::Context->dbh;
148 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
150 my $channel = $sth->fetchrow_hashref;
158 my $dbh = C4::Context->dbh;
159 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
168 my ($name, $url, $id_category, $notes) = @_;
169 my $dbh = C4::Context->dbh;
170 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
171 $sth->execute($name, $url, $id_category, $notes);
177 my ($id, $name, $url, $id_category, $notes) = @_;
178 my $dbh = C4::Context->dbh;
179 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
180 $sth->execute($name, $url, $id_category, $notes, $id);
185 sub news_channels_categories {
186 my $dbh = C4::Context->dbh;
188 my $query = "SELECT * FROM news_channels_categories";
189 my $sth = $dbh->prepare($query);
191 while (my $row = $sth->fetchrow_hashref) {
192 push @categories, $row;
195 return (scalar(@categories), @categories);
199 sub get_new_channel_category {
201 my $dbh = C4::Context->dbh;
202 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
204 my $category = $sth->fetchrow_hashref;
209 sub del_channels_categories {
212 my $dbh = C4::Context->dbh;
213 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
215 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
223 sub add_channel_category {
225 my $dbh = C4::Context->dbh;
226 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
227 $sth->execute($name);
232 sub update_channel_category {
233 my ($id, $name) = @_;
234 my $dbh = C4::Context->dbh;
235 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
236 $sth->execute($name, $id);
242 my ($title, $new, $lang, $expirationdate, $number) = @_;
243 my $dbh = C4::Context->dbh;
244 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, number) VALUES (?,?,?,?,?)");
245 $sth->execute($title, $new, $lang, $expirationdate, $number);
251 my ($idnew, $title, $new, $lang, $expirationdate, $number) = @_;
252 my $dbh = C4::Context->dbh;
253 my $sth = $dbh->prepare("
262 $sth->execute($title, $new, $lang, $expirationdate,$number,$idnew);
270 my $dbh = C4::Context->dbh;
271 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
282 my $dbh = C4::Context->dbh;
283 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
284 $sth->execute($idnew);
285 my $data = $sth->fetchrow_hashref;
286 $data->{$data->{'lang'}} = 1;
287 $data->{expirationdate} = format_date($data->{expirationdate});
293 my ($limit, $lang) = @_;
294 my $dbh = C4::Context->dbh;
295 my $query = "SELECT *, timestamp AS newdate FROM opac_news";
297 $query.= " WHERE lang = '" .$lang ."' ";
299 $query.= " ORDER BY timestamp DESC ";
301 # $query.= "LIMIT 0, " . $limit;
303 my $sth = $dbh->prepare($query);
307 while (my $row = $sth->fetchrow_hashref) {
308 if ((($limit) && ($count < $limit)) || (!$limit)) {
309 $row->{'newdate'} = format_date($row->{'newdate'});
310 $row->{'expirationdate'} = format_date($row->{'expirationdate'});
311 push @opac_news, $row;
315 return ($count, \@opac_news);
318 =head2 GetNewsToDisplay
320 $news = &GetNewsToDisplay($lang);
321 C<$news> is a ref to an array which containts
322 all news with expirationdate > today or expirationdate is null.
326 sub GetNewsToDisplay {
328 my $dbh = C4::Context->dbh;
330 SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
333 expirationdate > CURRENT_DATE()
334 OR expirationdate IS NULL
335 OR expirationdate = '00-00-0000'
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 ){
349 ### get electronic databases
351 sub add_opac_electronic {
352 my ($title, $edata, $lang,$image,$href,$section) = @_;
353 my $dbh = C4::Context->dbh;
354 my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
355 $sth->execute($title, $edata, $lang,$image,$href,$section);
360 sub upd_opac_electronic {
361 my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
362 my $dbh = C4::Context->dbh;
363 my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
364 $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
369 sub del_opac_electronic {
372 my $dbh = C4::Context->dbh;
373 my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
382 sub get_opac_electronic {
383 my ($idelectronic) = @_;
384 my $dbh = C4::Context->dbh;
385 my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
386 $sth->execute($idelectronic);
387 my $data = $sth->fetchrow_hashref;
388 $data->{$data->{'lang'}} = 1;
389 $data->{$data->{'section'}} = 1;
394 sub get_opac_electronics {
395 my ($section, $lang) = @_;
396 my $dbh = C4::Context->dbh;
397 my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";
399 $query.= " WHERE lang = '" .$lang ."' ";
402 $query.= " and section= '" . $section."' ";
404 $query.= " ORDER BY title ";
406 my $sth = $dbh->prepare($query);
410 while (my $row = $sth->fetchrow_hashref) {
411 push @opac_electronic, $row;
417 return ($count,\@opac_electronic);
419 END { } # module clean-up code here (global destructor)