X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FNewsChannels.pm;h=cbcc702a4cda9ab94d7bb46dc20a85a68372ab65;hb=798d38e4c7de0c920070b5db2249030cc9cb742c;hp=cc931d2d658a4255799e3c9ce0c5f182bebf8759;hpb=d3eaa627176d3945888da33082bfb12e05e5ee25;p=srvgit diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm index cc931d2d65..cbcc702a4c 100644 --- a/C4/NewsChannels.pm +++ b/C4/NewsChannels.pm @@ -3,8 +3,7 @@ package C4::NewsChannels; # This file is part of Koha. # # Copyright (C) 2000-2002 Katipo Communications -# -# This file is part of Koha. +# Copyright (C) 2013 Mark Tompsett # # Koha is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -21,17 +20,16 @@ package C4::NewsChannels; use Modern::Perl; use C4::Context; -use C4::Dates qw(format_date); +use Koha::DateUtils; -use vars qw($VERSION @ISA @EXPORT); +use vars qw(@ISA @EXPORT); BEGIN { - $VERSION = 3.07.00.049; # set the version for version checking - @ISA = qw(Exporter); - @EXPORT = qw( - &GetNewsToDisplay - &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news - ); + @ISA = qw(Exporter); + @EXPORT = qw( + &GetNewsToDisplay + &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news + ); } =head1 NAME @@ -46,29 +44,69 @@ This module provides the functions needed to mange OPAC and intranet news. =cut +=head2 add_opac_new + + $retval = add_opac_new($hashref); + + $hashref should contains all the fields found in opac_news, + except idnew. The idnew field is auto-generated. + +=cut + sub add_opac_new { - my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)"); - $sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number); - return 1; + my ($href_entry) = @_; + my $retval = 0; + + if ($href_entry) { + my @fields = keys %{$href_entry}; + my @values = values %{$href_entry}; + my $field_string = join ',', @fields; + $field_string = $field_string // q{}; + my $values_string = join(',', map { '?' } @fields); + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string )"); + $sth->execute(@values); + $retval = 1; + } + return $retval; } +=head2 upd_opac_new + + $retval = upd_opac_new($hashref); + + $hashref should contains all the fields found in opac_news, + including idnew, since it is the key for the SQL UPDATE. + +=cut + sub upd_opac_new { - my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare(" - UPDATE opac_news SET - title = ?, - new = ?, - lang = ?, - expirationdate = ?, - timestamp = ?, - number = ? - WHERE idnew = ? - "); - $sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew); - return 1; + my ($href_entry) = @_; + my $retval = 0; + + if ($href_entry) { + # take the keys of hash entry and make a list, but... + my @fields = keys %{$href_entry}; + my @values; + $#values = -1; + my $field_string = q{}; + foreach my $field_name (@fields) { + # exclude idnew + if ( $field_name ne 'idnew' ) { + $field_string = $field_string . "$field_name = ?,"; + push @values,$href_entry->{$field_name}; + } + } + # put idnew at the end, so we know which record to update + push @values,$href_entry->{'idnew'}; + chop $field_string; # remove that excess , + + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;"); + $sth->execute(@values); + $retval = 1; + } + return $retval; } sub del_opac_new { @@ -86,28 +124,50 @@ sub del_opac_new { sub get_opac_new { my ($idnew) = @_; my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?"); + my $query = q{ + SELECT opac_news.*,branches.branchname + FROM opac_news LEFT JOIN branches + ON opac_news.branchcode=branches.branchcode + WHERE opac_news.idnew = ?; + }; + my $sth = $dbh->prepare($query); $sth->execute($idnew); my $data = $sth->fetchrow_hashref; $data->{$data->{'lang'}} = 1 if defined $data->{lang}; - $data->{expirationdate} = format_date($data->{expirationdate}); - $data->{timestamp} = format_date($data->{timestamp}); + $data->{expirationdate} = output_pref({ dt => dt_from_string( $data->{expirationdate} ), dateonly => 1 }); + $data->{timestamp} = output_pref({ dt => dt_from_string( $data->{timestamp} ), dateonly => 1 }) ; return $data; } sub get_opac_news { - my ($limit, $lang) = @_; + my ($limit, $lang, $branchcode) = @_; + my @values; my $dbh = C4::Context->dbh; - my $query = "SELECT *, timestamp AS newdate FROM opac_news"; + my $query = q{ + SELECT opac_news.*, branches.branchname, + timestamp AS newdate, + borrowers.title AS author_title, + borrowers.firstname AS author_firstname, + borrowers.surname AS author_surname + FROM opac_news LEFT JOIN branches + ON opac_news.branchcode=branches.branchcode + LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber + }; + $query .= ' WHERE 1'; if ($lang) { - $query.= " WHERE lang = '" .$lang ."' "; + $query .= " AND (opac_news.lang='' OR opac_news.lang=?)"; + push @values,$lang; + } + if ($branchcode) { + $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)'; + push @values,$branchcode; } - $query.= " ORDER BY timestamp DESC "; + $query.= ' ORDER BY timestamp DESC '; #if ($limit) { - # $query.= "LIMIT 0, " . $limit; + # $query.= 'LIMIT 0, ' . $limit; #} my $sth = $dbh->prepare($query); - $sth->execute(); + $sth->execute(@values); my @opac_news; my $count = 0; while (my $row = $sth->fetchrow_hashref) { @@ -121,33 +181,42 @@ sub get_opac_news { =head2 GetNewsToDisplay - $news = &GetNewsToDisplay($lang); + $news = &GetNewsToDisplay($lang,$branch); C<$news> is a ref to an array which containts - all news with expirationdate > today or expirationdate is null. + all news with expirationdate > today or expirationdate is null + that is applicable for a given branch. =cut sub GetNewsToDisplay { - my $lang = shift; + my ($lang,$branch) = @_; my $dbh = C4::Context->dbh; # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate - my $query = " - SELECT *,timestamp AS newdate + my $query = q{ + SELECT opac_news.*,timestamp AS newdate, + borrowers.title AS author_title, + borrowers.firstname AS author_firstname, + borrowers.surname AS author_surname FROM opac_news + LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber WHERE ( expirationdate >= CURRENT_DATE() OR expirationdate IS NULL OR expirationdate = '00-00-0000' - ) - AND `timestamp` <= CURRENT_DATE() - AND lang = ? - ORDER BY number - "; # expirationdate field is NOT in ISO format? + ) + AND DATE(timestamp) < DATE_ADD(CURDATE(), INTERVAL 1 DAY) + AND (lang = '' OR lang = ?) + AND (opac_news.branchcode IS NULL OR opac_news.branchcode = ?) + ORDER BY number + }; # expirationdate field is NOT in ISO format? + # timestamp has HH:mm:ss, CURRENT_DATE generates 00:00:00 + # by adding 1, that captures today correctly. my $sth = $dbh->prepare($query); - $sth->execute($lang); + $lang = $lang // q{}; + $sth->execute($lang,$branch); my @results; while ( my $row = $sth->fetchrow_hashref ){ - $row->{newdate} = format_date($row->{newdate}); + $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 }); push @results, $row; } return \@results;