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

(-)a/C4/NewsChannels.pm (-41 / +1 lines)
Lines 28-34 BEGIN { Link Here
28
    @ISA = qw(Exporter);
28
    @ISA = qw(Exporter);
29
    @EXPORT = qw(
29
    @EXPORT = qw(
30
        &GetNewsToDisplay
30
        &GetNewsToDisplay
31
        &add_opac_new &upd_opac_new &del_opac_new &get_opac_news
31
        &add_opac_new &upd_opac_new &del_opac_new
32
    );
32
    );
33
}
33
}
34
34
Lines 121-166 sub del_opac_new { Link Here
121
    }
121
    }
122
}
122
}
123
123
124
sub get_opac_news {
125
    my ($limit, $lang, $branchcode) = @_;
126
    my @values;
127
    my $dbh = C4::Context->dbh;
128
    my $query = q{
129
                  SELECT opac_news.*, branches.branchname,
130
                         timestamp AS newdate,
131
                         borrowers.title AS author_title,
132
                         borrowers.firstname AS author_firstname,
133
                         borrowers.surname AS author_surname
134
                  FROM opac_news LEFT JOIN branches
135
                      ON opac_news.branchcode=branches.branchcode
136
                  LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
137
                };
138
    $query .= ' WHERE 1';
139
    if ($lang) {
140
        $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
141
        push @values,$lang;
142
    }
143
    if ($branchcode) {
144
        $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
145
        push @values,$branchcode;
146
    }
147
    $query.= ' ORDER BY timestamp DESC ';
148
    #if ($limit) {
149
    #    $query.= 'LIMIT 0, ' . $limit;
150
    #}
151
    my $sth = $dbh->prepare($query);
152
    $sth->execute(@values);
153
    my @opac_news;
154
    my $count = 0;
155
    while (my $row = $sth->fetchrow_hashref) {
156
        if ((($limit) && ($count < $limit)) || (!$limit)) {
157
            push @opac_news, $row;
158
        }
159
        $count++;
160
    }
161
    return ($count, \@opac_news);
162
}
163
164
=head2 GetNewsToDisplay
124
=head2 GetNewsToDisplay
165
125
166
    $news = &GetNewsToDisplay($lang,$branch);
126
    $news = &GetNewsToDisplay($lang,$branch);
