Lines 1-37
Link Here
|
1 |
package C4::NewsChannels; |
1 |
package C4::NewsChannels; |
2 |
|
2 |
|
3 |
# Copyright 2000-2002 Katipo Communications |
|
|
4 |
# |
5 |
# This file is part of Koha. |
3 |
# This file is part of Koha. |
6 |
# |
4 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
5 |
# Copyright (C) 2000 Katipo Communications |
8 |
# terms of the GNU General Public License as published by the Free Software |
6 |
# Copyright (C) 2013 Mark Tompsett |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
7 |
# |
10 |
# version. |
8 |
# Koha is free software; you can redistribute it and/or modify it |
|
|
9 |
# under the terms of the GNU General Public License as published by |
10 |
# the Free Software Foundation; either version 3 of the License, or |
11 |
# (at your option) any later version. |
11 |
# |
12 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
16 |
# GNU General Public License for more details. |
15 |
# |
17 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
18 |
# You should have received a copy of the GNU General Public License |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
19 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
|
19 |
|
20 |
|
20 |
use strict; |
|
|
21 |
use warnings; |
22 |
|
21 |
|
|
|
22 |
use Modern::Perl; |
23 |
use C4::Context; |
23 |
use C4::Context; |
24 |
use C4::Dates qw(format_date); |
24 |
use C4::Dates qw(format_date); |
25 |
|
25 |
|
26 |
use vars qw($VERSION @ISA @EXPORT); |
26 |
use vars qw($VERSION @ISA @EXPORT); |
27 |
|
27 |
|
28 |
BEGIN { |
28 |
BEGIN { |
29 |
$VERSION = 3.07.00.049; # set the version for version checking |
29 |
$VERSION = 3.07.00.049; # set the version for version checking |
30 |
@ISA = qw(Exporter); |
30 |
@ISA = qw(Exporter); |
31 |
@EXPORT = qw( |
31 |
@EXPORT = qw( |
32 |
&GetNewsToDisplay |
32 |
&add_opac_new |
33 |
&add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news |
33 |
&upd_opac_new |
34 |
); |
34 |
&del_opac_new |
|
|
35 |
&get_opac_new |
36 |
&get_opac_news |
37 |
&GetNewsToDisplay |
38 |
); |
35 |
} |
39 |
} |
36 |
|
40 |
|
37 |
=head1 NAME |
41 |
=head1 NAME |
Lines 47-76
This module provides the functions needed to mange OPAC and intranet news.
Link Here
|
47 |
=cut |
51 |
=cut |
48 |
|
52 |
|
49 |
sub add_opac_new { |
53 |
sub add_opac_new { |
50 |
my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_; |
54 |
my ($href_entry) = @_; |
51 |
my $dbh = C4::Context->dbh; |
55 |
my $retval=0; |
52 |
my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)"); |
56 |
|
53 |
$sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number); |
57 |
if ($href_entry) { |
54 |
$sth->finish; |
58 |
my @fields = keys %{$href_entry}; |
55 |
return 1; |
59 |
my @values = values %{$href_entry}; |
|
|
60 |
my $field_string = join ",",@fields; |
61 |
$field_string = $field_string // q{}; |
62 |
my $values_string = "?," x ($#fields) . "?"; |
63 |
|
64 |
my $dbh = C4::Context->dbh; |
65 |
my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string );"); |
66 |
$sth->execute(@values); |
67 |
$retval = 1; |
68 |
} |
69 |
return $retval; |
56 |
} |
70 |
} |
57 |
|
71 |
|
58 |
sub upd_opac_new { |
72 |
sub upd_opac_new { |
59 |
my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_; |
73 |
my ($href_entry) = @_; |
60 |
my $dbh = C4::Context->dbh; |
74 |
my $retval = 0; |
61 |
my $sth = $dbh->prepare(" |
75 |
|
62 |
UPDATE opac_news SET |
76 |
if ($href_entry) { |
63 |
title = ?, |
77 |
# take the keys of hash entry and make a list, but... |
64 |
new = ?, |
78 |
my @fields = keys %{$href_entry}; |
65 |
lang = ?, |
79 |
my @values; |
66 |
expirationdate = ?, |
80 |
$#values = -1; |
67 |
timestamp = ?, |
81 |
my $field_string = q{}; |
68 |
number = ? |
82 |
foreach my $field_name (@fields) { |
69 |
WHERE idnew = ? |
83 |
# exclude idnew |
70 |
"); |
84 |
if ( $field_name ne 'idnew' ) { |
71 |
$sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew); |
85 |
$field_string = $field_string . "$field_name = ?,"; |
72 |
$sth->finish; |
86 |
push @values,$href_entry->{$field_name}; |
73 |
return 1; |
87 |
} |
|
|
88 |
} |
89 |
# put idnew at the end, so we know which record to update |
90 |
push @values,$href_entry->{'idnew'}; |
91 |
chop $field_string; # remove that excess , |
92 |
|
93 |
my $dbh = C4::Context->dbh; |
94 |
my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;"); |
95 |
$sth->execute(@values); |
96 |
$retval = 1; |
97 |
} |
98 |
return $retval; |
74 |
} |
99 |
} |
75 |
|
100 |
|
76 |
sub del_opac_new { |
101 |
sub del_opac_new { |
Lines 79-85
sub del_opac_new {
Link Here
|
79 |
my $dbh = C4::Context->dbh; |
104 |
my $dbh = C4::Context->dbh; |
80 |
my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)"); |
105 |
my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)"); |
81 |
$sth->execute(); |
106 |
$sth->execute(); |
82 |
$sth->finish; |
|
|
83 |
return 1; |
107 |
return 1; |
84 |
} else { |
108 |
} else { |
85 |
return 0; |
109 |
return 0; |
Lines 89-123
sub del_opac_new {
Link Here
|
89 |
sub get_opac_new { |
113 |
sub get_opac_new { |
90 |
my ($idnew) = @_; |
114 |
my ($idnew) = @_; |
91 |
my $dbh = C4::Context->dbh; |
115 |
my $dbh = C4::Context->dbh; |
92 |
my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?"); |
116 |
my $query = q{ |
|
|
117 |
SELECT opac_news.*,branches.branchname,timestamp AS newdate |
118 |
FROM opac_news LEFT JOIN branches ON opac_news.branchcode=branches.branchcode |
119 |
WHERE opac_news.idnew = ?; |
120 |
}; |
121 |
my $sth = $dbh->prepare($query); |
93 |
$sth->execute($idnew); |
122 |
$sth->execute($idnew); |
94 |
my $data = $sth->fetchrow_hashref; |
123 |
my $data = $sth->fetchrow_hashref; |
95 |
$data->{$data->{'lang'}} = 1 if defined $data->{lang}; |
124 |
$data->{$data->{'lang'}} = 1 if defined $data->{lang}; |
96 |
$data->{expirationdate} = format_date($data->{expirationdate}); |
125 |
$data->{expirationdate} = format_date($data->{expirationdate}); |
97 |
$data->{timestamp} = format_date($data->{timestamp}); |
126 |
$data->{timestamp} = format_date($data->{timestamp}); |
98 |
$sth->finish; |
127 |
if (!defined($data->{branchcode}) || $data->{branchcode} eq '') { |
|
|
128 |
$data->{branchname} = "All Branches"; |
129 |
} |
99 |
return $data; |
130 |
return $data; |
100 |
} |
131 |
} |
101 |
|
132 |
|
102 |
sub get_opac_news { |
133 |
sub get_opac_news { |
103 |
my ($limit, $lang) = @_; |
134 |
my ($limit, $lang, $branchcode) = @_; |
|
|
135 |
my @values; |
104 |
my $dbh = C4::Context->dbh; |
136 |
my $dbh = C4::Context->dbh; |
105 |
my $query = "SELECT *, timestamp AS newdate FROM opac_news"; |
137 |
my $query = q{ |
106 |
if ($lang) { |
138 |
SELECT opac_news.*,branches.branchname,timestamp AS newdate |
107 |
$query.= " WHERE lang = '" .$lang ."' "; |
139 |
FROM opac_news LEFT JOIN branches ON opac_news.branchcode=branches.branchcode |
|
|
140 |
}; |
141 |
if ($lang && $branchcode) { |
142 |
$query.= " WHERE (opac_news.lang='' OR opac_news.lang=?)"; |
143 |
$query .= " AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)"; |
144 |
push @values,$lang; |
145 |
push @values,$branchcode; |
146 |
} |
147 |
elsif ($lang) { |
148 |
$query.= " WHERE (opac_news.lang='' OR opac_news.lang=?)"; |
149 |
push @values,$lang; |
150 |
} |
151 |
elsif ($branchcode) { |
152 |
$query .= " WHERE (opac_news.branchcode IS NULL OR opac_news.branchcode=?)"; |
153 |
push @values,$branchcode; |
154 |
} |
155 |
else { |
156 |
# nothing |
108 |
} |
157 |
} |
109 |
$query.= " ORDER BY timestamp DESC "; |
158 |
$query.= " ORDER BY timestamp DESC "; |
110 |
#if ($limit) { |
159 |
#if ($limit) { |
111 |
# $query.= "LIMIT 0, " . $limit; |
160 |
# $query.= "LIMIT 0, " . $limit; |
112 |
#} |
161 |
#} |
113 |
my $sth = $dbh->prepare($query); |
162 |
my $sth = $dbh->prepare($query); |
114 |
$sth->execute(); |
163 |
$sth->execute(@values); |
115 |
my @opac_news; |
164 |
my @opac_news; |
116 |
my $count = 0; |
165 |
my $count = 0; |
117 |
while (my $row = $sth->fetchrow_hashref) { |
166 |
while (my $row = $sth->fetchrow_hashref) { |
118 |
if ((($limit) && ($count < $limit)) || (!$limit)) { |
167 |
if ((($limit) && ($count < $limit)) || (!$limit)) { |
119 |
$row->{'newdate'} = format_date($row->{'newdate'}); |
168 |
$row->{'newdate'} = format_date($row->{'newdate'}); |
120 |
$row->{'expirationdate'} = format_date($row->{'expirationdate'}); |
169 |
$row->{'expirationdate'} = format_date($row->{'expirationdate'}); |
|
|
170 |
$row->{'branchname'} = "All Branches" if !$row->{'branchcode'}; |
121 |
push @opac_news, $row; |
171 |
push @opac_news, $row; |
122 |
} |
172 |
} |
123 |
$count++; |
173 |
$count++; |
Lines 127-143
sub get_opac_news {
Link Here
|
127 |
|
177 |
|
128 |
=head2 GetNewsToDisplay |
178 |
=head2 GetNewsToDisplay |
129 |
|
179 |
|
130 |
$news = &GetNewsToDisplay($lang); |
180 |
$news = &GetNewsToDisplay($lang,$branch); |
131 |
C<$news> is a ref to an array which containts |
181 |
C<$news> is a ref to an array which containts |
132 |
all news with expirationdate > today or expirationdate is null. |
182 |
all news with expirationdate > today or expirationdate is null |
|
|
183 |
that is applicable for a given branch. |
133 |
|
184 |
|
134 |
=cut |
185 |
=cut |
135 |
|
186 |
|
136 |
sub GetNewsToDisplay { |
187 |
sub GetNewsToDisplay { |
137 |
my $lang = shift; |
188 |
my ($lang,$branch) = @_; |
138 |
my $dbh = C4::Context->dbh; |
189 |
my $dbh = C4::Context->dbh; |
139 |
# SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate |
190 |
# SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate |
140 |
my $query = " |
191 |
my $query = q{ |
141 |
SELECT *,timestamp AS newdate |
192 |
SELECT *,timestamp AS newdate |
142 |
FROM opac_news |
193 |
FROM opac_news |
143 |
WHERE ( |
194 |
WHERE ( |
Lines 145-159
sub GetNewsToDisplay {
Link Here
|
145 |
OR expirationdate IS NULL |
196 |
OR expirationdate IS NULL |
146 |
OR expirationdate = '00-00-0000' |
197 |
OR expirationdate = '00-00-0000' |
147 |
) |
198 |
) |
148 |
AND `timestamp` <= CURRENT_DATE() |
199 |
AND `timestamp` < CURRENT_DATE()+1 |
149 |
AND lang = ? |
200 |
AND (lang = '' OR lang = ?) |
|
|
201 |
AND (branchcode IS NULL OR branchcode = ?) |
150 |
ORDER BY number |
202 |
ORDER BY number |
151 |
"; # expirationdate field is NOT in ISO format? |
203 |
}; # expirationdate field is NOT in ISO format? |
|
|
204 |
# timestamp has HH:mm:ss, CURRENT_DATE() generates 00:00:00 |
205 |
# by adding 1, that captures today correctly. |
152 |
my $sth = $dbh->prepare($query); |
206 |
my $sth = $dbh->prepare($query); |
153 |
$sth->execute($lang); |
207 |
$lang = $lang // ''; |
|
|
208 |
$branch = $branch // ''; |
209 |
$sth->execute($lang,$branch); |
154 |
my @results; |
210 |
my @results; |
155 |
while ( my $row = $sth->fetchrow_hashref ){ |
211 |
while ( my $row = $sth->fetchrow_hashref ){ |
156 |
$row->{newdate} = format_date($row->{newdate}); |
212 |
$row->{newdate} = format_date($row->{newdate}); |
157 |
push @results, $row; |
213 |
push @results, $row; |
158 |
} |
214 |
} |
159 |
return \@results; |
215 |
return \@results; |