-
Notifications
You must be signed in to change notification settings - Fork 0
/
uc_pdf_invoice_mail.module
119 lines (106 loc) · 3.6 KB
/
uc_pdf_invoice_mail.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @file
* Generates PDF of order invoice and sends as attachment in mail.
*/
require_once libraries_get_path('dompdf') . '/autoload.inc.php';
use Dompdf\Dompdf;
/**
* Implements hook_help().
*/
function uc_pdf_invoice_mail_help($path, $arg) {
switch ($path) {
case 'admin/help#uc_pdf_invoice_mail':
$filepath = dirname(__FILE__) . '/README.md';
if (file_exists($filepath)) {
$readme = file_get_contents($filepath);
}
else {
$filepath = dirname(__FILE__) . '/README.txt';
if (file_exists($filepath)) {
$readme = file_get_contents($filepath);
}
}
if (!isset($readme)) {
return NULL;
}
if (module_exists('markdown')) {
$filters = module_invoke('markdown', 'filter_info');
$info = $filters['filter_markdown'];
if (function_exists($info['process callback'])) {
$output = $info['process callback']($readme, NULL);
}
else {
$output = '<pre>' . $readme . '</pre>';
}
}
else {
$output = '<pre>' . $readme . '</pre>';
}
return $output;
}
}
/**
* Implements hook_mail_alter().
*/
function uc_pdf_invoice_mail_mail_alter(&$message) {
if (strpos($message['id'], 'uc_order_action-mail') !== FALSE) {
if (file_exists(libraries_get_path('dompdf') . '/autoload.inc.php')) {
$body = $message['body'];
$invoiceData = implode("", $body);
if (!empty($invoiceData)) {
$dompdf = new Dompdf();
$dompdf->loadHtml($invoiceData);
$dompdf->render();
$filename = drupal_tempnam('temporary://', 'invoice_') . '.pdf';
$output = $dompdf->output();
file_put_contents($filename, $output);
$file = new StdClass();
$file->uid = 1;
$file->filename = $filename;
$file->uri = $filename;
$file->filemime = file_get_mimetype($filename);
$file->filesize = filesize($filename);
$file->timestamp = REQUEST_TIME;
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
$pdf_attachment = array(
'filecontent' => file_get_contents($file->uri),
'filemime' => $file->filemime,
'filename' => 'invoice.pdf',
);
$message['params']['attachment'] = $pdf_attachment;
uc_pdf_invoice_mail_add_attachment($message);
}
}
}
}
/**
* Update Message ContentType.
*/
function uc_pdf_invoice_mail_add_attachment(array &$message) {
if (!empty($message['params']['attachment'])) {
$params = $message['params'];
$attachment = $params['attachment'];
$filename = basename($attachment['filename']);
$filecontent = $attachment['filecontent'];
$content = chunk_split(base64_encode($filecontent));
$uid = md5(uniqid(time()));
$messageHeader = &$message['headers'];
$originalContentType = $messageHeader['Content-Type'];
$messageHeader['Content-Type'] = "multipart/mixed; boundary=\"$uid\"";
$header = "This is a multi-part message in MIME format.\r\n";
$header .= "--$uid\r\n";
$header .= "Content-Type: $originalContentType \r\n\r\n";
$header .= wordwrap(implode('', $message['body'])) . "\r\n\r\n";
$header .= "--$uid\r\n";
$header .= "Content-Type: $attachment[filemime];
name=\"" . $filename . "\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment;
filename=\"" . $filename . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--$uid--";
$message['body'] = [$header];
}
}