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.01; # set the version for version checking
32 &news_channels &get_new_channel &del_channels &add_channel &update_channel
33 &news_channels_categories &get_new_channel_category &del_channels_categories
34 &add_channel_category &update_channel_category &news_channels_by_category
35 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
36 &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics
42 C4::NewsChannels - Functions to manage the news channels and its categories
46 This module provides the functions needed to admin the news channels and its categories
54 ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);
56 Looks up news channels by name or category.
58 C<$channel_name> is the channel name to search.
60 C<$id_category> is the channel category code to search.
62 C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category
64 if none of the params are set C<&news_channels> returns all the news channels.
66 C<&news_channels> returns two values: an integer giving the number of
67 news channels found and a reference to an array
68 of references to hash, which has the news_channels and news_channels_categories fields.
73 my ($channel_name, $id_category, $unclassified) = @_;
74 my $dbh = C4
::Context
->dbh;
76 my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";
77 if ( ($channel_name ne '') && ($id_category ne '') ) {
78 $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;
79 } elsif ($channel_name ne '') {
80 $query.= " WHERE channel_name like '" . $channel_name . "%'";
81 } elsif ($id_category ne '') {
82 $query.= " WHERE news_channels.id_category = " . $id_category;
83 } elsif ($unclassified) {
84 $query.= " WHERE news_channels.id_category IS NULL ";
86 my $sth = $dbh->prepare($query);
88 while (my $row = $sth->fetchrow_hashref) {
92 return (scalar(@channels), @channels);
95 =item news_channels_by_category
97 ($count, @results) = &news_channels_by_category();
99 Looks up news channels grouped by category.
101 C<&news_channels_by_category> returns two values: an integer giving the number of
102 categories found and a reference to an array
103 of references to hash, which the following keys:
107 =item C<channels_count>
109 The number of news channels in that category
113 A reference to an array of references to hash which keys are the new_channels fields.
115 Additionally the last index of results has a reference to all the news channels which don't have a category
119 sub news_channels_by_category
{
121 my ($categories_count, @results) = &news_channels_categories
();
122 foreach my $row (@results) {
124 my ($channels_count, @channels) = &news_channels
('', $row->{'id_category'});
125 $row->{'channels_count'} = $channels_count;
126 $row->{'channels'} = \
@channels;
129 my ($channels_count, @channels) = &news_channels
('', '', 1);
131 $row{'id_category'} = -1;
132 $row{'unclassified'} = 1;
133 $row{'channels_count'} = $channels_count;
134 $row{'channels'} = \
@channels;
135 push @results, \
%row;
137 return (scalar(@results), @results);
140 sub get_new_channel
{
142 my $dbh = C4
::Context
->dbh;
143 my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");
145 my $channel = $sth->fetchrow_hashref;
153 my $dbh = C4
::Context
->dbh;
154 my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");
163 my ($name, $url, $id_category, $notes) = @_;
164 my $dbh = C4
::Context
->dbh;
165 my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");
166 $sth->execute($name, $url, $id_category, $notes);
172 my ($id, $name, $url, $id_category, $notes) = @_;
173 my $dbh = C4
::Context
->dbh;
174 my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?, url = ?, id_category = ?, notes = ? WHERE id = ?");
175 $sth->execute($name, $url, $id_category, $notes, $id);
180 sub news_channels_categories
{
181 my $dbh = C4
::Context
->dbh;
183 my $query = "SELECT * FROM news_channels_categories";
184 my $sth = $dbh->prepare($query);
186 while (my $row = $sth->fetchrow_hashref) {
187 push @categories, $row;
190 return (scalar(@categories), @categories);
194 sub get_new_channel_category
{
196 my $dbh = C4
::Context
->dbh;
197 my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");
199 my $category = $sth->fetchrow_hashref;
204 sub del_channels_categories
{
207 my $dbh = C4
::Context
->dbh;
208 my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) ");
210 $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");
218 sub add_channel_category
{
220 my $dbh = C4
::Context
->dbh;
221 my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");
222 $sth->execute($name);
227 sub update_channel_category
{
228 my ($id, $name) = @_;
229 my $dbh = C4
::Context
->dbh;
230 my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");
231 $sth->execute($name, $id);
237 my ($title, $new, $lang, $expirationdate, $number) = @_;
238 my $dbh = C4
::Context
->dbh;
239 my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, number) VALUES (?,?,?,?,?)");
240 $sth->execute($title, $new, $lang, $expirationdate, $number);
246 my ($idnew, $title, $new, $lang, $expirationdate, $number) = @_;
247 my $dbh = C4
::Context
->dbh;
248 my $sth = $dbh->prepare("
257 $sth->execute($title, $new, $lang, $expirationdate,$number,$idnew);
265 my $dbh = C4
::Context
->dbh;
266 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
277 my $dbh = C4
::Context
->dbh;
278 my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
279 $sth->execute($idnew);
280 my $data = $sth->fetchrow_hashref;
281 $data->{$data->{'lang'}} = 1;
282 $data->{expirationdate
} = format_date
($data->{expirationdate
});
288 my ($limit, $lang) = @_;
289 my $dbh = C4
::Context
->dbh;
290 my $query = "SELECT *, timestamp AS newdate FROM opac_news";
292 $query.= " WHERE lang = '" .$lang ."' ";
294 $query.= " ORDER BY timestamp DESC ";
296 # $query.= "LIMIT 0, " . $limit;
298 my $sth = $dbh->prepare($query);
302 while (my $row = $sth->fetchrow_hashref) {
303 if ((($limit) && ($count < $limit)) || (!$limit)) {
304 $row->{'newdate'} = format_date
($row->{'newdate'});
305 $row->{'expirationdate'} = format_date
($row->{'expirationdate'});
306 push @opac_news, $row;
310 return ($count, \
@opac_news);
313 =head2 GetNewsToDisplay
315 $news = &GetNewsToDisplay($lang);
316 C<$news> is a ref to an array which containts
317 all news with expirationdate > today or expirationdate is null.
321 sub GetNewsToDisplay
{
323 my $dbh = C4
::Context
->dbh;
324 # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
326 SELECT *,timestamp AS newdate
329 expirationdate > CURRENT_DATE()
330 OR expirationdate IS NULL
331 OR expirationdate = '00-00-0000'
335 "; # expirationdate field is NOT in ISO format?
336 my $sth = $dbh->prepare($query);
337 $sth->execute($lang);
339 while ( my $row = $sth->fetchrow_hashref ){
340 $row->{newdate
} = format_date
($row->{newdate
});
346 ### get electronic databases
348 sub add_opac_electronic
{
349 my ($title, $edata, $lang,$image,$href,$section) = @_;
350 my $dbh = C4
::Context
->dbh;
351 my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");
352 $sth->execute($title, $edata, $lang,$image,$href,$section);
357 sub upd_opac_electronic
{
358 my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;
359 my $dbh = C4
::Context
->dbh;
360 my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");
361 $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);
366 sub del_opac_electronic
{
369 my $dbh = C4
::Context
->dbh;
370 my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");
379 sub get_opac_electronic
{
380 my ($idelectronic) = @_;
381 my $dbh = C4
::Context
->dbh;
382 my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");
383 $sth->execute($idelectronic);
384 my $data = $sth->fetchrow_hashref;
385 $data->{$data->{'lang'}} = 1;
386 $data->{$data->{'section'}} = 1;
391 sub get_opac_electronics
{
392 my ($section, $lang) = @_;
393 my $dbh = C4
::Context
->dbh;
394 my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";
396 $query.= " WHERE lang = '" .$lang ."' ";
399 $query.= " and section= '" . $section."' ";
401 $query.= " ORDER BY title ";
403 my $sth = $dbh->prepare($query);
407 while (my $row = $sth->fetchrow_hashref) {
408 push @opac_electronic, $row;
412 return ($count,\
@opac_electronic);