Bug 29325: Fix commit_file.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);
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             )->as_list
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     my $idnew      = $cgi->param('idnew');
86
87     my @lang       = $cgi->multi_param('lang');
88
89     my $expirationdate;
90     if ( $cgi->param('expirationdate') ) {
91         $expirationdate = dt_from_string( scalar $cgi->param('expirationdate') );
92     }
93     my $published_on = dt_from_string( scalar $cgi->param('published_on') );
94     my $number = $cgi->param('number');
95
96     my $original_default = $idnew ? Koha::AdditionalContents->find($idnew) : undef;
97
98     my $success = 1;
99     for my $lang ( sort {$a ne 'default'} @lang ) { # Process 'default' first
100         my $title   = $cgi->param( 'title_' . $lang );
101         my $content = $cgi->param( 'content_' . $lang );
102         my $additional_content = Koha::AdditionalContents->find(
103             {
104                 category   => $category,
105                 code       => $code,
106                 branchcode => $original_default ? $original_default->branchcode : $branchcode,
107                 lang       => $lang,
108             }
109         );
110         # Delete if title or content is empty
111         unless ( $title and $content ) {
112             if ( $additional_content ) {
113                 eval { $additional_content->delete };
114                 unless ($@) {
115                     logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $additional_content->code, $additional_content->title, $additional_content->lang, $additional_content->content));
116                 }
117             }
118             next;
119         } elsif ( $additional_content ) {
120             my $updated;
121             eval {
122                 $additional_content->set(
123                     {
124                         category       => $category,
125                         code           => $code,
126                         location       => $location,
127                         branchcode     => $branchcode,
128                         title          => $title,
129                         content        => $content,
130                         lang           => $lang,
131                         expirationdate => $expirationdate,
132                         published_on   => $published_on,
133                         number         => $number,
134                         borrowernumber => $borrowernumber,
135                     }
136                 );
137                 $updated = $additional_content->_result->get_dirty_columns;
138                 $additional_content->store;
139             };
140             if ($@) {
141                 $success = 0;
142                 push @messages, { type => 'error', code => 'error_on_update' };
143                 last;
144             }
145
146             logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
147                 if C4::Context->preference("NewsLog") && $updated;
148         }
149         else {
150             my $additional_content = Koha::AdditionalContent->new(
151                 {
152                     category       => $category,
153                     code           => $code || 'tmp_code',
154                     location       => $location,
155                     branchcode     => $branchcode,
156                     title          => $title,
157                     content        => $content,
158                     lang           => $lang,
159                     expirationdate => $expirationdate,
160                     published_on   => $published_on,
161                     number         => $number,
162                     borrowernumber => $borrowernumber,
163                 }
164             )->store;
165             eval {
166                 $additional_content->store;
167                 unless ($code) {
168                     $additional_content->discard_changes;
169                     $code = $category eq 'news'
170                       ? 'News_' . $additional_content->idnew
171                       : $location . '_' . $additional_content->idnew;
172                     $additional_content->code($code)->store;
173                 }
174             };
175             if ($@) {
176                 $success = 0;
177                 push @messages, { type => 'error', code => 'error_on_insert' };
178                 last;
179             }
180
181             logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
182                 if C4::Context->preference("NewsLog");
183         }
184
185     }
186     $op = 'list';
187 }
188 elsif ( $op eq 'delete_confirmed' ) {
189     my @ids = $cgi->multi_param('ids');
190     my $deleted = eval {
191
192         my $schema = Koha::Database->new->schema;
193         $schema->txn_do(
194             sub {
195                 my $contents =
196                   Koha::AdditionalContents->search( { idnew => \@ids } );
197
198                 while ( my $c = $contents->next ) {
199                     Koha::AdditionalContents->search( { code => $c->code } )->delete;
200                     if ( C4::Context->preference("NewsLog") ) {
201                         logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $c->title, $c->lang, $c->content));
202                     }
203                 }
204             }
205         );
206     };
207
208     if ( $@ or not $deleted ) {
209         push @messages, { type => 'error', code => 'error_on_delete' };
210     }
211     else {
212         push @messages, { type => 'message', code => 'success_on_delete' };
213     }
214
215     $op = 'list';
216 }
217
218 if ( $op eq 'list' ) {
219     my $additional_contents = Koha::AdditionalContents->search(
220         { category => $category, lang => 'default' },
221         { order_by => { -desc => 'published_on' } }
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     languages => \@languages,
256 );
257
258 output_html_with_http_headers $cgi, $cookie, $template->output;