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