|
Lines 24-29
Link Here
|
| 24 |
|
24 |
|
| 25 |
use Modern::Perl; |
25 |
use Modern::Perl; |
| 26 |
use CGI qw ( -utf8 ); |
26 |
use CGI qw ( -utf8 ); |
|
|
27 |
use Try::Tiny; |
| 28 |
use Array::Utils qw( array_minus ); |
| 27 |
use C4::Auth qw(get_template_and_user); |
29 |
use C4::Auth qw(get_template_and_user); |
| 28 |
use C4::Koha; |
30 |
use C4::Koha; |
| 29 |
use C4::Context; |
31 |
use C4::Context; |
|
Lines 65-82
if ( $op eq 'add_form' ) {
Link Here
|
| 65 |
|
67 |
|
| 66 |
my $additional_content = Koha::AdditionalContents->find($id); |
68 |
my $additional_content = Koha::AdditionalContents->find($id); |
| 67 |
my $translated_contents; |
69 |
my $translated_contents; |
| 68 |
if ( $additional_content ) { |
70 |
if ($additional_content) { |
| 69 |
$translated_contents = { |
71 |
$translated_contents = { map { $_->lang => $_ } $additional_content->translated_contents->as_list }; |
| 70 |
map { $_->lang => $_ } Koha::AdditionalContents->search( |
72 |
$category = $additional_content->category; |
| 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 |
} |
73 |
} |
| 81 |
$template->param( |
74 |
$template->param( |
| 82 |
additional_content => $additional_content, |
75 |
additional_content => $additional_content, |
|
Lines 88-94
elsif ( $op eq 'add_validate' ) {
Link Here
|
| 88 |
my $location = $cgi->param('location'); |
81 |
my $location = $cgi->param('location'); |
| 89 |
my $code = $cgi->param('code'); |
82 |
my $code = $cgi->param('code'); |
| 90 |
my $branchcode = $cgi->param('branchcode') || undef; |
83 |
my $branchcode = $cgi->param('branchcode') || undef; |
| 91 |
my $idnew = $cgi->param('idnew'); |
|
|
| 92 |
|
84 |
|
| 93 |
my @lang = $cgi->multi_param('lang'); |
85 |
my @lang = $cgi->multi_param('lang'); |
| 94 |
|
86 |
|
|
Lines 96-196
elsif ( $op eq 'add_validate' ) {
Link Here
|
| 96 |
my $published_on = $cgi->param('published_on'); |
88 |
my $published_on = $cgi->param('published_on'); |
| 97 |
my $number = $cgi->param('number'); |
89 |
my $number = $cgi->param('number'); |
| 98 |
|
90 |
|
| 99 |
my $original_default = $idnew ? Koha::AdditionalContents->find($idnew) : undef; |
91 |
try { |
| 100 |
|
92 |
Koha::Database->new->schema->txn_do( |
| 101 |
my $success = 1; |
93 |
sub { |
| 102 |
for my $lang ( sort {$a ne 'default'} @lang ) { # Process 'default' first |
94 |
my $additional_content; |
| 103 |
my $title = $cgi->param( 'title_' . $lang ); |
95 |
my $params = { |
| 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, |
96 |
location => $location, |
| 133 |
branchcode => $branchcode, |
97 |
branchcode => $branchcode, |
| 134 |
title => $title, |
|
|
| 135 |
content => $content, |
| 136 |
lang => $lang, |
| 137 |
expirationdate => $expirationdate, |
98 |
expirationdate => $expirationdate, |
| 138 |
published_on => $published_on, |
99 |
published_on => $published_on, |
| 139 |
number => $number, |
100 |
number => $number, |
| 140 |
borrowernumber => $borrowernumber, |
101 |
borrowernumber => $borrowernumber, |
| 141 |
} |
102 |
}; |
| 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 |
|
103 |
|
| 153 |
logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content)) |
104 |
if ( $id ) { |
| 154 |
if C4::Context->preference("NewsLog") && $updated; |
105 |
$additional_content = Koha::AdditionalContents->find($id); |
| 155 |
} |
106 |
$additional_content->set($params)->store; |
| 156 |
else { |
107 |
} else { |
| 157 |
my $additional_content = Koha::AdditionalContent->new( |
108 |
$additional_content = Koha::AdditionalContent->new( |
| 158 |
{ |
109 |
{ |
| 159 |
category => $category, |
110 |
category => $category, |
| 160 |
code => $code || 'tmp_code', |
111 |
code => $code, |
| 161 |
location => $location, |
112 |
branchcode => $branchcode, |
| 162 |
branchcode => $branchcode, |
113 |
%$params, |
| 163 |
title => $title, |
114 |
} |
| 164 |
content => $content, |
115 |
)->store; |
| 165 |
lang => $lang, |
|
|
| 166 |
expirationdate => $expirationdate, |
| 167 |
published_on => $published_on, |
| 168 |
number => $number, |
| 169 |
borrowernumber => $borrowernumber, |
| 170 |
} |
116 |
} |
| 171 |
)->store; |
|
|
| 172 |
eval { |
| 173 |
$additional_content->store; |
| 174 |
unless ($code) { |
117 |
unless ($code) { |
| 175 |
$additional_content->discard_changes; |
118 |
$additional_content->discard_changes; |
| 176 |
$code = $category eq 'news' |
119 |
$code = |
| 177 |
? 'News_' . $additional_content->idnew |
120 |
$category eq 'news' |
| 178 |
: $location . '_' . $additional_content->idnew; |
121 |
? 'News_' . $additional_content->id |
|
|
122 |
: $location . '_' . $additional_content->id; |
| 179 |
$additional_content->code($code)->store; |
123 |
$additional_content->code($code)->store; |
| 180 |
$id = $additional_content->idnew; |
124 |
$id = $additional_content->id; |
| 181 |
} |
125 |
} |
| 182 |
}; |
126 |
my @translated_contents; |
| 183 |
if ($@) { |
127 |
my $existing_contents = $additional_content->translated_contents; |
| 184 |
$success = 0; |
128 |
my @seen_ids; |
| 185 |
push @messages, { type => 'error', code => 'error_on_insert' }; |
129 |
for my $lang (@lang) { |
| 186 |
last; |
130 |
my $id = $cgi->param( 'id_' . $lang ); |
| 187 |
} |
131 |
my $title = $cgi->param( 'title_' . $lang ); |
|
|
132 |
my $content = $cgi->param( 'content_' . $lang ); |
| 133 |
$content ||= '<!-- no_content -->' if $lang eq 'default'; |
| 188 |
|
134 |
|
| 189 |
logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content)) |
135 |
next unless $title || $content; |
| 190 |
if C4::Context->preference("NewsLog"); |
|
|
| 191 |
} |
| 192 |
|
136 |
|
| 193 |
} |
137 |
push @seen_ids, $id; |
|
|
138 |
push @translated_contents, |
| 139 |
{ |
| 140 |
title => $title, |
| 141 |
content => $content, |
| 142 |
lang => $lang, |
| 143 |
}; |
| 144 |
|
| 145 |
if ( C4::Context->preference("NewsLog") ) { |
| 146 |
my $existing_content = $existing_contents->find($id); |
| 147 |
if ( $existing_content ) { |
| 148 |
if ( $existing_content->title ne $title || $existing_content->content ne $content ) { |
| 149 |
logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content)); |
| 150 |
} |
| 151 |
} else { |
| 152 |
logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content)) |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if ( C4::Context->preference("NewsLog") ) { |
| 158 |
my @existing_ids = $existing_contents->get_column('id'); |
| 159 |
my @deleted_ids = array_minus( @existing_ids, @seen_ids ); |
| 160 |
for my $id ( @deleted_ids ) { |
| 161 |
my $c = $existing_contents->find($id); |
| 162 |
logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s", $code, $c->lang, $c->content)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
$additional_content->translated_contents( \@translated_contents ); |
| 167 |
} |
| 168 |
); |
| 169 |
} catch { |
| 170 |
warn $_; |
| 171 |
if ( $id ) { |
| 172 |
push @messages, { type => 'error', code => 'error_on_update' }; |
| 173 |
} else { |
| 174 |
push @messages, { type => 'error', code => 'error_on_insert' }; |
| 175 |
} |
| 176 |
}; |
| 194 |
|
177 |
|
| 195 |
if( $redirect eq "just_save" ){ |
178 |
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"); |
179 |
print $cgi->redirect("/cgi-bin/koha/tools/additional-contents.pl?op=add_form&id=$id&category=$category&editmode=$editmode&redirect=done"); |
|
Lines 202-239
elsif ( $op eq 'add_validate' ) {
Link Here
|
| 202 |
elsif ( $op eq 'delete_confirmed' ) { |
185 |
elsif ( $op eq 'delete_confirmed' ) { |
| 203 |
output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' }); |
186 |
output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' }); |
| 204 |
my @ids = $cgi->multi_param('ids'); |
187 |
my @ids = $cgi->multi_param('ids'); |
| 205 |
my $deleted = eval { |
|
|
| 206 |
|
188 |
|
| 207 |
my $schema = Koha::Database->new->schema; |
189 |
try { |
| 208 |
$schema->txn_do( |
190 |
Koha::Database->new->schema->txn_do( |
| 209 |
sub { |
191 |
sub { |
| 210 |
my $contents = |
192 |
my $contents = |
| 211 |
Koha::AdditionalContents->search( { idnew => \@ids } ); |
193 |
Koha::AdditionalContents->search( { id => \@ids } ); |
| 212 |
|
194 |
|
| 213 |
while ( my $c = $contents->next ) { |
195 |
if ( C4::Context->preference("NewsLog") ) { |
| 214 |
Koha::AdditionalContents->search( { code => $c->code } )->delete; |
196 |
while ( my $c = $contents->next ) { |
| 215 |
if ( C4::Context->preference("NewsLog") ) { |
197 |
my $translated_contents = $c->translated_contents; |
| 216 |
logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $c->title, $c->lang, $c->content)); |
198 |
while ( my $translated_content = $translated_contents->next ) { |
|
|
199 |
logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $translated_content->lang, $translated_content->content)); |
| 200 |
} |
| 217 |
} |
201 |
} |
| 218 |
} |
202 |
} |
|
|
203 |
$contents->delete; |
| 219 |
} |
204 |
} |
| 220 |
); |
205 |
); |
| 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' }; |
206 |
push @messages, { type => 'message', code => 'success_on_delete' }; |
| 228 |
} |
207 |
} catch { |
|
|
208 |
warn $_; |
| 209 |
push @messages, { type => 'error', code => 'error_on_delete' }; |
| 210 |
}; |
| 229 |
|
211 |
|
| 230 |
$op = 'list'; |
212 |
$op = 'list'; |
| 231 |
} |
213 |
} |
| 232 |
|
214 |
|
| 233 |
if ( $op eq 'list' ) { |
215 |
if ( $op eq 'list' ) { |
| 234 |
my $additional_contents = Koha::AdditionalContents->search( |
216 |
my $additional_contents = Koha::AdditionalContents->search( |
| 235 |
{ category => $category, lang => 'default' }, |
217 |
{ category => $category, 'additional_contents_localizations.lang' => 'default' }, |
| 236 |
{ order_by => { -desc => 'published_on' } } |
218 |
{ order_by => { -desc => 'published_on' }, join => 'additional_contents_localizations' } |
| 237 |
); |
219 |
); |
| 238 |
$template->param( additional_contents => $additional_contents ); |
220 |
$template->param( additional_contents => $additional_contents ); |
| 239 |
} |
221 |
} |
|
Lines 269-274
$template->param(
Link Here
|
| 269 |
wysiwyg => $wysiwyg, |
251 |
wysiwyg => $wysiwyg, |
| 270 |
editmode => $editmode, |
252 |
editmode => $editmode, |
| 271 |
languages => \@languages, |
253 |
languages => \@languages, |
|
|
254 |
messages => \@messages, |
| 272 |
); |
255 |
); |
| 273 |
|
256 |
|
| 274 |
output_html_with_http_headers $cgi, $cookie, $template->output; |
257 |
output_html_with_http_headers $cgi, $cookie, $template->output; |