File: | C4/NewsChannels.pm |
Coverage: | 23.8% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package C4::NewsChannels; | |||||
2 | ||||||
3 | # Copyright 2000-2002 Katipo Communications | |||||
4 | # | |||||
5 | # This file is part of Koha. | |||||
6 | # | |||||
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 | |||||
10 | # version. | |||||
11 | # | |||||
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. | |||||
15 | # | |||||
16 | # You should have received a copy of the GNU General Public License along | |||||
17 | # with Koha; if not, write to the Free Software Foundation, Inc., | |||||
18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |||||
19 | ||||||
20 | 4 4 4 | 49187 57 184 | use strict; | |||
21 | 4 4 4 | 62 45 168 | use warnings; | |||
22 | ||||||
23 | 4 4 4 | 396 55 110 | use C4::Context; | |||
24 | 4 4 4 | 343 55 227 | use C4::Dates qw(format_date); | |||
25 | ||||||
26 | 4 4 4 | 68 64 418 | use vars qw($VERSION @ISA @EXPORT); | |||
27 | ||||||
28 | BEGIN { | |||||
29 | 4 | 47 | $VERSION = 3.01; # set the version for version checking | |||
30 | 4 | 131 | @ISA = qw(Exporter); | |||
31 | 4 | 2758 | @EXPORT = qw( | |||
32 | &GetNewsToDisplay | |||||
33 | &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news | |||||
34 | ); | |||||
35 | } | |||||
36 | ||||||
37 - 47 | =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 | |||||
48 | ||||||
49 | sub add_opac_new { | |||||
50 | 0 | my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_; | ||||
51 | 0 | my $dbh = C4::Context->dbh; | ||||
52 | 0 | my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)"); | ||||
53 | 0 | $sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number); | ||||
54 | 0 | $sth->finish; | ||||
55 | 0 | return 1; | ||||
56 | } | |||||
57 | ||||||
58 | sub upd_opac_new { | |||||
59 | 0 | my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_; | ||||
60 | 0 | my $dbh = C4::Context->dbh; | ||||
61 | 0 | my $sth = $dbh->prepare(" | ||||
62 | UPDATE opac_news SET | |||||
63 | title = ?, | |||||
64 | new = ?, | |||||
65 | lang = ?, | |||||
66 | expirationdate = ?, | |||||
67 | timestamp = ?, | |||||
68 | number = ? | |||||
69 | WHERE idnew = ? | |||||
70 | "); | |||||
71 | 0 | $sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew); | ||||
72 | 0 | $sth->finish; | ||||
73 | 0 | return 1; | ||||
74 | } | |||||
75 | ||||||
76 | sub del_opac_new { | |||||
77 | 0 | my ($ids) = @_; | ||||
78 | 0 | if ($ids) { | ||||
79 | 0 | my $dbh = C4::Context->dbh; | ||||
80 | 0 | my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)"); | ||||
81 | 0 | $sth->execute(); | ||||
82 | 0 | $sth->finish; | ||||
83 | 0 | return 1; | ||||
84 | } else { | |||||
85 | 0 | return 0; | ||||
86 | } | |||||
87 | } | |||||
88 | ||||||
89 | sub get_opac_new { | |||||
90 | 0 | my ($idnew) = @_; | ||||
91 | 0 | my $dbh = C4::Context->dbh; | ||||
92 | 0 | my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?"); | ||||
93 | 0 | $sth->execute($idnew); | ||||
94 | 0 | my $data = $sth->fetchrow_hashref; | ||||
95 | 0 | $data->{$data->{'lang'}} = 1 if defined $data->{lang}; | ||||
96 | 0 | $data->{expirationdate} = format_date($data->{expirationdate}); | ||||
97 | 0 | $data->{timestamp} = format_date($data->{timestamp}); | ||||
98 | 0 | $sth->finish; | ||||
99 | 0 | return $data; | ||||
100 | } | |||||
101 | ||||||
102 | sub get_opac_news { | |||||
103 | 0 | my ($limit, $lang) = @_; | ||||
104 | 0 | my $dbh = C4::Context->dbh; | ||||
105 | 0 | my $query = "SELECT *, timestamp AS newdate FROM opac_news"; | ||||
106 | 0 | if ($lang) { | ||||
107 | 0 | $query.= " WHERE lang = '" .$lang ."' "; | ||||
108 | } | |||||
109 | 0 | $query.= " ORDER BY timestamp DESC "; | ||||
110 | #if ($limit) { | |||||
111 | # $query.= "LIMIT 0, " . $limit; | |||||
112 | #} | |||||
113 | 0 | my $sth = $dbh->prepare($query); | ||||
114 | 0 | $sth->execute(); | ||||
115 | 0 | my @opac_news; | ||||
116 | 0 | my $count = 0; | ||||
117 | 0 | while (my $row = $sth->fetchrow_hashref) { | ||||
118 | 0 | if ((($limit) && ($count < $limit)) || (!$limit)) { | ||||
119 | 0 | $row->{'newdate'} = format_date($row->{'newdate'}); | ||||
120 | 0 | $row->{'expirationdate'} = format_date($row->{'expirationdate'}); | ||||
121 | 0 | push @opac_news, $row; | ||||
122 | } | |||||
123 | 0 | $count++; | ||||
124 | } | |||||
125 | 0 | return ($count, \@opac_news); | ||||
126 | } | |||||
127 | ||||||
128 - 134 | =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 | |||||
135 | ||||||
136 | sub GetNewsToDisplay { | |||||
137 | 0 | my $lang = shift; | ||||
138 | 0 | my $dbh = C4::Context->dbh; | ||||
139 | # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate | |||||
140 | 0 | my $query = " | ||||
141 | SELECT *,timestamp AS newdate | |||||
142 | FROM opac_news | |||||
143 | WHERE ( | |||||
144 | expirationdate >= CURRENT_DATE() | |||||
145 | OR expirationdate IS NULL | |||||
146 | OR expirationdate = '00-00-0000' | |||||
147 | ) | |||||
148 | AND `timestamp` <= CURRENT_DATE() | |||||
149 | AND lang = ? | |||||
150 | ORDER BY number | |||||
151 | "; # expirationdate field is NOT in ISO format? | |||||
152 | 0 | my $sth = $dbh->prepare($query); | ||||
153 | 0 | $sth->execute($lang); | ||||
154 | 0 | my @results; | ||||
155 | 0 | while ( my $row = $sth->fetchrow_hashref ){ | ||||
156 | 0 | $row->{newdate} = format_date($row->{newdate}); | ||||
157 | 0 | push @results, $row; | ||||
158 | } | |||||
159 | 0 | return \@results; | ||||
160 | } | |||||
161 | ||||||
162 | 1; |