(-)a/Koha/NewsItem.pm (+51 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::DateUtils;
26
use Koha::Libraries;
27
use Koha::Patrons;
25
28
26
use base qw(Koha::Object);
29
use base qw(Koha::Object);
27
30
Lines 37-42 Koha::NewsItem represents a single piece of news from the opac_news table Link Here
37
40
38
=cut
41
=cut
39
42
43
=head3 is_expired
44
45
my $is_expired = $news_item->is_expired;
46
47
Returns 1 if the news item is expired or 0;
48
49
=cut
50
51
sub is_expired {
52
    my ( $self ) = @_;
53
54
    return 0 unless $self->expirationdate;
55
    return 1 if dt_from_string( $self->expirationdate ) < dt_from_string->truncate( to => 'day' );
56
    return 0;
57
}
58
59
=head3 library
60
61
my $library = $news_item->library;
62
63
Returns Koha::Library object or undef
64
65
=cut
66
67
sub library {
68
    my ( $self ) = @_;
69
70
    my $library_rs = $self->_result->branchcode;
71
    return unless $library_rs;
72
    return Koha::Library->_new_from_dbic( $library_rs );
73
}
74
75
=head3 author
76
77
my $patron = $news_item->author;
78
79
Returns Koha::Patron object or undef
80
81
=cut
82
83
sub author {
84
    my ( $self ) = @_;
85
86
    my $author_rs = $self->_result->borrowernumber;
87
    return unless $author_rs;
88
    return Koha::Patron->_new_from_dbic( $author_rs );
89
}
90
40
=head3 type
91
=head3 type
41
92
42
=cut
93
=cut
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt (-9 / +7 lines)
Lines 6-13 Link Here
6
[% INCLUDE 'doc-head-open.inc' %]
6
[% INCLUDE 'doc-head-open.inc' %]
7
<title>Koha &rsaquo; Tools &rsaquo; News</title>
7
<title>Koha &rsaquo; Tools &rsaquo; News</title>
8
[% INCLUDE 'doc-head-close.inc' %]
8
[% INCLUDE 'doc-head-close.inc' %]
9
[% IF ( opac_news_count ) %]
10
[% END %]
11
</head>
9
</head>
12
10
13
<body id="tools_koha-news" class="tools">
11
<body id="tools_koha-news" class="tools">
Lines 100-106 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
100
                <fieldset class="action"><input class="button" type="submit" value="Submit" /> <a class="cancel" href="/cgi-bin/koha/tools/koha-news.pl">Cancel</a></fieldset>
98
                <fieldset class="action"><input class="button" type="submit" value="Submit" /> <a class="cancel" href="/cgi-bin/koha/tools/koha-news.pl">Cancel</a></fieldset>
101
        </form>
99
        </form>
102
    [% ELSE %]
100
    [% ELSE %]
103
        [% IF ( opac_news_count ) %]
101
        [% IF ( opac_news.count ) %]
104
        <form id="del_form" method="post" action="/cgi-bin/koha/tools/koha-news.pl">
102
        <form id="del_form" method="post" action="/cgi-bin/koha/tools/koha-news.pl">
105
                <table id="newst">
103
                <table id="newst">
106
                   <thead> <tr>
104
                   <thead> <tr>
Lines 116-122 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
116
                        <th class="NoSort">Actions</th>
114
                        <th class="NoSort">Actions</th>
117
                    </tr></thead>
115
                    </tr></thead>
118
                    <tbody>[% FOREACH opac_new IN opac_news %]
116
                    <tbody>[% FOREACH opac_new IN opac_news %]
119
                         [% IF ( opac_new.expired ) %]
117
                         [% IF ( opac_new.is_expired ) %]
120
                            <tr class="expired">
118
                            <tr class="expired">
121
                            [% ELSE %]
119
                            [% ELSE %]
122
                            <tr>
120
                            <tr>
Lines 137-149 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
137
                             </td>
135
                             </td>
138
                            <td>[% IF ( opac_new.branchcode == "" ) -%]
136
                            <td>[% IF ( opac_new.branchcode == "" ) -%]
139
                                All libraries
137
                                All libraries
140
                                [% ELSE %][% opac_new.branchname | html %]
138
                                [% ELSE %][% opac_new.library.branchname | html %]
141
                                [% END %]</td>
139
                                [% END %]</td>
142
                            <td>[% opac_new.number | html %]</td>
140
                            <td>[% opac_new.number | html %]</td>
143
                            <td><span title="[% opac_new.newdate | html %]">[% opac_new.newdate | $KohaDates %]</span></td>
141
                            <td><span title="[% opac_new.timestamp | html %]">[% opac_new.timestamp | $KohaDates %]</span></td>
144
                            <td><span title="[% opac_new.expirationdate | html %]">[% opac_new.expirationdate | $KohaDates %] [% IF ( opac_new.expired ) %](<span class="expired">expired</span>)[% END %]</span></td>
142
                            <td><span title="[% opac_new.expirationdate | html %]">[% opac_new.expirationdate | $KohaDates %] [% IF ( opac_new.is_expired ) %](<span class="expired">expired</span>)[% END %]</span></td>
145
                            <td>[% opac_new.title | html %]</td>
143
                            <td>[% opac_new.title | html %]</td>
146
                            <td>[% opac_new.author_title | html %] [% opac_new.author_firstname | html %] [% opac_new.author_surname | html %]</td>
144
                            <td>[% IF ( opac_new.author) %][% INCLUDE 'patron-title.inc' patron=opac_new.author hide_patron_infos_if_needed=1 %][% END %]</td>
147
                           <td>
145
                           <td>
148
                                [% opac_new.content | $raw %]
146
                                [% opac_new.content | $raw %]
149
                            </td>
147
                            </td>
Lines 206-212 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
206
[% MACRO jsinclude BLOCK %]
204
[% MACRO jsinclude BLOCK %]
207
    [% INCLUDE 'calendar.inc' %]
205
    [% INCLUDE 'calendar.inc' %]
208
    [% Asset.js("js/tools-menu.js") | $raw %]
206
    [% Asset.js("js/tools-menu.js") | $raw %]
209
    [% IF ( opac_news_count ) %]
207
    [% IF ( opac_news.count ) %]
210
        [% INCLUDE 'datatables.inc' %]
208
        [% INCLUDE 'datatables.inc' %]
211
        <script>
209
        <script>
212
            function Checkbox(){
210
            function Checkbox(){
(-)a/t/db_dependent/NewsChannels.t (-38 / +3 lines)
Lines 4-11 use Modern::Perl; Link Here
4
use Koha::Database;
4
use Koha::Database;
5
use Koha::DateUtils;
5
use Koha::DateUtils;
6
use Koha::Libraries;
6
use Koha::Libraries;
7
use Koha::News;
7
8
8
use Test::More tests => 11;
9
use Test::More tests => 9;
9
10
10
BEGIN {
11
BEGIN {
11
    use_ok('C4::NewsChannels');
12
    use_ok('C4::NewsChannels');
Lines 134-176 $href_entry2->{idnew} = $idnew2; Link Here
134
$rv                   = upd_opac_new($href_entry2);
135
$rv                   = upd_opac_new($href_entry2);
135
is( $rv, 1, 'Successfully updated second dummy news item!' );
136
is( $rv, 1, 'Successfully updated second dummy news item!' );
136
137
137
# Test get_opac_news (multiple news items)
138
my ( $opac_news_count, $arrayref_opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
139
140
# using >= 2, because someone may have LIB1 news already.
141
ok( $opac_news_count >= 2, 'Successfully tested get_opac_news for LIB1!' );
142
143
# Test GetNewsToDisplay
138
# Test GetNewsToDisplay
144
( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
139
my ( $opac_news_count, $arrayref_opac_news ) = GetNewsToDisplay( q{}, 'LIB1' );
145
ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
140
ok( $opac_news_count >= 2, 'Successfully tested GetNewsToDisplay for LIB1!' );
146
141
147
# Regression test 14248 -- make sure author_title, author_firstname, and
148
# author_surname exist.
149
150
subtest 'Regression tests on author title, firstname, and surname.', sub {
151
    my ( $opac_news_count, $opac_news ) = get_opac_news( 0, q{}, 'LIB1' );
152
    my $check = 0; # bitwise flag to confirm NULL and not NULL borrowernumber.
153
    ok($opac_news_count>0,'Data exists for regression testing');
154
    foreach my $news_item (@$opac_news) {
155
        ok(exists $news_item->{author_title},    'Author title exists');
156
        ok(exists $news_item->{author_firstname},'Author first name exists');
157
        ok(exists $news_item->{author_surname},  'Author surname exists');
158
        if ($news_item->{borrowernumber}) {
159
            ok(defined $news_item->{author_title} ||
160
               defined $news_item->{author_firstname} ||
161
               defined $news_item->{author_surname},  'Author data defined');
162
            $check = $check | 2; # bitwise flag;
163
        }
164
        else {
165
            ok(!defined $news_item->{author_title},
166
               'Author title undefined as expected');
167
            ok(!defined $news_item->{author_firstname},
168
               'Author first name undefined as expected');
169
            ok(!defined $news_item->{author_surname},
170
               'Author surname undefined as expected');
171
            $check = $check | 1; # bitwise flag;
172
        }
173
    }
174
    ok($check==3,'Both with and without author data tested');
175
    done_testing();
176
};
(-)a/tools/koha-news.pl (-15 / +8 lines)
Lines 144-164 elsif ( $op eq 'del' ) { Link Here
144
}
144
}
145
145
146
else {
146
else {
147
147
    my $params;
148
    my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, undef, undef );
148
    $params->{lang} = $lang if $lang;
149
    
149
    my $opac_news = Koha::News->search(
150
    foreach my $new ( @$opac_news ) {
150
        $params,
151
        next unless $new->{'expirationdate'};
151
        {
152
        my @date = split (/-/,$new->{'expirationdate'});
152
            order_by => { -desc =>  'timestamp' },
153
        if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
154
			$new->{'expired'} = 1;
155
        }
153
        }
156
    }
154
    );
157
    
155
    $template->param( opac_news => $opac_news );
158
    $template->param(
159
        opac_news       => $opac_news,
160
        opac_news_count => $opac_news_count,
161
		);
162
}
156
}
163
$template->param( lang => $lang );
157
$template->param( lang => $lang );
164
output_html_with_http_headers $cgi, $cookie, $template->output;
158
output_html_with_http_headers $cgi, $cookie, $template->output;
165
- 

Return to bug 22544