Merge pull request #15142 from eileenmcnaughton/pradeep2
[civicrm-core.git] / CRM / Contribute / Form / Task / PDFLetterCommon.php
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 */
7 class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDFLetterCommon {
8
9 /**
10 * Build the form object.
11 *
12 * @var CRM_Core_Form $form
13 */
14 public static function buildQuickForm(&$form) {
15 // use contact form as a base
16 CRM_Contact_Form_Task_PDFLetterCommon::buildQuickForm($form);
17
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
23 /**
24 * Process the form after the input has been submitted and validated.
25 *
26 * @param CRM_Contribute_Form_Task $form
27 * @param array $formValues
28 */
29 public static function postProcess(&$form, $formValues = NULL) {
30 if (empty($formValues)) {
31 $formValues = $form->controller->exportValues($form->getName());
32 }
33 list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($formValues);
34 $isPDF = FALSE;
35 $emailParams = [];
36 if (!empty($formValues['email_options'])) {
37 $returnProperties['email'] = $returnProperties['on_hold'] = $returnProperties['is_deceased'] = $returnProperties['do_not_email'] = 1;
38 $emailParams = [
39 'subject' => CRM_Utils_Array::value('subject', $formValues),
40 'from' => CRM_Utils_Array::value('from_email_address', $formValues),
41 ];
42
43 $emailParams['from'] = CRM_Utils_Mail::formatFromAddress($emailParams['from']);
44
45 // We need display_name for emailLetter() so add to returnProperties here
46 $returnProperties['display_name'] = 1;
47 if (stristr($formValues['email_options'], 'pdfemail')) {
48 $isPDF = TRUE;
49 }
50 }
51 // update dates ?
52 $receipt_update = isset($formValues['receipt_update']) ? $formValues['receipt_update'] : FALSE;
53 $thankyou_update = isset($formValues['thankyou_update']) ? $formValues['thankyou_update'] : FALSE;
54 $nowDate = date('YmdHis');
55 $receipts = $thanks = $emailed = 0;
56 $updateStatus = '';
57 $task = 'CRM_Contribution_Form_Task_PDFLetterCommon';
58 $realSeparator = ', ';
59 $tableSeparators = [
60 'td' => '</td><td>',
61 'tr' => '</td></tr><tr><td>',
62 ];
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
65 if (isset($formValues['group_by_separator'])) {
66 if (in_array($formValues['group_by_separator'], ['td', 'tr'])) {
67 $realSeparator = $tableSeparators[$formValues['group_by_separator']];
68 }
69 elseif ($formValues['group_by_separator'] == 'br') {
70 $realSeparator = "<br />";
71 }
72 }
73 // a placeholder in case the separator is common in the string - e.g ', '
74 $separator = '****~~~~';
75 $groupBy = $formValues['group_by'];
76
77 // skip some contacts ?
78 $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
79 $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
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);
86 $html = [];
87 $contactHtml = $emailedHtml = [];
88 foreach ($contributions as $contributionId => $contribution) {
89 $contact = &$contacts[$contribution['contact_id']];
90 $grouped = FALSE;
91 $groupByID = 0;
92 if ($groupBy) {
93 $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
94 $contribution = $contact['combined'][$groupBy][$groupByID];
95 $grouped = TRUE;
96 }
97
98 if (empty($groupBy) || empty($contact['is_sent'][$groupBy][$groupByID])) {
99 $html[$contributionId] = self::generateHtml($contact, $contribution, $groupBy, $contributions, $realSeparator, $tableSeparators, $messageToken, $html_message, $separator, $grouped, $groupByID);
100 $contactHtml[$contact['contact_id']][] = $html[$contributionId];
101 if (!empty($formValues['email_options'])) {
102 if (self::emailLetter($contact, $html[$contributionId], $isPDF, $formValues, $emailParams)) {
103 $emailed++;
104 if (!stristr($formValues['email_options'], 'both')) {
105 $emailedHtml[$contributionId] = TRUE;
106 }
107 }
108 }
109 $contact['is_sent'][$groupBy][$groupByID] = TRUE;
110 }
111 // Update receipt/thankyou dates
112 $contributionParams = ['id' => $contributionId];
113 if ($receipt_update) {
114 $contributionParams['receipt_date'] = $nowDate;
115 }
116 if ($thankyou_update) {
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);
123 }
124 }
125
126 $contactIds = array_keys($contacts);
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);
129
130 if (!empty($formValues['is_unit_test'])) {
131 return $html;
132 }
133
134 //CRM-19761
135 if (!empty($html)) {
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 }
144 }
145
146 $form->postProcessHook();
147
148 if ($emailed) {
149 $updateStatus = ts('Receipts have been emailed to %1 contributions.', [1 => $emailed]);
150 }
151 if ($receipts) {
152 $updateStatus = ts('Receipt date has been updated for %1 contributions.', [1 => $receipts]);
153 }
154 if ($thanks) {
155 $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', [1 => $thanks]);
156 }
157
158 if ($updateStatus) {
159 CRM_Core_Session::setStatus($updateStatus);
160 }
161 if (!empty($html)) {
162 // ie. we have only sent emails - lets no show a white screen
163 CRM_Utils_System::civiExit();
164 }
165 }
166
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 */
177 public static function isValidHTMLWithTableSeparator($tokens, $html) {
178 $relevantEntities = ['contribution'];
179 foreach ($relevantEntities as $entity) {
180 if (isset($tokens[$entity]) && is_array($tokens[$entity])) {
181 foreach ($tokens[$entity] as $token) {
182 if (!self::isHtmlTokenInTableCell($token, $entity, $html)) {
183 return FALSE;
184 }
185 }
186 }
187 }
188 return TRUE;
189 }
190
191 /**
192 * Check that the token only appears in a table cell. The '</td><td>' separator cannot otherwise work
193 * Calculate the number of times it appears IN the cell & the number of times it appears - should be the same!
194 *
195 * @param string $token
196 * @param string $entity
197 * @param string $textToSearch
198 *
199 * @return bool
200 */
201 public static function isHtmlTokenInTableCell($token, $entity, $textToSearch) {
202 $tokenToMatch = $entity . '\.' . $token;
203 $pattern = '|<td(?![\w-])((?!</td>).)*\{' . $tokenToMatch . '\}.*?</td>|si';
204 $within = preg_match_all($pattern, $textToSearch);
205 $total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch);
206 return ($within == $total);
207 }
208
209 /**
210 *
211 * @param string $html_message
212 * @param array $contact
213 * @param array $contribution
214 * @param array $messageToken
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 */
221 private static function resolveTokens($html_message, $contact, $contribution, $messageToken, $grouped, $separator) {
222 $categories = self::getTokenCategories();
223 $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact, TRUE, $messageToken);
224 if ($grouped) {
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) {
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 *
245 * @param string $groupBy
246 * @param array $contributionIDs
247 * @param array $returnProperties
248 * @param bool $skipOnHold
249 * @param bool $skipDeceased
250 * @param array $messageToken
251 * @param string $task
252 * @param string $separator
253 * @param bool $isIncludeSoftCredits
254 *
255 * @return array
256 */
257 public static function buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $isIncludeSoftCredits) {
258 $contributions = $contacts = [];
259 foreach ($contributionIDs as $item => $contributionId) {
260 $contribution = CRM_Contribute_BAO_Contribution::getContributionTokenValues($contributionId, $messageToken)['values'][$contributionId];
261 $contribution['campaign'] = CRM_Utils_Array::value('contribution_campaign_title', $contribution);
262 $contributions[$contributionId] = $contribution;
263
264 if ($isIncludeSoftCredits) {
265 //@todo find out why this happens & add comments
266 list($contactID) = explode('-', $item);
267 $contactID = (int) $contactID;
268 }
269 else {
270 $contactID = $contribution['contact_id'];
271 }
272 if (!isset($contacts[$contactID])) {
273 $contacts[$contactID] = [];
274 $contacts[$contactID]['contact_aggregate'] = 0;
275 $contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = [];
276 }
277
278 $contacts[$contactID]['contact_aggregate'] += $contribution['total_amount'];
279 $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
280
281 $contacts[$contactID]['contribution_ids'][$groupBy][$groupByID][$contributionId] = TRUE;
282 if (!isset($contacts[$contactID]['combined'][$groupBy]) || !isset($contacts[$contactID]['combined'][$groupBy][$groupByID])) {
283 $contacts[$contactID]['combined'][$groupBy][$groupByID] = $contribution;
284 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] = $contribution['total_amount'];
285 }
286 else {
287 $contacts[$contactID]['combined'][$groupBy][$groupByID] = self::combineContributions($contacts[$contactID]['combined'][$groupBy][$groupByID], $contribution, $separator);
288 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] += $contribution['total_amount'];
289 }
290 }
291 // Assign the available contributions before calling tokens so hooks parsing smarty can access it.
292 // Note that in core code you can only use smarty here if enable if for the whole site, incl
293 // CiviMail, with a big performance impact.
294 // Hooks allow more nuanced smarty usage here.
295 CRM_Core_Smarty::singleton()->assign('contributions', $contributions);
296 foreach ($contacts as $contactID => $contact) {
297 $tokenResolvedContacts = CRM_Utils_Token::getTokenDetails(['contact_id' => $contactID],
298 $returnProperties,
299 $skipOnHold,
300 $skipDeceased,
301 NULL,
302 $messageToken,
303 $task
304 );
305 $contacts[$contactID] = array_merge($tokenResolvedContacts[0][$contactID], $contact);
306 }
307 return [$contributions, $contacts];
308 }
309
310 /**
311 * We combine the contributions by adding the contribution to each field with the separator in
312 * between the existing value and the new one. We put the separator there even if empty so it is clear what the
313 * value for previous contributions was
314 *
315 * @param array $existing
316 * @param array $contribution
317 * @param string $separator
318 *
319 * @return array
320 */
321 public static function combineContributions($existing, $contribution, $separator) {
322 foreach ($contribution as $field => $value) {
323 $existing[$field] = isset($existing[$field]) ? $existing[$field] . $separator : '';
324 $existing[$field] .= $value;
325 }
326 return $existing;
327 }
328
329 /**
330 * We are going to retrieve the combined contribution and if smarty mail is enabled we
331 * will also assign an array of contributions for this contact to the smarty template
332 *
333 * @param array $contact
334 * @param array $contributions
335 * @param $groupBy
336 * @param int $groupByID
337 */
338 public static function assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID) {
339 CRM_Core_Smarty::singleton()->assign('contact_aggregate', $contact['contact_aggregate']);
340 CRM_Core_Smarty::singleton()
341 ->assign('contributions', array_intersect_key($contributions, $contact['contribution_ids'][$groupBy][$groupByID]));
342 CRM_Core_Smarty::singleton()->assign('contribution_aggregate', $contact['aggregates'][$groupBy][$groupByID]);
343
344 }
345
346 /**
347 * Send pdf by email.
348 *
349 * @param array $contact
350 * @param string $html
351 *
352 * @param $is_pdf
353 * @param array $format
354 * @param array $params
355 *
356 * @return bool
357 */
358 public static function emailLetter($contact, $html, $is_pdf, $format = [], $params = []) {
359 try {
360 if (empty($contact['email'])) {
361 return FALSE;
362 }
363 $mustBeEmpty = ['do_not_email', 'is_deceased', 'on_hold'];
364 foreach ($mustBeEmpty as $emptyField) {
365 if (!empty($contact[$emptyField])) {
366 return FALSE;
367 }
368 }
369
370 $defaults = [
371 'toName' => $contact['display_name'],
372 'toEmail' => $contact['email'],
373 'text' => '',
374 'html' => $html,
375 ];
376 if (empty($params['from'])) {
377 $emails = CRM_Core_BAO_Email::getFromEmail();
378 $emails = array_keys($emails);
379 $defaults['from'] = array_pop($emails);
380 }
381 else {
382 $defaults['from'] = $params['from'];
383 }
384 if (!empty($params['subject'])) {
385 $defaults['subject'] = $params['subject'];
386 }
387 else {
388 $defaults['subject'] = ts('Thank you for your contribution/s');
389 }
390 if ($is_pdf) {
391 $defaults['html'] = ts('Please see attached');
392 $defaults['attachments'] = [CRM_Utils_Mail::appendPDF('ThankYou.pdf', $html, $format)];
393 }
394 $params = array_merge($defaults);
395 return CRM_Utils_Mail::send($params);
396 }
397 catch (CRM_Core_Exception $e) {
398 return FALSE;
399 }
400 }
401
402 /**
403 * @param $contact
404 * @param $formValues
405 * @param $contribution
406 * @param $groupBy
407 * @param $contributions
408 * @param $realSeparator
409 * @param $tableSeparators
410 * @param $messageToken
411 * @param $html_message
412 * @param $separator
413 * @param $categories
414 * @param bool $grouped
415 * @param int $groupByID
416 *
417 * @return string
418 */
419 protected static function generateHtml(&$contact, $contribution, $groupBy, $contributions, $realSeparator, $tableSeparators, $messageToken, $html_message, $separator, $grouped, $groupByID) {
420 static $validated = FALSE;
421 $html = NULL;
422
423 self::assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID);
424
425 if (empty($groupBy) || empty($contact['is_sent'][$groupBy][$groupByID])) {
426 if (!$validated && in_array($realSeparator, $tableSeparators) && !self::isValidHTMLWithTableSeparator($messageToken, $html_message)) {
427 $realSeparator = ', ';
428 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.'));
429 }
430 $validated = TRUE;
431 $html = str_replace($separator, $realSeparator, self::resolveTokens($html_message, $contact, $contribution, $messageToken, $grouped, $separator));
432 }
433
434 return $html;
435 }
436
437 }