Merge pull request #14589 from eileenmcnaughton/money
[civicrm-core.git] / CRM / Contribute / Form / Task / PDFLetterCommon.php
CommitLineData
6a488035
TO
1<?php
2
3/**
4 * This class provides the common functionality for creating PDF letter for
5 * one or a group of contact ids.
6 */
7class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDFLetterCommon {
8
2cac4853
MW
9 /**
10 * Build the form object.
11 *
12 * @var CRM_Core_Form $form
13 */
14 public static function buildQuickForm(&$form) {
99ad38df
MW
15 // use contact form as a base
16 CRM_Contact_Form_Task_PDFLetterCommon::buildQuickForm($form);
17
2cac4853
MW
18 // Contribute PDF tasks allow you to email as well, so we need to add email address to those forms
19 $form->add('select', 'from_email_address', ts('From Email Address'), $form->_fromEmails, TRUE);
20 parent::buildQuickForm($form);
21 }
22
6a488035 23 /**
fe482240 24 * Process the form after the input has been submitted and validated.
6a488035 25 *
493078cb 26 * @param CRM_Contribute_Form_Task $form
59d861cb 27 * @param array $formValues
6a488035 28 */
59d861cb 29 public static function postProcess(&$form, $formValues = NULL) {
30 if (empty($formValues)) {
31 $formValues = $form->controller->exportValues($form->getName());
32 }
b0738a9b 33 list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($formValues);
493078cb 34 $isPDF = FALSE;
be2fb01f 35 $emailParams = [];
22e263ad 36 if (!empty($formValues['email_options'])) {
383c047b 37 $returnProperties['email'] = $returnProperties['on_hold'] = $returnProperties['is_deceased'] = $returnProperties['do_not_email'] = 1;
be2fb01f 38 $emailParams = [
2cac4853
MW
39 'subject' => CRM_Utils_Array::value('subject', $formValues),
40 'from' => CRM_Utils_Array::value('from_email_address', $formValues),
be2fb01f 41 ];
80b646c0
ML
42
43 $emailParams['from'] = CRM_Utils_Mail::formatFromAddress($emailParams['from']);
44
f83f6352
DG
45 // We need display_name for emailLetter() so add to returnProperties here
46 $returnProperties['display_name'] = 1;
22e263ad 47 if (stristr($formValues['email_options'], 'pdfemail')) {
383c047b
DG
48 $isPDF = TRUE;
49 }
50 }
6a488035 51 // update dates ?
353ffa53 52 $receipt_update = isset($formValues['receipt_update']) ? $formValues['receipt_update'] : FALSE;
6a488035 53 $thankyou_update = isset($formValues['thankyou_update']) ? $formValues['thankyou_update'] : FALSE;
353ffa53 54 $nowDate = date('YmdHis');
383c047b 55 $receipts = $thanks = $emailed = 0;
353ffa53 56 $updateStatus = '';
383c047b
DG
57 $task = 'CRM_Contribution_Form_Task_PDFLetterCommon';
58 $realSeparator = ', ';
be2fb01f 59 $tableSeparators = [
b26a113f 60 'td' => '</td><td>',
61 'tr' => '</td></tr><tr><td>',
be2fb01f 62 ];
383c047b
DG
63 //the original thinking was mutliple options - but we are going with only 2 (comma & td) for now in case
64 // there are security (& UI) issues we need to think through
22e263ad 65 if (isset($formValues['group_by_separator'])) {
be2fb01f 66 if (in_array($formValues['group_by_separator'], ['td', 'tr'])) {
b26a113f 67 $realSeparator = $tableSeparators[$formValues['group_by_separator']];
6edcec04
YR
68 }
69 elseif ($formValues['group_by_separator'] == 'br') {
70 $realSeparator = "<br />";
71 }
72 }
1330f57a
SL
73 // a placeholder in case the separator is common in the string - e.g ', '
74 $separator = '****~~~~';
383c047b 75 $groupBy = $formValues['group_by'];
6a488035
TO
76
77 // skip some contacts ?
78 $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
79 $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
e9379b58 80 $contributionIDs = $form->getVar('_contributionIds');
81 if ($form->_includesSoftCredits) {
82 //@todo - comment on what is stored there
83 $contributionIDs = $form->getVar('_contributionContactIds');
84 }
85 list($contributions, $contacts) = self::buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $form->_includesSoftCredits);
be2fb01f
CW
86 $html = [];
87 $contactHtml = $emailedHtml = [];
383c047b
DG
88 foreach ($contributions as $contributionId => $contribution) {
89 $contact = &$contacts[$contribution['contact_id']];
2fe8b920 90 $grouped = FALSE;
91 $groupByID = 0;
22e263ad 92 if ($groupBy) {
383c047b
DG
93 $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
94 $contribution = $contact['combined'][$groupBy][$groupByID];
95 $grouped = TRUE;
6a488035
TO
96 }
97
22e263ad 98 if (empty($groupBy) || empty($contact['is_sent'][$groupBy][$groupByID])) {
2fe8b920 99 $html[$contributionId] = self::generateHtml($contact, $contribution, $groupBy, $contributions, $realSeparator, $tableSeparators, $messageToken, $html_message, $separator, $grouped, $groupByID);
100 $contactHtml[$contact['contact_id']][] = $html[$contributionId];
22e263ad
TO
101 if (!empty($formValues['email_options'])) {
102 if (self::emailLetter($contact, $html[$contributionId], $isPDF, $formValues, $emailParams)) {
d3e86119 103 $emailed++;
22e263ad 104 if (!stristr($formValues['email_options'], 'both')) {
2fe8b920 105 $emailedHtml[$contributionId] = TRUE;
383c047b
DG
106 }
107 }
108 }
2fe8b920 109 $contact['is_sent'][$groupBy][$groupByID] = TRUE;
6a488035 110 }
fd578207 111 // Update receipt/thankyou dates
be2fb01f 112 $contributionParams = ['id' => $contributionId];
6a488035 113 if ($receipt_update) {
fd578207 114 $contributionParams['receipt_date'] = $nowDate;
6a488035
TO
115 }
116 if ($thankyou_update) {
fd578207
MW
117 $contributionParams['thankyou_date'] = $nowDate;
118 }
119 if ($receipt_update || $thankyou_update) {
120 civicrm_api3('Contribution', 'create', $contributionParams);
121 $receipts = ($receipt_update ? $receipts + 1 : $receipts);
122 $thanks = ($thankyou_update ? $thanks + 1 : $thanks);
6a488035
TO
123 }
124 }
59d861cb 125
fe61faf3 126 $contactIds = array_keys($contacts);
3280f327 127 self::createActivities($form, $html_message, $contactIds, CRM_Utils_Array::value('subject', $formValues, ts('Thank you letter')), CRM_Utils_Array::value('campaign_id', $formValues), $contactHtml);
128 $html = array_diff_key($html, $emailedHtml);
2fe8b920 129
59d861cb 130 if (!empty($formValues['is_unit_test'])) {
131 return $html;
132 }
2fe8b920 133
0f2a9bd2 134 //CRM-19761
22e263ad 135 if (!empty($html)) {
0f2a9bd2
BS
136 $type = $formValues['document_type'];
137
138 if ($type == 'pdf') {
139 CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
140 }
141 else {
142 CRM_Utils_PDF_Document::html2doc($html, "CiviLetter.$type", $formValues);
143 }
383c047b 144 }
6a488035
TO
145
146 $form->postProcessHook();
147
383c047b 148 if ($emailed) {
be2fb01f 149 $updateStatus = ts('Receipts have been emailed to %1 contributions.', [1 => $emailed]);
383c047b 150 }
6a488035 151 if ($receipts) {
be2fb01f 152 $updateStatus = ts('Receipt date has been updated for %1 contributions.', [1 => $receipts]);
6a488035
TO
153 }
154 if ($thanks) {
be2fb01f 155 $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', [1 => $thanks]);
6a488035
TO
156 }
157
158 if ($updateStatus) {
159 CRM_Core_Session::setStatus($updateStatus);
160 }
22e263ad 161 if (!empty($html)) {
383c047b 162 // ie. we have only sent emails - lets no show a white screen
292c8687 163 CRM_Utils_System::civiExit();
383c047b
DG
164 }
165 }
665e5ec7 166
8cb33f3b
EM
167 /**
168 * Check whether any of the tokens exist in the html outside a table cell.
169 * If they do the table cell separator is not supported (return false)
170 * At this stage we are only anticipating contributions passed in this way but
171 * it would be easy to add others
172 * @param $tokens
173 * @param $html
174 *
175 * @return bool
176 */
00be9182 177 public static function isValidHTMLWithTableSeparator($tokens, $html) {
be2fb01f 178 $relevantEntities = ['contribution'];
8cb33f3b 179 foreach ($relevantEntities as $entity) {
045bdb93 180 if (isset($tokens[$entity]) && is_array($tokens[$entity])) {
8cb33f3b 181 foreach ($tokens[$entity] as $token) {
353ffa53 182 if (!self::isHtmlTokenInTableCell($token, $entity, $html)) {
8cb33f3b
EM
183 return FALSE;
184 }
185 }
186 }
187 }
188 return TRUE;
189 }
190
191 /**
100fef9d 192 * Check that the token only appears in a table cell. The '</td><td>' separator cannot otherwise work
045bdb93
EM
193 * Calculate the number of times it appears IN the cell & the number of times it appears - should be the same!
194 *
8dfeb798
SM
195 * @param string $token
196 * @param string $entity
197 * @param string $textToSearch
045bdb93 198 *
045bdb93 199 * @return bool
8cb33f3b 200 */
00be9182 201 public static function isHtmlTokenInTableCell($token, $entity, $textToSearch) {
5aece37d
SM
202 $tokenToMatch = $entity . '\.' . $token;
203 $pattern = '|<td(?![\w-])((?!</td>).)*\{' . $tokenToMatch . '\}.*?</td>|si';
8dfeb798
SM
204 $within = preg_match_all($pattern, $textToSearch);
205 $total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch);
045bdb93 206 return ($within == $total);
8cb33f3b
EM
207 }
208
874c9be7
TO
209 /**
210 *
211 * @param string $html_message
212 * @param array $contact
213 * @param array $contribution
214 * @param array $messageToken
874c9be7
TO
215 * @param bool $grouped
216 * Does this letter represent more than one contribution.
217 * @param string $separator
218 * What is the preferred letter separator.
219 * @return string
220 */
3280f327 221 private static function resolveTokens($html_message, $contact, $contribution, $messageToken, $grouped, $separator) {
222 $categories = self::getTokenCategories();
874c9be7 223 $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact, TRUE, $messageToken);
22e263ad 224 if ($grouped) {
874c9be7
TO
225 $tokenHtml = CRM_Utils_Token::replaceMultipleContributionTokens($separator, $tokenHtml, $contribution, TRUE, $messageToken);
226 }
227 else {
228 // no change to normal behaviour to avoid risk of breakage
229 $tokenHtml = CRM_Utils_Token::replaceContributionTokens($tokenHtml, $contribution, TRUE, $messageToken);
230 }
231 $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact, $categories, TRUE);
232 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
383c047b
DG
233 $smarty = CRM_Core_Smarty::singleton();
234 // also add the tokens to the template
235 $smarty->assign_by_ref('contact', $contact);
236 $tokenHtml = $smarty->fetch("string:$tokenHtml");
237 }
238 return $tokenHtml;
239 }
240
241 /**
242 * Generate the contribution array from the form, we fill in the contact details and determine any aggregation
243 * around contact_id of contribution_recur_id
244 *
045bdb93 245 * @param string $groupBy
e9379b58 246 * @param array $contributionIDs
354bc6e1 247 * @param array $returnProperties
014c4014
TO
248 * @param bool $skipOnHold
249 * @param bool $skipDeceased
354bc6e1 250 * @param array $messageToken
8cb33f3b 251 * @param string $task
045bdb93 252 * @param string $separator
e9379b58 253 * @param bool $isIncludeSoftCredits
fe7febc7 254 *
a6c01b45 255 * @return array
383c047b 256 */
e9379b58 257 public static function buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $isIncludeSoftCredits) {
be2fb01f 258 $contributions = $contacts = [];
383c047b 259 foreach ($contributionIDs as $item => $contributionId) {
3280f327 260 // Basic return attributes available to the template.
be2fb01f 261 $returnValues = ['contact_id', 'total_amount', 'financial_type', 'receive_date', 'contribution_campaign_title'];
39b23159
ERL
262 if (!empty($messageToken['contribution'])) {
263 $returnValues = array_merge($messageToken['contribution'], $returnValues);
264 }
265 // retrieve contribution tokens listed in $returnProperties using Contribution.Get API
be2fb01f 266 $contribution = civicrm_api3('Contribution', 'getsingle', [
39b23159
ERL
267 'id' => $contributionId,
268 'return' => $returnValues,
be2fb01f 269 ]);
d31d0888 270 $contribution['campaign'] = CRM_Utils_Array::value('contribution_campaign_title', $contribution);
39b23159 271 $contributions[$contributionId] = $contribution;
8ea4b051 272
e9379b58 273 if ($isIncludeSoftCredits) {
ff926d67
EM
274 //@todo find out why this happens & add comments
275 list($contactID) = explode('-', $item);
276 $contactID = (int) $contactID;
874c9be7 277 }
ff926d67 278 else {
874c9be7 279 $contactID = $contribution['contact_id'];
ff926d67 280 }
22e263ad 281 if (!isset($contacts[$contactID])) {
be2fb01f 282 $contacts[$contactID] = [];
ff926d67 283 $contacts[$contactID]['contact_aggregate'] = 0;
be2fb01f 284 $contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = [];
ff926d67 285 }
383c047b 286
ff926d67
EM
287 $contacts[$contactID]['contact_aggregate'] += $contribution['total_amount'];
288 $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
383c047b 289
ff926d67 290 $contacts[$contactID]['contribution_ids'][$groupBy][$groupByID][$contributionId] = TRUE;
22e263ad 291 if (!isset($contacts[$contactID]['combined'][$groupBy]) || !isset($contacts[$contactID]['combined'][$groupBy][$groupByID])) {
ff926d67
EM
292 $contacts[$contactID]['combined'][$groupBy][$groupByID] = $contribution;
293 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] = $contribution['total_amount'];
383c047b 294 }
ff926d67
EM
295 else {
296 $contacts[$contactID]['combined'][$groupBy][$groupByID] = self::combineContributions($contacts[$contactID]['combined'][$groupBy][$groupByID], $contribution, $separator);
297 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] += $contribution['total_amount'];
383c047b
DG
298 }
299 }
8ea4b051 300 // Assign the available contributions before calling tokens so hooks parsing smarty can access it.
301 // Note that in core code you can only use smarty here if enable if for the whole site, incl
302 // CiviMail, with a big performance impact.
303 // Hooks allow more nuanced smarty usage here.
304 CRM_Core_Smarty::singleton()->assign('contributions', $contributions);
305 foreach ($contacts as $contactID => $contact) {
be2fb01f 306 $tokenResolvedContacts = CRM_Utils_Token::getTokenDetails(['contact_id' => $contactID],
8ea4b051 307 $returnProperties,
308 $skipOnHold,
309 $skipDeceased,
310 NULL,
311 $messageToken,
312 $task
313 );
314 $contacts[$contactID] = array_merge($tokenResolvedContacts[0][$contactID], $contact);
315 }
be2fb01f 316 return [$contributions, $contacts];
383c047b
DG
317 }
318
319 /**
320 * We combine the contributions by adding the contribution to each field with the separator in
321 * between the existing value and the new one. We put the separator there even if empty so it is clear what the
322 * value for previous contributions was
fe7febc7 323 *
045bdb93
EM
324 * @param array $existing
325 * @param array $contribution
326 * @param string $separator
fe7febc7 327 *
045bdb93 328 * @return array
383c047b 329 */
00be9182 330 public static function combineContributions($existing, $contribution, $separator) {
383c047b 331 foreach ($contribution as $field => $value) {
7eebfcb9 332 $existing[$field] = isset($existing[$field]) ? $existing[$field] . $separator : '';
353ffa53 333 $existing[$field] .= $value;
383c047b
DG
334 }
335 return $existing;
336 }
337
338 /**
339 * We are going to retrieve the combined contribution and if smarty mail is enabled we
340 * will also assign an array of contributions for this contact to the smarty template
fe7febc7 341 *
383c047b
DG
342 * @param array $contact
343 * @param array $contributions
fe7febc7 344 * @param $groupBy
100fef9d 345 * @param int $groupByID
383c047b 346 */
00be9182 347 public static function assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID) {
383c047b 348 CRM_Core_Smarty::singleton()->assign('contact_aggregate', $contact['contact_aggregate']);
353ffa53
TO
349 CRM_Core_Smarty::singleton()
350 ->assign('contributions', array_intersect_key($contributions, $contact['contribution_ids'][$groupBy][$groupByID]));
383c047b
DG
351 CRM_Core_Smarty::singleton()->assign('contribution_aggregate', $contact['aggregates'][$groupBy][$groupByID]);
352
353 }
354
355 /**
fe482240 356 * Send pdf by email.
fe7febc7 357 *
383c047b
DG
358 * @param array $contact
359 * @param string $html
fe7febc7
EM
360 *
361 * @param $is_pdf
362 * @param array $format
363 * @param array $params
364 *
365 * @return bool
383c047b 366 */
be2fb01f 367 public static function emailLetter($contact, $html, $is_pdf, $format = [], $params = []) {
383c047b 368 try {
22e263ad 369 if (empty($contact['email'])) {
383c047b
DG
370 return FALSE;
371 }
be2fb01f 372 $mustBeEmpty = ['do_not_email', 'is_deceased', 'on_hold'];
383c047b 373 foreach ($mustBeEmpty as $emptyField) {
22e263ad 374 if (!empty($contact[$emptyField])) {
383c047b
DG
375 return FALSE;
376 }
377 }
378
be2fb01f 379 $defaults = [
383c047b
DG
380 'toName' => $contact['display_name'],
381 'toEmail' => $contact['email'],
383c047b
DG
382 'text' => '',
383 'html' => $html,
be2fb01f 384 ];
22e263ad 385 if (empty($params['from'])) {
383c047b
DG
386 $emails = CRM_Core_BAO_Email::getFromEmail();
387 $emails = array_keys($emails);
388 $defaults['from'] = array_pop($emails);
389 }
beac1417
MW
390 else {
391 $defaults['from'] = $params['from'];
392 }
ab0f580c 393 if (!empty($params['subject'])) {
c285f292 394 $defaults['subject'] = $params['subject'];
ab0f580c
BM
395 }
396 else {
c285f292 397 $defaults['subject'] = ts('Thank you for your contribution/s');
ab0f580c 398 }
22e263ad 399 if ($is_pdf) {
383c047b 400 $defaults['html'] = ts('Please see attached');
be2fb01f 401 $defaults['attachments'] = [CRM_Utils_Mail::appendPDF('ThankYou.pdf', $html, $format)];
383c047b
DG
402 }
403 $params = array_merge($defaults);
404 return CRM_Utils_Mail::send($params);
405 }
406 catch (CRM_Core_Exception $e) {
407 return FALSE;
408 }
6a488035 409 }
96025800 410
2fe8b920 411 /**
412 * @param $contact
413 * @param $formValues
414 * @param $contribution
415 * @param $groupBy
416 * @param $contributions
417 * @param $realSeparator
418 * @param $tableSeparators
419 * @param $messageToken
420 * @param $html_message
421 * @param $separator
422 * @param $categories
423 * @param bool $grouped
424 * @param int $groupByID
425 *
426 * @return string
427 */
428 protected static function generateHtml(&$contact, $contribution, $groupBy, $contributions, $realSeparator, $tableSeparators, $messageToken, $html_message, $separator, $grouped, $groupByID) {
429 static $validated = FALSE;
430 $html = NULL;
431
2fe8b920 432 self::assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID);
433
434 if (empty($groupBy) || empty($contact['is_sent'][$groupBy][$groupByID])) {
435 if (!$validated && in_array($realSeparator, $tableSeparators) && !self::isValidHTMLWithTableSeparator($messageToken, $html_message)) {
436 $realSeparator = ', ';
437 CRM_Core_Session::setStatus(ts('You have selected the table cell separator, but one or more token fields are not placed inside a table cell. This would result in invalid HTML, so comma separators have been used instead.'));
438 }
439 $validated = TRUE;
3280f327 440 $html = str_replace($separator, $realSeparator, self::resolveTokens($html_message, $contact, $contribution, $messageToken, $grouped, $separator));
2fe8b920 441 }
442
443 return $html;
444 }
445
6a488035 446}