View | Details | Raw Unified | Return to bug 7567
Collapse All | Expand All

(-)a/C4/NewsChannels.pm (-41 / +86 lines)
Lines 3-10 package C4::NewsChannels; Link Here
3
# This file is part of Koha.
3
# This file is part of Koha.
4
#
4
#
5
# Copyright (C) 2000-2002  Katipo Communications
5
# Copyright (C) 2000-2002  Katipo Communications
6
#
6
# Copyright (C) 2013       Mark Tompsett
7
# This file is part of Koha.
8
#
7
#
9
# Koha is free software; you can redistribute it and/or modify it
8
# Koha is free software; you can redistribute it and/or modify it
10
# under the terms of the GNU General Public License as published by
9
# under the terms of the GNU General Public License as published by
Lines 26-37 use C4::Dates qw(format_date); Link Here
26
use vars qw($VERSION @ISA @EXPORT);
25
use vars qw($VERSION @ISA @EXPORT);
27
26
28
BEGIN { 
27
BEGIN { 
29
    $VERSION = 3.07.00.049;	# set the version for version checking
28
    $VERSION = 3.07.00.049;    # set the version for version checking
30
	@ISA = qw(Exporter);
29
    @ISA = qw(Exporter);
31
	@EXPORT = qw(
30
    @EXPORT = qw(
32
		&GetNewsToDisplay
31
        &GetNewsToDisplay
33
		&add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
32
        &add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news
34
	);
33
    );
35
}
34
}
36
35
37
=head1 NAME
36
=head1 NAME
Lines 46-74 This module provides the functions needed to mange OPAC and intranet news. Link Here
46
45
47
=cut
46
=cut
48
47
48
=head2 add_opac_new
49
50
    $retval = add_opac_new($hashref);
51
52
    $hashref should contains all the fields found in opac_news,
53
    except idnew. The idnew field is auto-generated.
54
55
=cut
56
49
sub add_opac_new {
57
sub add_opac_new {
50
    my ($title, $new, $lang, $expirationdate, $timestamp, $number) = @_;
58
    my ($href_entry) = @_;
51
    my $dbh = C4::Context->dbh;
59
    my $retval = 0;
52
    my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang, expirationdate, timestamp, number) VALUES (?,?,?,?,?,?)");
60
53
    $sth->execute($title, $new, $lang, $expirationdate, $timestamp, $number);
61
    if ($href_entry) {
54
    return 1;
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 = '?,' x ($#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);
70
        $retval = 1;
71
    }
72
    return $retval;
55
}
73
}
56
74
75
=head2 upd_opac_new
76
77
    $retval = upd_opac_new($hashref);
78
79
    $hashref should contains all the fields found in opac_news,
80
    including idnew, since it is the key for the SQL UPDATE.
