Bug 17600: Standardize our EXPORT_OK
[srvgit] / 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 qw( get_template_and_user );
28 use C4::Context;
29 use C4::Output qw( output_html_with_http_headers );
30 use C4::Languages qw( getTranslatedLanguages );
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::News;
33
34 my $cgi = CGI->new;
35
36 my $id             = $cgi->param('id');
37 my $title          = $cgi->param('title');
38 my $content        = $cgi->param('content');
39 my $expirationdate;
40 if ( $cgi->param('expirationdate') ) {
41     $expirationdate = output_pref({ dt => dt_from_string( scalar $cgi->param('expirationdate') ), dateformat => 'iso', dateonly => 1 });
42 }
43 my $published_on= output_pref({ dt => dt_from_string( scalar $cgi->param('published_on') ), dateformat => 'iso', dateonly => 1 });
44 my $number         = $cgi->param('number');
45 my $lang           = $cgi->param('lang');
46 my $branchcode     = $cgi->param('branch');
47 my $error_message  = $cgi->param('error_message');
48 my $wysiwyg;
49 if( $cgi->param('editmode') ){
50     $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
51 } else {
52     $wysiwyg = C4::Context->preference("NewsToolEditor") eq "tinymce" ? 1 : 0;
53 }
54
55 # Foreign Key constraints work with NULL, not ''
56 # NULL = All branches.
57 $branchcode = undef if (defined($branchcode) && $branchcode eq '');
58
59 my $new_detail = Koha::News->find( $id );
60
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62     {
63         template_name   => "tools/koha-news.tt",
64         query           => $cgi,
65         type            => "intranet",
66         flagsrequired   => { tools => 'edit_news' },
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 && $new_detail->lang eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
83         };
84     }
85 }
86
87 $template->param( lang_list   => \@lang_list,
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         my $new = Koha::NewsItem->new({
109             title          => $title,
110             content        => $content,
111             lang           => $lang,
112             expirationdate => $expirationdate,
113             published_on   => $published_on,
114             number         => $number,
115             branchcode     => $branchcode,
116             borrowernumber => $borrowernumber,
117         })->store;
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     my $news_item = Koha::News->find( $id );
126     if ( $news_item ) {
127         $news_item->set({
128             title          => $title,
129             content        => $content,
130             lang           => $lang,
131             expirationdate => $expirationdate,
132             published_on=> $published_on,
133             number         => $number,
134             branchcode     => $branchcode,
135         })->store;
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     Koha::News->search({ idnew => @ids })->delete;
142     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
143 }
144
145 else {
146     my $params;
147     $params->{lang} = $lang if $lang;
148     my $opac_news = Koha::News->search(
149         $params,
150         {
151             order_by => { -desc =>  'published_on' },
152         }
153     );
154     $template->param( opac_news => $opac_news );
155 }
156 $template->param(
157     lang => $lang,
158     wysiwyg => $wysiwyg,
159 );
160 output_html_with_http_headers $cgi, $cookie, $template->output;