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

(-)a/C4/Reports/Guided.pm (+97 lines)
Lines 27-35 use C4::Context; Link Here
27
use C4::Templates qw/themelanguage/;
27
use C4::Templates qw/themelanguage/;
28
use C4::Koha;
28
use C4::Koha;
29
use Koha::DateUtils;
29
use Koha::DateUtils;
30
use Koha::Patrons;
31
use Koha::Reports;
30
use C4::Output;
32
use C4::Output;
31
use C4::Debug;
33
use C4::Debug;
32
use C4::Log;
34
use C4::Log;
35
use Koha::Notice::Templates;
36
use C4::Letters;
33
37
34
use Koha::AuthorisedValues;
38
use Koha::AuthorisedValues;
35
use Koha::Patron::Categories;
39
use Koha::Patron::Categories;
Lines 928-933 sub ValidateSQLParameters { Link Here
928
    return \@problematic_parameters;
932
    return \@problematic_parameters;
929
}
933
}
930
934
935
=head2 EmailReport
936
937
    my ( $emails, $arrayrefs ) = EmailReport($report_id, $letter_code, $module, $branch, $email)
938
939
Take a report and use it to process a Template Toolkit formatted notice
940
Returns arrayrefs containing prepared letters and errors respectively
941
942
=cut
943
944
sub EmailReport {
945
946
    my $params     = shift;
947
    my $report_id  = $params->{report_id};
948
    my $from       = $params->{from};
949
    my $email      = $params->{email};
950
    my $module     = $params->{module};
951
    my $code       = $params->{code};
952
    my $branch     = $params->{branch} || "";
953
954
    my @errors = ();
955
    my @emails = ();
956
957
    return ( undef, [{ FATAL => "MISSING_PARAMS" }] ) unless ($report_id && $module && $code);
958
959
    return ( undef, [{ FATAL => "NO_LETTER" }] ) unless
960
    my $letter = Koha::Notice::Templates->find({
961
        module     => $module,
962
        code       => $code,
963
        branchcode => $branch,
964
        message_transport_type => 'email',
965
    });
966
    $letter = $letter->unblessed;
967
968
    my $report = Koha::Reports->find( $report_id );
969
    my $sql = $report->savedsql;
970
    return ( { FATAL => "NO_REPORT" } ) unless $sql;
971
972
    my ( $sth, $errors ) = execute_query( $sql ); #don't pass offset or limit, hardcoded limit of 999,999 will be used
973
    return ( undef, [{ FATAL => "REPORT_FAIL" }] ) if $errors;
974
975
    my $counter = 1;
976
    my $template = $letter->{content};
977
978
    while ( my $row = $sth->fetchrow_hashref() ) {
979
        my $email;
980
        my $err_count = scalar @errors;
981
        push ( @errors, { NO_BOR_COL => $counter } ) unless defined $row->{borrowernumber};
982
        push ( @errors, { NO_EMAIL_COL => $counter } ) unless ( (defined $email && defined $row->{$email}) || defined $row->{email} );
983
        push ( @errors, { NO_FROM_COL => $counter } ) unless defined ( $from || $row->{from} );
984
        push ( @errors, { NO_BOR => $row->{borrowernumber} } ) unless Koha::Patrons->find({borrowernumber=>$row->{borrowernumber}});
985
986
        my $from_address = $from || $row->{from};
987
        my $to_address = $email ? $row->{$email} : $row->{email};
988
        push ( @errors, { NOT_PARSE => $counter } ) unless my $content = _process_row_TT( $row, $template );
989
        $counter++;
990
        next if scalar @errors > $err_count; #If any problems, try next
991
992
        $letter->{content}       = $content;
993
        $email->{borrowernumber} = $row->{borrowernumber};
994
        $email->{letter}         = $letter;
995
        $email->{from_address}   = $from_address;
996
        $email->{to_address}     = $to_address;
997
998
        push ( @emails, $email );
999
    }
1000
1001
    return ( \@emails, \@errors );
1002
1003
}
1004
1005
1006
1007
=head2 ProcessRowTT
1008
1009
   my $content = ProcessRowTT($row_hashref, $template);
