From 79a6fc80e0254472c4f7d7e6513eb7e38a55cc33 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 6 Nov 2013 23:11:41 -0500 Subject: [PATCH] Bug 7567 - News by Branch --- C4/NewsChannels.pm | 156 +++++++++++++------- installer/data/mysql/kohastructure.sql | 1 + installer/data/mysql/updatedatabase.pl | 13 ++ .../prog/en/modules/tools/koha-news.tt | 140 +++++++++++++++--- mainpage.pl | 6 +- opac/opac-main.pl | 8 +- t/NewsChannels.t | 14 -- t/db_dependent/NewsChannels.t | 120 +++++++++++++++ tools/koha-news.pl | 38 ++++- 9 files changed, 397 insertions(+), 99 deletions(-) delete mode 100755 t/NewsChannels.t create mode 100755 t/db_dependent/NewsChannels.t diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm index 0201729..5db8fc4 100644 --- a/C4/NewsChannels.pm +++ b/C4/NewsChannels.pm @@ -1,25 +1,25 @@ package C4::NewsChannels; -# Copyright 2000-2002 Katipo Communications -# # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Copyright (C) 2000 Katipo Communications +# 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 +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -use strict; -use warnings; +use Modern::Perl; use C4::Context; use C4::Dates qw(format_date); @@ -29,9 +29,13 @@ BEGIN { $VERSION = 3.07.00.049; # set the version for version checking @ISA = qw(Exporter); @EXPORT = qw( + &add_opac_new + &upd_opac_new + &del_opac_new + &get_opac_new + &get_opac_news &GetNewsToDisplay - &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news - ); + ); } =head1 NAME @@ -47,30 +51,51 @@ This module provides the functions needed to mange OPAC and intranet news. =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); - $sth->finish; - 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 = "?," x ($#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; } 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); - $sth->finish; - 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 { @@ -79,7 +104,6 @@ sub del_opac_new { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)"); $sth->execute(); - $sth->finish; return 1; } else { return 0; @@ -89,35 +113,61 @@ 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,timestamp AS newdate + 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}); - $sth->finish; + if (!defined($data->{branchcode}) || $data->{branchcode} eq '') { + $data->{branchname} = "All Branches"; + } 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"; - if ($lang) { - $query.= " WHERE lang = '" .$lang ."' "; + my $query = q{ + SELECT opac_news.*,branches.branchname,timestamp AS newdate + FROM opac_news LEFT JOIN branches ON opac_news.branchcode=branches.branchcode + }; + if ($lang && $branchcode) { + $query.= " WHERE lang=?"; + $query .= " AND (opac_news.branchcode=? OR opac_news.branchcode='')"; + push @values,$lang; + push @values,$branchcode; + } + elsif ($lang) { + $query.= " WHERE lang=?"; + push @values,$lang; + } + elsif ($branchcode) { + $query .= " WHERE (opac_news.branchcode=? OR opac_news.branchcode='')"; + push @values,$branchcode; + } + else { + # nothing } $query.= " ORDER BY timestamp DESC "; #if ($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) { if ((($limit) && ($count < $limit)) || (!$limit)) { $row->{'newdate'} = format_date($row->{'newdate'}); $row->{'expirationdate'} = format_date($row->{'expirationdate'}); + $row->{'branchname'} = "All Branches" if !$row->{'branchcode'}; push @opac_news, $row; } $count++; @@ -127,17 +177,18 @@ 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 = " + my $query = q{ SELECT *,timestamp AS newdate FROM opac_news WHERE ( @@ -147,13 +198,14 @@ sub GetNewsToDisplay { ) AND `timestamp` <= CURRENT_DATE() AND lang = ? + AND (branchcode='' OR branchcode = ?) ORDER BY number - "; # expirationdate field is NOT in ISO format? + }; # expirationdate field is NOT in ISO format? my $sth = $dbh->prepare($query); - $sth->execute($lang); + $sth->execute($lang,$branch); my @results; while ( my $row = $sth->fetchrow_hashref ){ - $row->{newdate} = format_date($row->{newdate}); + $row->{newdate} = format_date($row->{newdate}); push @results, $row; } return \@results; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index fefc985..13e4dcc 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1636,6 +1636,7 @@ CREATE TABLE `old_reserves` ( -- this table holds all holds/reserves that have b DROP TABLE IF EXISTS `opac_news`; CREATE TABLE `opac_news` ( -- data from the news tool `idnew` int(10) unsigned NOT NULL auto_increment, -- unique identifier for the news article + `branchcode` varchar(10) NOT NULL default '', -- branch code used to create branch specific news `title` varchar(250) NOT NULL default '', -- title of the news article `new` text NOT NULL, -- the body of your news article `lang` varchar(25) NOT NULL default '', -- location for the article (koha is the staff client, slip is the circulation receipt and language codes are for the opac) diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 6162bc5..cf47951 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -7743,6 +7743,19 @@ if(CheckVersion($DBversion)) { SetVersion($DBversion); } +$DBversion = "3.13.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do(q{ + ALTER TABLE opac_news ADD branchcode varchar(10) NOT NULL DEFAULT ''; + }); + $dbh->do(q{ + UPDATE opac_news SET branchcode=''; + }); + print "Upgrade to $DBversion done (Bug 7567: Add branchcode flag to opac_news)\n"; + SetVersion($DBversion); +} + + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt index b92f63a..e8057da 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt @@ -56,8 +56,8 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %] [% IF ( add_form ) %]
[% ELSE %]
[% END %]
-
-
+
+
[% UNLESS ( add_form ) %]
@@ -69,31 +69,79 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %]
-
+
OPAC and Koha news
  1. + + +
  2. +
  3. -
    [% INCLUDE 'date-format.inc' %]
    +
    [% INCLUDE 'date-format.inc' %]
  4. -
    [% INCLUDE 'date-format.inc' %]
    +
    [% INCLUDE 'date-format.inc' %]
  5. @@ -106,7 +154,7 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %]
  • - +
    Cancel
    @@ -115,17 +163,56 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %]
    + + @@ -137,6 +224,7 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %]
      Location + Branch Number Creation date Expiration date @@ -154,16 +242,20 @@ Edit News Item[% ELSE %]Add News Item[% END %][% ELSE %]News[% END %]
    [% IF ( opac_new.lang == 'koha' ) %] - Librarian interface - [% ELSE %] + Librarian interface + [% ELSE %] [% IF ( opac_new.lang == 'slip' ) %] - Slip + Slip [% ELSE %] - OPAC - [% END %] - [% END %] - - + [% IF ( opac_new.lang == '' ) %] + All + [% ELSE %] + OPAC ([% opac_new.lang %]) + [% END %] + [% END %] + [% END %] + + [% opac_new.branchname %] [% opac_new.number %] [% opac_new.newdate %] [% opac_new.expirationdate %] [% IF ( opac_new.expired ) %](expired)[% END %] diff --git a/mainpage.pl b/mainpage.pl index 4159618..51fbc97 100755 --- a/mainpage.pl +++ b/mainpage.pl @@ -42,7 +42,11 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( } ); -my $all_koha_news = &GetNewsToDisplay("koha"); +my $homebranch; +if (C4::Context->userenv) { + $homebranch = C4::Context->userenv->{'branch'}; +} +my $all_koha_news = &GetNewsToDisplay("koha",$homebranch); my $koha_news_count = scalar @$all_koha_news; $template->param( diff --git a/opac/opac-main.pl b/opac/opac-main.pl index 7c6ee36..5553e97 100755 --- a/opac/opac-main.pl +++ b/opac/opac-main.pl @@ -21,7 +21,7 @@ use warnings; use CGI; use C4::Auth; # get_template_and_user use C4::Output; -use C4::NewsChannels; # get_opac_news +use C4::NewsChannels; # GetNewsToDisplay use C4::Languages qw(getTranslatedLanguages accept_language); use C4::Koha qw( GetDailyQuote ); @@ -48,7 +48,11 @@ $template->param( # use cookie setting for language, bug default to syspref if it's not set my ($theme, $news_lang, $availablethemes) = C4::Templates::themelanguage(C4::Context->config('opachtdocs'),'opac-main.tt','opac',$input); -my $all_koha_news = &GetNewsToDisplay($news_lang); +my $homebranch; +if (C4::Context->userenv) { + $homebranch = C4::Context->userenv->{'branch'}; +} +my $all_koha_news = &GetNewsToDisplay($news_lang,$homebranch); my $koha_news_count = scalar @$all_koha_news; my $quote = GetDailyQuote(); # other options are to pass in an exact quote id or select a random quote each pass... see perldoc C4::Koha diff --git a/t/NewsChannels.t b/t/NewsChannels.t deleted file mode 100755 index bbcaab1..0000000 --- a/t/NewsChannels.t +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -# -# This Koha test module is a stub! -# Add more tests here!!! - -use strict; -use warnings; - -use Test::More tests => 1; - -BEGIN { - use_ok('C4::NewsChannels'); -} - diff --git a/t/db_dependent/NewsChannels.t b/t/db_dependent/NewsChannels.t new file mode 100755 index 0000000..9b200fb --- /dev/null +++ b/t/db_dependent/NewsChannels.t @@ -0,0 +1,120 @@ +#!/usr/bin/perl +# +# This Koha test module is a stub! +# Add more tests here!!! + +use Modern::Perl; +use C4::Dates qw(format_date); +use C4::Branch qw(GetBranchName); +use Test::More tests => 8; + +BEGIN { + use_ok('C4::NewsChannels'); +} + + +my $dbh = C4::Context->dbh; + +# Start transaction +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +# Add LIB1, if it doesn't exist. +my $addbra = 'LIB1'; +$dbh->do(q{ + INSERT INTO branches (branchcode,branchname) VALUES (?,?) + }, undef, ($addbra, "$addbra branch") + ) unless GetBranchName($addbra); + +# Test add_opac_new +my $timestamp = '2000-01-01'; +my $hashref_entry1 = { + 'title' => 'News Title', + 'new' => '

    We have some exciting news!

    ', + 'lang' => '', + 'timestamp' => $timestamp, + 'expirationdate' => '2999-12-30', + 'number' => 1, + 'branchcode' => 'LIB1', + }; +my $rv = add_opac_new( $hashref_entry1 ); +ok($rv==1,"Successfully added the first dummy news item!"); + +my $hashref_entry2 = { + "title" => 'News Title2', + "new" => '

    We have some exciting news!

    ', + "lang" => '', + "timestamp" => $timestamp, + "expirationdate" => '2999-12-31', + "number" => 1, + "branchcode" => 'LIB1', + }; +$rv = add_opac_new( $hashref_entry2 ); +ok($rv==1,"Successfully added the second dummy news item!"); + +# We need to determine the idnew in a non-MySQLism way. +# This should be good enough. +my $sth = $dbh->prepare(q{ + SELECT idnew from opac_news + WHERE timestamp='2000-01-01' AND + expirationdate='2999-12-30' AND + branchcode='LIB1'; + }); +$sth->execute(); +my $idnew1 = $sth->fetchrow; +$sth = $dbh->prepare(q{ + SELECT idnew from opac_news + WHERE timestamp='2000-01-01' AND + expirationdate='2999-12-31' AND + branchcode='LIB1'; + }); +$sth->execute(); +my $idnew2 = $sth->fetchrow; + +# Test upd_opac_new +$hashref_entry2->{new} = '

    Update! There is no news!

    '; +$hashref_entry2->{idnew} = $idnew2; +$rv = upd_opac_new($hashref_entry2); +ok($rv==1,"Successfully updated second dummy news item!"); + +# Test get_opac_new (single news item) +$hashref_entry1->{timestamp} = + format_date( $hashref_entry1->{timestamp} ); +$hashref_entry1->{expirationdate} = + format_date( $hashref_entry1->{expirationdate} ); +$hashref_entry2->{timestamp} = + format_date( $hashref_entry2->{timestamp} ); +$hashref_entry2->{expirationdate} = + format_date( $hashref_entry2->{expirationdate} ); +my $hashref_check = get_opac_new($idnew1); +my $failure = 0; +foreach my $key (keys %{$hashref_entry1}) { + if ($hashref_entry1->{$key} ne $hashref_check->{$key}) { + $failure = 1; + print STDERR "\nKey mismatch: " . $hashref_entry1->{$key} . " vs "; + print STDERR $hashref_check->{$key} . "\n"; + } +} +ok($failure==0,"Successfully tested get_opac_new id1!"); + +# Test get_opac_new (single news item) +$hashref_check = get_opac_new($idnew2); +$failure = 0; +foreach my $key (keys %{$hashref_entry2}) { + if ($hashref_entry2->{$key} ne $hashref_check->{$key}) { + $failure = 1; + print STDERR "\nKey mismatch: " . $hashref_entry1->{$key} . " vs "; + print STDERR $hashref_check->{$key} . "\n"; + } +} +ok($failure==0,"Successfully tested get_opac_new id2!"); + +# Test get_opac_news (multiple news items) +my ($opac_news_count, $arrayref_opac_news) = get_opac_news(0,'','LIB1'); +ok($opac_news_count>=2,"Successfully tested get_opac_news for LIB1!"); + +# Test GetNewsToDisplay +($opac_news_count, $arrayref_opac_news) = GetNewsToDisplay('','LIB1'); +ok($opac_news_count>=2,"Successfully tested GetNewsToDisplay for LIB1!"); + +$dbh->rollback; diff --git a/tools/koha-news.pl b/tools/koha-news.pl index 4dd07a9..05bbde4 100755 --- a/tools/koha-news.pl +++ b/tools/koha-news.pl @@ -32,6 +32,7 @@ use C4::Output; use C4::NewsChannels; use C4::Languages qw(getTranslatedLanguages); use Date::Calc qw/Date_to_Days Today/; +use C4::Branch qw/GetBranches/; my $cgi = new CGI; @@ -42,6 +43,8 @@ my $expirationdate = format_date_in_iso($cgi->param('expirationdate')); my $timestamp = format_date_in_iso($cgi->param('timestamp')); my $number = $cgi->param('number'); my $lang = $cgi->param('lang'); +my $branchcode = $cgi->param('branch'); +my $hashref_entry; my $new_detail = get_opac_new($id); @@ -67,9 +70,13 @@ foreach my $language ( @$tlangs ) { }; } -$template->param( lang_list => \@lang_list ); +my $branches = GetBranches; -my $op = $cgi->param('op'); +$template->param( lang_list => \@lang_list, + branch_list => $branches, + branchcode => $branchcode ); + +my $op = $cgi->param('op') // ''; if ( $op eq 'add_form' ) { $template->param( add_form => 1 ); @@ -86,11 +93,30 @@ if ( $op eq 'add_form' ) { } } elsif ( $op eq 'add' ) { - add_opac_new( $title, $new, $lang, $expirationdate, $timestamp, $number ); + $hashref_entry = { + "title" => $title, + "new" => $new, + "lang" => $lang, + "timestamp" => $timestamp, + "expirationdate" => $expirationdate, + "number" => $number, + "branchcode" => $branchcode, + }; + add_opac_new( $hashref_entry ); print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl"); } elsif ( $op eq 'edit' ) { - upd_opac_new( $id, $title, $new, $lang, $expirationdate, $timestamp ,$number ); + $hashref_entry = { + "idnew" => $id, + "title" => $title, + "new" => $new, + "lang" => $lang, + "timestamp" => $timestamp, + "expirationdate" => $expirationdate, + "number" => $number, + "branchcode" => $branchcode, + }; + upd_opac_new( $hashref_entry ); print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl"); } elsif ( $op eq 'del' ) { @@ -101,7 +127,7 @@ elsif ( $op eq 'del' ) { else { - my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang ); + my ( $opac_news_count, $opac_news ) = &get_opac_news( 0, $lang, $branchcode ); foreach my $new ( @$opac_news ) { next unless $new->{'expirationdate'}; @@ -113,9 +139,9 @@ else { } $template->param( - $lang => 1, opac_news => $opac_news, opac_news_count => $opac_news_count, ); + $template->param( lang => $lang ); } output_html_with_http_headers $cgi, $cookie, $template->output; -- 1.7.9.5