Bug 16154: CGI->multi_param - Assign a list
[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 = output_pref({ dt => dt_from_string( $cgi->param('expirationdate') ), dateformat => 'iso', dateonly => 1 });
44 my $timestamp      = output_pref({ dt => dt_from_string( $cgi->param('timestamp') ), dateformat => 'iso', dateonly => 1 });
45 my $number         = $cgi->param('number');
46 my $lang           = $cgi->param('lang');
47 my $branchcode     = $cgi->param('branch');
48 my $error_message  = $cgi->param('error_message');
49
50 # Foreign Key constraints work with NULL, not ''
51 # NULL = All branches.
52 $branchcode = undef if (defined($branchcode) && $branchcode eq '');
53
54 my $new_detail = get_opac_new($id);
55
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57     {
58         template_name   => "tools/koha-news.tt",
59         query           => $cgi,
60         type            => "intranet",
61         authnotrequired => 0,
62         flagsrequired   => { tools => 'edit_news' },
63         debug           => 1,
64     }
65 );
66
67 # Pass error message if there is one.
68 $template->param( error_message => $error_message ) if $error_message;
69
70 # get lang list
71 my @lang_list;
72 my $tlangs = getTranslatedLanguages() ;
73
74 foreach my $language ( @$tlangs ) {
75     foreach my $sublanguage ( @{$language->{'sublanguages_loop'}} ) {
76         push @lang_list,
77         {
78             language => $sublanguage->{'rfc4646_subtag'},
79             selected => ( $new_detail->{lang} eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
80         };
81     }
82 }
83
84 my $branches = GetBranches;
85
86 $template->param( lang_list   => \@lang_list,
87                   branch_list => $branches,
88                   branchcode  => $branchcode );
89
90 my $op = $cgi->param('op') // '';
91
92 if ( $op eq 'add_form' ) {
93     $template->param( add_form => 1 );
94     if ($id) {
95         if($new_detail->{lang} eq "slip"){ $template->param( slip => 1); }
96         $template->param( 
97             op => 'edit',
98             id => $new_detail->{'idnew'}
99         );
100         $template->{VARS}->{'new_detail'} = $new_detail;
101     }
102     else {
103         $template->param( op => 'add' );
104     }
105 }
106 elsif ( $op eq 'add' ) {
107     if ($title) {
108         add_opac_new(
109             {
110                 title          => $title,
111                 new            => $new,
112                 lang           => $lang,
113                 expirationdate => $expirationdate,
114                 timestamp      => $timestamp,
115                 number         => $number,
116                 branchcode     => $branchcode,
117                 borrowernumber => $borrowernumber,
118             }
119         );
120         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
121     }
122     else {
123         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl?error_message=title_missing");
124     }
125 }
126 elsif ( $op eq 'edit' ) {
127     upd_opac_new(
128         {
129             idnew          => $id,
130             title          => $title,
131             new            => $new,
132             lang           => $lang,
133             expirationdate => $expirationdate,
134             timestamp      => $timestamp,
135             number         => $number,
136             branchcode     => $branchcode,
137         }
138     );
139     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
140 }
141 elsif ( $op eq 'del' ) {
142     my @ids = $cgi->multi_param('ids');
143     del_opac_new( join ",", @ids );
144     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
145 }
146
147 else {
148
149     my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang, $branchcode );
150     
151     foreach my $new ( @$opac_news ) {
152         next unless $new->{'expirationdate'};
153         my @date = split (/-/,$new->{'expirationdate'});
154         if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
155                         $new->{'expired'} = 1;
156         }
157     }
158     
159     $template->param(
160         opac_news       => $opac_news,
161         opac_news_count => $opac_news_count,
162                 );
163 }
164 $template->param( lang => $lang );
165 output_html_with_http_headers $cgi, $cookie, $template->output;