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>.
25 use vars
qw(@ISA @EXPORT);
31 &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
37 C4::NewsChannels - Functions to manage OPAC and intranet news
41 This module provides the functions needed to mange OPAC and intranet news.
49 $retval = add_opac_new($hashref);
51 $hashref should contains all the fields found in opac_news,
52 except idnew. The idnew field is auto-generated.
57 my ($href_entry) = @_;
61 $href_entry->{number
} = 0 if $href_entry->{number
} !~ /^\d+$/;
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);
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.
85 my ($href_entry) = @_;
89 $href_entry->{number
} = 0 if $href_entry->{number
} !~ /^\d+$/;
90 # take the keys of hash entry and make a list, but...
91 my @fields = keys %{$href_entry};
94 my $field_string = q{};
95 foreach my $field_name (@fields) {
97 if ( $field_name ne 'idnew' ) {
98 $field_string = $field_string . "$field_name = ?,";
99 push @values,$href_entry->{$field_name};
102 # put idnew at the end, so we know which record to update
103 push @values,$href_entry->{'idnew'};
104 chop $field_string; # remove that excess ,
106 my $dbh = C4
::Context
->dbh;
107 my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;");
108 $sth->execute(@values);
117 my $dbh = C4
::Context
->dbh;
118 my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");
128 my $dbh = C4
::Context
->dbh;
130 SELECT opac_news.*,branches.branchname
131 FROM opac_news LEFT JOIN branches
132 ON opac_news.branchcode=branches.branchcode
133 WHERE opac_news.idnew = ?;
135 my $sth = $dbh->prepare($query);
136 $sth->execute($idnew);
137 my $data = $sth->fetchrow_hashref;
138 $data->{expirationdate
} = output_pref
({ dt
=> dt_from_string
( $data->{expirationdate
} ), dateonly
=> 1 }) if ( $data->{expirationdate
} );
139 $data->{published_on
} = output_pref
({ dt
=> dt_from_string
( $data->{published_on
} ), dateonly
=> 1 });
144 my ($limit, $lang, $branchcode) = @_;
146 my $dbh = C4
::Context
->dbh;
148 SELECT opac_news.*, branches.branchname,
149 published_on 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';
159 $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
163 $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
164 push @values,$branchcode;
166 $query.= ' ORDER BY published_on DESC ';
168 # $query.= 'LIMIT 0, ' . $limit;
170 my $sth = $dbh->prepare($query);
171 $sth->execute(@values);
174 while (my $row = $sth->fetchrow_hashref) {
175 if ((($limit) && ($count < $limit)) || (!$limit)) {
176 push @opac_news, $row;
180 return ($count, \
@opac_news);
183 =head2 GetNewsToDisplay
185 $news = &GetNewsToDisplay($lang,$branch);
186 C<$news> is a ref to an array which contains
187 all news with expirationdate > today or expirationdate is null
188 that is applicable for a given branch.
192 sub GetNewsToDisplay
{
193 my ($lang,$branch) = @_;
194 my $dbh = C4
::Context
->dbh;
196 SELECT opac_news.*,published_on AS newdate,
197 borrowers.title AS author_title,
198 borrowers.firstname AS author_firstname,
199 borrowers.surname AS author_surname
201 LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
203 expirationdate >= CURRENT_DATE()
204 OR expirationdate IS NULL
206 AND published_on <= CURDATE()
207 AND (opac_news.lang = '' OR opac_news.lang = ?)
208 AND (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
211 my $sth = $dbh->prepare($query);
212 $lang = $lang // q{};
213 $sth->execute($lang,$branch);
215 while ( my $row = $sth->fetchrow_hashref ){
216 $row->{newdate
} = output_pref
({ dt
=> dt_from_string
( $row->{newdate
} ), dateonly
=> 1 });