Bug 7567: (code cleanup) remove finish calls
[koha.git] / C4 / NewsChannels.pm
1 package C4::NewsChannels;
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2000-2002  Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use C4::Context;
24 use C4::Dates qw(format_date);
25
26 use vars qw($VERSION @ISA @EXPORT);
27
28 BEGIN { 
29     $VERSION = 3.07.00.049;     # set the version for version checking
30         @ISA = qw(Exporter);
31         @EXPORT = qw(
32                 &GetNewsToDisplay
33                 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
34         );
35 }
36
37 =head1 NAME
38
39 C4::NewsChannels - Functions to manage OPAC and intranet news
40
41 =head1 DESCRIPTION
42
43 This module provides the functions needed to mange OPAC and intranet news.
44
45 =head1 FUNCTIONS
46
47 =cut
48
49 sub add_opac_new {
50     my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_;
51     my $dbh = C4::Context->dbh;
52     my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)");
53     $sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number);
54     return 1;
55 }
56
57 sub upd_opac_new {
58     my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_;
59     my $dbh = C4::Context->dbh;
60     my $sth = $dbh->prepare("
61         UPDATE opac_news SET 
62             title = ?,
63             new = ?,
64             lang = ?,
65             expirationdate = ?,
66             timestamp = ?,
67             number = ?
68         WHERE idnew = ?
69     ");
70     $sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew);
71     return 1;
72 }
73
74 sub del_opac_new {
75     my ($ids) = @_;
76     if ($ids) {
77         my $dbh = C4::Context->dbh;
78         my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
79         $sth->execute();
80         return 1;
81     } else {
82         return 0;
83     }
84 }
85
86 sub get_opac_new {
87     my ($idnew) = @_;
88     my $dbh = C4::Context->dbh;
89     my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
90     $sth->execute($idnew);
91     my $data = $sth->fetchrow_hashref;
92     $data->{$data->{'lang'}} = 1 if defined $data->{lang};
93     $data->{expirationdate} = format_date($data->{expirationdate});
94     $data->{timestamp}      = format_date($data->{timestamp});
95     return $data;
96 }
97
98 sub get_opac_news {
99     my ($limit, $lang) = @_;
100     my $dbh = C4::Context->dbh;
101     my $query = "SELECT *, timestamp AS newdate FROM opac_news";
102     if ($lang) {
103         $query.= " WHERE lang = '" .$lang ."' ";
104     }
105     $query.= " ORDER BY timestamp DESC ";
106     #if ($limit) {
107     #    $query.= "LIMIT 0, " . $limit;
108     #}
109     my $sth = $dbh->prepare($query);
110     $sth->execute();
111     my @opac_news;
112     my $count = 0;
113     while (my $row = $sth->fetchrow_hashref) {
114         if ((($limit) && ($count < $limit)) || (!$limit)) {
115             push @opac_news, $row;
116         }
117         $count++;
118     }
119     return ($count, \@opac_news);
120 }
121
122 =head2 GetNewsToDisplay
123
124     $news = &GetNewsToDisplay($lang);
125     C<$news> is a ref to an array which containts
126     all news with expirationdate > today or expirationdate is null.
127
128 =cut
129
130 sub GetNewsToDisplay {
131     my $lang = shift;
132     my $dbh = C4::Context->dbh;
133     # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
134     my $query = "
135      SELECT *,timestamp AS newdate
136      FROM   opac_news
137      WHERE   (
138         expirationdate >= CURRENT_DATE()
139         OR    expirationdate IS NULL
140         OR    expirationdate = '00-00-0000'
141       )
142       AND   `timestamp` <= CURRENT_DATE()
143       AND   lang = ?
144       ORDER BY number
145     ";                          # expirationdate field is NOT in ISO format?
146     my $sth = $dbh->prepare($query);
147     $sth->execute($lang);
148     my @results;
149     while ( my $row = $sth->fetchrow_hashref ){
150                 $row->{newdate} = format_date($row->{newdate});
151         push @results, $row;
152     }
153     return \@results;
154 }
155
156 1;
157 __END__
158
159 =head1 AUTHOR
160
161 TG
162
163 =cut