Bug 24190: (follow-up) Rename AcqLog
[koha.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 qw(get_template_and_user);
28 use C4::Koha;
29 use C4::Context;
30 use C4::Output qw(output_html_with_http_headers);
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         $category = $additional_content->category;
73     }
74     $template->param(
75         additional_content => $additional_content,
76         translated_contents => $translated_contents,
77     );
78 }
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;
83
84     my @title      = $cgi->multi_param('title');
85     my @content    = $cgi->multi_param('content');
86     my @lang       = $cgi->multi_param('lang');
87
88     my $expirationdate;
89     if ( $cgi->param('expirationdate') ) {
90         $expirationdate = output_pref(
91             {
92                 dt => dt_from_string( scalar $cgi->param('expirationdate') ),
93                 dateformat => 'iso',
94                 dateonly   => 1
95             }
96         );
97     }
98     my $published_on = output_pref(
99         {
100             dt         => dt_from_string( scalar $cgi->param('published_on') ),
101             dateformat => 'iso',
102             dateonly   => 1
103         }
104     );
105     my $number = $cgi->param('number');
106
107     my $success = 1;
108     for my $lang ( @lang ) {
109         my $title = shift @title;
110         my $content = shift @content;
111         my $additional_content = Koha::AdditionalContents->find(
112             {
113                 category   => $category,
114                 code       => $code,
115                 branchcode => $branchcode,
116                 lang       => $lang,
117             }
118         );
119         # Delete if title or content is empty
120         unless ( $title and $content ) {
121             $additional_content->delete if $additional_content;
122             next;
123         } elsif ( $additional_content ) {
124             eval {
125                 $additional_content->update(
126                     {
127                         category       => $category,
128                         code           => $code,
129                         location       => $location,
130                         branchcode     => $branchcode,
131                         title          => $title,
132                         content        => $content,
133                         lang           => $lang,
134                         expirationdate => $expirationdate,
135                         published_on   => $published_on,
136                         number         => $number,
137                         borrowernumber => $borrowernumber,
138                     }
139                 );
140             };
141             if ($@) {
142                 $success = 0;
143                 push @messages, { type => 'error', code => 'error_on_update' };
144                 last;
145             }
146         }
147         else {
148             my $additional_content = Koha::AdditionalContent->new(
149                 {
150                     category       => $category,
151                     code           => $code,
152                     location       => $location,
153                     branchcode     => $branchcode,
154                     title          => $title,
155                     content        => $content,
156                     lang           => $lang,
157                     expirationdate => $expirationdate,
158                     published_on   => $published_on,
159                     number         => $number,
160                     borrowernumber => $borrowernumber,
161                 }
162             )->store;
163             eval { $additional_content->store; };
164             if ($@) {
165                 $success = 0;
166                 push @messages, { type => 'error', code => 'error_on_insert' };
167                 last;
168             }
169         }
170
171     }
172     $op = 'list';
173 }
174 elsif ( $op eq 'delete_confirmed' ) {
175     my @ids = $cgi->multi_param('ids');
176     my $deleted =
177       eval { Koha::AdditionalContents->search( { idnew => @ids } )->delete; };
178
179     if ( $@ or not $deleted ) {
180         push @messages, { type => 'error', code => 'error_on_delete' };
181     }
182     else {
183         push @messages, { type => 'message', code => 'success_on_delete' };
184     }
185
186     $op = 'list';
187 }
188
189 if ( $op eq 'list' ) {
190     my $additional_contents = Koha::AdditionalContents->search(
191         { category => $category, lang => 'default' },
192         { order_by => { -desc => 'published_on' } }
193     );
194     $template->param( additional_contents => $additional_contents );
195 }
196
197 my $translated_languages = C4::Languages::getTranslatedLanguages;
198 my @languages;
199 for my $language (@$translated_languages) {
200     for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
201         if ( $language->{plural} ) {
202             push @languages,
203               {
204                 lang        => $sublanguage->{rfc4646_subtag},
205                 description => $sublanguage->{native_description} . ' '
206                   . $sublanguage->{region_description} . ' ('
207                   . $sublanguage->{rfc4646_subtag} . ')',
208               };
209         }
210         else {
211             push @languages,
212               {
213                 lang        => $sublanguage->{rfc4646_subtag},
214                 description => $sublanguage->{native_description} . ' ('
215                   . $sublanguage->{rfc4646_subtag} . ')',
216               };
217         }
218     }
219 }
220 unshift @languages, {lang => 'default'} if @languages;
221
222 $template->param(
223     op        => $op,
224     category  => $category,
225     wysiwyg   => $wysiwyg,
226     languages => \@languages,
227 );
228
229 output_html_with_http_headers $cgi, $cookie, $template->output;