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 123-168 sub del_opac_new { Link Here
123
    }
123
    }
124
}
124
}
125
125
126
sub get_opac_news {
127
    my ($limit, $lang, $branchcode) = @_;
128
    my @values;
129
    my $dbh = C4::Context->dbh;
130
    my $query = q{
131
                  SELECT opac_news.*, branches.branchname,
132
                         published_on AS newdate,
133
                         borrowers.title AS author_title,
134
                         borrowers.firstname AS author_firstname,
135
                         borrowers.surname AS author_surname
136
                  FROM opac_news LEFT JOIN branches
137
                      ON opac_news.branchcode=branches.branchcode
138
                  LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
139
                };
140
    $query .= ' WHERE 1';
141
    if ($lang) {
142
        $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
143
        push @values,$lang;
144
    }
145
    if ($branchcode) {
146
        $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
147
        push @values,$branchcode;
148
    }
149
    $query.= ' ORDER BY published_on DESC ';
150
    #if ($limit) {
151
    #    $query.= 'LIMIT 0, ' . $limit;
152
    #}
153
    my $sth = $dbh->prepare($query);
154
    $sth->execute(@values);
155
    my @opac_news;
156
    my $count = 0;
157
    while (my $row = $sth->fetchrow_hashref) {
158
        if ((($limit) && ($count < $limit)) || (!$limit)) {
159
            push @opac_news, $row;
160
        }
161
        $count++;
162
    }
163
    return ($count, \@opac_news);
164
}
165
166
=head2 GetNewsToDisplay
126
=head2 GetNewsToDisplay
167
127
168
    $news = &GetNewsToDisplay($lang,$branch);
128
    $news = &GetNewsToDisplay($lang,$branch);
(-)a/Koha/NewsItem.pm (+35 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;
25
use Koha::Patrons;
27
use Koha::Patrons;
26
28
27
use base qw(Koha::Object);
29
use base qw(Koha::Object);
Lines 53-58 sub author { Link Here
53
    return Koha::Patron->_new_from_dbic($author_rs);
55
    return Koha::Patron->_new_from_dbic($author_rs);
54
}
56
}
55
57
58
=head3 is_expired
59
60
my $is_expired = $news_item->is_expired;
61
62
Returns 1 if the news item is expired or 0;
63
64
=cut
65
66
sub is_expired {
67
    my ( $self ) = @_;
68
69
    return 0 unless $self->expirationdate;
70
    return 1 if dt_from_string( $self->expirationdate ) < dt_from_string->truncate( to => 'day' );
71
    return 0;
72
}
73
74
=head3 library
75
76
my $library = $news_item->library;
77
78
Returns Koha::Library object or undef
79
80
=cut
81
82
sub library {
83
    my ( $self ) = @_;
84
85
    my $library_rs = $self->_result->branchcode;
86
    return unless $library_rs;
87
    return Koha::Library->_new_from_dbic( $library_rs );
88
}
89
90
56
=head3 _type
91
=head3 _type
57
92
58
=cut
93
=cut
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/koha-news.tt (-9 / +7 lines)
Lines 7-14 Link Here
7
[% INCLUDE 'doc-head-open.inc' %]
7
[% INCLUDE 'doc-head-open.inc' %]
8
<title>Koha &rsaquo; Tools &rsaquo; News</title>
8
<title>Koha &rsaquo; Tools &rsaquo; News</title>
9
[% INCLUDE 'doc-head-close.inc' %]
9
[% INCLUDE 'doc-head-close.inc' %]
10
[% IF ( opac_news_count ) %]
11
[% END %]
12
[% UNLESS ( wysiwyg ) %]
10
[% UNLESS ( wysiwyg ) %]
13
    [% Asset.css("lib/codemirror/codemirror.css") | $raw %]
11
    [% Asset.css("lib/codemirror/codemirror.css") | $raw %]
14
    [% Asset.css("lib/codemirror/lint.min.css") | $raw %]
12
    [% Asset.css("lib/codemirror/lint.min.css") | $raw %]
Lines 111-117 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
111
                <fieldset class="action"><input class="button" type="submit" value="Submit" /> <a class="cancel" href="/cgi-bin/koha/tools/koha-news.pl">Cancel</a></fieldset>
109
                <fieldset class="action"><input class="button" type="submit" value="Submit" /> <a class="cancel" href="/cgi-bin/koha/tools/koha-news.pl">Cancel</a></fieldset>
112
        </form>
110
        </form>
113
    [% ELSE %]
111
    [% ELSE %]
114
        [% IF ( opac_news_count ) %]
112
        [% IF ( opac_news.count ) %]
115
        <form id="del_form" method="post" action="/cgi-bin/koha/tools/koha-news.pl">
113
        <form id="del_form" method="post" action="/cgi-bin/koha/tools/koha-news.pl">
