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_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)");
149 my ($limit, $lang, $branchcode) = @_;
151 my $dbh = C4::Context->dbh;
153 SELECT opac_news.*, branches.branchname,
154 published_on AS newdate,
155 borrowers.title AS author_title,
156 borrowers.firstname AS author_firstname,
157 borrowers.surname AS author_surname
158 FROM opac_news LEFT JOIN branches
159 ON opac_news.branchcode=branches.branchcode
160 LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
162 $query .= ' WHERE 1';
164 $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
168 $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
169 push @values,$branchcode;
171 $query.= ' ORDER BY published_on DESC ';
173 # $query.= 'LIMIT 0, ' . $limit;
175 my $sth = $dbh->prepare($query);
176 $sth->execute(@values);
179 while (my $row = $sth->fetchrow_hashref) {
180 if ((($limit) && ($count < $limit)) || (!$limit)) {
181 push @opac_news, $row;
185 return ($count, \@opac_news);
188 =head2 GetNewsToDisplay
190 $news = &GetNewsToDisplay($lang,$branch);
191 C<$news> is a ref to an array which contains
192 all news with expirationdate > today or expirationdate is null
193 that is applicable for a given branch.
197 sub GetNewsToDisplay {
198 my ($lang,$branch) = @_;
199 my $dbh = C4::Context->dbh;
201 SELECT opac_news.*,published_on AS newdate,
202 borrowers.title AS author_title,
203 borrowers.firstname AS author_firstname,
204 borrowers.surname AS author_surname
206 LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
208 expirationdate >= CURRENT_DATE()
209 OR expirationdate IS NULL
211 AND published_on <= CURDATE()
212 AND (opac_news.lang = '' OR opac_news.lang = ?)
213 AND (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
216 my $sth = $dbh->prepare($query);
217 $lang = $lang // q{};
218 $sth->execute($lang,$branch);
220 while ( my $row = $sth->fetchrow_hashref ){
221 $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 });