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-117
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; |
|
|
99 |
return $data; |
127 |
return $data; |
100 |
} |
128 |
} |
101 |
|
129 |
|
102 |
sub get_opac_news { |
130 |
sub get_opac_news { |
103 |
my ($limit, $lang) = @_; |
131 |
my ($limit, $lang, $branchcode) = @_; |
|
|
132 |
my @values; |
104 |
my $dbh = C4::Context->dbh; |
133 |
my $dbh = C4::Context->dbh; |
105 |
my $query = "SELECT *, timestamp AS newdate FROM opac_news"; |
134 |
my $query = q{ |
106 |
if ($lang) { |
135 |
SELECT opac_news.*,branches.branchname,timestamp AS newdate |
107 |
$query.= " WHERE lang = '" .$lang ."' "; |
136 |
FROM opac_news LEFT JOIN branches ON opac_news.branchcode=branches.branchcode |
|
|
137 |
}; |
138 |
if ($lang && $branchcode) { |
139 |
$query.= " WHERE (opac_news.lang='' OR opac_news.lang=?)"; |
140 |
$query .= " AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)"; |
141 |
push @values,$lang; |
142 |
push @values,$branchcode; |
143 |
} |
144 |
elsif ($lang) { |
145 |
$query.= " WHERE (opac_news.lang='' OR opac_news.lang=?)"; |
146 |
push @values,$lang; |
147 |
} |
148 |
elsif ($branchcode) { |
149 |
$query .= " WHERE (opac_news.branchcode IS NULL OR opac_news.branchcode=?)"; |
150 |
push @values,$branchcode; |
151 |
} |
152 |
else { |
153 |
# nothing |
108 |
} |
154 |
} |
109 |
$query.= " ORDER BY timestamp DESC "; |
155 |
$query.= " ORDER BY timestamp DESC "; |
110 |
#if ($limit) { |
156 |
#if ($limit) { |
111 |
# $query.= "LIMIT 0, " . $limit; |
157 |
# $query.= "LIMIT 0, " . $limit; |
112 |
#} |
158 |
#} |
113 |
my $sth = $dbh->prepare($query); |
159 |
my $sth = $dbh->prepare($query); |
114 |
$sth->execute(); |
160 |
$sth->execute(@values); |
115 |
my @opac_news; |
161 |
my @opac_news; |
116 |
my $count = 0; |
162 |
my $count = 0; |
117 |
while (my $row = $sth->fetchrow_hashref) { |
163 |
while (my $row = $sth->fetchrow_hashref) { |
Lines 127-143
sub get_opac_news {
Link Here
|
127 |
|
173 |
|
128 |
=head2 GetNewsToDisplay |
174 |
=head2 GetNewsToDisplay |
129 |
|
175 |
|
130 |
$news = &GetNewsToDisplay($lang); |
176 |
$news = &GetNewsToDisplay($lang,$branch); |
131 |
C<$news> is a ref to an array which containts |
177 |
C<$news> is a ref to an array which containts |
132 |
all news with expirationdate > today or expirationdate is null. |
178 |
all news with expirationdate > today or expirationdate is null |
|
|
179 |
that is applicable for a given branch. |
133 |
|
180 |
|
134 |
=cut |
181 |
=cut |
135 |
|
182 |
|
136 |
sub GetNewsToDisplay { |
183 |
sub GetNewsToDisplay { |
137 |
my $lang = shift; |
184 |
my ($lang,$branch) = @_; |
138 |
my $dbh = C4::Context->dbh; |
185 |
my $dbh = C4::Context->dbh; |
139 |
# SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate |
186 |
# SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate |
140 |
my $query = " |
187 |
my $query = q{ |
141 |
SELECT *,timestamp AS newdate |
188 |
SELECT *,timestamp AS newdate |
142 |
FROM opac_news |
189 |
FROM opac_news |
143 |
WHERE ( |
190 |
WHERE ( |
Lines 145-159
sub GetNewsToDisplay {
Link Here
|
145 |
OR expirationdate IS NULL |
192 |
OR expirationdate IS NULL |
146 |
OR expirationdate = '00-00-0000' |
193 |
OR expirationdate = '00-00-0000' |
147 |
) |
194 |
) |
148 |
AND `timestamp` <= CURRENT_DATE() |
195 |
AND `timestamp` < CURRENT_DATE()+1 |
149 |
AND lang = ? |
196 |
AND (lang = '' OR lang = ?) |
|
|
197 |
AND (branchcode IS NULL OR branchcode = ?) |
150 |
ORDER BY number |
198 |
ORDER BY number |
151 |
"; # expirationdate field is NOT in ISO format? |
199 |
}; # expirationdate field is NOT in ISO format? |
|
|
200 |
# timestamp has HH:mm:ss, CURRENT_DATE() generates 00:00:00 |
201 |
# by adding 1, that captures today correctly. |
152 |
my $sth = $dbh->prepare($query); |
202 |
my $sth = $dbh->prepare($query); |
153 |
$sth->execute($lang); |
203 |
$lang = $lang // ''; |
|
|
204 |
$sth->execute($lang,$branch); |
154 |
my @results; |
205 |
my @results; |
155 |
while ( my $row = $sth->fetchrow_hashref ){ |
206 |
while ( my $row = $sth->fetchrow_hashref ){ |
156 |
$row->{newdate} = format_date($row->{newdate}); |
207 |
$row->{newdate} = format_date($row->{newdate}); |
157 |
push @results, $row; |
208 |
push @results, $row; |
158 |
} |
209 |
} |
159 |
return \@results; |
210 |
return \@results; |