Bug 22571: Handle control fields in MMT for conditionals
[koha.git] / misc / cronjobs / gather_print_notices.pl
blob6b64a76f90fa95082ecb3b6c1d477dd56d332828
1 #!/usr/bin/perl -w
3 use Modern::Perl;
5 BEGIN {
6 # find Koha's Perl modules
7 # test carefully before changing this
8 use FindBin;
9 eval { require "$FindBin::Bin/../kohalib.pl" };
12 use CGI qw( utf8 ); # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
13 use Koha::Script -cron;
14 use C4::Context;
15 use C4::Debug;
16 use C4::Letters;
17 use C4::Templates;
18 use File::Spec;
19 use Pod::Usage;
20 use Getopt::Long;
21 use C4::Log;
23 use Koha::DateUtils;
24 use Koha::Util::OpenDocument;
25 use MIME::Lite;
27 my (
28 $stylesheet,
29 $help,
30 $split,
31 $html,
32 $csv,
33 $ods,
34 $delimiter,
35 @letter_codes,
36 $send,
37 @emails,
40 $send = 1;
41 GetOptions(
42 'h|help' => \$help,
43 's|split' => \$split,
44 'html' => \$html,
45 'csv' => \$csv,
46 'ods' => \$ods,
47 'd|delimiter:s' => \$delimiter,
48 'letter_code:s' => \@letter_codes,
49 'send!' => \$send,
50 'e|email:s' => \@emails,
51 ) || pod2usage(1);
53 pod2usage(0) if $help;
55 my $output_directory = $ARGV[0];
57 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
58 pod2usage({
59 -exitval => 1,
60 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
61 });
64 # Default value is html
65 $html = 1 if not $html and not $csv and not $ods;
67 if ( $csv and @letter_codes != 1 ) {
68 pod2usage({
69 -exitval => 1,
70 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
71 });
74 if ( $ods and @letter_codes != 1 ) {
75 pod2usage({
76 -exitval => 1,
77 -msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n},
78 });
81 $delimiter ||= q|,|;
83 cronlogaction();
85 my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) ;
86 my $today_syspref = output_pref( { dt => dt_from_string, dateonly => 1 } );
88 my @all_messages = @{ GetPrintMessages() };
90 # Filter by letter_code
91 @all_messages = map {
92 my $letter_code = $_->{letter_code};
94 grep { /^$letter_code$/ } @letter_codes
95 ) ? $_ : ()
96 } @all_messages if @letter_codes;
97 exit unless @all_messages;
99 my ( $html_filenames, $csv_filenames, $ods_filenames );
100 $csv_filenames = print_notices({
101 messages => \@all_messages,
102 split => $split,
103 output_directory => $output_directory,
104 format => 'csv',
105 }) if $csv;
107 $ods_filenames = print_notices({
108 messages => \@all_messages,
109 split => $split,
110 output_directory => $output_directory,
111 format => 'ods',
112 }) if $ods;
114 if ( $html ) {
115 ## carriage return replaced by <br/> as output is html
116 foreach my $message (@all_messages) {
117 local $_ = $message->{'content'};
118 s/\n/<br \/>/g;
119 s/\r//g;
120 $message->{'content'} = $_;
123 $html_filenames = print_notices({
124 messages => \@all_messages,
125 split => $split,
126 output_directory => $output_directory,
127 format => 'html',
131 if ( @emails ) {
132 my $files = {
133 html => $html_filenames,
134 csv => $csv_filenames,
135 ods => $ods_filenames,
137 for my $email ( @emails ) {
138 send_files({
139 directory => $output_directory,
140 files => $files,
141 to => $email,
142 from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
147 sub print_notices {
148 my ( $params ) = @_;
150 my $messages = $params->{messages};
151 my $split = $params->{split};
152 my $output_directory = $params->{output_directory};
153 my $format = $params->{format} // 'html';
155 die "Format $format is not known"
156 unless $format =~ m[^html$|^csv$|^ods$];
158 my ( @filenames, $messages_by_branch );
160 if ( $split ) {
161 foreach my $message (@$messages) {
162 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
164 } else {
165 $messages_by_branch->{all_branches} = $messages;
168 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
169 my $letter_codes = @letter_codes == 0 ? 'all' : join '_', @letter_codes;
170 my $filename = $split
171 ? "notices_$letter_codes-" . $today_iso . "-$branchcode.$format"
172 : "notices_$letter_codes-" . $today_iso . ".$format";
173 my $filepath = File::Spec->catdir( $output_directory, $filename );
174 if ( $format eq 'html' ) {
175 generate_html({
176 messages => $branch_messages,
177 filepath => $filepath,
179 } elsif ( $format eq 'csv' ) {
180 generate_csv ({
181 messages => $branch_messages,
182 filepath => $filepath,
184 } elsif ( $format eq 'ods' ) {
185 _generate_ods ({
186 messages => $branch_messages,
187 filepath => $filepath,
191 if ( $send ) {
192 foreach my $message ( @$branch_messages ) {
193 C4::Letters::_set_message_status(
195 message_id => $message->{'message_id'},
196 status => 'sent'
201 push @filenames, $filename;
203 return \@filenames;
206 sub generate_html {
207 my ( $params ) = @_;
208 my $messages = $params->{messages};
209 my $filepath = $params->{filepath};
211 my $template =
212 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
213 new CGI );
215 $template->param(
216 stylesheet => C4::Context->preference("NoticeCSS"),
217 today => $today_syspref,
218 messages => $messages,
221 open my $OUTPUT, '>encoding(utf-8)', $filepath
222 or die "Could not open $filepath: $!";
223 print $OUTPUT $template->output;
224 close $OUTPUT;
227 sub generate_csv {
228 my ( $params ) = @_;
229 my $messages = $params->{messages};
230 my $filepath = $params->{filepath};
232 open my $OUTPUT, '>encoding(utf-8)', $filepath
233 or die "Could not open $filepath: $!";
234 my ( @csv_lines, $headers );
235 foreach my $message ( @$messages ) {
236 my @lines = split /\n/, $message->{content};
237 chomp for @lines;
239 # We don't have headers, get them
240 unless ( $headers ) {
241 $headers = $lines[0];
242 say $OUTPUT $headers;
245 shift @lines;
246 for my $line ( @lines ) {
247 next if $line =~ /^\s$/;
248 say $OUTPUT $line;
253 sub _generate_ods {
254 my ( $params ) = @_;
255 my $messages = $params->{messages};
256 my $ods_filepath = $params->{filepath};
258 # Prepare sheet
259 my $ods_content;
260 my $has_headers;
261 foreach my $message ( @$messages ) {
262 my @message_lines = split /\n/, $message->{content};
263 chomp for @message_lines;
264 # Get headers from first message
265 if ($has_headers) {
266 shift @message_lines;
267 } else {
268 $has_headers = 1;
270 foreach my $message_line ( @message_lines ) {
271 my @content_row;
272 my @message_cells = split $delimiter, $message_line;
273 foreach ( @message_cells ) {
274 push @content_row, Encode::encode( 'UTF8', $_ );
276 push @$ods_content, \@content_row;
280 # Process
281 generate_ods($ods_filepath, $ods_content);
284 sub send_files {
285 my ( $params ) = @_;
286 my $directory = $params->{directory};
287 my $files = $params->{files};
288 my $to = $params->{to};
289 my $from = $params->{from};
290 return unless $to and $from;
292 my $mail = MIME::Lite->new(
293 From => $from,
294 To => $to,
295 Subject => 'Print notices for ' . $today_syspref,
296 Type => 'multipart/mixed',
299 while ( my ( $type, $filenames ) = each %$files ) {
300 for my $filename ( @$filenames ) {
301 my $mimetype = $type eq 'html'
302 ? 'text/html'
303 : $type eq 'csv'
304 ? 'text/csv'
305 : $type eq 'ods'
306 ? 'application/vnd.oasis.opendocument.spreadsheet'
307 : undef;
309 next unless $mimetype;
311 my $filepath = File::Spec->catdir( $directory, $filename );
313 next unless $filepath or -f $filepath;
315 $mail->attach(
316 Type => $mimetype,
317 Path => $filepath,
318 Filename => $filename,
319 Encoding => 'base64',
324 $mail->send;
327 =head1 NAME
329 gather_print_notices - Print waiting print notices
331 =head1 SYNOPSIS
333 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
335 Will print all waiting print notices to the output_directory.
337 The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
339 =head1 OPTIONS
341 =over
343 =item B<output_directory>
345 Define the output directory where the files will be generated.
347 =item B<--send|--nosend>
349 After files have been generated, messages status is changed from 'pending' to
350 'sent'. This is the default action, without this parameter or with --send.
351 Using --nosend, the message status is not changed.
353 =item B<-s|--split>
355 Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
357 =item B<--html>
359 Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
361 =item B<--csv>
363 Generate the print notices in a csv file.
364 If you use this parameter, the template should contain 2 lines.
365 The first one the csv headers and the second one the value list.
367 For example:
368 cardnumber:patron:email:item
369 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
371 You have to combine this option with one (and only one) letter_code.
373 =item B<--ods>
375 Generate the print notices in a ods file.
377 This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
379 =item B<--letter_code>
381 Filter print messages by letter_code.
382 Several letter_code parameters can be given.
384 =item B<-e|--email>
386 Repeatable.
387 E-mail address to send generated files to.
389 =item B<-h|--help>
391 Print a brief help message
393 =back
395 =head1 AUTHOR
397 Jesse Weaver <pianohacker@gmail.com>
399 Jonathan Druart <jonathan.druart@biblibre.com>
401 =head1 COPYRIGHT
403 Copyright 2009 Jesse Weaver
405 Copyright 2014 BibLibre
407 =head1 LICENSE
408 This file is part of Koha.
410 Koha is free software; you can redistribute it and/or modify it
411 under the terms of the GNU General Public License as published by
412 the Free Software Foundation; either version 3 of the License, or
413 (at your option) any later version.
415 Koha is distributed in the hope that it will be useful, but
416 WITHOUT ANY WARRANTY; without even the implied warranty of
417 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
418 GNU General Public License for more details.
420 You should have received a copy of the GNU General Public License
421 along with Koha; if not, see <http://www.gnu.org/licenses>.
423 =cut