Bug 15358: Fix authorities merge
[koha.git] / C4 / NewsChannels.pm
blob4dfde1317595d3db50369cee34f9e01ec6d2e5b2
1 package C4::NewsChannels;
3 # This file is part of Koha.
5 # Copyright (C) 2000-2002 Katipo Communications
6 # Copyright (C) 2013 Mark Tompsett
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.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
22 use C4::Context;
23 use Koha::DateUtils;
25 use vars qw($VERSION @ISA @EXPORT);
27 BEGIN {
28 $VERSION = 3.07.00.049; # set the version for version checking
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 &GetNewsToDisplay
32 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
36 =head1 NAME
38 C4::NewsChannels - Functions to manage OPAC and intranet news
40 =head1 DESCRIPTION
42 This module provides the functions needed to mange OPAC and intranet news.
44 =head1 FUNCTIONS
46 =cut
48 =head2 add_opac_new
50 $retval = add_opac_new($hashref);
52 $hashref should contains all the fields found in opac_news,
53 except idnew. The idnew field is auto-generated.
55 =cut
57 sub add_opac_new {
58 my ($href_entry) = @_;
59 my $retval = 0;
61 if ($href_entry) {
62 my @fields = keys %{$href_entry};
63 my @values = values %{$href_entry};
64 my $field_string = join ',', @fields;
65 $field_string = $field_string // q{};
66 my $values_string = join(',', map { '?' } @fields);
67 my $dbh = C4::Context->dbh;
68 my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string )");
69 $sth->execute(@values);
70 $retval = 1;
72 return $retval;
75 =head2 upd_opac_new
77 $retval = upd_opac_new($hashref);
79 $hashref should contains all the fields found in opac_news,
80 including idnew, since it is the key for the SQL UPDATE.
82 =cut
84 sub upd_opac_new {
85 my ($href_entry) = @_;
86 my $retval = 0;
88 if ($href_entry) {
89 # take the keys of hash entry and make a list, but...
90 my @fields = keys %{$href_entry};
91 my @values;
92 $#values = -1;
93 my $field_string = q{};
94 foreach my $field_name (@fields) {
95 # exclude idnew
96 if ( $field_name ne 'idnew' ) {
97 $field_string = $field_string . "$field_name = ?,";
98 push @values,$href_entry->{$field_name};
101 # put idnew at the end, so we know which record to update
102 push @values,$href_entry->{'idnew'};
103 chop $field_string; # remove that excess ,
105 my $dbh = C4::Context->dbh;
106 my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;");
107 $sth->execute(@values);
108 $retval = 1;
110 return $retval;
113 sub del_opac_new {
114 my ($ids) = @_;
115 if ($ids) {
116 my $dbh = C4::Context->dbh;
117 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
118 $sth->execute();
119 return 1;
120 } else {
121 return 0;
125 sub get_opac_new {
126 my ($idnew) = @_;
127 my $dbh = C4::Context->dbh;
128 my $query = q{
129 SELECT opac_news.*,branches.branchname
130 FROM opac_news LEFT JOIN branches
131 ON opac_news.branchcode=branches.branchcode
132 WHERE opac_news.idnew = ?;
134 my $sth = $dbh->prepare($query);
135 $sth->execute($idnew);
136 my $data = $sth->fetchrow_hashref;
137 $data->{$data->{'lang'}} = 1 if defined $data->{lang};
138 $data->{expirationdate} = output_pref({ dt => dt_from_string( $data->{expirationdate} ), dateonly => 1 });
139 $data->{timestamp} = output_pref({ dt => dt_from_string( $data->{timestamp} ), dateonly => 1 }) ;
140 return $data;
143 sub get_opac_news {
144 my ($limit, $lang, $branchcode) = @_;
145 my @values;
146 my $dbh = C4::Context->dbh;
147 my $query = q{
148 SELECT opac_news.*, branches.branchname,
149 timestamp AS newdate,
150 borrowers.title AS author_title,
151 borrowers.firstname AS author_firstname,
152 borrowers.surname AS author_surname
153 FROM opac_news LEFT JOIN branches
154 ON opac_news.branchcode=branches.branchcode
155 LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
157 $query .= ' WHERE 1';
158 if ($lang) {
159 $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
160 push @values,$lang;
162 if ($branchcode) {
163 $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
164 push @values,$branchcode;
166 $query.= ' ORDER BY timestamp DESC ';
167 #if ($limit) {
168 # $query.= 'LIMIT 0, ' . $limit;
170 my $sth = $dbh->prepare($query);
171 $sth->execute(@values);
172 my @opac_news;
173 my $count = 0;
174 while (my $row = $sth->fetchrow_hashref) {
175 if ((($limit) && ($count < $limit)) || (!$limit)) {
176 push @opac_news, $row;
178 $count++;
180 return ($count, \@opac_news);
183 =head2 GetNewsToDisplay
185 $news = &GetNewsToDisplay($lang,$branch);
186 C<$news> is a ref to an array which containts
187 all news with expirationdate > today or expirationdate is null
188 that is applicable for a given branch.
190 =cut
192 sub GetNewsToDisplay {
193 my ($lang,$branch) = @_;
194 my $dbh = C4::Context->dbh;
195 # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
196 my $query = q{
197 SELECT opac_news.*,timestamp AS newdate,
198 borrowers.title AS author_title,
199 borrowers.firstname AS author_firstname,
200 borrowers.surname AS author_surname
201 FROM opac_news
202 LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
203 WHERE (
204 expirationdate >= CURRENT_DATE()
205 OR expirationdate IS NULL
206 OR expirationdate = '00-00-0000'
208 AND DATE(timestamp) < DATE_ADD(CURDATE(), INTERVAL 1 DAY)
209 AND (lang = '' OR lang = ?)
210 AND (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
211 ORDER BY number
212 }; # expirationdate field is NOT in ISO format?
213 # timestamp has HH:mm:ss, CURRENT_DATE generates 00:00:00
214 # by adding 1, that captures today correctly.
215 my $sth = $dbh->prepare($query);
216 $lang = $lang // q{};
217 $sth->execute($lang,$branch);
218 my @results;
219 while ( my $row = $sth->fetchrow_hashref ){
220 $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 });
221 push @results, $row;
223 return \@results;
227 __END__
229 =head1 AUTHOR
233 =cut