Bug 27945: Add max_daily_article_requests circulation rule
[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::Log qw( logaction );
31 use C4::Output qw(output_html_with_http_headers);
32 use C4::Languages qw(getTranslatedLanguages);
33 use Koha::DateUtils;
34 use Koha::AdditionalContents;
35
36 my $cgi = CGI->new;
37
38 my $op             = $cgi->param('op') || 'list';
39 my $id             = $cgi->param('id');
40 my $category       = $cgi->param('category') || 'news';
41 my $wysiwyg;
42 if( $cgi->param('editmode') ){
43     $wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
44 } else {
45     $wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
46 }
47
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
49     {
50         template_name   => "tools/additional-contents.tt",
51         query           => $cgi,
52         type            => "intranet",
53         flagsrequired   => { tools => 'edit_additional_contents' },
54     }
55 );
56
57 my @messages;
58 if ( $op eq 'add_form' ) {
59
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(
65                 {
66                     category   => $additional_content->category,
67                     code       => $additional_content->code,
68                     location   => $additional_content->location,
69                     branchcode => $additional_content->branchcode,
70                 }
71             )
72         };
73         $category = $additional_content->category;
74     }
75     $template->param(
76         additional_content => $additional_content,
77         translated_contents => $translated_contents,
78     );
79 }
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;
84
85     my @title      = $cgi->multi_param('title');
86     my @content    = $cgi->multi_param('content');
87     my @lang       = $cgi->multi_param('lang');
88
89     my $expirationdate;
90     if ( $cgi->param('expirationdate') ) {
91         $expirationdate = output_pref(
92             {
93                 dt => dt_from_string( scalar $cgi->param('expirationdate') ),
94                 dateformat => 'iso',
95                 dateonly   => 1
96             }
97         );
98     }
99     my $published_on = output_pref(
100         {
101             dt         => dt_from_string( scalar $cgi->param('published_on') ),
102             dateformat => 'iso',
103             dateonly   => 1
104         }
105     );
106     my $number = $cgi->param('number');
107
108     my $success = 1;
109     for my $lang ( @lang ) {
110         my $title = shift @title;
111         my $content = shift @content;
112         my $additional_content = Koha::AdditionalContents->find(
113             {
114                 category   => $category,
115                 code       => $code,
116                 branchcode => $branchcode,
117                 lang       => $lang,
118             }
119         );
120         # Delete if title or content is empty
121         unless ( $title and $content ) {
122             if ( $additional_content ) {
123                 eval { $additional_content->delete };
124                 unless ($@) {
125                     logaction('NEWS', 'DELETE' , undef, sprintf("%s|%s|%s|%s", $additional_content->code, $additional_content->title, $additional_content->lang, $additional_content->content));
126                 }
127             }
128             next;
129         } elsif ( $additional_content ) {
130             my $updated;
131             eval {
132                 $additional_content->set(
133                     {
134                         category       => $category,
135                         code           => $code,
136                         location       => $location,
137                         branchcode     => $branchcode,
138                         title          => $title,
139                         content        => $content,
140                         lang           => $lang,
141                         expirationdate => $expirationdate,
142                         published_on   => $published_on,
143                         number         => $number,
144                         borrowernumber => $borrowernumber,
145                     }
146                 );
147                 $updated = $additional_content->_result->get_dirty_columns;
148                 $additional_content->store;
149             };
150             if ($@) {
151                 $success = 0;
152                 push @messages, { type => 'error', code => 'error_on_update' };
153                 last;
154             }
155
156             logaction('NEWS', 'MODIFY' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
157                 if C4::Context->preference("NewsLog") && $updated;
158         }
159         else {
160             my $additional_content = Koha::AdditionalContent->new(
161                 {
162                     category       => $category,
163                     code           => $code,
164                     location       => $location,
165                     branchcode     => $branchcode,
166                     title          => $title,
167                     content        => $content,
168                     lang           => $lang,
169                     expirationdate => $expirationdate,
170                     published_on   => $published_on,
171                     number         => $number,
172                     borrowernumber => $borrowernumber,
173                 }
174             )->store;
175             eval { $additional_content->store; };
176             if ($@) {
177                 $success = 0;
178                 push @messages, { type => 'error', code => 'error_on_insert' };
179                 last;
180             }
181
182             logaction('NEWS', 'ADD' , undef, sprintf("%s|%s|%s|%s", $code, $title, $lang, $content))
183                 if C4::Context->preference("NewsLog");
184         }
185
186     }
187     $op = 'list';
188 }
189 elsif ( $op eq 'delete_confirmed' ) {
190     my @ids = $cgi->multi_param('ids');
191     my $deleted = eval {
192
193         my $schema = Koha::Database->new->schema;
194         $schema->txn_do(
195             sub {
196                 my $contents =
197                   Koha::AdditionalContents->search( { idnew => \@ids } );
198
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));
202                     }
203                 }
204                 $contents->delete;
205             }
206         );
207     };
208
209     if ( $@ or not $deleted ) {
210         push @messages, { type => 'error', code => 'error_on_delete' };
211     }
212     else {
213         push @messages, { type => 'message', code => 'success_on_delete' };
214     }
215
216     $op = 'list';
217 }
218
219 if ( $op eq 'list' ) {
220     my $additional_contents = Koha::AdditionalContents->search(
221         { category => $category, lang => 'default' },
222         { order_by => { -desc => 'published_on' } }
223     );
224     $template->param( additional_contents => $additional_contents );
225 }
226
227 my $translated_languages = C4::Languages::getTranslatedLanguages;
228 my @languages;
229 for my $language (@$translated_languages) {
230     for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
231         if ( $language->{plural} ) {
232             push @languages,
233               {
234                 lang        => $sublanguage->{rfc4646_subtag},
235                 description => $sublanguage->{native_description} . ' '
236                   . $sublanguage->{region_description} . ' ('
237                   . $sublanguage->{rfc4646_subtag} . ')',
238               };
239         }
240         else {
241             push @languages,
242               {
243                 lang        => $sublanguage->{rfc4646_subtag},
244                 description => $sublanguage->{native_description} . ' ('
245                   . $sublanguage->{rfc4646_subtag} . ')',
246               };
247         }
248     }
249 }
250 unshift @languages, {lang => 'default'} if @languages;
251
252 $template->param(
253     op        => $op,
254     category  => $category,
255     wysiwyg   => $wysiwyg,
256     languages => \@languages,
257 );
258
259 output_html_with_http_headers $cgi, $cookie, $template->output;