Bug 22544: Move GetNewsToDisplay to Koha namespace
[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::Languages qw(getTranslatedLanguages);
32 use Date::Calc qw/Date_to_Days Today/;
33 use Koha::DateUtils;
34 use Koha::News;
35
36 my $cgi = CGI->new;
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 my $wysiwyg;
51 if( $cgi->param('editmode') ){
52     $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
53 } else {
54     $wysiwyg = C4::Context->preference("NewsToolEditor") eq "tinymce" ? 1 : 0;
55 }
56
57 # Foreign Key constraints work with NULL, not ''
58 # NULL = All branches.
59 $branchcode = undef if (defined($branchcode) && $branchcode eq '');
60
61 my $new_detail = Koha::News->find( $id );
62
63 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
64     {
65         template_name   => "tools/koha-news.tt",
66         query           => $cgi,
67         type            => "intranet",
68         flagsrequired   => { tools => 'edit_news' },
69     }
70 );
71
72 # Pass error message if there is one.
73 $template->param( error_message => $error_message ) if $error_message;
74
75 # get lang list
76 my @lang_list;
77 my $tlangs = getTranslatedLanguages() ;
78
79 foreach my $language ( @$tlangs ) {
80     foreach my $sublanguage ( @{$language->{'sublanguages_loop'}} ) {
81         push @lang_list,
82         {
83             language => $sublanguage->{'rfc4646_subtag'},
84             selected => ( $new_detail && $new_detail->lang eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
85         };
86     }
87 }
88
89 $template->param( lang_list   => \@lang_list,
90                   branchcode  => $branchcode );
91
92 my $op = $cgi->param('op') // '';
93
94 if ( $op eq 'add_form' ) {
95     $template->param( add_form => 1 );
96     if ($id) {
97         if($new_detail->lang eq "slip"){ $template->param( slip => 1); }
98         $template->param( 
99             op => 'edit',
100             id => $new_detail->idnew
101         );
102         $template->{VARS}->{'new_detail'} = $new_detail;
103     }
104     else {
105         $template->param( op => 'add' );
106     }
107 }
108 elsif ( $op eq 'add' ) {
109     if ($title) {
110         my $new = Koha::NewsItem->new({
111             title          => $title,
112             content        => $content,
113             lang           => $lang,
114             expirationdate => $expirationdate,
115             timestamp      => $timestamp,
116             number         => $number,
117             branchcode     => $branchcode,
118             borrowernumber => $borrowernumber,
119         })->store;
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     my $news_item = Koha::News->find( $id );
128     if ( $news_item ) {
129         $news_item->set({
130             title          => $title,
131             content        => $content,
132             lang           => $lang,
133             expirationdate => $expirationdate,
134             published_on=> $published_on,
135             number         => $number,
136             branchcode     => $branchcode,
137         })->store;
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     Koha::News->search({ idnew => @ids })->delete;
144     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
145 }
146
147 else {
148     my $params;
149     $params->{lang} = $lang if $lang;
150     my $opac_news = Koha::News->search(
151         $params,
152         {
153             order_by => { -desc =>  'timestamp' },
154         }
155     );
156     $template->param( opac_news => $opac_news );
157 }
158 $template->param(
159     lang => $lang,
160     wysiwyg => $wysiwyg,
161 );
162 output_html_with_http_headers $cgi, $cookie, $template->output;