Bug 36328: Add p span div to Scrubber
[koha.git] / tools / additional-contents.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 Try::Tiny;
28 use Array::Utils qw( array_minus );
29 use C4::Auth qw(get_template_and_user);
30 use C4::Koha;
31 use C4::Context;
32 use C4::Log qw( logaction );
33 use C4::Output qw(output_html_with_http_headers output_and_exit_if_error);
34 use C4::Languages qw(getTranslatedLanguages);
35 use Koha::DateUtils qw( dt_from_string output_pref );
36
37 use Koha::AdditionalContents;
38
39 my $cgi = CGI->new;
40
41 my $op             = $cgi->param('op') || 'list';
42 my $id             = $cgi->param('id');
43 my $category       = $cgi->param('category') || 'news';
44 my $wysiwyg;
45 my $redirect       = $cgi->param('redirect');
46 my $editmode;
47
48 if( $cgi->param('editmode') ){
49     $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
50 } else {
51     $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
52 }
53
54 $editmode = $wysiwyg eq 1 ? "wysiwyg" : "text";
55
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57     {
58         template_name   => "tools/additional-contents.tt",
59         query           => $cgi,
60         type            => "intranet",
61         flagsrequired   => { tools => 'edit_additional_contents' },
62     }
63 );
64
65 my @messages;
66 if ( $op eq 'add_form' ) {
67
68     my $additional_content = Koha::AdditionalContents->find($id);
69     my $translated_contents;
70     if ($additional_content) {
71         $translated_contents = { map { $_->lang => $_ } $additional_content->translated_contents->as_list };
72         $category            = $additional_content->category;
73     }
74     $template->param(
75         additional_content => $additional_content,
76         translated_contents => $translated_contents,
77     );
78 }
79 elsif ( $op eq 'add_validate' ) {
80     output_and_exit_if_error( $cgi, $cookie, $template, { check => 'csrf_token' } );
81     my $location   = $cgi->param('location');
82     my $code       = $cgi->param('code');
83     my $branchcode = $cgi->param('branchcode') || undef;
84
85     my @lang       = $cgi->multi_param('lang');
86
87     my $expirationdate = $cgi->param('expirationdate');
88     my $published_on = $cgi->param('published_on');
89     my $number = $cgi->param('number');
90
91     try {
92         Koha::Database->new->schema->txn_do(
93             sub {
94                 my $additional_content;
95                 my $params = {
96                     location       => $location,
97                     branchcode     => $branchcode,
98                     expirationdate => $expirationdate,
99                     published_on   => $published_on,
100                     number         => $number,
101                     borrowernumber => $borrowernumber,
102                 };
103
104                 if ($id) {
105                     $additional_content = Koha::AdditionalContents->find($id);
106                     $additional_content->set($params)->store;
107                 } else {
108                     $additional_content = Koha::AdditionalContent->new(
109                         {
110                             category   => $category,
111                             code       => $code,
112                             branchcode => $branchcode,
113                             %$params,
114                         }
115                     )->store;
116                 }
117                 unless ($code) {
118                     $additional_content->discard_changes;
119                     $code =
120                         $category eq 'news'
121                         ? 'News_' . $additional_content->id
122                         : $location . '_' . $additional_content->id;
123                     $additional_content->code($code)->store;
124                     $id = $additional_content->id;
125                 }
126                 my @translated_contents;
127                 my $existing_contents = $additional_content->translated_contents;
128                 my @seen_ids;
129                 for my $lang (@lang) {
130                     my $id      = $cgi->param( 'id_' . $lang );
131                     my $title   = $cgi->param( 'title_' . $lang );
132                     my $content = $cgi->param( 'content_' . $lang );
133                     $content ||= '<!-- no_content -->' if $lang eq 'default';
134
135                     next unless $title || $content;
136
137                     push @seen_ids, $id;
138                     my $translated_content = {
139                         title   => $title,
140                         content => $content,
141                         lang    => $lang,
142                     };
143
144                     my $existing_content = $existing_contents->find($id);
145                     if ($existing_content) {
146                         if ( $existing_content->title ne $title || $existing_content->content ne $content ) {
147                             if ( C4::Context->preference("NewsLog") ) {
148                                 logaction(
149                                     'NEWS', 'MODIFY', undef,
150                                     sprintf( "%s|%s|%s|%s", $code, $title, $lang, $content )
151                                 );
152                             }
153                         } else {
154                             $translated_content->{updated_on} = $existing_content->updated_on;
155                         }
156                     } elsif ( C4::Context->preference("NewsLog") ) {
157                         logaction( 'NEWS', 'ADD', undef, sprintf( "%s|%s|%s|%s", $code, $title, $lang, $content ) );
158                     }
159
160                     push @translated_contents, $translated_content;
161                 }
162
163                 if ( C4::Context->preference("NewsLog") ) {
164                     my @existing_ids = $existing_contents->get_column('id');
165                     my @deleted_ids  = array_minus( @existing_ids, @seen_ids );
166                     for my $id (@deleted_ids) {
167                         my $c = $existing_contents->find($id);
168                         logaction( 'NEWS', 'DELETE', undef, sprintf( "%s|%s|%s", $code, $c->lang, $c->content ) );
169                     }
170                 }
171
172                 $additional_content->translated_contents( \@translated_contents );
173             }
174         );
175     } catch {
176         warn $_;
177         if ($id) {
178             push @messages, { type => 'error', code => 'error_on_update' };
179         } else {
180             push @messages, { type => 'error', code => 'error_on_insert' };
181         }
182     };
183
184     if( $redirect eq "just_save" ){
185         print $cgi->redirect("/cgi-bin/koha/tools/additional-contents.pl?op=add_form&id=$id&category=$category&editmode=$editmode&redirect=done");
186         exit;
187     } else {
188         $op = 'list';
189     }
190 }
191 elsif ( $op eq 'delete_confirmed' ) {
192     output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' });
193     my @ids = $cgi->multi_param('ids');
194
195     try {
196         Koha::Database->new->schema->txn_do(
197             sub {
198                 my $contents =
199                   Koha::AdditionalContents->search( { id => \@ids } );
200
201                 if ( C4::Context->preference("NewsLog") ) {
202                     while ( my $c = $contents->next ) {
203                         my $translated_contents = $c->translated_contents;
204                         while ( my $translated_content = $translated_contents->next ) {
205                             logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $translated_content->lang, $translated_content->content));
206                         }
207                     }
208                 }
209                 $contents->delete;
210             }
211         );
212         push @messages, { type => 'message', code => 'success_on_delete' };
213     } catch {
214         warn $_;
215         push @messages, { type => 'error', code => 'error_on_delete' };
216     };
217
218     $op = 'list';
219 }
220
221 if ( $op eq 'list' ) {
222     my $additional_contents = Koha::AdditionalContents->search(
223         { category => $category, 'additional_contents_localizations.lang' => 'default' },
224         { order_by => { -desc => 'published_on' }, join => 'additional_contents_localizations' }
225     );
226     $template->param( additional_contents => $additional_contents );
227 }
228
229 my $translated_languages = C4::Languages::getTranslatedLanguages;
230 my @languages;
231 for my $language (@$translated_languages) {
232     for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
233         if ( $language->{plural} ) {
234             push @languages,
235               {
236                 lang        => $sublanguage->{rfc4646_subtag},
237                 description => $sublanguage->{native_description} . ' '
238                   . $sublanguage->{region_description} . ' ('
239                   . $sublanguage->{rfc4646_subtag} . ')',
240               };
241         }
242         else {
243             push @languages,
244               {
245                 lang        => $sublanguage->{rfc4646_subtag},
246                 description => $sublanguage->{native_description} . ' ('
247                   . $sublanguage->{rfc4646_subtag} . ')',
248               };
249         }
250     }
251 }
252 unshift @languages, {lang => 'default'} if @languages;
253
254 $template->param(
255     op        => $op,
256     category  => $category,
257     wysiwyg   => $wysiwyg,
258     editmode  => $editmode,
259     languages => \@languages,
260     messages  => \@messages,
261 );
262
263 output_html_with_http_headers $cgi, $cookie, $template->output;