View | Details | Raw Unified | Return to bug 11603
Collapse All | Expand All

(-)a/misc/cronjobs/gather_print_notices.pl (-71 / +92 lines)
Lines 1-24 Link Here
1
#!/usr/bin/perl -w
1
#!/usr/bin/perl -w
2
2
3
# Copyright 2009 Jesse Weaver
3
use Modern::Perl;
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
4
23
BEGIN {
5
BEGIN {
24
    # find Koha's Perl modules
6
    # find Koha's Perl modules
Lines 35-68 use C4::Debug; Link Here
35
use C4::Letters;
17
use C4::Letters;
36
use C4::Templates;
18
use C4::Templates;
37
use File::Spec;
19
use File::Spec;
20
use Pod::Usage;
38
use Getopt::Long;
21
use Getopt::Long;
39
22
use Koha::DateUtils;
40
sub usage {
41
    print STDERR <<USAGE;
42
Usage: $0 OUTPUT_DIRECTORY
43
  Will print all waiting print notices to
44
  OUTPUT_DIRECTORY/notices-CURRENT_DATE.html .
45
46
  -s --split  Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.html
47
USAGE
48
    exit $_[0];
49
}
50
23
51
my ( $stylesheet, $help, $split );
24
my ( $stylesheet, $help, $split );
52
25
53
GetOptions(
26
GetOptions(
54
    'h|help'  => \$help,
27
    'h|help'  => \$help,
55
    's|split' => \$split,
28
    's|split' => \$split,
56
) || usage(1);
29
) || pod2usage(1);
57
30
58
usage(0) if ($help);
31
pod2usage(0) if $help;
59
32
60
my $output_directory = $ARGV[0];
33
my $output_directory = $ARGV[0];
61
34
62
if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
35
if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
63
    print STDERR
36
    pod2usage({
64
"Error: You must specify a valid and writeable directory to dump the print notices in.\n";
37
        -exitval => 1,
65
    usage(1);
38
        -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
39
    });
66
}
40
}
67
41
68
my $today        = C4::Dates->new();
42
my $today        = C4::Dates->new();
Lines 79-96 foreach my $message (@all_messages) { Link Here
79
53
80
my $OUTPUT;
54
my $OUTPUT;
81
55
82
if ($split) {
56
print_notices_html({ messages => \@all_messages, split => $split });
83
    my %messages_by_branch;
57
84
    foreach my $message (@all_messages) {
58
sub print_notices_html {
85
        push( @{ $messages_by_branch{ $message->{'branchcode'} } }, $message );
59
    my ( $params ) = @_;
60
61
    my $messages = $params->{messages};
62
    my $split = $params->{split};
63
64
    my $messages_by_branch;
65
    if ( $split ) {
66
        foreach my $message (@$messages) {
67
            push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
68
        }
69
    } else {
70
        $messages_by_branch->{all_branches} = $messages;
86
    }
71
    }
87
72
88
    foreach my $branchcode ( keys %messages_by_branch ) {
73
    while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
89
        my @messages = @{ $messages_by_branch{$branchcode} };
74
        my $filename = $split
90
        my $output_file = File::Spec->catdir( $output_directory,
75
            ? 'holdnotices-' . $today->output('iso') . "-$branchcode.html"
91
            "holdnotices-" . $today->output('iso') . "-$branchcode.html" );
76
            : 'holdnotices-' . $today->output('iso') . ".html";
92
        open $OUTPUT, '>', $output_file
93
            or die "Could not open $output_file: $!";
94
77
95
        my $template =
78
        my $template =
96
          C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
79
          C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
Lines 99-141 if ($split) { Link Here
99
        $template->param(
82
        $template->param(
100
            stylesheet => C4::Context->preference("NoticeCSS"),
83
            stylesheet => C4::Context->preference("NoticeCSS"),
101
            today      => $today->output(),
84
            today      => $today->output(),
102
            messages   => \@messages,
85
            messages   => $branch_messages,
103
        );
86
        );
104
87
88
        my $output_file = File::Spec->catdir( $output_directory, $filename )
89
        open my $OUTPUT, '>', $output_file
90
            or die "Could not open $output_file: $!";
105
        print $OUTPUT $template->output;
91
        print $OUTPUT $template->output;
92
        close $OUTPUT;
106
93
107
        foreach my $message (@messages) {
94
        foreach my $message ( @$branch_messages ) {
108
            C4::Letters::_set_message_status(
95
            C4::Letters::_set_message_status(
109
                { message_id => $message->{'message_id'}, status => 'sent' } );
96
                {
97
                    message_id => $message->{'message_id'},
98
                    status => 'sent'
99
                }
100
            );
110
        }
101
        }
111
112
        close $OUTPUT;
113
    }
102
    }
114
}
103
}
115
else {
116
    my $output_file = File::Spec->catdir( $output_directory,
117
        "holdnotices-" . $today->output('iso') . ".html" );
118
    open $OUTPUT, '>', $output_file
119
        or die "Could not open $output_file: $!";
120
104
105
=head1 NAME
121
106
122
    my $template =
107
gather_print_notices - Print waiting print notices
123
      C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
124
        new CGI );
125
108
126
    $template->param(
109
=head1 SYNOPSIS
127
        stylesheet => C4::Context->preference("NoticeCSS"),
128
        today      => $today->output(),
129
        messages   => \@all_messages,
130
    );
131
110
132
    print $OUTPUT $template->output;
111
gather_print_notices output_directory [-s|--split] [-h|--help]
133
112
134
    foreach my $message (@all_messages) {
113
Will print all waiting print notices to the output_directory.
135
        C4::Letters::_set_message_status(
136
            { message_id => $message->{'message_id'}, status => 'sent' } );
137
    }
138
114
139
    close $OUTPUT;
115
The generated filename will be holdnotices-TODAY.html or holdnotices-TODAY-BRANCHCODE.html if the --split parameter is given.
140
116
141
}
117
=head1 OPTIONS
118
119
=over
120
121
=item B<output_directory>
122
123
Define the output directory where the files will be generated.
124
125
=item B<-s|--split>
126
127
Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.html
128
129
=item B<-h|--help>
130
131
Print a brief help message
132
133
=back
134
135
=head1 AUTHOR
136
137
Jesse Weaver <pianohacker@gmail.com>
138
139
Jonathan Druart <jonathan.druart@biblibre.com>
140
141
=head1 COPYRIGHT
142
143
Copyright 2009 Jesse Weaver
144
145
Copyright 2014 BibLibre
146
147
=head1 LICENSE
148
This file is part of Koha.
149
150
Koha is free software; you can redistribute it and/or modify it
151
under the terms of the GNU General Public License as published by
152
the Free Software Foundation; either version 3 of the License, or
153
(at your option) any later version.
154
155
Koha is distributed in the hope that it will be useful, but
156
WITHOUT ANY WARRANTY; without even the implied warranty of
157
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
158
GNU General Public License for more details.
159
160
You should have received a copy of the GNU General Public License
161
along with Koha; if not, see <http://www.gnu.org/licenses>.
162
163
=cut
142
- 

Return to bug 11603