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