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);
32 use C4::Languages qw(getTranslatedLanguages);
34 use Koha::AdditionalContents;
38 my $op = $cgi->param('op') || 'list';
39 my $id = $cgi->param('id');
40 my $category = $cgi->param('category') || 'news';
42 if( $cgi->param('editmode') ){
43 $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
45 $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50 template_name => "tools/additional-contents.tt",
53 flagsrequired => { tools => 'edit_additional_contents' },
58 if ( $op eq 'add_form' ) {
60 my $additional_content = Koha::AdditionalContents->find($id);
61 my $translated_contents;
62 if ( $additional_content ) {
63 $translated_contents = {
64 map { $_->lang => $_ } Koha::AdditionalContents->search(
66 category => $additional_content->category,
67 code => $additional_content->code,
68 location => $additional_content->location,
69 branchcode => $additional_content->branchcode,
73 $category = $additional_content->category;
76 additional_content => $additional_content,
77 translated_contents => $translated_contents,
80 elsif ( $op eq 'add_validate' ) {
81 my $location = $cgi->param('location');
82 my $code = $cgi->param('code');
83 my $branchcode = $cgi->param('branchcode') || undef;
85 my @title = $cgi->multi_param('title');
86 my @content = $cgi->multi_param('content');
87 my @lang = $cgi->multi_param('lang');
90 if ( $cgi->param('expirationdate') ) {
91 $expirationdate = output_pref(
93 dt => dt_from_string( scalar $cgi->param('expirationdate') ),
99 my $published_on = output_pref(
101 dt => dt_from_string( scalar $cgi->param('published_on') ),
106 my $number = $cgi->param('number');
109 for my $lang ( @lang ) {
110 my $title = shift @title;
111 my $content = shift @content;
112 my $additional_content = Koha::AdditionalContents->find(
114 category => $category,
116 branchcode => $branchcode,
120 # Delete if title or content is empty
121 unless ( $title and $content ) {
122 if ( $additional_content ) {
123 eval { $additional_content->delete };
125 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $additional_content->code, $additional_content->title, $additional_content->lang, $additional_content->content));
129 } elsif ( $additional_content ) {
132 $additional_content->set(
134 category => $category,
136 location => $location,
137 branchcode => $branchcode,
141 expirationdate => $expirationdate,
142 published_on => $published_on,
144 borrowernumber => $borrowernumber,
147 $updated = $additional_content->_result->get_dirty_columns;
148 $additional_content->store;
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,
164 location => $location,
165 branchcode => $branchcode,
169 expirationdate => $expirationdate,
170 published_on => $published_on,
172 borrowernumber => $borrowernumber,
175 eval { $additional_content->store; };
178 push @messages, { type => 'error', code => 'error_on_insert' };
182 logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
183 if C4::Context->preference("NewsLog");
189 elsif ( $op eq 'delete_confirmed' ) {
190 my @ids = $cgi->multi_param('ids');
193 my $schema = Koha::Database->new->schema;
197 Koha::AdditionalContents->search( { idnew => \@ids } );
199 if ( C4::Context->preference("NewsLog") ) {
200 while ( my $c = $contents->next ) {
201 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $c->title, $c->lang, $c->content));
209 if ( $@ or not $deleted ) {
210 push @messages, { type => 'error', code => 'error_on_delete' };
213 push @messages, { type => 'message', code => 'success_on_delete' };
219 if ( $op eq 'list' ) {
220 my $additional_contents = Koha::AdditionalContents->search(
221 { category => $category, lang => 'default' },
222 { order_by => { -desc => 'published_on' } }
224 $template->param( additional_contents => $additional_contents );
227 my $translated_languages = C4::Languages::getTranslatedLanguages;
229 for my $language (@$translated_languages) {
230 for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
231 if ( $language->{plural} ) {
234 lang => $sublanguage->{rfc4646_subtag},
235 description => $sublanguage->{native_description} . ' '
236 . $sublanguage->{region_description} . ' ('
237 . $sublanguage->{rfc4646_subtag} . ')',
243 lang => $sublanguage->{rfc4646_subtag},
244 description => $sublanguage->{native_description} . ' ('
245 . $sublanguage->{rfc4646_subtag} . ')',
250 unshift @languages, {lang => 'default'} if @languages;
254 category => $category,
256 languages => \@languages,
259 output_html_with_http_headers $cgi, $cookie, $template->output;