4 * This class provides the common functionality for creating PDF letter for
5 * one or a group of contact ids.
7 class CRM_Contribute_Form_Task_PDFLetterCommon
extends CRM_Contact_Form_Task_PDFLetterCommon
{
10 * process the form after the input has been submitted and validated
14 * @param CRM_Contribute_Form_Task $form
18 static function postProcess(&$form) {
19 list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self
::processMessageTemplate($form);
21 $emailParams = array();
22 if(!empty($formValues['email_options'])) {
23 $returnProperties['email'] = $returnProperties['on_hold'] = $returnProperties['is_deceased'] = $returnProperties['do_not_email'] = 1;
25 'subject' => $formValues['subject']
27 // We need display_name for emailLetter() so add to returnProperties here
28 $returnProperties['display_name'] = 1;
29 if(stristr($formValues['email_options'], 'pdfemail')) {
34 $receipt_update = isset($formValues['receipt_update']) ?
$formValues['receipt_update'] : FALSE;
35 $thankyou_update = isset($formValues['thankyou_update']) ?
$formValues['thankyou_update'] : FALSE;
36 $nowDate = date('YmdHis');
37 $receipts = $thanks = $emailed = 0;
39 $task = 'CRM_Contribution_Form_Task_PDFLetterCommon';
40 $realSeparator = ', ';
41 //the original thinking was mutliple options - but we are going with only 2 (comma & td) for now in case
42 // there are security (& UI) issues we need to think through
43 if(isset($formValues['group_by_separator'])) {
44 if($formValues['group_by_separator'] == 'td') {
45 $realSeparator = "</td><td>";
48 $separator = '****~~~~';// a placeholder in case the separator is common in the string - e.g ', '
51 $groupBy = $formValues['group_by'];
53 // skip some contacts ?
54 $skipOnHold = isset($form->skipOnHold
) ?
$form->skipOnHold
: FALSE;
55 $skipDeceased = isset($form->skipDeceased
) ?
$form->skipDeceased
: TRUE;
57 list($contributions, $contacts) = self
::buildContributionArray($groupBy, $form, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator);
59 foreach ($contributions as $contributionId => $contribution) {
60 $contact = &$contacts[$contribution['contact_id']];
61 $grouped = $groupByID = 0;
63 $groupByID = empty($contribution[$groupBy]) ?
0 : $contribution[$groupBy];
64 $contribution = $contact['combined'][$groupBy][$groupByID];
68 self
::assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID);
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.'));
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)) {
81 if(!stristr($formValues['email_options'], 'both')) {
82 unset($html[$contributionId]);
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);
96 if ($thankyou_update) {
97 $result = CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'thankyou_date', $nowDate);
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
);
108 CRM_Utils_PDF_Utils
::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
111 $form->postProcessHook();
114 $updateStatus = ts('Receipts have been emailed to %1 contributions.', array(1 => $emailed));
117 $updateStatus = ts('Receipt date has been updated for %1 contributions.', array(1 => $receipts));
120 $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', array(1 => $thanks));
124 CRM_Core_Session
::setStatus($updateStatus);
127 // ie. we have only sent emails - lets no show a white screen
128 CRM_Utils_System
::civiExit(1);
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
142 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)) {;
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!
162 * @param $textToSearch
164 * @internal param $html
168 static function isHtmlTokenInTableCell($token, $entity, $textToSearch) {
169 $tokenToMatch = $entity . '.' . $token;
171 $within = preg_match_all("|<td.+?{".$tokenToMatch."}.+?</td|si", $textToSearch, $dontCare);
172 $total = preg_match_all("|{".$tokenToMatch."}|", $textToSearch, $dontCare);
173 return ($within == $total);
178 * @param string $html_message
179 * @param array $contact
180 * @param array $contribution
181 * @param array $messageToken
182 * @param array $categories
183 * @param bool $grouped Does this letter represent more than one contribution
184 * @param string $separator What is the preferred letter separator
187 private static function resolveTokens($html_message, $contact, $contribution, $messageToken, $categories, $grouped, $separator) {
188 $tokenHtml = CRM_Utils_Token
::replaceContactTokens($html_message, $contact, TRUE, $messageToken);
190 $tokenHtml = CRM_Utils_Token
::replaceMultipleContributionTokens($separator, $tokenHtml, $contribution, TRUE, $messageToken);
193 // no change to normal behaviour to avoid risk of breakage
194 $tokenHtml = CRM_Utils_Token
::replaceContributionTokens($tokenHtml, $contribution, TRUE, $messageToken);
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");
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
210 * @param string $groupBy
211 * @param CRM_Contribute_Form_Task $form
212 * @param array $returnProperties
213 * @param boolean $skipOnHold
214 * @param boolean $skipDeceased
215 * @param array $messageToken
216 * @param string $task
217 * @param string $separator
221 static function buildContributionArray($groupBy, $form, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator) {
222 $contributions = $contacts = $notSent = array();
223 $contributionIDs = $form->getVar('_contributionIds');
224 if ($form->_includesSoftCredits
) {
225 //@todo - comment on what is stored there
226 $contributionIDs = $form->getVar('_contributionContactIds');
228 foreach ($contributionIDs as $item => $contributionId) {
229 // get contribution information
230 $contribution = CRM_Utils_Token
::getContributionTokenDetails(array('contribution_id' => $contributionId),
236 $contribution = $contributions[$contributionId] = $contribution[$contributionId];
237 if ($form->_includesSoftCredits
) {
238 //@todo find out why this happens & add comments
239 list($contactID) = explode('-', $item);
240 $contactID = (int) $contactID;
243 $contactID = $contribution['contact_id'];
245 if(!isset($contacts[$contactID])) {
246 list($contact) = CRM_Utils_Token
::getTokenDetails(array('contact_id' => $contactID),
254 $contacts[$contactID] = $contact[$contactID];
255 $contacts[$contactID]['contact_aggregate'] = 0;
256 $contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = array();
259 $contacts[$contactID]['contact_aggregate'] +
= $contribution['total_amount'];
260 $groupByID = empty($contribution[$groupBy]) ?
0 : $contribution[$groupBy];
262 $contacts[$contactID]['contribution_ids'][$groupBy][$groupByID][$contributionId] = TRUE;
263 if(!isset($contacts[$contactID]['combined'][$groupBy]) ||
!isset($contacts[$contactID]['combined'][$groupBy][$groupByID])) {
264 $contacts[$contactID]['combined'][$groupBy][$groupByID] = $contribution;
265 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] = $contribution['total_amount'];
268 $contacts[$contactID]['combined'][$groupBy][$groupByID] = self
::combineContributions($contacts[$contactID]['combined'][$groupBy][$groupByID], $contribution, $separator);
269 $contacts[$contactID]['aggregates'][$groupBy][$groupByID] +
= $contribution['total_amount'];
272 return array($contributions, $contacts);
276 * We combine the contributions by adding the contribution to each field with the separator in
277 * between the existing value and the new one. We put the separator there even if empty so it is clear what the
278 * value for previous contributions was
280 * @param array $existing
281 * @param array $contribution
282 * @param string $separator
286 static function combineContributions($existing, $contribution, $separator) {
287 foreach ($contribution as $field => $value) {
288 $existing[$field] = isset($existing[$field]) ?
$existing[$field] . $separator : '';
289 $existing[$field] .= $value;
295 * We are going to retrieve the combined contribution and if smarty mail is enabled we
296 * will also assign an array of contributions for this contact to the smarty template
298 * @param array $contact
299 * @param array $contributions
303 static function assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID) {
304 if (!defined('CIVICRM_MAIL_SMARTY') ||
!CIVICRM_MAIL_SMARTY
) {
307 CRM_Core_Smarty
::singleton()->assign('contact_aggregate', $contact['contact_aggregate']);
308 CRM_Core_Smarty
::singleton()->assign('contributions', array_intersect_key($contributions, $contact['contribution_ids'][$groupBy][$groupByID]));
309 CRM_Core_Smarty
::singleton()->assign('contribution_aggregate', $contact['aggregates'][$groupBy][$groupByID]);
316 * @param array $contact
317 * @param string $html
320 * @param array $format
321 * @param array $params
325 static function emailLetter($contact, $html, $is_pdf, $format = array(), $params = array()) {
327 if(empty($contact['email'])) {
330 $mustBeEmpty = array('do_not_email', 'is_deceased', 'on_hold');
331 foreach ($mustBeEmpty as $emptyField) {
332 if(!empty($contact[$emptyField])) {
338 'toName' => $contact['display_name'],
339 'toEmail' => $contact['email'],
340 'subject' => ts('Thank you for your contribution/s'),
344 if(empty($params['from'])) {
345 $emails = CRM_Core_BAO_Email
::getFromEmail();
346 $emails = array_keys($emails);
347 $defaults['from'] = array_pop($emails);
350 $defaults['html'] = ts('Please see attached');
351 $defaults['attachments'] = array(CRM_Utils_Mail
::appendPDF('ThankYou.pdf', $html, $format));
353 $params = array_merge($defaults);
354 return CRM_Utils_Mail
::send($params);
356 catch (CRM_Core_Exception
$e) {