Merge remote branch 'kc/new/bug_4908' into kcmaster
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 BEGIN {
24
25     # find Koha's Perl modules
26     # test carefully before changing this
27     use FindBin;
28     eval { require "$FindBin::Bin/../kohalib.pl" };
29 }
30
31 use CGI; # NOT a CGI script, this is just to keep C4::Output::gettemplate happy
32 use C4::Context;
33 use C4::Dates;
34 use C4::Debug;
35 use C4::Letters;
36 use C4::Output;
37 use File::Spec;
38 use Getopt::Long;
39
40 sub usage {
41     print STDERR <<USAGE;
42 Usage: $0 [ -s STYLESHEET ] OUTPUT_DIRECTORY
43   Will print all waiting print notices to
44   OUTPUT_DIRECTORY/notices-CURRENT_DATE.html .
45   If the filename of a CSS stylesheet is specified with -s, the contents of that
46   file will be included in the HTML.
47 USAGE
48     exit $_[0];
49 }
50
51 my ( $stylesheet, $help );
52
53 GetOptions(
54     's:s' => \$stylesheet,
55     'h|help' => \$help,
56 ) || usage( 1 );
57
58 usage( 0 ) if ( $help );
59
60 my $output_directory = $ARGV[0];
61
62 if ( !$output_directory || !-d $output_directory ) {
63     print STDERR "Error: You must specify a valid directory to dump the print notices in.\n";
64     usage( 1 );
65 }
66
67 my $today = C4::Dates->new();
68 my @messages = @{ GetPrintMessages() };
69 exit unless( @messages );
70
71 open OUTPUT, '>', File::Spec->catdir( $output_directory, "holdnotices-" . $today->output( 'iso' ) . ".html" );
72
73 my $template = C4::Output::gettemplate( 'batch/print-notices.tmpl', 'intranet', new CGI );
74 my $stylesheet_contents = '';
75
76 if ($stylesheet) {
77   open STYLESHEET, '<', $stylesheet;
78   while ( <STYLESHEET> ) { $stylesheet_contents .= $_ }
79   close STYLESHEET;
80 }
81
82 $template->param(
83     stylesheet => $stylesheet_contents,
84     today => $today->output(),
85     messages => \@messages,
86 );
87
88 print OUTPUT $template->output;
89
90 foreach my $message ( @messages ) {
91     C4::Letters::_set_message_status( { message_id => $message->{'message_id'}, status => 'sent' } );
92 }