3 # This file is part of Koha.
5 # Script to manage the opac news.
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
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.
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.
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>.
28 use Array::Utils qw( array_minus );
29 use C4::Auth qw(get_template_and_user);
32 use C4::Log qw( logaction );
33 use C4::Output qw(output_html_with_http_headers output_and_exit_if_error);
34 use C4::Languages qw(getTranslatedLanguages);
35 use Koha::DateUtils qw( dt_from_string output_pref );
37 use Koha::AdditionalContents;
41 my $op = $cgi->param('op') || 'list';
42 my $id = $cgi->param('id');
43 my $category = $cgi->param('category') || 'news';
45 my $redirect = $cgi->param('redirect');
48 if( $cgi->param('editmode') ){
49 $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
51 $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
54 $editmode = $wysiwyg eq 1 ? "wysiwyg" : "text";
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58 template_name => "tools/additional-contents.tt",
61 flagsrequired => { tools => 'edit_additional_contents' },
66 if ( $op eq 'add_form' ) {
68 my $additional_content = Koha::AdditionalContents->find($id);
69 my $translated_contents;
70 if ($additional_content) {
71 $translated_contents = { map { $_->lang => $_ } $additional_content->translated_contents->as_list };
72 $category = $additional_content->category;
75 additional_content => $additional_content,
76 translated_contents => $translated_contents,
79 elsif ( $op eq 'add_validate' ) {
80 output_and_exit_if_error( $cgi, $cookie, $template, { check => 'csrf_token' } );
81 my $location = $cgi->param('location');
82 my $code = $cgi->param('code');
83 my $branchcode = $cgi->param('branchcode') || undef;
85 my @lang = $cgi->multi_param('lang');
87 my $expirationdate = $cgi->param('expirationdate');
88 my $published_on = $cgi->param('published_on');
89 my $number = $cgi->param('number');
92 Koha::Database->new->schema->txn_do(
94 my $additional_content;
96 location => $location,
97 branchcode => $branchcode,
98 expirationdate => $expirationdate,
99 published_on => $published_on,
101 borrowernumber => $borrowernumber,
105 $additional_content = Koha::AdditionalContents->find($id);
106 $additional_content->set($params)->store;
108 $additional_content = Koha::AdditionalContent->new(
110 category => $category,
112 branchcode => $branchcode,
118 $additional_content->discard_changes;
121 ? 'News_' . $additional_content->id
122 : $location . '_' . $additional_content->id;
123 $additional_content->code($code)->store;
124 $id = $additional_content->id;
126 my @translated_contents;
127 my $existing_contents = $additional_content->translated_contents;
129 for my $lang (@lang) {
130 my $id = $cgi->param( 'id_' . $lang );
131 my $title = $cgi->param( 'title_' . $lang );
132 my $content = $cgi->param( 'content_' . $lang );
133 $content ||= '<!-- no_content -->' if $lang eq 'default';
135 next unless $title || $content;
138 my $translated_content = {
144 my $existing_content = $existing_contents->find($id);
145 if ($existing_content) {
146 if ( $existing_content->title ne $title || $existing_content->content ne $content ) {
147 if ( C4::Context->preference("NewsLog") ) {
149 'NEWS', 'MODIFY', undef,
150 sprintf( "%s|%s|%s|%s", $code, $title, $lang, $content )
154 $translated_content->{updated_on} = $existing_content->updated_on;
156 } elsif ( C4::Context->preference("NewsLog") ) {
157 logaction( 'NEWS', 'ADD', undef, sprintf( "%s|%s|%s|%s", $code, $title, $lang, $content ) );
160 push @translated_contents, $translated_content;
163 if ( C4::Context->preference("NewsLog") ) {
164 my @existing_ids = $existing_contents->get_column('id');
165 my @deleted_ids = array_minus( @existing_ids, @seen_ids );
166 for my $id (@deleted_ids) {
167 my $c = $existing_contents->find($id);
168 logaction( 'NEWS', 'DELETE', undef, sprintf( "%s|%s|%s", $code, $c->lang, $c->content ) );
172 $additional_content->translated_contents( \@translated_contents );
178 push @messages, { type => 'error', code => 'error_on_update' };
180 push @messages, { type => 'error', code => 'error_on_insert' };
184 if( $redirect eq "just_save" ){
185 print $cgi->redirect("/cgi-bin/koha/tools/additional-contents.pl?op=add_form&id=$id&category=$category&editmode=$editmode&redirect=done");
191 elsif ( $op eq 'delete_confirmed' ) {
192 output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' });
193 my @ids = $cgi->multi_param('ids');
196 Koha::Database->new->schema->txn_do(
199 Koha::AdditionalContents->search( { id => \@ids } );
201 if ( C4::Context->preference("NewsLog") ) {
202 while ( my $c = $contents->next ) {
203 my $translated_contents = $c->translated_contents;
204 while ( my $translated_content = $translated_contents->next ) {
205 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $translated_content->lang, $translated_content->content));
212 push @messages, { type => 'message', code => 'success_on_delete' };
215 push @messages, { type => 'error', code => 'error_on_delete' };
221 if ( $op eq 'list' ) {
222 my $additional_contents = Koha::AdditionalContents->search(
223 { category => $category, 'additional_contents_localizations.lang' => 'default' },
224 { order_by => { -desc => 'published_on' }, join => 'additional_contents_localizations' }
226 $template->param( additional_contents => $additional_contents );
229 my $translated_languages = C4::Languages::getTranslatedLanguages;
231 for my $language (@$translated_languages) {
232 for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
233 if ( $language->{plural} ) {
236 lang => $sublanguage->{rfc4646_subtag},
237 description => $sublanguage->{native_description} . ' '
238 . $sublanguage->{region_description} . ' ('
239 . $sublanguage->{rfc4646_subtag} . ')',
245 lang => $sublanguage->{rfc4646_subtag},
246 description => $sublanguage->{native_description} . ' ('
247 . $sublanguage->{rfc4646_subtag} . ')',
252 unshift @languages, {lang => 'default'} if @languages;
256 category => $category,
258 editmode => $editmode,
259 languages => \@languages,
260 messages => \@messages,
263 output_html_with_http_headers $cgi, $cookie, $template->output;