Bug 21579: Make showdiffmarc.pl work for authorities and biblios
[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 $timestamp      = output_pref({ dt => dt_from_string( scalar $cgi->param('timestamp') ), 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         authnotrequired => 0,
63         flagsrequired   => { tools => 'edit_news' },
64         debug           => 1,
65     }
66 );
67
68 # Pass error message if there is one.
69 $template->param( error_message => $error_message ) if $error_message;
70
71 # get lang list
72 my @lang_list;
73 my $tlangs = getTranslatedLanguages() ;
74
75 foreach my $language ( @$tlangs ) {
76     foreach my $sublanguage ( @{$language->{'sublanguages_loop'}} ) {
77         push @lang_list,
78         {
79             language => $sublanguage->{'rfc4646_subtag'},
80             selected => ( $new_detail->{lang} eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
81         };
82     }
83 }
84
85 $template->param( lang_list   => \@lang_list,
86                   branchcode  => $branchcode );
87
88 my $op = $cgi->param('op') // '';
89
90 if ( $op eq 'add_form' ) {
91     $template->param( add_form => 1 );
92     if ($id) {
93         if($new_detail->{lang} eq "slip"){ $template->param( slip => 1); }
94         $template->param( 
95             op => 'edit',
96             id => $new_detail->{'idnew'}
97         );
98         $template->{VARS}->{'new_detail'} = $new_detail;
99     }
100     else {
101         $template->param( op => 'add' );
102     }
103 }
104 elsif ( $op eq 'add' ) {
105     if ($title) {
106         add_opac_new(
107             {
108                 title          => $title,
109                 content        => $content,
110                 lang           => $lang,
111                 expirationdate => $expirationdate,
112                 timestamp      => $timestamp,
113                 number         => $number,
114                 branchcode     => $branchcode,
115                 borrowernumber => $borrowernumber,
116             }
117         );
118         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
119     }
120     else {
121         print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl?error_message=title_missing");
122     }
123 }
124 elsif ( $op eq 'edit' ) {
125     upd_opac_new(
126         {
127             idnew          => $id,
128             title          => $title,
129             content        => $content,
130             lang           => $lang,
131             expirationdate => $expirationdate,
132             timestamp      => $timestamp,
133             number         => $number,
134             branchcode     => $branchcode,
135         }
136     );
137     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
138 }
139 elsif ( $op eq 'del' ) {
140     my @ids = $cgi->multi_param('ids');
141     del_opac_new( join ",", @ids );
142     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
143 }
144
145 else {
146
147     my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang, $branchcode );
148     
149     foreach my $new ( @$opac_news ) {
150         next unless $new->{'expirationdate'};
151         my @date = split (/-/,$new->{'expirationdate'});
152         if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
153                         $new->{'expired'} = 1;
154         }
155     }
156     
157     $template->param(
158         opac_news       => $opac_news,
159         opac_news_count => $opac_news_count,
160                 );
161 }
162 $template->param( lang => $lang );
163 output_html_with_http_headers $cgi, $cookie, $template->output;