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
23 use C4::Dates qw(DHTMLcalendar format_date);
\r
25 use vars qw($VERSION @ISA @EXPORT);
\r
27 # set the version for version checking
\r
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
\r
29 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
\r
34 C4::NewsChannels - Functions to manage the news channels and its categories
\r
38 This module provides the functions needed to admin the news channels and its categories
\r
47 @ISA = qw(Exporter);
\r
50 &news_channels &get_new_channel &del_channels &add_channel &update_channel
\r
51 &news_channels_categories &get_new_channel_category &del_channels_categories
\r
52 &add_channel_category &update_channel_category &news_channels_by_category
\r
53 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
\r
54 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
\r
60 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
\r
62 Looks up news channels by name or category.
\r
64 C<$channel_name> is the channel name to search.
\r
66 C<$id_category> is the channel category code to search.
\r
68 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
\r
70 if none of the params are set C<&news_channels> returns all the news channels.
\r
72 C<&news_channels> returns two values: an integer giving the number of
\r
73 news channels found and a reference to an array
\r
74 of references to hash, which has the news_channels and news_channels_categories fields.
\r
79 my ($channel_name, $id_category, $unclassified) = @_;
\r
80 my $dbh = C4::Context->dbh;
\r
82 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
\r
83 if ( ($channel_name ne '') && ($id_category ne '') ) {
\r
84 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
\r
85 } elsif ($channel_name ne '') {
\r
86 $query.= " WHERE channel_name like '" . $channel_name . "%'";
\r
87 } elsif ($id_category ne '') {
\r
88 $query.= " WHERE news_channels.id_category = " . $id_category;
\r
89 } elsif ($unclassified) {
\r
90 $query.= " WHERE news_channels.id_category IS NULL ";
\r
92 my $sth = $dbh->prepare($query);
\r
94 while (my $row = $sth->fetchrow_hashref) {
\r
95 push @channels, $row;
\r
98 return (scalar(@channels), @channels);
\r
101 =item news_channels_by_category
\r
103 ($count, @results) = &news_channels_by_category();
\r
105 Looks up news channels grouped by category.
\r
107 C<&news_channels_by_category> returns two values: an integer giving the number of
\r
108 categories found and a reference to an array
\r
109 of references to hash, which the following keys:
\r
113 =item C<channels_count>
\r
115 The number of news channels in that category
\r
119 A reference to an array of references to hash which keys are the new_channels fields.
\r
121 Additionally the last index of results has a reference to all the news channels which don't have a category
\r
125 sub news_channels_by_category {
\r
127 my ($categories_count, @results) = &news_channels_categories();
\r
128 foreach my $row (@results) {
\r
130 my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});
\r
131 $row->{'channels_count'} = $channels_count;
\r
132 $row->{'channels'} = \@channels;
\r
135 my ($channels_count, @channels) = &news_channels('', '', 1);
\r
137 $row{'id_category'} = -1;
\r
138 $row{'unclassified'} = 1;
\r
139 $row{'channels_count'} = $channels_count;
\r
140 $row{'channels'} = \@channels;
\r
141 push @results, \%row;
\r
143 return (scalar(@results), @results);
\r
146 sub get_new_channel {
\r
148 my $dbh = C4::Context->dbh;
\r
149 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
\r
150 $sth->execute($id);
\r
151 my $channel = $sth->fetchrow_hashref;
\r
159 my $dbh = C4::Context->dbh;
\r
160 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
\r
169 my ($name, $url, $id_category, $notes) = @_;
\r
170 my $dbh = C4::Context->dbh;
\r
171 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
\r
172 $sth->execute($name, $url, $id_category, $notes);
\r
177 sub update_channel {
\r
178 my ($id, $name, $url, $id_category, $notes) = @_;
\r
179 my $dbh = C4::Context->dbh;
\r
180 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
\r
181 $sth->execute($name, $url, $id_category, $notes, $id);
\r
186 sub news_channels_categories {
\r
187 my $dbh = C4::Context->dbh;
\r
189 my $query = "SELECT * FROM news_channels_categories";
\r
190 my $sth = $dbh->prepare($query);
\r
192 while (my $row = $sth->fetchrow_hashref) {
\r
193 push @categories, $row;
\r
196 return (scalar(@categories), @categories);
\r
200 sub get_new_channel_category {
\r
202 my $dbh = C4::Context->dbh;
\r
203 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
\r
204 $sth->execute($id);
\r
205 my $category = $sth->fetchrow_hashref;
\r
210 sub del_channels_categories {
\r
213 my $dbh = C4::Context->dbh;
\r
214 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
\r
216 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
\r
224 sub add_channel_category {
\r
226 my $dbh = C4::Context->dbh;
\r
227 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
\r
228 $sth->execute($name);
\r
233 sub update_channel_category {
\r
234 my ($id, $name) = @_;
\r
235 my $dbh = C4::Context->dbh;
\r
236 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
\r
237 $sth->execute($name, $id);
\r
243 my ($title, $new, $lang, $expirationdate, $number) = @_;
\r
244 my $dbh = C4::Context->dbh;
\r
245 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, number) VALUES (?,?,?,?,?)");
\r
246 $sth->execute($title, $new, $lang, $expirationdate, $number);
\r
252 my ($idnew, $title, $new, $lang, $expirationdate, $number) = @_;
\r
253 my $dbh = C4::Context->dbh;
\r
254 my $sth = $dbh->prepare("
\r
255 UPDATE opac_news SET
\r
259 expirationdate = ?,
\r
263 $sth->execute($title, $new, $lang, $expirationdate,$number,$idnew);
\r
271 my $dbh = C4::Context->dbh;
\r
272 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
\r
283 my $dbh = C4::Context->dbh;
\r
284 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
\r
285 $sth->execute($idnew);
\r
286 my $data = $sth->fetchrow_hashref;
\r
287 $data->{$data->{'lang'}} = 1;
\r
288 $data->{expirationdate} = format_date($data->{expirationdate});
\r
293 sub get_opac_news {
\r
294 my ($limit, $lang) = @_;
\r
295 my $dbh = C4::Context->dbh;
\r
296 my $query = "SELECT *, timestamp AS newdate FROM opac_news";
\r
298 $query.= " WHERE lang = '" .$lang ."' ";
\r
300 $query.= " ORDER BY timestamp DESC ";
\r
302 # $query.= "LIMIT 0, " . $limit;
\r
304 my $sth = $dbh->prepare($query);
\r
308 while (my $row = $sth->fetchrow_hashref) {
\r
309 if ((($limit) && ($count < $limit)) || (!$limit)) {
\r
310 # format the dates in the user's requested format defined in sysprefs
\r
311 $row->{'newdate'} = format_date($row->{'newdate'});
\r
312 $row->{'expirationdate'} = format_date($row->{'expirationdate'});
\r
313 push @opac_news, $row;
\r
317 return ($count, \@opac_news);
\r
320 =head2 GetNewsToDisplay
\r
322 $news = &GetNewsToDisplay($lang);
\r
323 C<$news> is a ref to an array which containts
\r
324 all news with expirationdate > today or expirationdate is null.
\r
328 sub GetNewsToDisplay {
\r
330 my $dbh = C4::Context->dbh;
\r
331 my $dateformat = C4::Dates->DHTMLcalendar;
\r
333 SELECT *,DATE_FORMAT(timestamp, '$dateformat') AS newdate
\r
336 expirationdate > CURRENT_DATE()
\r
337 OR expirationdate IS NULL
\r
338 OR expirationdate = '00-00-0000'
\r
343 my $sth = $dbh->prepare($query);
\r
344 $sth->execute($lang);
\r
346 while ( my $row = $sth->fetchrow_hashref ){
\r
347 push @results, $row;
\r
352 ### get electronic databases
\r
354 sub add_opac_electronic {
\r
355 my ($title, $edata, $lang,$image,$href,$section) = @_;
\r
356 my $dbh = C4::Context->dbh;
\r
357 my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
\r
358 $sth->execute($title, $edata, $lang,$image,$href,$section);
\r
363 sub upd_opac_electronic {
\r
364 my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
\r
365 my $dbh = C4::Context->dbh;
\r
366 my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
\r
367 $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
\r
372 sub del_opac_electronic {
\r
375 my $dbh = C4::Context->dbh;
\r
376 my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
\r
385 sub get_opac_electronic {
\r
386 my ($idelectronic) = @_;
\r
387 my $dbh = C4::Context->dbh;
\r
388 my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
\r
389 $sth->execute($idelectronic);
\r
390 my $data = $sth->fetchrow_hashref;
\r
391 $data->{$data->{'lang'}} = 1;
\r
392 $data->{$data->{'section'}} = 1;
\r
397 sub get_opac_electronics {
\r
398 my ($section, $lang) = @_;
\r
399 my $dbh = C4::Context->dbh;
\r
400 my $dateformat = C4::Dates->DHTMLcalendar;
\r
401 my $query = "SELECT *, DATE_FORMAT(timestamp, '$dateformat') AS newdate FROM opac_electronic";
\r
403 $query.= " WHERE lang = '" .$lang ."' ";
\r
406 $query.= " and section= '" . $section."' ";
\r
408 $query.= " ORDER BY title ";
\r
410 my $sth = $dbh->prepare($query);
\r
412 my @opac_electronic;
\r
414 while (my $row = $sth->fetchrow_hashref) {
\r
415 push @opac_electronic, $row;
\r
421 return ($count,\@opac_electronic);
\r
423 END { } # module clean-up code here (global destructor)
\r