Bug 24387: Rename "News" with "Additional contents"
[koha-ffzg.git] / tools / additional-contents.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Script to manage the opac news.
6 # written 11/04
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
11 #
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.
16 #
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.
21 #
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>.
24
25 use Modern::Perl;
26 use CGI qw ( -utf8 );
27 use C4::Auth;
28 use C4::Koha;
29 use C4::Context;
30 use C4::Output;
31 use C4::Languages qw(getTranslatedLanguages);
32 use Koha::DateUtils;
33 use Koha::AdditionalContents;
34
35 my $cgi = CGI->new;
36
37 my $op             = $cgi->param('op') || 'list';
38 my $id             = $cgi->param('id');
39 my $category       = $cgi->param('category') || 'news';
40 my $wysiwyg;
41 if( $cgi->param('editmode') ){
42     $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
43 } else {
44     $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
45 }
46
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
48     {
49         template_name   => "tools/additional-contents.tt",
50         query           => $cgi,
51         type            => "intranet",
52         flagsrequired   => { tools => 'edit_additional_contents' },
53     }
54 );
55
56 my @messages;
57 if ( $op eq 'add_form' ) {
58
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(
64                 {
65                     category   => $additional_content->category,
66                     code       => $additional_content->code,
67                     location   => $additional_content->location,
68                     branchcode => $additional_content->branchcode,
69                 }
70             )
71         };
72     }
73     $template->param(
74         additional_content => $additional_content,
75         translated_contents => $translated_contents,
76     );
77 }
78 elsif ( $op eq 'add_validate' ) {
79     my $location   = $cgi->param('location');
80     my $code       = $cgi->param('code');
81     my $branchcode = $cgi->param('branchcode') || undef;
82
83     my @title      = $cgi->multi_param('title');
84     my @content    = $cgi->multi_param('content');
85     my @lang       = $cgi->multi_param('lang');
86
87     my $expirationdate;
88     if ( $cgi->param('expirationdate') ) {
89         $expirationdate = output_pref(
90             {
91                 dt => dt_from_string( scalar $cgi->param('expirationdate') ),
92                 dateformat => 'iso',
93                 dateonly   => 1
94             }
95         );
96     }
97     my $published_on = output_pref(
98         {
99             dt         => dt_from_string( scalar $cgi->param('published_on') ),
100             dateformat => 'iso',
101             dateonly   => 1
102         }
103     );
104     my $number = $cgi->param('number');
105
106     my $success = 1;
107     for my $lang ( @lang ) {
108         my $title = shift @title;
109         my $content = shift @content;
110         my $additional_content = Koha::AdditionalContents->find(
111             {
112                 category   => $category,
113                 code       => $code,
114                 branchcode => $branchcode,
115                 lang       => $lang,
116             }
117         );
118         # Delete if title or content is empty
119         unless ( $title and $content ) {
120             $additional_content->delete if $additional_content;
121             next;
122         } elsif ( $additional_content ) {
123             eval {
124                 $additional_content->update(
125                     {
126                         category       => $category,
127                         code           => $code,
128                         location       => $location,
129                         branchcode     => $branchcode,
130                         title          => $title,
131                         content        => $content,
132                         lang           => $lang,
133                         expirationdate => $expirationdate,
134                         published_on   => $published_on,
135                         number         => $number,
136                         borrowernumber => $borrowernumber,
137                     }
138                 );
139             };
140             if ($@) {
141                 $success = 0;
142                 push @messages, { type => 'error', code => 'error_on_update' };
143                 last;
144             }
145         }
146         else {
147             my $additional_content = Koha::AdditionalContent->new(
148                 {
149                     category       => $category,
150                     code           => $code,
151                     location       => $location,
152                     branchcode     => $branchcode,
153                     title          => $title,
154                     content        => $content,
155                     lang           => $lang,
156                     expirationdate => $expirationdate,
157                     published_on   => $published_on,
158                     number         => $number,
159                     borrowernumber => $borrowernumber,
160                 }
161             )->store;
162             eval { $additional_content->store; };
163             if ($@) {
164                 $success = 0;
165                 push @messages, { type => 'error', code => 'error_on_insert' };
166                 last;
167             }
168         }
169
170     }
171     $op = 'list';
172 }
173 elsif ( $op eq 'delete_confirmed' ) {
174     my @ids = $cgi->multi_param('ids');
175     my $deleted =
176       eval { Koha::AdditionalContents->search( { idnew => @ids } )->delete; };
177
178     if ( $@ or not $deleted ) {
179         push @messages, { type => 'error', code => 'error_on_delete' };
180     }
181     else {
182         push @messages, { type => 'message', code => 'success_on_delete' };
183     }
184
185     $op = 'list';
186 }
187
188 if ( $op eq 'list' ) {
189     my $additional_contents = Koha::AdditionalContents->search(
190         { category => $category, lang => 'default' },
191         { order_by => { -desc => 'published_on' } }
192     );
193     $template->param( additional_contents => $additional_contents );
194 }
195
196 my $translated_languages = C4::Languages::getTranslatedLanguages;
197 my @languages;
198 for my $language (@$translated_languages) {
199     for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
200         if ( $language->{plural} ) {
201             push @languages,
202               {
203                 lang        => $sublanguage->{rfc4646_subtag},
204                 description => $sublanguage->{native_description} . ' '
205                   . $sublanguage->{region_description} . ' ('
206                   . $sublanguage->{rfc4646_subtag} . ')',
207               };
208         }
209         else {
210             push @languages,
211               {
212                 lang        => $sublanguage->{rfc4646_subtag},
213                 description => $sublanguage->{native_description} . ' ('
214                   . $sublanguage->{rfc4646_subtag} . ')',
215               };
216         }
217     }
218 }
219 unshift @languages, {lang => 'default'} if @languages;
220
221 $template->param(
222     op        => $op,
223     category  => $category,
224     wysiwyg   => $wysiwyg,
225     languages => \@languages,
226 );
227
228
229 output_html_with_http_headers $cgi, $cookie, $template->output;