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

(-)a/C4/Reports/Guided.pm (-1 / +90 lines)
Lines 27-32 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;
30
use C4::Output;
31
use C4::Output;
31
use C4::Debug;
32
use C4::Debug;
32
use C4::Log;
33
use C4::Log;
Lines 854-860 sub get_sql { Link Here
854
sub get_results {
855
sub get_results {
855
    my ( $report_id ) = @_;
856
    my ( $report_id ) = @_;
856
    my $dbh = C4::Context->dbh;
857
    my $dbh = C4::Context->dbh;
857
    warn $report_id;
858
    return $dbh->selectall_arrayref(q|
858
    return $dbh->selectall_arrayref(q|
859
        SELECT id, report, date_run
859
        SELECT id, report, date_run
860
        FROM saved_reports
860
        FROM saved_reports
Lines 976-981 sub ValidateSQLParameters { Link Here
976
    return \@problematic_parameters;
976
    return \@problematic_parameters;
977
}
977
}
978
978
979
=head2 EmailReport
980
981
    EmailReport($report_id, $letter_code, $module, $branch, $email)
982
983
Take a report and use it to process a Template Toolkit formatted notice
984
985
=cut
986
987
sub EmailReport {
988
989
    my $params = shift;
990
    my $report = $params->{report};
991
    my $from   = $params->{from};
992
    my $email  = $params->{email};
993
    my $module = $params->{module};
994
    my $code   = $params->{code};
995
    my $branch = $params->{branch} || "";
996
997
    return ("You must provide a report, notice and module at minium") unless ($report && $module && $code);
998
999
    return ("Specified letter not found") unless
1000
    my $letter = Koha::Notice::Templates->find({
1001
        module =>$module,
1002
        code => $code,
1003
        branchcode => $branch,
1004
        message_transport_type => 'email',
1005
    })->unblessed;
1006
1007
    return ("Report not found!") unless my $sql = get_saved_report( $report )->{savedsql};
1008
1009
    my ( $sth, $errors ) = execute_query( $sql ); #don't pass offset or limit, hardcoded limit of 999,999 will be used
1010
    return ("Report failed: $errors") unless $sth;
1011
1012
    my $counter = 0;
1013
    my $template = $letter->{content};
1014
    my @errors;
1015
1016
    while ( my $row = $sth->fetchrow_hashref() ) {
1017
        my $err_count = scalar @errors;
1018
        push ( @errors, "No borrowernumber column in report at row $counter" ) unless defined $row->{borrowernumber};
1019
        push ( @errors, "Expected email column not found in report at row $counter" ) unless ( (defined $email && defined $row->{$email}) || defined $row->{email} );
1020
        push ( @errors, "No 'from' email column found in report at row $counter") unless defined ( $from || $row->{from} );
1021
        push ( @errors, "Borrower not found for borrowernumber ".$row->{borrowernumber}.", skipping." ) unless Koha::Patrons->find({borrowernumber=>$row->{borrowernumber}});
1022
1023
        my $from_address = $from || $row->{from};
1024
        my $to_address = $email ? $row->{$email} : $row->{email};
1025
        push ( @errors, "Unable to parse content at row $counter" ) unless my $content = _process_row_TT( $row, $template );
1026
        $letter->{content}=$content;
1027
1028
        $counter++;
1029
        next if scalar @errors > $err_count;
1030
1031
        C4::Letters::EnqueueLetter({
1032
            letter => $letter,
1033
            borrowernumber         => $row->{borrowernumber},
1034
            message_transport_type => 'email',
1035
            from_address           => $from_address,
1036
            to_address             => $to_address,
1037
        });
1038
1039
    }
1040
1041
    return @errors;
1042
1043
}
1044
1045
1046
1047
=head2 ProcessRowTT
1048
1049
   my $content = ProcessRowTT($row_hashref, $template);
1050
1051
Accepts a hashref containing values and processes them against Template Toolkit
1052
to produce content
1053
1054
=cut
1055
1056
sub _process_row_TT {
1057
1058
    my ($row, $template) = @_;
1059
1060
    return 0 unless ($row && $template);
1061
    my $content;
1062
    my $processor = Template->new();
1063
    $processor->process( \$template, $row, \$content);
1064
    return $content;
1065
1066
}
1067
979
sub _get_display_value {
1068
sub _get_display_value {
980
    my ( $original_value, $column ) = @_;
1069
    my ( $original_value, $column ) = @_;
981
    if ( $column eq 'periodicity' ) {
1070
    if ( $column eq 'periodicity' ) {
(-)a/misc/cronjobs/patron_emailer.pl (-1 / +133 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::Reserves;
32
use C4::Log;
33
use C4::Reports::Guided;
34
use Koha::Holds;
35
use Koha::Calendar;
36
use Koha::DateUtils;
37
use Koha::Libraries;
38
use Koha::Notice::Templates;
39
40
cronlogaction();
41
42
=head1 NAME
43
44
patron_emailer.pl
45
46
=head1 SYNOPSIS
47
48
patron_emailer.pl
49
    [-report][-notice][-module][-library][-from]
50
51
 Options:
52
    -help                       brief help
53
    -report                     report to use as data for email template
54
    -notice                     specific notice code to use
55
    -module                     which module to find the above notice in
56
    -library                    specified branch for selecting notice, will use all libraries by default
57
    -from                       specified email for 'from' address, report column 'from' used if not specified
58
    -email                      specified column to use as 'to' email address, report column 'email' used if not specified
59
60
head1 OPTIONS
61
62
=over 8
63
64
=item B<-help>
65
66
Print brief help and exit.
67
68
=item B<-man>
69
70
Print full documentation and exit.
71
72
=item B<-report>
73
74
Specify a saved SQL report in the Koha system to user for the emails. All, and only,
75
    columns in the report will be available for notice template variables
76
77
=item B<-notice>
78
79
Specific notice (CODE) to select
80
81
=item B<-module>
82
83
Which module to find the specified notice in
84
85
=item B<-library>
86
87
Option to specify which branches notice should be used, 'All libraries' is used if not specified
88
89
=item B<-from>
90
91
Specify the sender address of the email, if not specified a 'from' column in the report will be used.
92
93
=item B<-from>
94
95
Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used.
96
97
=back
98
99
=cut
100
101
my $help = 0;
102
my $report;
103
my $notice;
104
my $module;  #this is only for selecting correct report - report itself defines available columns, not module
105
my $library; #as above, determines which notice to use, will use 'all libraries' if not specified
106
my $email;   #to specify which column should be used as email in report will use 'email' from borrwers table
107
my $from;    #to specify from address, will expect 'from' column in report if not specified
108
my $verbose      = 0;
109
110
GetOptions(
111
    'help|?'    => \$help,
112
    'report=i'    => \$report,
113
    'notice=s'    => \$notice,
114
    'module=s'    => \$module,
115
    'library=s'   => \$library,
116
    'email=s'     => \$email,
117
    'from=s'      => \$from,
118
) or pod2usage(1);
119
pod2usage(1) if $help;
120
121
my @errors = C4::Reports::Guided::EmailReport({
122
    email      => $email,
123
    from       => $from,
124
    report     => $report,
125
    module     => $module,
126
    code       => $notice,
127
    branchcode => $library,
128
});
129
130
foreach my $error ( @errors ){
131
    print $error."\n";
132
}
133

Return to bug 16149