Bug 29243: Correct call when adding items from staged records
[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 @title      = $cgi->multi_param('title');
87     my @content    = $cgi->multi_param('content');
88     my @lang       = $cgi->multi_param('lang');
89
90     my $expirationdate;
91     if ( $cgi->param('expirationdate') ) {
92         $expirationdate = output_pref(
93             {
94                 dt => dt_from_string( scalar $cgi->param('expirationdate') ),
95                 dateformat => 'iso',
96                 dateonly   => 1
97             }
98         );
99     }
100     my $published_on = output_pref(
101         {
102             dt         => dt_from_string( scalar $cgi->param('published_on') ),
103             dateformat => 'iso',
104             dateonly   => 1
105         }
106     );
107     my $number = $cgi->param('number');
108
109     my $success = 1;
110     for my $lang ( @lang ) {
111         my $title = shift @title;
112         my $content = shift @content;
113         my $additional_content = Koha::AdditionalContents->find(
114             {
115                 category   => $category,
116                 code       => $code,
117                 branchcode => $branchcode,
118                 lang       => $lang,
119             }
120         );
121         # Delete if title or content is empty
122         unless ( $title and $content ) {
123             if ( $additional_content ) {
124                 eval { $additional_content->delete };
125                 unless ($@) {
126                     logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $additional_content->code, $additional_content->title, $additional_content->lang, $additional_content->content));
127                 }
128             }
129             next;
130         } elsif ( $additional_content ) {
131             my $updated;
132             eval {
133                 $additional_content->set(
134                     {
135                         category       => $category,
136                         code           => $code,
137                         location       => $location,
138                         branchcode     => $branchcode,
139                         title          => $title,
140                         content        => $content,
141                         lang           => $lang,
142                         expirationdate => $expirationdate,
143                         published_on   => $published_on,
144                         number         => $number,
145                         borrowernumber => $borrowernumber,
146                     }
147                 );
148                 $updated = $additional_content->_result->get_dirty_columns;
149                 $additional_content->store;
150             };
151             if ($@) {
152                 $success = 0;
153                 push @messages, { type => 'error', code => 'error_on_update' };
154                 last;
155             }
156
157             logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
158                 if C4::Context->preference("NewsLog") && $updated;
159         }
160         else {
161             my $additional_content = Koha::AdditionalContent->new(
162                 {
163                     category       => $category,
164                     code           => $code,
165                     location       => $location,
166                     branchcode     => $branchcode,
167                     title          => $title,
168                     content        => $content,
169                     lang           => $lang,
170                     expirationdate => $expirationdate,
171                     published_on   => $published_on,
172                     number         => $number,
173                     borrowernumber => $borrowernumber,
174                 }
175             )->store;
176             eval { $additional_content->store; };
177             if ($@) {
178                 $success = 0;
179                 push @messages, { type => 'error', code => 'error_on_insert' };
180                 last;
181             }
182
183             logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
184                 if C4::Context->preference("NewsLog");
185         }
186
187     }
188     $op = 'list';
189 }
190 elsif ( $op eq 'delete_confirmed' ) {
191     my @ids = $cgi->multi_param('ids');
192     my $deleted = eval {
193
194         my $schema = Koha::Database->new->schema;
195         $schema->txn_do(
196             sub {
197                 my $contents =
198                   Koha::AdditionalContents->search( { idnew => \@ids } );
199
200                 if ( C4::Context->preference("NewsLog") ) {
201                     while ( my $c = $contents->next ) {
202                         logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $c->title, $c->lang, $c->content));
203                     }
204                 }
205                 $contents->delete;
206             }
207         );
208     };
209
210     if ( $@ or not $deleted ) {
211         push @messages, { type => 'error', code => 'error_on_delete' };
212     }
213     else {
214         push @messages, { type => 'message', code => 'success_on_delete' };
215     }
216
217     $op = 'list';
218 }
219
220 if ( $op eq 'list' ) {
221     my $additional_contents = Koha::AdditionalContents->search(
222         { category => $category, lang => 'default' },
223         { order_by => { -desc => 'published_on' } }
224     );
225     $template->param( additional_contents => $additional_contents );
226 }
227
228 my $translated_languages = C4::Languages::getTranslatedLanguages;
229 my @languages;
230 for my $language (@$translated_languages) {
231     for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
232         if ( $language->{plural} ) {
233             push @languages,
234               {
235                 lang        => $sublanguage->{rfc4646_subtag},
236                 description => $sublanguage->{native_description} . ' '
237                   . $sublanguage->{region_description} . ' ('
238                   . $sublanguage->{rfc4646_subtag} . ')',
239               };
240         }
241         else {
242             push @languages,
243               {
244                 lang        => $sublanguage->{rfc4646_subtag},
245                 description => $sublanguage->{native_description} . ' ('
246                   . $sublanguage->{rfc4646_subtag} . ')',
247               };
248         }
249     }
250 }
251 unshift @languages, {lang => 'default'} if @languages;
252
253 $template->param(
254     op        => $op,
255     category  => $category,
256     wysiwyg   => $wysiwyg,
257     languages => \@languages,
258 );
259
260 output_html_with_http_headers $cgi, $cookie, $template->output;