Bug 24807: Simplify with new and imporved value_callbacks
[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 Modern::Perl;
26 use CGI qw ( -utf8 );
27 use C4::Auth;
28 use C4::Koha;
29 use C4::Context;
30 use C4::Output;
31 use C4::NewsChannels;
32 use C4::Languages qw(getTranslatedLanguages);
33 use Date::Calc qw/Date_to_Days Today/;
34 use Koha::DateUtils;
35
36 my $cgi = new CGI;
37
38 my $id             = $cgi->param('id');
39 my $title          = $cgi->param('title');
40 my $content        = $cgi->param('content');
41 my $expirationdate;
42 if ( $cgi->param('expirationdate') ) {
43     $expirationdate = output_pref({ dt => dt_from_string( scalar $cgi->param('expirationdate') ), dateformat => 'iso', dateonly => 1 });
44 }
45 my $published_on= output_pref({ dt => dt_from_string( scalar $cgi->param('published_on') ), dateformat => 'iso', dateonly => 1 });
46 my $number         = $cgi->param('number');
47 my $lang           = $cgi->param('lang');
48 my $branchcode     = $cgi->param('branch');
49 my $error_message  = $cgi->param('error_message');
50
51 # Foreign Key constraints work with NULL, not ''
52 # NULL = All branches.
53 $branchcode = undef if (defined($branchcode) && $branchcode eq '');
54
55 my $new_detail = get_opac_new($id);
56
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58     {
59         template_name   => "tools/koha-news.tt",
60         query           => $cgi,
61         type            => "intranet",
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 $template->param( lang_list   => \@lang_list,
85                   branchcode  => $branchcode );
86
87 my $op = $cgi->param('op') // '';
88
89 if ( $op eq 'add_form' ) {
90     $template->param( add_form => 1 );
91     if ($id) {
92         if($new_detail->{lang} eq "slip"){ $template->param( slip => 1); }
93         $template->param( 
94             op => 'edit',
95             id => $new_detail->{'idnew'}
96         );
97         $template->{VARS}->{'new_detail'} = $new_detail;
98     }
99     else {
100         $template->param( op => 'add' );
101     }
102 }
103 elsif ( $op eq 'add' ) {
104     if ($title) {
105         add_opac_new(
106             {
107                 title          => $title,
108                 content        => $content,
109                 lang           => $lang,
110                 expirationdate => $expirationdate,
111                 published_on=> $published_on,
112                 number         => $number,
113                 branchcode     => $branchcode,
114                 borrowernumber => $borrowernumber,
115             }
116         );
117         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
118     }
119     else {
120         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl?error_message=title_missing");
121     }
122 }
123 elsif ( $op eq 'edit' ) {
124     upd_opac_new(
125         {
126             idnew          => $id,
127             title          => $title,
128             content        => $content,
129             lang           => $lang,
130             expirationdate => $expirationdate,
131             published_on=> $published_on,
132             number         => $number,
133             branchcode     => $branchcode,
134         }
135     );
136     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
137 }
138 elsif ( $op eq 'del' ) {
139     my @ids = $cgi->multi_param('ids');
140     del_opac_new( join ",", @ids );
141     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
142 }
143
144 else {
145
146     my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, undef, undef );
147     
148     foreach my $new ( @$opac_news ) {
149         next unless $new->{'expirationdate'};
150         my @date = split (/-/,$new->{'expirationdate'});
151         if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
152                         $new->{'expired'} = 1;
153         }
154     }
155     
156     $template->param(
157         opac_news       => $opac_news,
158         opac_news_count => $opac_news_count,
159                 );
160 }
161 $template->param( lang => $lang );
162 output_html_with_http_headers $cgi, $cookie, $template->output;