Bug 15023: Allow patron anonymize/bulk delete tool to be limited by branch
[koha.git] / tools / koha-news.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Script to manage the opac news.
6 # written 11/04
7 # Casta�eda, Carlos Sebastian - seba3c@yahoo.com.ar - Physics Library UNLP Argentina
8 # Modified to include news to KOHA intranet - tgarip@neu.edu.tr NEU library -Cyprus
9 # Copyright 2000-2002 Katipo Communications
10 # Copyright (C) 2013    Mark Tompsett
11 #
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24
25 use strict;
26 # use warnings; FIXME - Bug 2505
27 use CGI qw ( -utf8 );
28 use C4::Auth;
29 use C4::Koha;
30 use C4::Context;
31 use C4::Output;
32 use C4::NewsChannels;
33 use C4::Languages qw(getTranslatedLanguages);
34 use Date::Calc qw/Date_to_Days Today/;
35 use C4::Branch qw/GetBranches/;
36 use Koha::DateUtils;
37
38 my $cgi = new CGI;
39
40 my $id             = $cgi->param('id');
41 my $title          = $cgi->param('title');
42 my $new            = $cgi->param('new');
43 my $expirationdate;
44 if ( $cgi->param('expirationdate') ) {
45     $expirationdate = output_pref({ dt => dt_from_string( scalar $cgi->param('expirationdate') ), dateformat => 'iso', dateonly => 1 });
46 }
47 my $timestamp      = output_pref({ dt => dt_from_string( scalar $cgi->param('timestamp') ), dateformat => 'iso', dateonly => 1 });
48 my $number         = $cgi->param('number');
49 my $lang           = $cgi->param('lang');
50 my $branchcode     = $cgi->param('branch');
51 my $error_message  = $cgi->param('error_message');
52
53 # Foreign Key constraints work with NULL, not ''
54 # NULL = All branches.
55 $branchcode = undef if (defined($branchcode) && $branchcode eq '');
56
57 my $new_detail = get_opac_new($id);
58
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60     {
61         template_name   => "tools/koha-news.tt",
62         query           => $cgi,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { tools => 'edit_news' },
66         debug           => 1,
67     }
68 );
69
70 # Pass error message if there is one.
71 $template->param( error_message => $error_message ) if $error_message;
72
73 # get lang list
74 my @lang_list;
75 my $tlangs = getTranslatedLanguages() ;
76
77 foreach my $language ( @$tlangs ) {
78     foreach my $sublanguage ( @{$language->{'sublanguages_loop'}} ) {
79         push @lang_list,
80         {
81             language => $sublanguage->{'rfc4646_subtag'},
82             selected => ( $new_detail->{lang} eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
83         };
84     }
85 }
86
87 my $branches = GetBranches;
88
89 $template->param( lang_list   => \@lang_list,
90                   branch_list => $branches,
91                   branchcode  => $branchcode );
92
93 my $op = $cgi->param('op') // '';
94
95 if ( $op eq 'add_form' ) {
96     $template->param( add_form => 1 );
97     if ($id) {
98         if($new_detail->{lang} eq "slip"){ $template->param( slip => 1); }
99         $template->param( 
100             op => 'edit',
101             id => $new_detail->{'idnew'}
102         );
103         $template->{VARS}->{'new_detail'} = $new_detail;
104     }
105     else {
106         $template->param( op => 'add' );
107     }
108 }
109 elsif ( $op eq 'add' ) {
110     if ($title) {
111         add_opac_new(
112             {
113                 title          => $title,
114                 new            => $new,
115                 lang           => $lang,
116                 expirationdate => $expirationdate,
117                 timestamp      => $timestamp,
118                 number         => $number,
119                 branchcode     => $branchcode,
120                 borrowernumber => $borrowernumber,
121             }
122         );
123         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
124     }
125     else {
126         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl?error_message=title_missing");
127     }
128 }
129 elsif ( $op eq 'edit' ) {
130     upd_opac_new(
131         {
132             idnew          => $id,
133             title          => $title,
134             new            => $new,
135             lang           => $lang,
136             expirationdate => $expirationdate,
137             timestamp      => $timestamp,
138             number         => $number,
139             branchcode     => $branchcode,
140         }
141     );
142     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
143 }
144 elsif ( $op eq 'del' ) {
145     my @ids = $cgi->multi_param('ids');
146     del_opac_new( join ",", @ids );
147     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
148 }
149
150 else {
151
152     my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang, $branchcode );
153     
154     foreach my $new ( @$opac_news ) {
155         next unless $new->{'expirationdate'};
156         my @date = split (/-/,$new->{'expirationdate'});
157         if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
158                         $new->{'expired'} = 1;
159         }
160     }
161     
162     $template->param(
163         opac_news       => $opac_news,
164         opac_news_count => $opac_news_count,
165                 );
166 }
167 $template->param( lang => $lang );
168 output_html_with_http_headers $cgi, $cookie, $template->output;