Bug 13889: Add information about cron jobs to system log
[koha.git] / misc / cronjobs / gather_print_notices.pl
1 #!/usr/bin/perl -w
2
3 # Copyright 2009 Jesse Weaver
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 BEGIN {
24     # find Koha's Perl modules
25     # test carefully before changing this
26     use FindBin;
27     eval { require "$FindBin::Bin/../kohalib.pl" };
28 }
29
30 use
31   CGI; # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
32 use C4::Context;
33 use C4::Dates;
34 use C4::Debug;
35 use C4::Letters;
36 use C4::Templates;
37 use File::Spec;
38 use Getopt::Long;
39 use C4::Log;
40
41 sub usage {
42     print STDERR <<USAGE;
43 Usage: $0 OUTPUT_DIRECTORY
44   Will print all waiting print notices to
45   OUTPUT_DIRECTORY/notices-CURRENT_DATE.html .
46
47   -s --split  Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.html
48 USAGE
49     exit $_[0];
50 }
51
52 my ( $stylesheet, $help, $split );
53
54 GetOptions(
55     'h|help'  => \$help,
56     's|split' => \$split,
57 ) || usage(1);
58
59 usage(0) if ($help);
60
61 my $output_directory = $ARGV[0];
62
63 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
64     print STDERR
65 "Error: You must specify a valid and writeable directory to dump the print notices in.\n";
66     usage(1);
67 }
68
69 cronlogaction();
70
71 my $today        = C4::Dates->new();
72 my @all_messages = @{ GetPrintMessages() };
73 exit unless (@all_messages);
74
75 ## carriage return replaced by <br/> as output is html
76 foreach my $message (@all_messages) {
77     local $_ = $message->{'content'};
78     s/\n/<br \/>/g;
79     s/\r//g;
80     $message->{'content'} = $_;
81 }
82
83 my $OUTPUT;
84
85 if ($split) {
86     my %messages_by_branch;
87     foreach my $message (@all_messages) {
88         push( @{ $messages_by_branch{ $message->{'branchcode'} } }, $message );
89     }
90
91     foreach my $branchcode ( keys %messages_by_branch ) {
92         my @messages = @{ $messages_by_branch{$branchcode} };
93         my $output_file = File::Spec->catdir( $output_directory,
94             "holdnotices-" . $today->output('iso') . "-$branchcode.html" );
95         open $OUTPUT, '>', $output_file
96             or die "Could not open $output_file: $!";
97
98         my $template =
99           C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
100             new CGI );
101
102         $template->param(
103             stylesheet => C4::Context->preference("NoticeCSS"),
104             today      => $today->output(),
105             messages   => \@messages,
106         );
107
108         print $OUTPUT $template->output;
109
110         foreach my $message (@messages) {
111             C4::Letters::_set_message_status(
112                 { message_id => $message->{'message_id'}, status => 'sent' } );
113         }
114
115         close $OUTPUT;
116     }
117 }
118 else {
119     my $output_file = File::Spec->catdir( $output_directory,
120         "holdnotices-" . $today->output('iso') . ".html" );
121     open $OUTPUT, '>', $output_file
122         or die "Could not open $output_file: $!";
123
124
125     my $template =
126       C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
127         new CGI );
128
129     $template->param(
130         stylesheet => C4::Context->preference("NoticeCSS"),
131         today      => $today->output(),
132         messages   => \@all_messages,
133     );
134
135     print $OUTPUT $template->output;
136
137     foreach my $message (@all_messages) {
138         C4::Letters::_set_message_status(
139             { message_id => $message->{'message_id'}, status => 'sent' } );
140     }
141
142     close $OUTPUT;
143
144 }