81
82
=cut
83
57
sub upd_opac_new {
84
sub upd_opac_new {
58
    my ($idnew, $title, $new, $lang, $expirationdate, $timestamp,$number) = @_;
85
    my ($href_entry) = @_;
59
    my $dbh = C4::Context->dbh;
86
    my $retval = 0;
60
    my $sth = $dbh->prepare("
87
61
        UPDATE opac_news SET 
88
    if ($href_entry) {
62
            title = ?,
89
        # take the keys of hash entry and make a list, but...
63
            new = ?,
90
        my @fields = keys %{$href_entry};
64
            lang = ?,
91
        my @values;
65
            expirationdate = ?,
92
        $#values = -1;
66
            timestamp = ?,
93
        my $field_string = q{};
67
            number = ?
94
        foreach my $field_name (@fields) {
68
        WHERE idnew = ?
95
            # exclude idnew
69
    ");
96
            if ( $field_name ne 'idnew' ) {
70
    $sth->execute($title, $new, $lang, $expirationdate, $timestamp,$number,$idnew);
97
                $field_string = $field_string . "$field_name = ?,";
71
    return 1;
98
                push @values,$href_entry->{$field_name};
99
            }
100
        }
101
        # put idnew at the end, so we know which record to update
102
        push @values,$href_entry->{'idnew'};
103
        chop $field_string; # remove that excess ,
104
105
        my $dbh = C4::Context->dbh;
106
        my $sth = $dbh->prepare("UPDATE opac_news SET $field_string WHERE idnew = ?;");
107
        $sth->execute(@values);
108
        $retval = 1;
109
    }
110
    return $retval;
72
}
111
}
73
112
74
sub del_opac_new {
113
sub del_opac_new {
Lines 86-92 sub del_opac_new { Link Here
86
sub get_opac_new {
125
sub get_opac_new {
87
    my ($idnew) = @_;
126
    my ($idnew) = @_;
88
    my $dbh = C4::Context->dbh;
127
    my $dbh = C4::Context->dbh;
89
    my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");
128
    my $query = q{ SELECT * FROM opac_news WHERE idnew = ? };
129
    my $sth = $dbh->prepare($query);
90
    $sth->execute($idnew);
130
    $sth->execute($idnew);
91
    my $data = $sth->fetchrow_hashref;
131
    my $data = $sth->fetchrow_hashref;
92
    $data->{$data->{'lang'}} = 1 if defined $data->{lang};
132
    $data->{$data->{'lang'}} = 1 if defined $data->{lang};
Lines 97-113 sub get_opac_new { Link Here
97
137
98
sub get_opac_news {
138
sub get_opac_news {
99
    my ($limit, $lang) = @_;
139
    my ($limit, $lang) = @_;
140
    my @values;
100
    my $dbh = C4::Context->dbh;
141
    my $dbh = C4::Context->dbh;
101
    my $query = "SELECT *, timestamp AS newdate FROM opac_news";
142
    my $query = q{ SELECT *, timestamp AS newdate FROM opac_news };
102
    if ($lang) {
143
    if ($lang) {
103
        $query.= " WHERE lang = '" .$lang ."' ";
144
        $query.= " WHERE (lang='' OR lang=?)";
145
        push @values,$lang;
104
    }
146
    }
105
    $query.= " ORDER BY timestamp DESC ";
147
    $query.= ' ORDER BY timestamp DESC ';
106
    #if ($limit) {
148
    #if ($limit) {
107
    #    $query.= "LIMIT 0, " . $limit;
149
    #    $query.= 'LIMIT 0, ' . $limit;
108
    #}
150
    #}
109
    my $sth = $dbh->prepare($query);
151
    my $sth = $dbh->prepare($query);
110
    $sth->execute();
152
    $sth->execute(@values);
111
    my @opac_news;
153
    my @opac_news;
112
    my $count = 0;
154
    my $count = 0;
113
    while (my $row = $sth->fetchrow_hashref) {
155
    while (my $row = $sth->fetchrow_hashref) {
Lines 128-153 sub get_opac_news { Link Here
128
=cut
170
=cut
129
171
130
sub GetNewsToDisplay {
172
sub GetNewsToDisplay {
131
    my $lang = shift;
173
    my ($lang) = @_;
132
    my $dbh = C4::Context->dbh;
174
    my $dbh = C4::Context->dbh;
133
    # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
175
    # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
134
    my $query = "
176
    my $query = q{
135
     SELECT *,timestamp AS newdate
177
     SELECT *,timestamp AS newdate
136
     FROM   opac_news
178
     FROM   opac_news
137
     WHERE   (
179
     WHERE   (
138
        expirationdate >= CURRENT_DATE()
180
        expirationdate >= CURRENT_DATE()
139
        OR    expirationdate IS NULL
181
        OR    expirationdate IS NULL
140
        OR    expirationdate = '00-00-0000'
182
        OR    expirationdate = '00-00-0000'
141
      )
183
     )
142
      AND   `timestamp` <= CURRENT_DATE()
184
     AND   `timestamp` < CURRENT_DATE()+1
143
      AND   lang = ?
185
     AND   (lang = '' OR lang = ?)
144
      ORDER BY number
186
     ORDER BY number
145
    ";				# expirationdate field is NOT in ISO format?
187
    }; # expirationdate field is NOT in ISO format?
188
       # timestamp has HH:mm:ss, CURRENT_DATE generates 00:00:00
189
       #           by adding 1, that captures today correctly.
146
    my $sth = $dbh->prepare($query);
190
    my $sth = $dbh->prepare($query);
191
    $lang = $lang // q{};
147
    $sth->execute($lang);
192
    $sth->execute($lang);
148
    my @results;
193
    my @results;
149
    while ( my $row = $sth->fetchrow_hashref ){
194
    while ( my $row = $sth->fetchrow_hashref ){
150
		$row->{newdate} = format_date($row->{newdate});
195
        $row->{newdate} = format_date($row->{newdate});
151
        push @results, $row;
196
        push @results, $row;
152
    }
197
    }
153
    return \@results;
198
    return \@results;
(-)a/t/db_dependent/NewsChannels.t (-52 / +77 lines)
Lines 3-12 Link Here
3
use Modern::Perl;
3
use Modern::Perl;
4
use C4::Dates qw(format_date);
4
use C4::Dates qw(format_date);
5
use C4::Branch qw(GetBranchName);
5
use C4::Branch qw(GetBranchName);
6
use Test::More tests => 8;
6
use Test::More tests => 10;
7
use Readonly;
8
9
Readonly my $F1 => 1;
10
Readonly my $F2 => 2;
11
Readonly my $F3 => 3;
12
Readonly my $F4 => 4;
13
Readonly my $F5 => 5;
14
Readonly my $F6 => 6;
7
15
8
BEGIN {
16
BEGIN {
9
        use_ok('C4::NewsChannels');
17
    use_ok('C4::NewsChannels');
10
}
18
}
11
19
12
my $dbh = C4::Context->dbh;
20
my $dbh = C4::Context->dbh;
Lines 16-92 $dbh->{AutoCommit} = 0; Link Here
16
$dbh->{RaiseError} = 1;
24
$dbh->{RaiseError} = 1;
17
25
18
# Test add_opac_new
26
# Test add_opac_new
27
my $rv = add_opac_new();    # intentionally bad
28
ok( $rv == 0, 'Correctly failed on no parameter!' );
29
19
my $timestamp = '2000-01-01';
30
my $timestamp = '2000-01-01';
20
my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
31
my ( $timestamp1, $timestamp2 ) = ( $timestamp, $timestamp );
21
my ($title1,         $new1,
32
my ( $title1, $new1, $lang1, $expirationdate1, $number1 ) =
22
    $lang1, $expirationdate1, $number1) =
33
  ( 'News Title', '<p>We have some exciting news!</p>', q{}, '2999-12-30', 1 );
23
   ( 'News Title',  '<p>We have some exciting news!</p>',
34
my $href_entry1 = {
24
    '',    '2999-12-30',    1 );
35
    title          => $title1,
25
my $rv = add_opac_new( $title1, $new1, $lang1, $expirationdate1, $timestamp1, $number1 );
36
    new            => $new1,
26
ok($rv==1,"Successfully added the first dummy news item!");
37
    lang           => $lang1,
27
38
    expirationdate => $expirationdate1,
28
my ($title2,         $new2,
39
    timestamp      => $timestamp1,
29
    $lang2, $expirationdate2, $number2) =
40
    number         => $number1,
30
   ( 'News Title2', '<p>We have some exciting news!</p>',
41
};
31
    '',    '2999-12-31',    1 );
42
32
$rv = add_opac_new( $title2, $new2, $lang2, $expirationdate2, $timestamp2, $number2 );
43
$rv = add_opac_new($href_entry1);
33
ok($rv==1,"Successfully added the second dummy news item!");
44
ok( $rv == 1, 'Successfully added the first dummy news item!' );
45
46
my ( $title2, $new2, $lang2, $expirationdate2, $number2 ) =
47
  ( 'News Title2', '<p>We have some exciting news!</p>', q{}, '2999-12-31', 1 );
48
my $href_entry2 = {
49
    title          => $title2,
50
    new            => $new2,
51
    lang           => $lang2,
52
    expirationdate => $expirationdate2,
53
    timestamp      => $timestamp2,
54
    number         => $number2,
55
};
56
$rv = add_opac_new($href_entry2);
57
ok( $rv == 1, 'Successfully added the second dummy news item!' );
34
58
35
# We need to determine the idnew in a non-MySQLism way.
59
# We need to determine the idnew in a non-MySQLism way.
36
# This should be good enough.
60
# This should be good enough.
37
my $sth = $dbh->prepare(q{
61
my $query =
38
  SELECT idnew from opac_news
62
q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-30'; };
39
  WHERE timestamp='2000-01-01' AND
63
my $sth = $dbh->prepare($query);
40
        expirationdate='2999-12-30';
41
                        });
42
$sth->execute();
64
$sth->execute();
43
my $idnew1 = $sth->fetchrow;
65
my $idnew1 = $sth->fetchrow;
44
$sth = $dbh->prepare(q{
66
$query =
45
  SELECT idnew from opac_news
67
q{ SELECT idnew from opac_news WHERE timestamp='2000-01-01' AND expirationdate='2999-12-31'; };
46
  WHERE timestamp='2000-01-01' AND
68
$sth = $dbh->prepare($query);
47
        expirationdate='2999-12-31';
48
                      });
49
$sth->execute();
69
$sth->execute();
50
my $idnew2 = $sth->fetchrow;
70
my $idnew2 = $sth->fetchrow;
51
71
52
# Test upd_opac_new
72
# Test upd_opac_new
53
$new2 = '<p>Update! There is no news!</p>';
73
$rv = upd_opac_new();    # intentionally bad parmeters
54
$rv = upd_opac_new( $idnew2, $title2, $new2, $lang2, $expirationdate2, $timestamp2, $number2 );
74
ok( $rv == 0, 'Correctly failed on no parameter!' );
55
ok($rv==1,"Successfully updated second dummy news item!");
75
76
$new2                 = '<p>Update! There is no news!</p>';
77
$href_entry2->{new}   = $new2;
78
$href_entry2->{idnew} = $idnew2;
79
$rv                   = upd_opac_new($href_entry2);
80
ok( $rv == 1, 'Successfully updated second dummy news item!' );
56
81
57
# Test get_opac_new (single news item)
82
# Test get_opac_new (single news item)
58
$timestamp1 = format_date( $timestamp1 );
83
$timestamp1      = format_date($timestamp1);
59
$expirationdate1 = format_date( $expirationdate1 );
84
$expirationdate1 = format_date($expirationdate1);
60
$timestamp2 = format_date( $timestamp2 );
85
$timestamp2      = format_date($timestamp2);
61
$expirationdate2 = format_date( $expirationdate2 );
86
$expirationdate2 = format_date($expirationdate2);
62
87
63
my $hashref_check = get_opac_new($idnew1);
88
my $hashref_check = get_opac_new($idnew1);
64
my $failure = 0;
89
my $failure       = 0;
65
if ($hashref_check->{title}          ne $title1)          { $failure = 1; }
90
if ( $hashref_check->{title}          ne $title1 )          { $failure = $F1; }
66
if ($hashref_check->{new}            ne $new1)            { $failure = 1; }
91
if ( $hashref_check->{new}            ne $new1 )            { $failure = $F2; }
67
if ($hashref_check->{lang}           ne $lang1)           { $failure = 1; }
92
if ( $hashref_check->{lang}           ne $lang1 )           { $failure = $F3; }
68
if ($hashref_check->{expirationdate} ne $expirationdate1) { $failure = 1; }
93
if ( $hashref_check->{expirationdate} ne $expirationdate1 ) { $failure = $F4; }
69
if ($hashref_check->{timestamp}      ne $timestamp1)      { $failure = 1; }
94
if ( $hashref_check->{timestamp}      ne $timestamp1 )      { $failure = $F5; }
70
if ($hashref_check->{number}         ne $number1)         { $failure = 1; }
95
if ( $hashref_check->{number}         ne $number1 )         { $failure = $F6; }
71
ok($failure==0,"Successfully tested get_opac_new id1!");
96
ok( $failure == 0, "Successfully tested get_opac_new id1 ($failure)!" );
72
97
73
# Test get_opac_new (single news item)
98
# Test get_opac_new (single news item)
74
$hashref_check = get_opac_new($idnew2);
99
$hashref_check = get_opac_new($idnew2);
75
$failure = 0;
100
$failure       = 0;
76
if ($hashref_check->{title}          ne $title2)          { $failure = 1; }
101
if ( $hashref_check->{title}          ne $title2 )          { $failure = $F1; }
77
if ($hashref_check->{new}            ne $new2)            { $failure = 1; }
102
if ( $hashref_check->{new}            ne $new2 )            { $failure = $F2; }
78
if ($hashref_check->{lang}           ne $lang2)           { $failure = 1; }
103
if ( $hashref_check->{lang}           ne $lang2 )           { $failure = $F3; }
79
if ($hashref_check->{expirationdate} ne $expirationdate2) { $failure = 1; }
104
if ( $hashref_check->{expirationdate} ne $expirationdate2 ) { $failure = $F4; }
80
if ($hashref_check->{timestamp}      ne $timestamp2)      { $failure = 1; }
105
if ( $hashref_check->{timestamp}      ne $timestamp2 )      { $failure = $F5; }
81
if ($hashref_check->{number}         ne $number2)         { $failure = 1; }
106
if ( $hashref_check->{number}         ne $number2 )         { $failure = $F6; }
82
ok($failure==0,"Successfully tested get_opac_new id2!");
107
ok( $failure == 0, "Successfully tested get_opac_new id2 ($failure)!" );
83
108
84
# Test get_opac_news (multiple news items)
109
# Test get_opac_news (multiple news items)
85
my ($opac_news_count, $arrayref_opac_news) = get_opac_news(0,'');
110
my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{} );
86
ok($opac_news_count>=2,"Successfully tested get_opac_news!");
111
ok( $opac_news_count >= 2, 'Successfully tested get_opac_news!' );
87
112
88
# Test GetNewsToDisplay
113
# Test GetNewsToDisplay
89
($opac_news_count, $arrayref_opac_news) = GetNewsToDisplay('');
114
( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay(q{});
90
ok($opac_news_count>=2,"Successfully tested GetNewsToDisplay!");
115
ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay!' );
91
116
92
$dbh->rollback;
117
$dbh->rollback;
(-)a/tools/koha-news.pl (-3 / +19 lines)
Lines 87-97 if ( $op eq 'add_form' ) { Link Here
87
    }
87
    }
88
}
88
}
89
elsif ( $op eq 'add' ) {
89
elsif ( $op eq 'add' ) {
90
    add_opac_new( $title, $new, $lang, $expirationdate, $timestamp, $number );
90
    my $parameters = {
91
                      title          => $title,
92
                      new            => $new,
93
                      lang           => $lang,
94
                      expirationdate => $expirationdate,
95
                      timestamp      => $timestamp,
96
                      number         => $number,
97
                  };
98
    add_opac_new( $parameters );
91
    print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
99
    print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
92
}
100
}
93
elsif ( $op eq 'edit' ) {
101
elsif ( $op eq 'edit' ) {
94
    upd_opac_new( $id, $title, $new, $lang, $expirationdate, $timestamp ,$number );
102
    my $parameters = {
103
                      idnew          => $id,
104
                      title          => $title,
105
                      new            => $new,
106
                      lang           => $lang,
107
                      expirationdate => $expirationdate,
108
                      timestamp      => $timestamp,
109
                      number         => $number,
110
                  };
111
    upd_opac_new( $parameters );
95
    print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
112
    print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
96
}
113
}
97
elsif ( $op eq 'del' ) {
114
elsif ( $op eq 'del' ) {
98
- 

Return to bug 7567