Merge pull request #2948 from davecivicrm/CRM-13764
[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
9 /**
10 * process the form after the input has been submitted and validated
11 *
12 * @access public
13 *
355ba699 14 * @return void
6a488035
TO
15 */
16 static function postProcess(&$form) {
17
18 list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form);
19
20 // update dates ?
21 $receipt_update = isset($formValues['receipt_update']) ? $formValues['receipt_update'] : FALSE;
22 $thankyou_update = isset($formValues['thankyou_update']) ? $formValues['thankyou_update'] : FALSE;
23 $nowDate = date('YmdHis');
24 $receipts = 0;
25 $thanks = 0;
26 $updateStatus = '';
27
28 // skip some contacts ?
29 $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
30 $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
31
34eec215
DS
32 $contributionIDs = $form->getVar('_contributionIds');
33 if ($form->_includesSoftCredits) {
34 $contributionIDs = $form->getVar('_contributionContactIds');
35 }
6a488035 36
34eec215 37 foreach ($contributionIDs as $item => $contributionId) {
6a488035 38 // get contact information
34eec215
DS
39 if ($form->_includesSoftCredits) {
40 list($contactId) = explode('-', $item);
41 $contactId = (int) $contactId;
42 } else {
43 $contactId = civicrm_api("Contribution", "getvalue", array('version' => '3', 'id' => $contributionId, 'return' => 'contact_id'));
44 }
6a488035
TO
45 $params = array('contact_id' => $contactId);
46
47 list($contact) = CRM_Utils_Token::getTokenDetails($params,
48 $returnProperties,
49 $skipOnHold,
50 $skipDeceased,
51 NULL,
52 $messageToken,
53 'CRM_Contribution_Form_Task_PDFLetterCommon'
54 );
55 if (civicrm_error($contact)) {
56 $notSent[] = $contributionId;
57 continue;
58 }
59
60 // get contribution information
61 $params = array('contribution_id' => $contributionId);
62 $contribution = CRM_Utils_Token::getContributionTokenDetails($params,
63 $returnProperties,
64 NULL,
65 $messageToken,
66 'CRM_Contribution_Form_Task_PDFLetterCommon'
67 );
68 if (civicrm_error($contribution)) {
69 $notSent[] = $contributionId;
70 continue;
71 }
72
73 $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
74 $tokenHtml = CRM_Utils_Token::replaceContributionTokens($tokenHtml, $contribution[$contributionId], TRUE, $messageToken);
75 $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE);
76
77 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
78 $smarty = CRM_Core_Smarty::singleton();
79 // also add the tokens to the template
80 $smarty->assign_by_ref('contact', $contact);
81 $tokenHtml = $smarty->fetch("string:$tokenHtml");
82 }
83
84 $html[] = $tokenHtml;
85
86 // update dates (do it for each contribution including grouped recurring contribution)
87 if ($receipt_update) {
88 $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'receipt_date', $nowDate);
89 // We can't use CRM_Core_Error::fatal here because the api error elevates the exception level. FIXME. dgg
90 if ($result) {
91 $receipts++;
92 }
93 }
94 if ($thankyou_update) {
95 $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'thankyou_date', $nowDate);
96 // We can't use CRM_Core_Error::fatal here because the api error elevates the exception level. FIXME. dgg
97 if ($result) {
98 $thanks++;
99 }
100 }
101 }
102
103 self::createActivities($form, $html_message, $form->_contactIds);
104
105 CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
106
107 $form->postProcessHook();
108
109 if ($receipts) {
110 $updateStatus = ts('Receipt date has been updated for %1 contributions.', array(1 => $receipts));
111 }
112 if ($thanks) {
113 $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', array(1 => $thanks));
114 }
115
116 if ($updateStatus) {
117 CRM_Core_Session::setStatus($updateStatus);
118 }
665e5ec7 119
6a488035
TO
120 CRM_Utils_System::civiExit(1);
121 }
122 //end of function
123}
124