1 package C4::NewsChannels;
\r
3 # Copyright 2000-2002 Katipo Communications
\r
5 # This file is part of Koha.
\r
7 # Koha is free software; you can redistribute it and/or modify it under the
\r
8 # terms of the GNU General Public License as published by the Free Software
\r
9 # Foundation; either version 2 of the License, or (at your option) any later
\r
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
\r
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
\r
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
\r
16 # You should have received a copy of the GNU General Public License along with
\r
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
\r
18 # Suite 330, Boston, MA 02111-1307 USA
\r
25 use vars qw($VERSION @ISA @EXPORT);
\r
27 # set the version for version checking
\r
32 C4::NewsChannels - Functions to manage the news channels and its categories
\r
36 This module provides the functions needed to admin the news channels and its categories
\r
45 @ISA = qw(Exporter);
\r
47 &news_channels &get_new_channel &del_channels &add_channel &update_channel
\r
48 &news_channels_categories &get_new_channel_category &del_channels_categories
\r
49 &add_channel_category &update_channel_category &news_channels_by_category
\r
50 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
\r
51 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
\r
57 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
\r
59 Looks up news channels by name or category.
\r
61 C<$channel_name> is the channel name to search.
\r
63 C<$id_category> is the channel category code to search.
\r
65 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
\r
67 if none of the params are set C<&news_channels> returns all the news channels.
\r
69 C<&news_channels> returns two values: an integer giving the number of
\r
70 news channels found and a reference to an array
\r
71 of references to hash, which has the news_channels and news_channels_categories fields.
\r
76 my ($channel_name, $id_category, $unclassified) = @_;
\r
77 my $dbh = C4::Context->dbh;
\r
79 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
\r
80 if ( ($channel_name ne '') && ($id_category ne '') ) {
\r
81 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
\r
82 } elsif ($channel_name ne '') {
\r
83 $query.= " WHERE channel_name like '" . $channel_name . "%'";
\r
84 } elsif ($id_category ne '') {
\r
85 $query.= " WHERE news_channels.id_category = " . $id_category;
\r
86 } elsif ($unclassified) {
\r
87 $query.= " WHERE news_channels.id_category IS NULL ";
\r
89 my $sth = $dbh->prepare($query);
\r
91 while (my $row = $sth->fetchrow_hashref) {
\r
92 push @channels, $row;
\r
95 return (scalar(@channels), @channels);
\r
98 =item news_channels_by_category
\r
100 ($count, @results) = &news_channels_by_category();
\r
102 Looks up news channels grouped by category.
\r
104 C<&news_channels_by_category> returns two values: an integer giving the number of
\r
105 categories found and a reference to an array
\r
106 of references to hash, which the following keys:
\r
110 =item C<channels_count>
\r
112 The number of news channels in that category
\r
116 A reference to an array of references to hash which keys are the new_channels fields.
\r
118 Additionally the last index of results has a reference to all the news channels which don't have a category
\r
122 sub news_channels_by_category {
\r
124 my ($categories_count, @results) = &news_channels_categories();
\r
125 foreach my $row (@results) {
\r
127 my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
\r
128 $row->{'channels_count'} = $channels_count;
\r
129 $row->{'channels'} = \@channels;
\r
132 my ($channels_count, @channels) = &news_channels('', '', 1);
\r
134 $row{'id_category'} = -1;
\r
135 $row{'unclassified'} = 1;
\r
136 $row{'channels_count'} = $channels_count;
\r
137 $row{'channels'} = \@channels;
\r
138 push @results, \%row;
\r
140 return (scalar(@results), @results);
\r
143 sub get_new_channel {
\r
145 my $dbh = C4::Context->dbh;
\r
146 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
\r
147 $sth->execute($id);
\r
148 my $channel = $sth->fetchrow_hashref;
\r
156 my $dbh = C4::Context->dbh;
\r
157 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
\r
166 my ($name, $url, $id_category, $notes) = @_;
\r
167 my $dbh = C4::Context->dbh;
\r
168 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
\r
169 $sth->execute($name, $url, $id_category, $notes);
\r
174 sub update_channel {
\r
175 my ($id, $name, $url, $id_category, $notes) = @_;
\r
176 my $dbh = C4::Context->dbh;
\r
177 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
\r
178 $sth->execute($name, $url, $id_category, $notes, $id);
\r
183 sub news_channels_categories {
\r
184 my $dbh = C4::Context->dbh;
\r
186 my $query = "SELECT * FROM news_channels_categories";
\r
187 my $sth = $dbh->prepare($query);
\r
189 while (my $row = $sth->fetchrow_hashref) {
\r
190 push @categories, $row;
\r
193 return (scalar(@categories), @categories);
\r
197 sub get_new_channel_category {
\r
199 my $dbh = C4::Context->dbh;
\r
200 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
\r
201 $sth->execute($id);
\r
202 my $category = $sth->fetchrow_hashref;
\r
207 sub del_channels_categories {
\r
210 my $dbh = C4::Context->dbh;
\r
211 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
\r
213 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
\r
221 sub add_channel_category {
\r
223 my $dbh = C4::Context->dbh;
\r
224 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
\r
225 $sth->execute($name);
\r
230 sub update_channel_category {
\r
231 my ($id, $name) = @_;
\r
232 my $dbh = C4::Context->dbh;
\r
233 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
\r
234 $sth->execute($name, $id);
\r
241 my ($title, $new, $lang) = @_;
\r
242 my $dbh = C4::Context->dbh;
\r
243 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang) VALUES (?,?,?)");
\r
244 $sth->execute($title, $new, $lang);
\r
250 my ($idnew, $title, $new, $lang) = @_;
\r
251 my $dbh = C4::Context->dbh;
\r
252 my $sth = $dbh->prepare("UPDATE opac_news SET title = ?, new = ?, lang = ? WHERE idnew = ?");
\r
253 $sth->execute($title, $new, $lang, $idnew);
\r
261 my $dbh = C4::Context->dbh;
\r
262 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
\r
273 my $dbh = C4::Context->dbh;
\r
274 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
\r
275 $sth->execute($idnew);
\r
276 my $data = $sth->fetchrow_hashref;
\r
277 $data->{$data->{'lang'}} = 1;
\r
282 sub get_opac_news {
\r
283 my ($limit, $lang) = @_;
\r
284 my $dbh = C4::Context->dbh;
\r
285 my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_news";
\r
287 $query.= " WHERE lang = '" .$lang ."' ";
\r
289 $query.= " ORDER BY timestamp DESC ";
\r
291 # $query.= "LIMIT 0, " . $limit;
\r
293 my $sth = $dbh->prepare($query);
\r
297 while (my $row = $sth->fetchrow_hashref) {
\r
298 if ((($limit) && ($count < $limit)) || (!$limit)) {
\r
299 $row->{'newdate'} = format_date($row->{'newdate'});
\r
300 push @opac_news, $row;
\r
304 return ($count, \@opac_news);
\r
307 ### get electronic databases
\r
309 sub add_opac_electronic {
\r
310 my ($title, $edata, $lang,$image,$href,$section) = @_;
\r
311 my $dbh = C4::Context->dbh;
\r
312 my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
\r
313 $sth->execute($title, $edata, $lang,$image,$href,$section);
\r
318 sub upd_opac_electronic {
\r
319 my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
\r
320 my $dbh = C4::Context->dbh;
\r
321 my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
\r
322 $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
\r
327 sub del_opac_electronic {
\r
330 my $dbh = C4::Context->dbh;
\r
331 my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
\r
340 sub get_opac_electronic {
\r
341 my ($idelectronic) = @_;
\r
342 my $dbh = C4::Context->dbh;
\r
343 my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
\r
344 $sth->execute($idelectronic);
\r
345 my $data = $sth->fetchrow_hashref;
\r
346 $data->{$data->{'lang'}} = 1;
\r
347 $data->{$data->{'section'}} = 1;
\r
352 sub get_opac_electronics {
\r
353 my ($section, $lang) = @_;
\r
354 my $dbh = C4::Context->dbh;
\r
355 my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";
\r
357 $query.= " WHERE lang = '" .$lang ."' ";
\r
360 $query.= " and section= '" . $section."' ";
\r
362 $query.= " ORDER BY title ";
\r
364 my $sth = $dbh->prepare($query);
\r
366 my @opac_electronic;
\r
368 while (my $row = $sth->fetchrow_hashref) {
\r
369 push @opac_electronic, $row;
\r
375 return ($count,\@opac_electronic);
\r
377 END { } # module clean-up code here (global destructor)
\r