4dcba8ef7e1c8ecac33f86c2205a907cb5f07da5
[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 * Process the form after the input has been submitted and validated.
11 *
12 * @param CRM_Contribute_Form_Task $form
13 */
14 public static function postProcess(&$form) {
15 list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form);
16 $isPDF = FALSE;
17 $emailParams = array();
18 if (!empty($formValues['email_options'])) {
19 $returnProperties['email'] = $returnProperties['on_hold'] = $returnProperties['is_deceased'] = $returnProperties['do_not_email'] = 1;
20 $emailParams = array(
21 'subject' => $formValues['subject'],
22 );
23 // We need display_name for emailLetter() so add to returnProperties here
24 $returnProperties['display_name'] = 1;
25 if (stristr($formValues['email_options'], 'pdfemail')) {
26 $isPDF = TRUE;
27 }
28 }
29 // update dates ?
30 $receipt_update = isset($formValues['receipt_update']) ? $formValues['receipt_update'] : FALSE;
31 $thankyou_update = isset($formValues['thankyou_update']) ? $formValues['thankyou_update'] : FALSE;
32 $nowDate = date('YmdHis');
33 $receipts = $thanks = $emailed = 0;
34 $updateStatus = '';
35 $task = 'CRM_Contribution_Form_Task_PDFLetterCommon';
36 $realSeparator = ', ';
37 //the original thinking was mutliple options - but we are going with only 2 (comma & td) for now in case
38 // there are security (& UI) issues we need to think through
39 if (isset($formValues['group_by_separator'])) {
40 if ($formValues['group_by_separator'] == 'td') {
41 $realSeparator = "</td><td>";
42 }
43 }
44 $separator = '****~~~~';// a placeholder in case the separator is common in the string - e.g ', '
45 $validated = FALSE;
46
47 $groupBy = $formValues['group_by'];
48
49 // skip some contacts ?
50 $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
51 $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
52 $contributionIDs = $form->getVar('_contributionIds');
53 if ($form->_includesSoftCredits) {
54 //@todo - comment on what is stored there
55 $contributionIDs = $form->getVar('_contributionContactIds');
56 }
57 list($contributions, $contacts) = self::buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $form->_includesSoftCredits);
58 $html = array();
59 foreach ($contributions as $contributionId => $contribution) {
60 $contact = &$contacts[$contribution['contact_id']];
61 $grouped = $groupByID = 0;
62 if ($groupBy) {
63 $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
64 $contribution = $contact['combined'][$groupBy][$groupByID];
65 $grouped = TRUE;
66 }
67
68 self::assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID);
69
70 if (empty($groupBy) || empty($contact['is_sent'][$groupBy][$groupByID])) {
71 if (!$validated && $realSeparator == '</td><td>' && !self::isValidHTMLWithTableSeparator($messageToken, $html_message)) {
72 $realSeparator = ', ';
73 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.'));
74 }
75 $validated = TRUE;
76 $html[$contributionId] = str_replace($separator, $realSeparator, self::resolveTokens($html_message, $contact, $contribution, $messageToken, $categories, $grouped, $separator));
77 $contact['is_sent'][$groupBy][$groupByID] = TRUE;
78 if (!empty($formValues['email_options'])) {
79 if (self::emailLetter($contact, $html[$contributionId], $isPDF, $formValues, $emailParams)) {
80 $emailed++;
81 if (!stristr($formValues['email_options'], 'both')) {
82 unset($html[$contributionId]);
83 }
84 }
85 }
86 }
87
88 // update dates (do it for each contribution including grouped recurring contribution)
89 //@todo - the 2 calls below bypass all hooks. Using the api would possibly be slower than one call but not than 2
90 if ($receipt_update) {
91 $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'receipt_date', $nowDate);
92 if ($result) {
93 $receipts++;
94 }
95 }
96 if ($thankyou_update) {
97 $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'thankyou_date', $nowDate);
98 if ($result) {
99 $thanks++;
100 }
101 }
102 }
103 //createActivities requires both $form->_contactIds and $contacts -
104 //@todo - figure out why
105 $form->_contactIds = array_keys($contacts);
106 self::createActivities($form, $html_message, $form->_contactIds);
107 if (!empty($html)) {
108 CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
109 }
110
111 $form->postProcessHook();
112
113 if ($emailed) {
114 $updateStatus = ts('Receipts have been emailed to %1 contributions.', array(1 => $emailed));
115 }
116 if ($receipts) {
117 $updateStatus = ts('Receipt date has been updated for %1 contributions.', array(1 => $receipts));
118 }
119 if ($thanks) {
120 $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', array(1 => $thanks));
121 }
122
123 if ($updateStatus) {
124 CRM_Core_Session::setStatus($updateStatus);
125 }
126 if (!empty($html)) {
127 // ie. we have only sent emails - lets no show a white screen
128 CRM_Utils_System::civiExit(1);
129 }
130 }
131
132 /**
133 * Check whether any of the tokens exist in the html outside a table cell.
134 * If they do the table cell separator is not supported (return false)
135 * At this stage we are only anticipating contributions passed in this way but
136 * it would be easy to add others
137 * @param $tokens
138 * @param $html
139 *
140 * @return bool
141 */
142 public static function isValidHTMLWithTableSeparator($tokens, $html) {
143 $relevantEntities = array('contribution');
144 foreach ($relevantEntities as $entity) {
145 if (isset($tokens[$entity]) && is_array($tokens[$entity])) {
146 foreach ($tokens[$entity] as $token) {
147 if (!self::isHtmlTokenInTableCell($token, $entity, $html)) {
148 return FALSE;
149 }
150 }
151 }
152 }
153 return TRUE;
154 }
155
156 /**
157 * Check that the token only appears in a table cell. The '</td><td>' separator cannot otherwise work
158 * Calculate the number of times it appears IN the cell & the number of times it appears - should be the same!
159 *
160 * @param $token
161 * @param $entity
162 * @param $textToSearch
163 *
164 * @return bool
165 */
166 public static function isHtmlTokenInTableCell($token, $entity, $textToSearch) {
167 $tokenToMatch = $entity . '.' . $token;
168 $dontCare = array();
169 $within = preg_match_all("|<td.+?{" . $tokenToMatch . "}.+?</td|si", $textToSearch, $dontCare);
170 $total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch, $dontCare);
171 return ($within == $total);
172 }
173
174 /**
175 *
176 * @param string $html_message
177 * @param array $contact
178 * @param array $contribution
179 * @param array $messageToken
180 * @param array $categories
181 * @param bool $grouped
182 * Does this letter represent more than one contribution.
183 * @param string $separator
184 * What is the preferred letter separator.
185 * @return string
186 */
187 private static function resolveTokens($html_message, $contact, $contribution, $messageToken, $categories, $grouped, $separator) {
188 $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact, TRUE, $messageToken);
189 if ($grouped) {
190 $tokenHtml = CRM_Utils_Token::replaceMultipleContributionTokens($separator, $tokenHtml, $contribution, TRUE, $messageToken);
191 }
192 else {
193 // no change to normal behaviour to avoid risk of breakage
194 $tokenHtml = CRM_Utils_Token::replaceContributionTokens($tokenHtml, $contribution, TRUE, $messageToken);
195 }
196 $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact, $categories, TRUE);
197 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
198 $smarty = CRM_Core_Smarty::singleton();
199 // also add the tokens to the template
200 $smarty->assign_by_ref('contact', $contact);
201 $tokenHtml = $smarty->fetch("string:$tokenHtml");
202 }
203 return $tokenHtml;
204 }
205
206 /**
207 * Generate the contribution array from the form, we fill in the contact details and determine any aggregation
208 * around contact_id of contribution_recur_id
209 *
210 * @param string $groupBy
211 * @param array $contributionIDs
212 * @param array $returnProperties
213 * @param bool $skipOnHold
214 * @param bool $skipDeceased
215 * @param array $messageToken
216 * @param string $task
217 * @param string $separator
218 * @param bool $isIncludeSoftCredits
219 *
220 * @return array
221 */
222 public static function buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $isIncludeSoftCredits) {
223 $contributions = $contacts = $notSent = array();
224 foreach ($contributionIDs as $item => $contributionId) {
225 // get contribution information
226 $contribution = CRM_Utils_Token::getContributionTokenDetails(array('contribution_id' => $contributionId),
227 $returnProperties,
228 NULL,
229 $messageToken,
230 $task
231 );
232 $contribution = $contributions[$contributionId] = $contribution[$contributionId];
233
234 if ($isIncludeSoftCredits) {
235 //@todo find out why this happens & add comments
236 list($contactID) = explode('-', $item);
237 $contactID = (int) $contactID;
238 }
239 else {
240 $contactID = $contribution['contact_id'];
241 }
242 if (!isset($contacts[$contactID])) {
243 $contacts[$contactID] = array();
244 $contacts[$contactID]['contact_aggregate'] = 0;
245 $contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = array();
246 }
247
248 $contacts[$contactID]['contact_aggregate'] += $contribution['total_amount'];
249 $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
250
251 $contacts[$contactID]['contribution_ids'][$groupBy][$groupByID][$contributionId] = TRUE;
252 if (!isset($contacts[$contactID]['combined'][$groupBy]) || !isset($contacts[$contactID]['combined'][$groupBy][$groupByID])) {
253 $contacts[$contactID]['combined'][$groupBy][$groupByID] = $contribution;
254 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] = $contribution['total_amount'];
255 }
256 else {
257 $contacts[$contactID]['combined'][$groupBy][$groupByID] = self::combineContributions($contacts[$contactID]['combined'][$groupBy][$groupByID], $contribution, $separator);
258 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] += $contribution['total_amount'];
259 }
260 }
261 // Assign the available contributions before calling tokens so hooks parsing smarty can access it.
262 // Note that in core code you can only use smarty here if enable if for the whole site, incl
263 // CiviMail, with a big performance impact.
264 // Hooks allow more nuanced smarty usage here.
265 CRM_Core_Smarty::singleton()->assign('contributions', $contributions);
266 foreach ($contacts as $contactID => $contact) {
267 $tokenResolvedContacts = CRM_Utils_Token::getTokenDetails(array('contact_id' => $contactID),
268 $returnProperties,
269 $skipOnHold,
270 $skipDeceased,
271 NULL,
272 $messageToken,
273 $task
274 );
275 $contacts[$contactID] = array_merge($tokenResolvedContacts[0][$contactID], $contact);
276 }
277 return array($contributions, $contacts);
278 }
279
280 /**
281 * We combine the contributions by adding the contribution to each field with the separator in
282 * between the existing value and the new one. We put the separator there even if empty so it is clear what the
283 * value for previous contributions was
284 *
285 * @param array $existing
286 * @param array $contribution
287 * @param string $separator
288 *
289 * @return array
290 */
291 public static function combineContributions($existing, $contribution, $separator) {
292 foreach ($contribution as $field => $value) {
293 $existing[$field] = isset($existing[$field]) ? $existing[$field] . $separator : '';
294 $existing[$field] .= $value;
295 }
296 return $existing;
297 }
298
299 /**
300 * We are going to retrieve the combined contribution and if smarty mail is enabled we
301 * will also assign an array of contributions for this contact to the smarty template
302 *
303 * @param array $contact
304 * @param array $contributions
305 * @param $groupBy
306 * @param int $groupByID
307 */
308 public static function assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID) {
309 if (!defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY) {
310 return;
311 }
312 CRM_Core_Smarty::singleton()->assign('contact_aggregate', $contact['contact_aggregate']);
313 CRM_Core_Smarty::singleton()
314 ->assign('contributions', array_intersect_key($contributions, $contact['contribution_ids'][$groupBy][$groupByID]));
315 CRM_Core_Smarty::singleton()->assign('contribution_aggregate', $contact['aggregates'][$groupBy][$groupByID]);
316
317 }
318
319 /**
320 * Send pdf by email.
321 *
322 * @param array $contact
323 * @param string $html
324 *
325 * @param $is_pdf
326 * @param array $format
327 * @param array $params
328 *
329 * @return bool
330 */
331 public static function emailLetter($contact, $html, $is_pdf, $format = array(), $params = array()) {
332 try {
333 if (empty($contact['email'])) {
334 return FALSE;
335 }
336 $mustBeEmpty = array('do_not_email', 'is_deceased', 'on_hold');
337 foreach ($mustBeEmpty as $emptyField) {
338 if (!empty($contact[$emptyField])) {
339 return FALSE;
340 }
341 }
342
343 $defaults = array(
344 'toName' => $contact['display_name'],
345 'toEmail' => $contact['email'],
346 'text' => '',
347 'html' => $html,
348 );
349 if (empty($params['from'])) {
350 $emails = CRM_Core_BAO_Email::getFromEmail();
351 $emails = array_keys($emails);
352 $defaults['from'] = array_pop($emails);
353 }
354 if (!empty($params['subject'])) {
355 $defaults['subject'] = $params['subject'];
356 }
357 else {
358 $defaults['subject'] = ts('Thank you for your contribution/s');
359 }
360 if ($is_pdf) {
361 $defaults['html'] = ts('Please see attached');
362 $defaults['attachments'] = array(CRM_Utils_Mail::appendPDF('ThankYou.pdf', $html, $format));
363 }
364 $params = array_merge($defaults);
365 return CRM_Utils_Mail::send($params);
366 }
367 catch (CRM_Core_Exception $e) {
368 return FALSE;
369 }
370 }
371
372 }