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);
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 if( $cgi->param('editmode') ){
44 $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
46 $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
49 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
51 template_name => "tools/additional-contents.tt",
54 flagsrequired => { tools => 'edit_additional_contents' },
59 if ( $op eq 'add_form' ) {
61 my $additional_content = Koha::AdditionalContents->find($id);
62 my $translated_contents;
63 if ( $additional_content ) {
64 $translated_contents = {
65 map { $_->lang => $_ } Koha::AdditionalContents->search(
67 category => $additional_content->category,
68 code => $additional_content->code,
69 location => $additional_content->location,
70 branchcode => $additional_content->branchcode,
74 $category = $additional_content->category;
77 additional_content => $additional_content,
78 translated_contents => $translated_contents,
81 elsif ( $op eq 'add_validate' ) {
82 my $location = $cgi->param('location');
83 my $code = $cgi->param('code');
84 my $branchcode = $cgi->param('branchcode') || undef;
86 my @lang = $cgi->multi_param('lang');
89 if ( $cgi->param('expirationdate') ) {
90 $expirationdate = dt_from_string( scalar $cgi->param('expirationdate') );
92 my $published_on = dt_from_string( scalar $cgi->param('published_on') );
93 my $number = $cgi->param('number');
96 for my $lang ( sort {$a ne 'default'} @lang ) { # Process 'default' first
97 my $title = $cgi->param( 'title_' . $lang );
98 my $content = $cgi->param( 'content_' . $lang );
99 my $additional_content = Koha::AdditionalContents->find(
101 category => $category,
103 branchcode => $branchcode,
107 # Delete if title or content is empty
108 unless ( $title and $content ) {
109 if ( $additional_content ) {
110 eval { $additional_content->delete };
112 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $additional_content->code, $additional_content->title, $additional_content->lang, $additional_content->content));
116 } elsif ( $additional_content ) {
119 $additional_content->set(
121 category => $category,
123 location => $location,
124 branchcode => $branchcode,
128 expirationdate => $expirationdate,
129 published_on => $published_on,
131 borrowernumber => $borrowernumber,
134 $updated = $additional_content->_result->get_dirty_columns;
135 $additional_content->store;
139 push @messages, { type => 'error', code => 'error_on_update' };
143 logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
144 if C4::Context->preference("NewsLog") && $updated;
147 my $additional_content = Koha::AdditionalContent->new(
149 category => $category,
150 code => $code || 'tmp_code',
151 location => $location,
152 branchcode => $branchcode,
156 expirationdate => $expirationdate,
157 published_on => $published_on,
159 borrowernumber => $borrowernumber,
163 $additional_content->store;
165 $additional_content->discard_changes;
166 $code = $category eq 'news'
167 ? 'News_' . $additional_content->idnew
168 : $location . '_' . $additional_content->idnew;
169 $additional_content->code($code)->store;
174 push @messages, { type => 'error', code => 'error_on_insert' };
178 logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
179 if C4::Context->preference("NewsLog");
185 elsif ( $op eq 'delete_confirmed' ) {
186 my @ids = $cgi->multi_param('ids');
189 my $schema = Koha::Database->new->schema;
193 Koha::AdditionalContents->search( { idnew => \@ids } );
195 while ( my $c = $contents->next ) {
196 Koha::AdditionalContents->search( { code => $c->code } )->delete;
197 if ( C4::Context->preference("NewsLog") ) {
198 logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $c->code, $c->title, $c->lang, $c->content));
205 if ( $@ or not $deleted ) {
206 push @messages, { type => 'error', code => 'error_on_delete' };
209 push @messages, { type => 'message', code => 'success_on_delete' };
215 if ( $op eq 'list' ) {
216 my $additional_contents = Koha::AdditionalContents->search(
217 { category => $category, lang => 'default' },
218 { order_by => { -desc => 'published_on' } }
220 $template->param( additional_contents => $additional_contents );
223 my $translated_languages = C4::Languages::getTranslatedLanguages;
225 for my $language (@$translated_languages) {
226 for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
227 if ( $language->{plural} ) {
230 lang => $sublanguage->{rfc4646_subtag},
231 description => $sublanguage->{native_description} . ' '
232 . $sublanguage->{region_description} . ' ('
233 . $sublanguage->{rfc4646_subtag} . ')',
239 lang => $sublanguage->{rfc4646_subtag},
240 description => $sublanguage->{native_description} . ' ('
241 . $sublanguage->{rfc4646_subtag} . ')',
246 unshift @languages, {lang => 'default'} if @languages;
250 category => $category,
252 languages => \@languages,
255 output_html_with_http_headers $cgi, $cookie, $template->output;