1010
1011
Accepts a hashref containing values and processes them against Template Toolkit
1012
to produce content
1013
1014
=cut
1015
1016
sub _process_row_TT {
1017
1018
    my ($row, $template) = @_;
1019
1020
    return 0 unless ($row && $template);
1021
    my $content;
1022
    my $processor = Template->new();
1023
    $processor->process( \$template, $row, \$content);
1024
    return $content;
1025
1026
}
1027
931
sub _get_display_value {
1028
sub _get_display_value {
932
    my ( $original_value, $column ) = @_;
1029
    my ( $original_value, $column ) = @_;
933
    if ( $column eq 'periodicity' ) {
1030
    if ( $column eq 'periodicity' ) {
(-)a/misc/cronjobs/patron_emailer.pl (-1 / +176 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
#
4
# This file is part of Koha.
5
#
6
# Koha is free software; you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Koha is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19
use Modern::Perl;
20
21
BEGIN {
22
    # find Koha's Perl modules
23
    # test carefully before changing this
24
    use FindBin;
25
    eval { require "$FindBin::Bin/../kohalib.pl" };
26
}
27
28
use Getopt::Long;
29
use Pod::Usage;
30
31
use C4::Log;
32
use C4::Reports::Guided;
33
34
cronlogaction();
35
36
=head1 NAME
37
38
patron_emailer.pl
39
40
=head1 SYNOPSIS
41
42
patron_emailer.pl
43
    [--report ][--notice][--module] --library  --from
44
45
 Options:
46
    --help                       brief help
47
    --report                     report ID to use as data for email template
48
    --notice                     specific notice code to use
49
    --module                     which module to find the above notice in
50
    --library                    specified branch for selecting notice, will use all libraries by default
51
    --from                       specified email for 'from' address, report column 'from' used if not specified
52
    --email                      specified column to use as 'to' email address, report column 'email' used if not specified
53
    --verbose                    increased verbosity, will print notices and errors
54
    --commit                     send emails, without this script will only report
55
56
head1 OPTIONS
57
58
=over 8
59
60
=item B<-help>
61
62
Print brief help and exit.
63
64
=item B<-man>
65
66
Print full documentation and exit.
67
68
=item B<-report>
69
70
Specify a saved SQL report id in the Koha system to user for the emails. All, and only,
71
    columns in the report will be available for notice template variables
72
73
=item B<-notice>
74
75
Specific notice (CODE) to select
76
77
=item B<-module>
78
79
Which module to find the specified notice in
80
81
=item B<-library>
82
83
Option to specify which branches notice should be used, 'All libraries' is used if not specified
84
85
=item B<-from>
86
87
Specify the sender address of the email, if not specified a 'from' column in the report will be used.
88
89
=item B<-email>
90
91
Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used.
92
93
=item B<-verbose>
94
95
Increased verbosity, reports successes and errors.
96
97
=item B<-commit>
98
99
Send emails, if omitted script will report as verbose.
100
101
=back
102
103
=cut
104
105
my $help     = 0;
106
my $report_id;
107
my $notice;
108
my $module;  #this is only for selecting correct notice - report itself defines available columns, not module
109
my $library; #as above, determines which notice to use, will use 'all libraries' if not specified
110
my $email;   #to specify which column should be used as email in report will use 'email' from borrwers table
111
my $from;    #to specify from address, will expect 'from' column in report if not specified
112
my $verbose  = 0;
113
my $commit   = 0;
114
115
my $error_msgs = {
116
    MISSING_PARAMS => "You must supply a report ID, letter module and code at minimum\n",
117
    NO_LETTER      => "The specified letter was not found, please check your input\n",
118
    NO_REPORT      => "The specified report was not found, please check your input\n",
119
    REPORT_FAIL    => "There was an error running the report, please check your SQL\n",
120
    NO_BOR_COL     => "There was no borrowernumber found for row ",
121
    NO_EMAIL_COL   => "There was no email found for row ",
122
    NO_FROM_COL    => "No from email was specified for row ",
123
    NO_BOR         => "There is no borrower with borrowernumber "
124
};
125
126
GetOptions(
127
    'help|?'      => \$help,
128
    'report=i'    => \$report_id,
129
    'notice=s'    => \$notice,
130
    'module=s'    => \$module,
131
    'library=s'   => \$library,
132
    'email=s'     => \$email,
133
    'from=s'      => \$from,
134
    'verbose'     => \$verbose,
135
    'commit'      => \$commit
136
) or pod2usage(1);
137
pod2usage(1) if $help;
138
pod2usage(1) unless $report_id && $notice && $module;
139
140
my ( $emails, $errors ) = C4::Reports::Guided::EmailReport({
141
    email      => $email,
142
    from       => $from,
143
    report_id  => $report_id,
144
    module     => $module,
145
    code       => $notice,
146
    branch     => $library,
147
    verbose    => $verbose,
148
    commit     => $commit,
149
});
150
151
foreach my $email (@$emails){
152
    print "No emails will be sent!\n" unless $commit;
153
    if( $verbose || !$commit ){
154
        print "Email generated to $email->{to_address} from $email->{from_address}\n";
155
        print "Content:\n";
156
        print $email->{letter}->{content} ."\n";
157
    }
158
    C4::Letters::EnqueueLetter({
159
        letter => $email,
160
        borrowernumber         => $email->{borrowernumber},
161
        message_transport_type => 'email',
162
        from_address           => $email->{from_address},
163
        to_address             => $email->{to_address},
164
    }) if $commit;
165
}
166
167
if( $verbose || !$commit ){
168
    foreach my $error ( @$errors ){
169
        foreach ( keys %{$error} ){
170
            print "$_\n";
171
            if ( $_ eq 'FATAL' ) { print $error_msgs->{ ${$error}{$_} } }
172
            else { print $error_msgs->{$_} . ${$error}{$_} . "\n" }
173
        }
174
    }
175
}
176

Return to bug 16149