Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class provides the functionality to email a group of | |
20 | * contacts. | |
21 | */ | |
22 | class CRM_Contribute_Form_Task_PDF extends CRM_Contribute_Form_Task { | |
23 | ||
24 | /** | |
25 | * Are we operating in "single mode", i.e. updating the task of only | |
26 | * one specific contribution? | |
27 | * | |
b67daa72 | 28 | * @var bool |
6a488035 TO |
29 | */ |
30 | public $_single = FALSE; | |
31 | ||
32 | protected $_rows; | |
33 | ||
34 | /** | |
fe482240 | 35 | * Build all the data structures needed to build the form. |
95ea96be | 36 | */ |
389bcebf | 37 | public function preProcess() { |
6a488035 TO |
38 | $id = CRM_Utils_Request::retrieve('id', 'Positive', |
39 | $this, FALSE | |
40 | ); | |
41 | ||
42 | if ($id) { | |
be2fb01f | 43 | $this->_contributionIds = [$id]; |
6a488035 TO |
44 | $this->_componentClause = " civicrm_contribution.id IN ( $id ) "; |
45 | $this->_single = TRUE; | |
46 | $this->assign('totalSelectedContributions', 1); | |
47 | } | |
48 | else { | |
49 | parent::preProcess(); | |
50 | } | |
51 | ||
52 | // check that all the contribution ids have pending status | |
53 | $query = " | |
54 | SELECT count(*) | |
55 | FROM civicrm_contribution | |
56 | WHERE contribution_status_id != 1 | |
57 | AND {$this->_componentClause}"; | |
58 | $count = CRM_Core_DAO::singleValueQuery($query); | |
59 | if ($count != 0) { | |
60 | CRM_Core_Error::statusBounce("Please select only online contributions with Completed status."); | |
61 | } | |
62 | ||
6a488035 TO |
63 | $this->assign('single', $this->_single); |
64 | ||
65 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this); | |
66 | $urlParams = 'force=1'; | |
67 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
68 | $urlParams .= "&qfKey=$qfKey"; | |
69 | } | |
70 | ||
71 | $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams); | |
be2fb01f CW |
72 | $breadCrumb = [ |
73 | [ | |
353ffa53 | 74 | 'url' => $url, |
6a488035 | 75 | 'title' => ts('Search Results'), |
be2fb01f CW |
76 | ], |
77 | ]; | |
f68954cb | 78 | CRM_Contact_Form_Task_EmailCommon ::preProcessFromAddress($this, FALSE); |
7d86c73a | 79 | // we have all the contribution ids, so now we get the contact ids |
3c4ce98e | 80 | parent::setContactIDs(); |
6a488035 TO |
81 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
82 | CRM_Utils_System::setTitle(ts('Print Contribution Receipts')); | |
83 | } | |
84 | ||
85 | /** | |
fe482240 | 86 | * Build the form object. |
6a488035 TO |
87 | */ |
88 | public function buildQuickForm() { | |
89 | ||
90 | $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt', | |
be2fb01f | 91 | [ |
6d98bfad | 92 | 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'none'; |
1330f57a SL |
93 | document.getElementById('selectEmailFrom').style.display = 'block';", |
94 | ] | |
6a488035 TO |
95 | ); |
96 | $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt', | |
be2fb01f | 97 | [ |
99ad38df | 98 | 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'block'; |
1330f57a SL |
99 | document.getElementById('selectEmailFrom').style.display = 'none';", |
100 | ] | |
6a488035 TO |
101 | ); |
102 | $this->addRule('output', ts('Selection required'), 'required'); | |
103 | ||
104 | $this->add('select', 'pdf_format_id', ts('Page Format'), | |
be2fb01f | 105 | [0 => ts('- default -')] + CRM_Core_BAO_PdfFormat::getList(TRUE) |
6a488035 | 106 | ); |
10708045 | 107 | $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE); |
be4478f0 | 108 | $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do not email / Do not mail)'), FALSE); |
6a488035 | 109 | |
beac1417 | 110 | $this->add('select', 'from_email_address', ts('From Email'), $this->_fromEmails, FALSE); |
6d98bfad | 111 | |
be2fb01f | 112 | $this->addButtons([ |
1330f57a SL |
113 | [ |
114 | 'type' => 'next', | |
115 | 'name' => ts('Process Receipt(s)'), | |
116 | 'isDefault' => TRUE, | |
117 | ], | |
118 | [ | |
119 | 'type' => 'back', | |
120 | 'name' => ts('Cancel'), | |
121 | ], | |
122 | ]); | |
6a488035 TO |
123 | } |
124 | ||
125 | /** | |
fe482240 | 126 | * Set default values. |
6a488035 | 127 | */ |
00be9182 | 128 | public function setDefaultValues() { |
6a488035 | 129 | $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues(); |
be2fb01f | 130 | return ['pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0]; |
6a488035 TO |
131 | } |
132 | ||
133 | /** | |
fe482240 | 134 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
135 | */ |
136 | public function postProcess() { | |
137 | // get all the details needed to generate a receipt | |
be2fb01f | 138 | $message = []; |
2826a42e | 139 | $template = CRM_Core_Smarty::singleton(); |
6a488035 | 140 | |
6efffa5d PB |
141 | $params = $this->controller->exportValues($this->_name); |
142 | $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds); | |
6a488035 | 143 | |
2826a42e | 144 | foreach ($elements['details'] as $contribID => $detail) { |
be2fb01f | 145 | $input = $ids = $objects = []; |
6a488035 | 146 | |
3a1261ac | 147 | if (in_array($detail['contact'], $elements['excludeContactIds'])) { |
6a488035 TO |
148 | continue; |
149 | } | |
150 | ||
151 | $input['component'] = $detail['component']; | |
152 | ||
153 | $ids['contact'] = $detail['contact']; | |
2826a42e | 154 | $ids['contribution'] = $contribID; |
6a488035 TO |
155 | $ids['contributionRecur'] = NULL; |
156 | $ids['contributionPage'] = NULL; | |
9c1bc317 CW |
157 | $ids['membership'] = $detail['membership'] ?? NULL; |
158 | $ids['participant'] = $detail['participant'] ?? NULL; | |
159 | $ids['event'] = $detail['event'] ?? NULL; | |
6a488035 | 160 | |
3a1261ac | 161 | if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) { |
a39a65ee | 162 | throw new CRM_Core_Exception('invalid data'); |
6a488035 TO |
163 | } |
164 | ||
165 | $contribution = &$objects['contribution']; | |
6a488035 TO |
166 | |
167 | // set some fake input values so we can reuse IPN code | |
353ffa53 TO |
168 | $input['amount'] = $contribution->total_amount; |
169 | $input['is_test'] = $contribution->is_test; | |
6a488035 TO |
170 | $input['fee_amount'] = $contribution->fee_amount; |
171 | $input['net_amount'] = $contribution->net_amount; | |
353ffa53 | 172 | $input['trxn_id'] = $contribution->trxn_id; |
77c21b32 | 173 | $input['trxn_date'] = $contribution->trxn_date ?? NULL; |
cc7b912f | 174 | $input['receipt_update'] = $params['receipt_update']; |
d5580ed4 | 175 | $input['contribution_status_id'] = $contribution->contribution_status_id; |
35724050 BS |
176 | $input['paymentProcessor'] = empty($contribution->trxn_id) ? NULL : |
177 | CRM_Core_DAO::singleValueQuery("SELECT payment_processor_id | |
178 | FROM civicrm_financial_trxn | |
179 | WHERE trxn_id = %1 | |
be2fb01f | 180 | LIMIT 1", [ |
1330f57a SL |
181 | 1 => [$contribution->trxn_id, 'String'], |
182 | ]); | |
6a488035 | 183 | |
665e5ec7 | 184 | // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date |
6a488035 TO |
185 | $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date); |
186 | ||
beac1417 | 187 | if (isset($params['from_email_address']) && !$elements['createPdf']) { |
5b8e26b5 SL |
188 | // If a logged in user from email is used rather than a domain wide from email address |
189 | // the from_email_address params key will be numerical and we need to convert it to be | |
190 | // in normal from email format | |
191 | $from = CRM_Utils_Mail::formatFromAddress($params['from_email_address']); | |
df72e709 | 192 | // CRM-19129 Allow useres the choice of From Email to send the receipt from. |
5b8e26b5 | 193 | $fromDetails = explode(' <', $from); |
6d98bfad SL |
194 | $input['receipt_from_email'] = substr(trim($fromDetails[1]), 0, -1); |
195 | $input['receipt_from_name'] = str_replace('"', '', $fromDetails[0]); | |
196 | } | |
197 | ||
0d07fe4e | 198 | $mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $elements['createPdf']); |
6a488035 TO |
199 | |
200 | if ($mail['html']) { | |
201 | $message[] = $mail['html']; | |
202 | } | |
203 | else { | |
204 | $message[] = nl2br($mail['body']); | |
205 | } | |
206 | ||
207 | // reset template values before processing next transactions | |
208 | $template->clearTemplateVars(); | |
209 | } | |
3a1261ac RK |
210 | |
211 | if ($elements['createPdf']) { | |
6a488035 TO |
212 | CRM_Utils_PDF_Utils::html2pdf($message, |
213 | 'civicrmContributionReceipt.pdf', | |
214 | FALSE, | |
3a1261ac | 215 | $elements['params']['pdf_format_id'] |
6a488035 TO |
216 | ); |
217 | CRM_Utils_System::civiExit(); | |
218 | } | |
219 | else { | |
3a1261ac | 220 | if ($elements['suppressedEmails']) { |
be2fb01f | 221 | $status = ts('Email was NOT sent to %1 contacts (no email address on file, or communication preferences specify DO NOT EMAIL, or contact is deceased).', [1 => $elements['suppressedEmails']]); |
6a488035 TO |
222 | $msgTitle = ts('Email Error'); |
223 | $msgType = 'error'; | |
224 | } | |
225 | else { | |
226 | $status = ts('Your mail has been sent.'); | |
227 | $msgTitle = ts('Sent'); | |
228 | $msgType = 'success'; | |
229 | } | |
230 | CRM_Core_Session::setStatus($status, $msgTitle, $msgType); | |
231 | } | |
232 | } | |
3a1261ac RK |
233 | |
234 | /** | |
fe482240 | 235 | * Declaration of common variables for Invoice and PDF. |
3a1261ac | 236 | * |
3a1261ac | 237 | * |
014c4014 TO |
238 | * @param array $contribIds |
239 | * Contribution Id. | |
240 | * @param array $params | |
241 | * Parameter for pdf or email invoices. | |
242 | * @param array $contactIds | |
243 | * Contact Id. | |
6efffa5d | 244 | * |
a6c01b45 CW |
245 | * @return array |
246 | * array of common elements | |
2826a42e | 247 | * |
3a1261ac | 248 | */ |
1330f57a | 249 | public static function getElements($contribIds, $params, $contactIds) { |
be2fb01f | 250 | $pdfElements = []; |
3a1261ac | 251 | |
6efffa5d | 252 | $pdfElements['contribIDs'] = implode(',', $contribIds); |
3a1261ac | 253 | |
3e93468d | 254 | $pdfElements['details'] = self::getDetails($pdfElements['contribIDs']); |
3a1261ac RK |
255 | |
256 | $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN(); | |
257 | ||
6efffa5d | 258 | $pdfElements['params'] = $params; |
3a1261ac RK |
259 | |
260 | $pdfElements['createPdf'] = FALSE; | |
830b3e83 | 261 | if (!empty($pdfElements['params']['output']) && |
353ffa53 TO |
262 | ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt") |
263 | ) { | |
3a1261ac RK |
264 | $pdfElements['createPdf'] = TRUE; |
265 | } | |
266 | ||
be2fb01f | 267 | $excludeContactIds = []; |
3a1261ac | 268 | if (!$pdfElements['createPdf']) { |
be2fb01f | 269 | $returnProperties = [ |
353ffa53 TO |
270 | 'email' => 1, |
271 | 'do_not_email' => 1, | |
272 | 'is_deceased' => 1, | |
273 | 'on_hold' => 1, | |
be2fb01f | 274 | ]; |
3a1261ac | 275 | |
6efffa5d | 276 | list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE); |
3a1261ac | 277 | $pdfElements['suppressedEmails'] = 0; |
830b3e83 | 278 | $suppressedEmails = 0; |
3a1261ac | 279 | foreach ($contactDetails as $id => $values) { |
10708045 EM |
280 | if (empty($values['email']) || |
281 | (empty($params['override_privacy']) && !empty($values['do_not_email'])) | |
b99f3e96 | 282 | || !empty($values['is_deceased']) |
353ffa53 TO |
283 | || !empty($values['on_hold']) |
284 | ) { | |
874c9be7 TO |
285 | $suppressedEmails++; |
286 | $pdfElements['suppressedEmails'] = $suppressedEmails; | |
287 | $excludeContactIds[] = $values['contact_id']; | |
3a1261ac RK |
288 | } |
289 | } | |
290 | } | |
291 | $pdfElements['excludeContactIds'] = $excludeContactIds; | |
2826a42e | 292 | |
3a1261ac RK |
293 | return $pdfElements; |
294 | } | |
96025800 | 295 | |
3e93468d AH |
296 | /** |
297 | * @param string $contributionIDs | |
298 | * | |
299 | * @return array | |
300 | */ | |
301 | private static function getDetails($contributionIDs) { | |
302 | if (empty($contributionIDs)) { | |
303 | return []; | |
304 | } | |
305 | $query = " | |
306 | SELECT c.id as contribution_id, | |
307 | c.contact_id as contact_id , | |
308 | mp.membership_id as membership_id , | |
309 | pp.participant_id as participant_id , | |
310 | p.event_id as event_id | |
311 | FROM civicrm_contribution c | |
312 | LEFT JOIN civicrm_membership_payment mp ON mp.contribution_id = c.id | |
313 | LEFT JOIN civicrm_participant_payment pp ON pp.contribution_id = c.id | |
314 | LEFT JOIN civicrm_participant p ON pp.participant_id = p.id | |
315 | WHERE c.id IN ( $contributionIDs )"; | |
316 | ||
317 | $rows = []; | |
318 | $dao = CRM_Core_DAO::executeQuery($query); | |
319 | ||
320 | while ($dao->fetch()) { | |
321 | $rows[$dao->contribution_id]['component'] = $dao->participant_id ? 'event' : 'contribute'; | |
322 | $rows[$dao->contribution_id]['contact'] = $dao->contact_id; | |
323 | if ($dao->membership_id) { | |
324 | if (!array_key_exists('membership', $rows[$dao->contribution_id])) { | |
325 | $rows[$dao->contribution_id]['membership'] = []; | |
326 | } | |
327 | $rows[$dao->contribution_id]['membership'][] = $dao->membership_id; | |
328 | } | |
329 | if ($dao->participant_id) { | |
330 | $rows[$dao->contribution_id]['participant'] = $dao->participant_id; | |
331 | } | |
332 | if ($dao->event_id) { | |
333 | $rows[$dao->contribution_id]['event'] = $dao->event_id; | |
334 | } | |
335 | } | |
336 | return $rows; | |
337 | } | |
338 | ||
6a488035 | 339 | } |