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") ) {
148 logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content));
151 $translated_content->{updated_on} = $existing_content->updated_on;
153 } elsif( C4::Context->preference("NewsLog") ) {
154 logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
157 push @translated_contents, $translated_content;
160 if ( C4::Context->preference("NewsLog") ) {
161 my @existing_ids = $existing_contents->get_column('id');
162 my @deleted_ids = array_minus( @existing_ids, @seen_ids );
163 for my $id ( @deleted_ids ) {
164 my $c = $existing_contents->find($id);
165 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s", $code, $c->lang, $c->content));
169 $additional_content->translated_contents( \@translated_contents );
175 push @messages, { type => 'error', code => 'error_on_update' };
177 push @messages, { type => 'error', code => 'error_on_insert' };
181 if( $redirect eq "just_save" ){
182 print $cgi->redirect("/cgi-bin/koha/tools/additional-contents.pl?op=add_form&id=$id&category=$category&editmode=$editmode&redirect=done");
188 elsif ( $op eq 'delete_confirmed' ) {
189 output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' });
190 my @ids = $cgi->multi_param('ids');
193 Koha::Database->new->schema->txn_do(
196 Koha::AdditionalContents->search( { id => \@ids } );
198 if ( C4::Context->preference("NewsLog") ) {
199 while ( my $c = $contents->next ) {
200 my $translated_contents = $c->translated_contents;
201 while ( my $translated_content = $translated_contents->next ) {
202 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $translated_content->lang, $translated_content->content));
209 push @messages, { type => 'message', code => 'success_on_delete' };
212 push @messages, { type => 'error', code => 'error_on_delete' };
218 if ( $op eq 'list' ) {
219 my $additional_contents = Koha::AdditionalContents->search(
220 { category => $category, 'additional_contents_localizations.lang' => 'default' },
221 { order_by => { -desc => 'published_on' }, join => 'additional_contents_localizations' }
223 $template->param( additional_contents => $additional_contents );
226 my $translated_languages = C4::Languages::getTranslatedLanguages;
228 for my $language (@$translated_languages) {
229 for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
230 if ( $language->{plural} ) {
233 lang => $sublanguage->{rfc4646_subtag},
234 description => $sublanguage->{native_description} . ' '
235 . $sublanguage->{region_description} . ' ('
236 . $sublanguage->{rfc4646_subtag} . ')',
242 lang => $sublanguage->{rfc4646_subtag},
243 description => $sublanguage->{native_description} . ' ('
244 . $sublanguage->{rfc4646_subtag} . ')',
249 unshift @languages, {lang => 'default'} if @languages;
253 category => $category,
255 editmode => $editmode,
256 languages => \@languages,
257 messages => \@messages,
260 output_html_with_http_headers $cgi, $cookie, $template->output;