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>.
24 use C4::Log qw(logaction);
27 use vars qw(@ISA @EXPORT);
33 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
39 C4::NewsChannels - Functions to manage OPAC and intranet news
43 This module provides the functions needed to mange OPAC and intranet news.
51 $retval = add_opac_new($hashref);
53 $hashref should contains all the fields found in opac_news,
54 except idnew. The idnew field is auto-generated.
59 my ($href_entry) = @_;
63 $href_entry->{number} = 0 if $href_entry->{number} !~ /^\d+$/;
64 my @fields = keys %{$href_entry};
65 my @values = values %{$href_entry};
66 my $field_string = join ',', @fields;
67 $field_string = $field_string // q{};
68 my $values_string = join(',', map { '?' } @fields);
69 my $dbh = C4::Context->dbh;
70 my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string )");
71 $sth->execute(@values);
75 if (C4::Context->preference("NewsLog")) {
76 logaction('NEWS', 'ADD' , undef, $href_entry->{lang} . ' | ' . $href_entry->{content});
84 $retval = upd_opac_new($hashref);
86 $hashref should contains all the fields found in opac_news,
87 including idnew, since it is the key for the SQL UPDATE.
92 my ($href_entry) = @_;
96 $href_entry->{number} = 0 if $href_entry->{number} !~ /^\d+$/;
97 # take the keys of hash entry and make a list, but...
98 my @fields = keys %{$href_entry};
101 my $field_string = q{};
102 foreach my $field_name (@fields) {
104 if ( $field_name ne 'idnew' ) {
105 $field_string = $field_string . "$field_name = ?,";
106 push @values,$href_entry->{$field_name};
109 # put idnew at the end, so we know which record to update
110 push @values,$href_entry->{'idnew'};
111 chop $field_string; # remove that excess ,
113 my $dbh = C4::Context->dbh;
114 my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;");
115 $sth->execute(@values);
119 #Log news entry modification
120 if (C4::Context->preference("NewsLog")) {
121 logaction('NEWS', 'MODIFY' , undef, $href_entry->{lang} . ' | ' . $href_entry->{content});
131 if (C4::Context->preference("NewsLog")) {
132 foreach my $newsid ( split(/,/, $ids )) {
133 my $n = Koha::News->find( $newsid );
134 logaction('NEWS', 'DELETE', undef, $n->unblessed->{lang} . ' | ' . $n->unblessed->{content} );
138 my $dbh = C4::Context->dbh;
139 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
150 my $dbh = C4::Context->dbh;
152 SELECT opac_news.*,branches.branchname
153 FROM opac_news LEFT JOIN branches
154 ON opac_news.branchcode=branches.branchcode
155 WHERE opac_news.idnew = ?;
157 my $sth = $dbh->prepare($query);
158 $sth->execute($idnew);
159 my $data = $sth->fetchrow_hashref;
160 $data->{expirationdate} = output_pref({ dt => dt_from_string( $data->{expirationdate} ), dateonly => 1 }) if ( $data->{expirationdate} );
161 $data->{published_on} = output_pref({ dt => dt_from_string( $data->{published_on} ), dateonly => 1 });
166 my ($limit, $lang, $branchcode) = @_;
168 my $dbh = C4::Context->dbh;
170 SELECT opac_news.*, branches.branchname,
171 published_on AS newdate,
172 borrowers.title AS author_title,
173 borrowers.firstname AS author_firstname,
174 borrowers.surname AS author_surname
175 FROM opac_news LEFT JOIN branches
176 ON opac_news.branchcode=branches.branchcode
177 LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
179 $query .= ' WHERE 1';
181 $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
185 $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
186 push @values,$branchcode;
188 $query.= ' ORDER BY published_on DESC ';
190 # $query.= 'LIMIT 0, ' . $limit;
192 my $sth = $dbh->prepare($query);
193 $sth->execute(@values);
196 while (my $row = $sth->fetchrow_hashref) {
197 if ((($limit) && ($count < $limit)) || (!$limit)) {
198 push @opac_news, $row;
202 return ($count, \@opac_news);
205 =head2 GetNewsToDisplay
207 $news = &GetNewsToDisplay($lang,$branch);
208 C<$news> is a ref to an array which contains
209 all news with expirationdate > today or expirationdate is null
210 that is applicable for a given branch.
214 sub GetNewsToDisplay {
215 my ($lang,$branch) = @_;
216 my $dbh = C4::Context->dbh;
218 SELECT opac_news.*,published_on AS newdate,
219 borrowers.title AS author_title,
220 borrowers.firstname AS author_firstname,
221 borrowers.surname AS author_surname
223 LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
225 expirationdate >= CURRENT_DATE()
226 OR expirationdate IS NULL
228 AND published_on <= CURDATE()
229 AND (opac_news.lang = '' OR opac_news.lang = ?)
230 AND (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
233 my $sth = $dbh->prepare($query);
234 $lang = $lang // q{};
235 $sth->execute($lang,$branch);
237 while ( my $row = $sth->fetchrow_hashref ){
238 $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 });