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>.
27 use C4::Auth qw(get_template_and_user);
30 use C4::Log qw( logaction );
31 use C4::Output qw(output_html_with_http_headers output_and_exit_if_error);
32 use C4::Languages qw(getTranslatedLanguages);
33 use Koha::DateUtils qw( dt_from_string output_pref );
35 use Koha::AdditionalContents;
39 my $op = $cgi->param('op') || 'list';
40 my $id = $cgi->param('id');
41 my $category = $cgi->param('category') || 'news';
43 my $redirect = $cgi->param('redirect');
46 if( $cgi->param('editmode') ){
47 $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
49 $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
52 $editmode = $wysiwyg eq 1 ? "wysiwyg" : "text";
54 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
56 template_name => "tools/additional-contents.tt",
59 flagsrequired => { tools => 'edit_additional_contents' },
64 if ( $op eq 'add_form' ) {
66 my $additional_content = Koha::AdditionalContents->find($id);
67 my $translated_contents;
68 if ( $additional_content ) {
69 $translated_contents = {
70 map { $_->lang => $_ } Koha::AdditionalContents->search(
72 category => $additional_content->category,
73 code => $additional_content->code,
74 location => $additional_content->location,
75 branchcode => $additional_content->branchcode,
79 $category = $additional_content->category;
82 additional_content => $additional_content,
83 translated_contents => $translated_contents,
86 elsif ( $op eq 'add_validate' ) {
87 output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' });
88 my $location = $cgi->param('location');
89 my $code = $cgi->param('code');
90 my $branchcode = $cgi->param('branchcode') || undef;
91 my $idnew = $cgi->param('idnew');
93 my @lang = $cgi->multi_param('lang');
96 if ( $cgi->param('expirationdate') ) {
97 $expirationdate = dt_from_string( scalar $cgi->param('expirationdate') );
99 my $published_on = dt_from_string( scalar $cgi->param('published_on') );
100 my $number = $cgi->param('number');
102 my $original_default = $idnew ? Koha::AdditionalContents->find($idnew) : undef;
105 for my $lang ( sort {$a ne 'default'} @lang ) { # Process 'default' first
106 my $title = $cgi->param( 'title_' . $lang );
107 my $content = $cgi->param( 'content_' . $lang );
108 # Force a default record
109 $content ||= '<!-- no_content -->' if $lang eq 'default';
111 my $additional_content = Koha::AdditionalContents->find(
113 category => $category,
115 branchcode => $original_default ? $original_default->branchcode : $branchcode,
119 # Delete if title or content is empty
120 if( $lang ne 'default' && !$title && !$content ) {
121 if ( $additional_content ) {
122 eval { $additional_content->delete };
124 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $additional_content->code, $additional_content->title, $additional_content->lang, $additional_content->content));
128 } elsif ( $additional_content ) {
131 $additional_content->set(
133 category => $category,
135 location => $location,
136 branchcode => $branchcode,
140 expirationdate => $expirationdate,
141 published_on => $published_on,
143 borrowernumber => $borrowernumber,
146 $updated = $additional_content->_result->get_dirty_columns;
147 $additional_content->store;
148 $id = $additional_content->idnew;
152 push @messages, { type => 'error', code => 'error_on_update' };
156 logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
157 if C4::Context->preference("NewsLog") && $updated;
160 my $additional_content = Koha::AdditionalContent->new(
162 category => $category,
163 code => $code || 'tmp_code',
164 location => $location,
165 branchcode => $branchcode,
169 expirationdate => $expirationdate,
170 published_on => $published_on,
172 borrowernumber => $borrowernumber,
176 $additional_content->store;
178 $additional_content->discard_changes;
179 $code = $category eq 'news'
180 ? 'News_' . $additional_content->idnew
181 : $location . '_' . $additional_content->idnew;
182 $additional_content->code($code)->store;
183 $id = $additional_content->idnew;
188 push @messages, { type => 'error', code => 'error_on_insert' };
192 logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
193 if C4::Context->preference("NewsLog");
198 if( $redirect eq "just_save" ){
199 print $cgi->redirect("/cgi-bin/koha/tools/additional-contents.pl?op=add_form&id=$id&category=$category&editmode=$editmode&redirect=done");
205 elsif ( $op eq 'delete_confirmed' ) {
206 output_and_exit_if_error($cgi, $cookie, $template, { check => 'csrf_token' });
207 my @ids = $cgi->multi_param('ids');
210 my $schema = Koha::Database->new->schema;
214 Koha::AdditionalContents->search( { idnew => \@ids } );
216 while ( my $c = $contents->next ) {
217 Koha::AdditionalContents->search( { code => $c->code } )->delete;
218 if ( C4::Context->preference("NewsLog") ) {
219 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $c->title, $c->lang, $c->content));
226 if ( $@ or not $deleted ) {
227 push @messages, { type => 'error', code => 'error_on_delete' };
230 push @messages, { type => 'message', code => 'success_on_delete' };
236 if ( $op eq 'list' ) {
237 my $additional_contents = Koha::AdditionalContents->search(
238 { category => $category, lang => 'default' },
239 { order_by => { -desc => 'published_on' } }
241 $template->param( additional_contents => $additional_contents );
244 my $translated_languages = C4::Languages::getTranslatedLanguages;
246 for my $language (@$translated_languages) {
247 for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
248 if ( $language->{plural} ) {
251 lang => $sublanguage->{rfc4646_subtag},
252 description => $sublanguage->{native_description} . ' '
253 . $sublanguage->{region_description} . ' ('
254 . $sublanguage->{rfc4646_subtag} . ')',
260 lang => $sublanguage->{rfc4646_subtag},
261 description => $sublanguage->{native_description} . ' ('
262 . $sublanguage->{rfc4646_subtag} . ')',
267 unshift @languages, {lang => 'default'} if @languages;
271 category => $category,
273 editmode => $editmode,
274 languages => \@languages,
277 output_html_with_http_headers $cgi, $cookie, $template->output;