|
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' ) { |