Koha/C4/NewsChannels.pm
Mark Tompsett d3eaa62717 Bug 7567: (code cleanup) remove finish calls
"When all the data has been fetched from a SELECT statement,
the driver will automatically call finish for you. So you should
not call it explicitly except when you know that you've not
fetched all the data from a statement handle and the handle
won't be destroyed soon."
(http://search.cpan.org/~timb/DBI-1.627/DBI.pm#finish)

All the $sth variables were scoped within the functions,
and would be destroyed immediately. Additionally, there was
one after a SELECT, for only a single idnew, and so it was
not necessary.

TEST PLAN
---------
1) prove -v t/db_dependent/NewsChannels.t
2) apply patch
3) prove -v t/db_dependent/NewsChannels.t

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>

ok 1 - use C4::NewsChannels;
ok 2 - Successfully added the first dummy news item!
ok 3 - Successfully added the second dummy news item!
ok 4 - Successfully updated second dummy news item!
ok 5 - Successfully tested get_opac_new id1!
ok 6 - Successfully tested get_opac_new id2!
ok 7 - Successfully tested get_opac_news!
ok 8 - Successfully tested GetNewsToDisplay!

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
2014-04-07 18:05:41 +00:00

163 lines
4.3 KiB
Perl

package C4::NewsChannels;
# This file is part of Koha.
#
# Copyright (C) 2000-2002 Katipo Communications
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Context;
use C4::Dates qw(format_date);
use vars qw($VERSION @ISA @EXPORT);
BEGIN {
$VERSION = 3.07.00.049; # set the version for version checking
@ISA = qw(Exporter);
@EXPORT = qw(
&GetNewsToDisplay
&add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
);
}
=head1 NAME
C4::NewsChannels - Functions to manage OPAC and intranet news
=head1 DESCRIPTION
This module provides the functions needed to mange OPAC and intranet news.
=head1 FUNCTIONS
=cut
sub add_opac_new {
my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)");
$sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number);
return 1;
}
sub upd_opac_new {
my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("
UPDATE opac_news SET
title = ?,
new = ?,
lang = ?,
expirationdate = ?,
timestamp = ?,
number = ?
WHERE idnew = ?
");
$sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew);
return 1;
}
sub del_opac_new {
my ($ids) = @_;
if ($ids) {
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
$sth->execute();
return 1;
} else {
return 0;
}
}
sub get_opac_new {
my ($idnew) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
$sth->execute($idnew);
my $data = $sth->fetchrow_hashref;
$data->{$data->{'lang'}} = 1 if defined $data->{lang};
$data->{expirationdate} = format_date($data->{expirationdate});
$data->{timestamp} = format_date($data->{timestamp});
return $data;
}
sub get_opac_news {
my ($limit, $lang) = @_;
my $dbh = C4::Context->dbh;
my $query = "SELECT *, timestamp AS newdate FROM opac_news";
if ($lang) {
$query.= " WHERE lang = '" .$lang ."' ";
}
$query.= " ORDER BY timestamp DESC ";
#if ($limit) {
# $query.= "LIMIT 0, " . $limit;
#}
my $sth = $dbh->prepare($query);
$sth->execute();
my @opac_news;
my $count = 0;
while (my $row = $sth->fetchrow_hashref) {
if ((($limit) && ($count < $limit)) || (!$limit)) {
push @opac_news, $row;
}
$count++;
}
return ($count, \@opac_news);
}
=head2 GetNewsToDisplay
$news = &GetNewsToDisplay($lang);
C<$news> is a ref to an array which containts
all news with expirationdate > today or expirationdate is null.
=cut
sub GetNewsToDisplay {
my $lang = shift;
my $dbh = C4::Context->dbh;
# SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
my $query = "
SELECT *,timestamp AS newdate
FROM opac_news
WHERE (
expirationdate >= CURRENT_DATE()
OR expirationdate IS NULL
OR expirationdate = '00-00-0000'
)
AND `timestamp` <= CURRENT_DATE()
AND lang = ?
ORDER BY number
"; # expirationdate field is NOT in ISO format?
my $sth = $dbh->prepare($query);
$sth->execute($lang);
my @results;
while ( my $row = $sth->fetchrow_hashref ){
$row->{newdate} = format_date($row->{newdate});
push @results, $row;
}
return \@results;
}
1;
__END__
=head1 AUTHOR
TG
=cut