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::Output qw(output_html_with_http_headers);
31 use C4::Languages qw(getTranslatedLanguages);
33 use Koha::AdditionalContents;
37 my $op = $cgi->param('op') || 'list';
38 my $id = $cgi->param('id');
39 my $category = $cgi->param('category') || 'news';
41 if( $cgi->param('editmode') ){
42 $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
44 $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
49 template_name => "tools/additional-contents.tt",
52 flagsrequired => { tools => 'edit_additional_contents' },
57 if ( $op eq 'add_form' ) {
59 my $additional_content = Koha::AdditionalContents->find($id);
60 my $translated_contents;
61 if ( $additional_content ) {
62 $translated_contents = {
63 map { $_->lang => $_ } Koha::AdditionalContents->search(
65 category => $additional_content->category,
66 code => $additional_content->code,
67 location => $additional_content->location,
68 branchcode => $additional_content->branchcode,
72 $category = $additional_content->category;
75 additional_content => $additional_content,
76 translated_contents => $translated_contents,
79 elsif ( $op eq 'add_validate' ) {
80 my $location = $cgi->param('location');
81 my $code = $cgi->param('code');
82 my $branchcode = $cgi->param('branchcode') || undef;
84 my @title = $cgi->multi_param('title');
85 my @content = $cgi->multi_param('content');
86 my @lang = $cgi->multi_param('lang');
89 if ( $cgi->param('expirationdate') ) {
90 $expirationdate = output_pref(
92 dt => dt_from_string( scalar $cgi->param('expirationdate') ),
98 my $published_on = output_pref(
100 dt => dt_from_string( scalar $cgi->param('published_on') ),
105 my $number = $cgi->param('number');
108 for my $lang ( @lang ) {
109 my $title = shift @title;
110 my $content = shift @content;
111 my $additional_content = Koha::AdditionalContents->find(
113 category => $category,
115 branchcode => $branchcode,
119 # Delete if title or content is empty
120 unless ( $title and $content ) {
121 $additional_content->delete if $additional_content;
123 } elsif ( $additional_content ) {
125 $additional_content->update(
127 category => $category,
129 location => $location,
130 branchcode => $branchcode,
134 expirationdate => $expirationdate,
135 published_on => $published_on,
137 borrowernumber => $borrowernumber,
143 push @messages, { type => 'error', code => 'error_on_update' };
148 my $additional_content = Koha::AdditionalContent->new(
150 category => $category,
152 location => $location,
153 branchcode => $branchcode,
157 expirationdate => $expirationdate,
158 published_on => $published_on,
160 borrowernumber => $borrowernumber,
163 eval { $additional_content->store; };
166 push @messages, { type => 'error', code => 'error_on_insert' };
174 elsif ( $op eq 'delete_confirmed' ) {
175 my @ids = $cgi->multi_param('ids');
177 eval { Koha::AdditionalContents->search( { idnew => \@ids } )->delete; };
179 if ( $@ or not $deleted ) {
180 push @messages, { type => 'error', code => 'error_on_delete' };
183 push @messages, { type => 'message', code => 'success_on_delete' };
189 if ( $op eq 'list' ) {
190 my $additional_contents = Koha::AdditionalContents->search(
191 { category => $category, lang => 'default' },
192 { order_by => { -desc => 'published_on' } }
194 $template->param( additional_contents => $additional_contents );
197 my $translated_languages = C4::Languages::getTranslatedLanguages;
199 for my $language (@$translated_languages) {
200 for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
201 if ( $language->{plural} ) {
204 lang => $sublanguage->{rfc4646_subtag},
205 description => $sublanguage->{native_description} . ' '
206 . $sublanguage->{region_description} . ' ('
207 . $sublanguage->{rfc4646_subtag} . ')',
213 lang => $sublanguage->{rfc4646_subtag},
214 description => $sublanguage->{native_description} . ' ('
215 . $sublanguage->{rfc4646_subtag} . ')',
220 unshift @languages, {lang => 'default'} if @languages;
224 category => $category,
226 languages => \@languages,
229 output_html_with_http_headers $cgi, $cookie, $template->output;