Bug 31383: Restore updated_on for individual content
[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('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content));
149                             }
150                         } else {
151                             $translated_content->{updated_on} = $existing_content->updated_on;
152                         }
153                     } elsif( C4::Context->preference("NewsLog") ) {
154                         logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
155                     }
156
157                     push @translated_contents, $translated_content;
158                 }
159
160                 if ( C4::Context->preference("NewsLog") ) {
161                     my @existing_ids = $existing_contents->get_column('id');
162                     my @deleted_ids = array_minus( @existing_ids, @seen_ids );
163                     for my $id ( @deleted_ids ) {
164                         my $c = $existing_contents->find($id);
165                         logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s", $code, $c->lang, $c->content));
166                     }
167                 }
168
169                 $additional_content->translated_contents( \@translated_contents );
170             }
171         );
172     } catch {
173         warn $_;
174         if ( $id ) {
175             push @messages, { type => 'error', code => 'error_on_update' };
176         } else {
177             push @messages, { type => 'error', code => 'error_on_insert' };
178         }
179     };
180
181     if( $redirect eq "just_save" ){
182         print $cgi->redirect("/cgi-bin/koha/tools/additional-contents.pl?op=add_form&id=$id&category=$category&editmode=$editmode&redirect=done");
183         exit;
184     } else {
185         $op = 'list';
186     }
187 }
188 elsif ( $op eq 'delete_confirmed' ) {
189     output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' });
190     my @ids = $cgi->multi_param('ids');
191
192     try {
193         Koha::Database->new->schema->txn_do(
194             sub {
195                 my $contents =
196                   Koha::AdditionalContents->search( { id => \@ids } );
197
198                 if ( C4::Context->preference("NewsLog") ) {
199                     while ( my $c = $contents->next ) {
200                         my $translated_contents = $c->translated_contents;
201                         while ( my $translated_content = $translated_contents->next ) {
202                             logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $translated_content->lang, $translated_content->content));
203                         }
204                     }
205                 }
206                 $contents->delete;
207             }
208         );
209         push @messages, { type => 'message', code => 'success_on_delete' };
210     } catch {
211         warn $_;
212         push @messages, { type => 'error', code => 'error_on_delete' };
213     };
214
215     $op = 'list';
216 }
217
218 if ( $op eq 'list' ) {
219     my $additional_contents = Koha::AdditionalContents->search(
220         { category => $category, 'additional_contents_localizations.lang' => 'default' },
221         { order_by => { -desc => 'published_on' }, join => 'additional_contents_localizations' }
222     );
223     $template->param( additional_contents => $additional_contents );
224 }
225
226 my $translated_languages = C4::Languages::getTranslatedLanguages;
227 my @languages;
228 for my $language (@$translated_languages) {
229     for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
230         if ( $language->{plural} ) {
231             push @languages,
232               {
233                 lang        => $sublanguage->{rfc4646_subtag},
234                 description => $sublanguage->{native_description} . ' '
235                   . $sublanguage->{region_description} . ' ('
236                   . $sublanguage->{rfc4646_subtag} . ')',
237               };
238         }
239         else {
240             push @languages,
241               {
242                 lang        => $sublanguage->{rfc4646_subtag},
243                 description => $sublanguage->{native_description} . ' ('
244                   . $sublanguage->{rfc4646_subtag} . ')',
245               };
246         }
247     }
248 }
249 unshift @languages, {lang => 'default'} if @languages;
250
251 $template->param(
252     op        => $op,
253     category  => $category,
254     wysiwyg   => $wysiwyg,
255     editmode  => $editmode,
256     languages => \@languages,
257     messages  => \@messages,
258 );
259
260 output_html_with_http_headers $cgi, $cookie, $template->output;