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