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
32 C4::NewsChannels - Functions to manage the news channels and its categories
36 This module provides the functions needed to admin the news channels and its categories
47 &news_channels &get_new_channel &del_channels &add_channel &update_channel
48 &news_channels_categories &get_new_channel_category &del_channels_categories
49 &add_channel_category &update_channel_category &news_channels_by_category
50 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
51 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
57 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
59 Looks up news channels by name or category.
61 C<$channel_name> is the channel name to search.
63 C<$id_category> is the channel category code to search.
65 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
67 if none of the params are set C<&news_channels> returns all the news channels.
69 C<&news_channels> returns two values: an integer giving the number of
70 news channels found and a reference to an array
71 of references to hash, which has the news_channels and news_channels_categories fields.
76 my ($channel_name, $id_category, $unclassified) = @_;
77 my $dbh = C4::Context->dbh;
79 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
80 if ( ($channel_name ne '') && ($id_category ne '') ) {
81 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
82 } elsif ($channel_name ne '') {
83 $query.= " WHERE channel_name like '" . $channel_name . "%'";
84 } elsif ($id_category ne '') {
85 $query.= " WHERE news_channels.id_category = " . $id_category;
86 } elsif ($unclassified) {
87 $query.= " WHERE news_channels.id_category IS NULL ";
89 my $sth = $dbh->prepare($query);
91 while (my $row = $sth->fetchrow_hashref) {
95 return (scalar(@channels), @channels);
98 =item news_channels_by_category
100 ($count, @results) = &news_channels_by_category();
102 Looks up news channels grouped by category.
104 C<&news_channels_by_category> returns two values: an integer giving the number of
105 categories found and a reference to an array
106 of references to hash, which the following keys:
110 =item C<channels_count>
112 The number of news channels in that category
116 A reference to an array of references to hash which keys are the new_channels fields.
118 Additionally the last index of results has a reference to all the news channels which don't have a category
122 sub news_channels_by_category {
124 my ($categories_count, @results) = &news_channels_categories();
125 foreach my $row (@results) {
127 my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
128 $row->{'channels_count'} = $channels_count;
129 $row->{'channels'} = \@channels;
132 my ($channels_count, @channels) = &news_channels('', '', 1);
134 $row{'id_category'} = -1;
135 $row{'unclassified'} = 1;
136 $row{'channels_count'} = $channels_count;
137 $row{'channels'} = \@channels;
138 push @results, \%row;
140 return (scalar(@results), @results);
143 sub get_new_channel {
145 my $dbh = C4::Context->dbh;
146 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
148 my $channel = $sth->fetchrow_hashref;
156 my $dbh = C4::Context->dbh;
157 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
166 my ($name, $url, $id_category, $notes) = @_;
167 my $dbh = C4::Context->dbh;
168 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
169 $sth->execute($name, $url, $id_category, $notes);
175 my ($id, $name, $url, $id_category, $notes) = @_;
176 my $dbh = C4::Context->dbh;
177 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
178 $sth->execute($name, $url, $id_category, $notes, $id);
183 sub news_channels_categories {
184 my $dbh = C4::Context->dbh;
186 my $query = "SELECT * FROM news_channels_categories";
187 my $sth = $dbh->prepare($query);
189 while (my $row = $sth->fetchrow_hashref) {
190 push @categories, $row;
193 return (scalar(@categories), @categories);
197 sub get_new_channel_category {
199 my $dbh = C4::Context->dbh;
200 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
202 my $category = $sth->fetchrow_hashref;
207 sub del_channels_categories {
210 my $dbh = C4::Context->dbh;
211 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
213 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
221 sub add_channel_category {
223 my $dbh = C4::Context->dbh;
224 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
225 $sth->execute($name);
230 sub update_channel_category {
231 my ($id, $name) = @_;
232 my $dbh = C4::Context->dbh;
233 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
234 $sth->execute($name, $id);
241 my ($title, $new, $lang) = @_;
242 my $dbh = C4::Context->dbh;
243 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang) VALUES (?,?,?)");
244 $sth->execute($title, $new, $lang);
250 my ($idnew, $title, $new, $lang) = @_;
251 my $dbh = C4::Context->dbh;
252 my $sth = $dbh->prepare("UPDATE opac_news SET title = ?, new = ?, lang = ? WHERE idnew = ?");
253 $sth->execute($title, $new, $lang, $idnew);
261 my $dbh = C4::Context->dbh;
262 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
273 my $dbh = C4::Context->dbh;
274 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
275 $sth->execute($idnew);
276 my $data = $sth->fetchrow_hashref;
277 $data->{$data->{'lang'}} = 1;
283 my ($limit, $lang) = @_;
284 my $dbh = C4::Context->dbh;
285 my $query = "SELECT *, DATE_FORMAT(timestamp,'%Y-%m-%d') AS newdate FROM opac_news";
287 $query.= " WHERE lang = '" .$lang ."' ";
289 $query.= " ORDER BY timestamp DESC ";
291 # $query.= "LIMIT 0, " . $limit;
293 my $sth = $dbh->prepare($query);
297 while (my $row = $sth->fetchrow_hashref) {
298 if ((($limit) && ($count < $limit)) || (!$limit)) {
299 $row->{'newdate'} = format_date($row->{'newdate'});
300 push @opac_news, $row;
304 return ($count, \@opac_news);
307 ### get electronic databases
309 sub add_opac_electronic {
310 my ($title, $edata, $lang,$image,$href,$section) = @_;
311 my $dbh = C4::Context->dbh;
312 my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
313 $sth->execute($title, $edata, $lang,$image,$href,$section);
318 sub upd_opac_electronic {
319 my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
320 my $dbh = C4::Context->dbh;
321 my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
322 $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
327 sub del_opac_electronic {
330 my $dbh = C4::Context->dbh;
331 my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
340 sub get_opac_electronic {
341 my ($idelectronic) = @_;
342 my $dbh = C4::Context->dbh;
343 my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
344 $sth->execute($idelectronic);
345 my $data = $sth->fetchrow_hashref;
346 $data->{$data->{'lang'}} = 1;
347 $data->{$data->{'section'}} = 1;
352 sub get_opac_electronics {
353 my ($section, $lang) = @_;
354 my $dbh = C4::Context->dbh;
355 my $query = "SELECT *, DATE_FORMAT(timestamp, '%Y-%m-%d') AS newdate FROM opac_electronic";
357 $query.= " WHERE lang = '" .$lang ."' ";
360 $query.= " and section= '" . $section."' ";
362 $query.= " ORDER BY title ";
364 my $sth = $dbh->prepare($query);
368 while (my $row = $sth->fetchrow_hashref) {
369 $row->{'newdate'}=format_date($row->{'newdate'});
370 push @opac_electronic, $row;
376 return ($count,\@opac_electronic);
378 END { } # module clean-up code here (global destructor)