116
                <table id="newst">
114
                <table id="newst">
117
                   <thead> <tr>
115
                   <thead> <tr>
Lines 127-133 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
127
                        <th class="NoSort noExport">Actions</th>
125
                        <th class="NoSort noExport">Actions</th>
128
                    </tr></thead>
126
                    </tr></thead>
129
                    <tbody>[% FOREACH opac_new IN opac_news %]
127
                    <tbody>[% FOREACH opac_new IN opac_news %]
130
                         [% IF ( opac_new.expired ) %]
128
                         [% IF ( opac_new.is_expired ) %]
131
                            <tr class="expired">
129
                            <tr class="expired">
132
                            [% ELSE %]
130
                            [% ELSE %]
133
                            <tr>
131
                            <tr>
Lines 148-160 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
148
                             </td>
146
                             </td>
149
                            <td>[% IF ( opac_new.branchcode == "" ) -%]
147
                            <td>[% IF ( opac_new.branchcode == "" ) -%]
150
                                All libraries
148
                                All libraries
151
                                [% ELSE %][% opac_new.branchname | html %]
149
                                [% ELSE %][% opac_new.library.branchname | html %]
152
                                [% END %]</td>
150
                                [% END %]</td>
153
                            <td>[% opac_new.number | html %]</td>
151
                            <td>[% opac_new.number | html %]</td>
154
                            <td><span title="[% opac_new.newdate | html %]">[% opac_new.newdate | $KohaDates %]</span></td>
152
                            <td><span title="[% opac_new.timestamp | html %]">[% opac_new.timestamp | $KohaDates %]</span></td>
155
                            <td><span title="[% opac_new.expirationdate | html %]">[% opac_new.expirationdate | $KohaDates %] [% IF ( opac_new.expired ) %](<span class="expired">expired</span>)[% END %]</span></td>
153
                            <td><span title="[% opac_new.expirationdate | html %]">[% opac_new.expirationdate | $KohaDates %] [% IF ( opac_new.is_expired ) %](<span class="expired">expired</span>)[% END %]</span></td>
156
                            <td>[% opac_new.title | html %]</td>
154
                            <td>[% opac_new.title | html %]</td>
157
                            <td>[% opac_new.author_title | html %] [% opac_new.author_firstname | html %] [% opac_new.author_surname | html %]</td>
155
                            <td>[% IF ( opac_new.author) %][% INCLUDE 'patron-title.inc' patron=opac_new.author hide_patron_infos_if_needed=1 %][% END %]</td>
158
                            <td>
156
                            <td>
159
                                <div class="btn-group">
157
                                <div class="btn-group">
160
                                    <a class="preview_news btn btn-default btn-xs" data-number="[% loop.count | html %]"><i class="fa fa-eye" aria-hidden="true"></i> Preview content</a>
158
                                    <a class="preview_news btn btn-default btn-xs" data-number="[% loop.count | html %]"><i class="fa fa-eye" aria-hidden="true"></i> Preview content</a>
Lines 260-266 Edit news item[% ELSE %]Add news item[% END %][% ELSE %]News[% END %]</div> Link Here
260
[% MACRO jsinclude BLOCK %]
258
[% MACRO jsinclude BLOCK %]
261
    [% INCLUDE 'calendar.inc' %]
259
    [% INCLUDE 'calendar.inc' %]
262
    [% Asset.js("js/tools-menu.js") | $raw %]
260
    [% Asset.js("js/tools-menu.js") | $raw %]
263
    [% IF ( opac_news_count ) %]
261
    [% IF ( opac_news.count ) %]
264
        [% INCLUDE 'datatables.inc' %]
262
        [% INCLUDE 'datatables.inc' %]
265
        <script>
263
        <script>
266
            $('.preview_news').click( function() {
264
            $('.preview_news').click( function() {
(-)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 149-169 elsif ( $op eq 'del' ) { Link Here
149
}
149
}
150
150
151
else {
151
else {
152
152
    my $params;
153
    my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, undef, undef );
153
    $params->{lang} = $lang if $lang;
154
    
154
    my $opac_news = Koha::News->search(
155
    foreach my $new ( @$opac_news ) {
155
        $params,
156
        next unless $new->{'expirationdate'};
156
        {
157
        my @date = split (/-/,$new->{'expirationdate'});
157
            order_by => { -desc =>  'timestamp' },
158
        if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
159
			$new->{'expired'} = 1;
160
        }
158
        }
161
    }
159
    );
162
    
160
    $template->param( opac_news => $opac_news );
163
    $template->param(
164
        opac_news       => $opac_news,
165
        opac_news_count => $opac_news_count,
166
		);
167
}
161
}
168
$template->param(
162
$template->param(
169
    lang => $lang,
163
    lang => $lang,
170
- 

Return to bug 22544