forked from emaijala/MLInvoice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice_printer_email_trait.php
More file actions
341 lines (320 loc) · 12.6 KB
/
invoice_printer_email_trait.php
File metadata and controls
341 lines (320 loc) · 12.6 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
* Email invoice trait
*
* PHP version 5
*
* Copyright (C) 2010-2018 Ere Maijala
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
require_once 'htmlfuncs.php';
require_once 'miscfuncs.php';
require_once 'mailer.php';
/**
* Email invoice trait
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
trait InvoicePrinterEmailTrait
{
protected $emailFrom = '';
protected $emailTo = '';
protected $emailCC = '';
protected $emailBCC = '';
protected $emailSubject = '';
protected $emailBody = '';
/**
* Main method for printing
*
* @return void
*/
public function printInvoice()
{
if (!$this->authenticated) {
parent::printInvoice();
return;
}
$senderData = $this->senderData;
$recipientData = $this->recipientData;
$invoiceData = $this->invoiceData;
$defaultId = getRequest('default_body_text');
$defaultValue = $defaultId ? getDefaultValue($defaultId, true) : null;
$defaultSettings = $this->parseDefaultSettings($defaultValue);
$defaultRecipient = isset($recipientData['email'])
? $recipientData['email'] : '';
$recipients = [];
$contacts = $this->getContactPersons();
foreach ($contacts as $contact) {
if ($contact && !empty($contact['email'])) {
if (!empty(trim($contact['contact_person']))) {
$email = str_replace(',', ' ', $contact['contact_person'])
. ' <' . $contact['email'] . '>';
} else {
$email = $contact['email'];
}
$recipients[] = $email;
}
}
if ($recipients) {
$defaultRecipient = implode(', ', $recipients);
}
$this->emailFrom = !empty($defaultSettings['from'])
? $defaultSettings['from'] : getRequest('email_from', '');
if (!$this->emailFrom) {
if (!empty($senderData['invoice_email_from'])) {
$this->emailFrom = $senderData['invoice_email_from'];
} elseif (!empty($senderData['email'])) {
$this->emailFrom = $senderData['email'];
}
}
$this->emailTo = getRequest('email_to', $defaultRecipient);
$this->emailCC = !empty($defaultSettings['cc'])
? $defaultSettings['cc'] : getRequest('email_cc', '');
$this->emailBCC = !empty($defaultSettings['bcc'])
? $defaultSettings['bcc'] : getRequest(
'email_bcc',
isset($senderData['invoice_email_bcc'])
? $senderData['invoice_email_bcc'] : ''
);
$this->emailSubject = $this->replacePlaceholders(
!empty($defaultSettings['subject'])
? $defaultSettings['subject']
: getRequest('email_subject', $this->getDefaultSubject())
);
$emailBody = '';
if (!empty($defaultValue['content'])) {
$emailBody = $defaultValue['content'];
}
$this->emailBody = $this->replacePlaceholders(
$emailBody ? $emailBody
: getRequest('email_body', $this->getDefaultBody())
);
$send = getRequest('email_send', '');
if (!$send || !$this->emailFrom || !$this->emailTo || !$this->emailSubject
|| !$this->emailBody
) {
$this->showEmailForm(
$send ? Translator::translate('EmailFillRequiredFields') : ''
);
return;
}
// Don't merge attachments to the invoice PDF
$attachments = $this->attachments;
$this->attachments = [];
$result = $this->createPrintout();
$this->sendEmail($result, $attachments);
}
/**
* Get default message body
*
* @return string
*/
protected function getDefaultBody()
{
$key = 'invoice_email_body';
if ($this->printStyle == 'receipt') {
$key = 'receipt_email_body';
}
return isset($this->senderData[$key]) ? $this->senderData[$key] : '';
}
/**
* Get default subject
*
* @return string
*/
protected function getDefaultSubject()
{
$key = 'invoice_email_subject';
if ($this->printStyle == 'receipt') {
$key = 'receipt_email_subject';
}
return isset($this->senderData[$key]) ? $this->senderData[$key] : '';
}
/**
* Display the email form
*
* @param string $errorMsg Any error message
*
* @return void
*/
protected function showEmailForm($errorMsg = '')
{
$senderData = $this->senderData;
$recipientData = $this->recipientData;
echo htmlPageStart(Translator::translate('SendEmail'));
?>
<body>
<div class="pagewrapper ui-widget ui-widget-content">
<?php echo htmlMainTabs('open_invoices'); ?>
<div id="email_form_container" class="form_container">
<h1><?php echo Translator::translate('SendEmail')?></h1>
<?php if ($errorMsg) echo '<div class="ui-state-error-text">' . $errorMsg . "<br><br></div>\n";?>
<form method="POST" id="email_form">
<input type="hidden" name="id" value="<?php echo htmlspecialchars(getRequest('id', ''))?>">
<input type="hidden" name="template" value="<?php echo htmlspecialchars(getRequest('template', ''))?>">
<input type="hidden" id="email_send" name="email_send" value="0">
<input type="hidden" name="func" value="<?php echo htmlspecialchars(getRequest('func', ''))?>">
<div class="medium_label"><?php echo Translator::translate('EmailFrom')?></div>
<div class="field">
<input type="text" id="email_from" name="email_from" class="medium" value="<?php echo htmlspecialchars($this->emailFrom)?>">
</div>
<div class="medium_label"><?php echo Translator::translate('EmailTo')?></div>
<div class="field">
<input type="text" id="email_to" name="email_to" class="medium" value="<?php echo htmlspecialchars($this->emailTo)?>">
</div>
<div class="medium_label"><?php echo Translator::translate('EmailCC')?></div>
<div class="field">
<input type="text" id="email_cc" name="email_cc" class="medium" value="<?php echo htmlspecialchars($this->emailCC)?>">
</div>
<div class="medium_label"><?php echo Translator::translate('EmailBCC')?></div>
<div class="field">
<input type="text" id="email_bcc" name="email_bcc" class="medium" value="<?php echo htmlspecialchars($this->emailBCC)?>">
</div>
<div class="medium_label"><?php echo Translator::translate('EmailSubject')?></div>
<div class="field">
<input type="text" id="email_subject" name="email_subject" class="medium" value="<?php echo htmlspecialchars($this->emailSubject)?>">
</div>
<div class="medium_label"><?php echo Translator::translate('EmailBody')?></div>
<div class="field">
<textarea id="emailBody" name="email_body" class="email_body" cols="80" rows="24"><?php echo htmlspecialchars($this->emailBody)?></textarea>
<span class="select-default-text" data-type="email" data-target="email_form" data-send-form-param="default_body_text"></span>
</div>
<div class="medium_label"><?php echo Translator::translate('EmailAttachments')?></div>
<div class="field">
<?php
$filenames = [];
if (!isset($this->printParams['attachment'])
|| $this->printParams['attachment']
) {
$filename = $this->outputFileName ? $this->outputFileName
: getSetting('invoice_pdf_filename');
$filenames[] = htmlentities($this->getPrintOutFileName($filename));
}
foreach ($this->attachments as $attachment) {
$filenames[] = htmlentities($attachment['filename']);
}
echo $filenames ? implode('<br>', $filenames) : '-';
?>
</div>
<div class="form_buttons" style="clear: both">
<a class="actionlink" onclick="$('#email_send').val(1); $('#email_form').submit(); return false;" href="#">
<?php echo Translator::translate('Send')?>
</a>
<a class="actionlink" onclick="if (window.opener) { window.close(); } else { history.back(); } return false;" href="#">
<?php echo Translator::translate('Cancel')?>
</a>
<span id="spinner" style="display: none"><img src="images/spinner.gif" alt=""></span>
</div>
</form>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
$('#email_form').submit(function() {
$('#spinner').show();
});
});
</script>
</html>
<?php
}
/**
* Send email
*
* @param array $printout Printout data
* @param array $attachments Attachments
*
* @return void
*/
protected function sendEmail($printout, $attachments)
{
mb_internal_encoding('UTF-8');
$emailAttachments = [];
if (!isset($this->printParams['attachment'])
|| $this->printParams['attachment']
) {
$filename = $this->outputFileName ? $this->outputFileName
: getSetting('invoice_pdf_filename');
$filename = $this->getPrintOutFileName($filename);
$emailAttachments[] = [
'filename' => $filename,
'data' => $printout['data'],
'mimetype' => 'application/pdf'
];
foreach ($attachments as $attachment) {
$attachment = getInvoiceAttachment($attachment['id']);
$emailAttachments[] = [
'filename' => $attachment['filename'],
'data' => $attachment['filedata'],
'mimetype' => $attachment['mimetype']
];
}
}
$mailer = new Mailer();
$result = $mailer->sendEmail(
$this->emailFrom,
$this->emailTo,
$this->emailCC,
$this->emailBCC,
$this->emailSubject,
$this->emailBody,
$emailAttachments
);
if ($result) {
$this->showEmailForm($mailer->getErrorMessage());
}
if (is_callable([$this, 'emailSent'])) {
$this->emailSent();
}
$_SESSION['formMessage'] = 'EmailSent';
header(
'Location: index.php?func='
. sanitize(getRequest('func', 'open_invoices'))
. "&list=invoices&form=invoice&id={$this->invoiceId}"
);
}
/**
* Parse default value
*
* @param array $defaultValue Default value
*
* @return array
*/
protected function parseDefaultSettings($defaultValue)
{
if (empty($defaultValue['additional'])) {
return [];
}
$settings = [];
foreach (explode("\n", $defaultValue['additional']) as $line) {
$parts = explode(':', $line, 2);
if (isset($parts[1])) {
$settings[strtolower(trim($parts[0]))] = trim($parts[1]);
}
}
return $settings;
}
}