Merge pull request #23210 from eileenmcnaughton/cancel
[civicrm-core.git] / CRM / Contribute / Form / Task / PDF.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 *
28 * @var bool
29 */
30 public $_single = FALSE;
31
32 protected $_rows;
33
34 /**
35 * Build all the data structures needed to build the form.
36 */
37 public function preProcess() {
38 parent::preProcess();
39 // check that all the contribution ids have pending status
40 $query = "
41 SELECT count(*)
42 FROM civicrm_contribution
43 WHERE contribution_status_id != 1
44 AND {$this->_componentClause}";
45 if (CRM_Core_DAO::singleValueQuery($query)) {
46 CRM_Core_Error::statusBounce("Please select only contributions with Completed status.");
47 }
48
49 $this->assign('single', $this->_single);
50
51 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
52 $urlParams = 'force=1';
53 if (CRM_Utils_Rule::qfKey($qfKey)) {
54 $urlParams .= "&qfKey=$qfKey";
55 }
56
57 $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams);
58 $breadCrumb = [
59 [
60 'url' => $url,
61 'title' => ts('Search Results'),
62 ],
63 ];
64 CRM_Contact_Form_Task_EmailCommon ::preProcessFromAddress($this, FALSE);
65 // we have all the contribution ids, so now we get the contact ids
66 parent::setContactIDs();
67 CRM_Utils_System::appendBreadCrumb($breadCrumb);
68 $this->setTitle(ts('Print Contribution Receipts'));
69 // Ajax submit would interfere with pdf file download
70 $this->preventAjaxSubmit();
71 }
72
73 /**
74 * Build the form object.
75 */
76 public function buildQuickForm() {
77
78 $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt',
79 [
80 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';
81 document.getElementById('selectEmailFrom').style.display = 'block';",
82 ]
83 );
84 $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt',
85 [
86 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'block';
87 document.getElementById('selectEmailFrom').style.display = 'none';",
88 ]
89 );
90 $this->addRule('output', ts('Selection required'), 'required');
91
92 $this->add('select', 'pdf_format_id', ts('Page Format'),
93 [0 => ts('- default -')] + CRM_Core_BAO_PdfFormat::getList(TRUE)
94 );
95 $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE);
96 $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do not email / Do not mail)'), FALSE);
97
98 $this->add('select', 'from_email_address', ts('From Email'), $this->_fromEmails, FALSE);
99
100 $this->addButtons([
101 [
102 'type' => 'next',
103 'name' => ts('Process Receipt(s)'),
104 'isDefault' => TRUE,
105 ],
106 [
107 'type' => 'back',
108 'name' => ts('Cancel'),
109 ],
110 ]);
111 }
112
113 /**
114 * Set default values.
115 */
116 public function setDefaultValues() {
117 $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
118 return ['pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0];
119 }
120
121 /**
122 * Process the form after the input has been submitted and validated.
123 */
124 public function postProcess() {
125 // get all the details needed to generate a receipt
126 $message = [];
127 $template = CRM_Core_Smarty::singleton();
128
129 $params = $this->controller->exportValues($this->_name);
130 $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds);
131
132 foreach ($elements['details'] as $contribID => $detail) {
133 $input = $ids = [];
134
135 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
136 continue;
137 }
138 // @todo - CRM_Contribute_BAO_Contribution::sendMail re-does pretty much everything between here & when we call it.
139 $input['component'] = $detail['component'];
140
141 $ids['contact'] = $detail['contact'];
142 $ids['contribution'] = $contribID;
143 $ids['contributionRecur'] = NULL;
144 $ids['contributionPage'] = NULL;
145 $ids['membership'] = $detail['membership'] ?? NULL;
146 $ids['participant'] = $detail['participant'] ?? NULL;
147 $ids['event'] = $detail['event'] ?? NULL;
148
149 $contribution = new CRM_Contribute_BAO_Contribution();
150 $contribution->id = $contribID;
151 $contribution->fetch();
152
153 // set some fake input values so we can reuse IPN code
154 $input['amount'] = $contribution->total_amount;
155 $input['is_test'] = $contribution->is_test;
156 $input['fee_amount'] = $contribution->fee_amount;
157 $input['net_amount'] = $contribution->net_amount;
158 $input['trxn_id'] = $contribution->trxn_id;
159 $input['trxn_date'] = $contribution->trxn_date ?? NULL;
160 $input['receipt_update'] = $params['receipt_update'];
161 $input['contribution_status_id'] = $contribution->contribution_status_id;
162 $input['paymentProcessor'] = empty($contribution->trxn_id) ? NULL :
163 CRM_Core_DAO::singleValueQuery("SELECT payment_processor_id
164 FROM civicrm_financial_trxn
165 WHERE trxn_id = %1
166 LIMIT 1", [
167 1 => [$contribution->trxn_id, 'String'],
168 ]);
169
170 if (isset($params['from_email_address']) && !$elements['createPdf']) {
171 // If a logged in user from email is used rather than a domain wide from email address
172 // the from_email_address params key will be numerical and we need to convert it to be
173 // in normal from email format
174 $from = CRM_Utils_Mail::formatFromAddress($params['from_email_address']);
175 // CRM-19129 Allow useres the choice of From Email to send the receipt from.
176 $fromDetails = explode(' <', $from);
177 $input['receipt_from_email'] = substr(trim($fromDetails[1]), 0, -1);
178 $input['receipt_from_name'] = str_replace('"', '', $fromDetails[0]);
179 }
180
181 $mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contribID, $elements['createPdf']);
182
183 if (!empty($mail['html'])) {
184 $message[] = $mail['html'];
185 }
186 elseif (!empty($mail['body'])) {
187 $message[] = nl2br($mail['body']);
188 }
189
190 // reset template values before processing next transactions
191 $template->clearTemplateVars();
192 }
193
194 if ($elements['createPdf']) {
195 CRM_Utils_PDF_Utils::html2pdf($message,
196 'receipt.pdf',
197 FALSE,
198 $elements['params']['pdf_format_id']
199 );
200 CRM_Utils_System::civiExit();
201 }
202 else {
203 if ($elements['suppressedEmails']) {
204 $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']]);
205 $msgTitle = ts('Email Error');
206 $msgType = 'error';
207 }
208 else {
209 $status = ts('Your mail has been sent.');
210 $msgTitle = ts('Sent');
211 $msgType = 'success';
212 }
213 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
214 }
215 }
216
217 /**
218 * Declaration of common variables for Invoice and PDF.
219 *
220 *
221 * @param array $contribIds
222 * Contribution Id.
223 * @param array $params
224 * Parameter for pdf or email invoices.
225 * @param array $contactIds
226 * Contact Id.
227 *
228 * @return array
229 * array of common elements
230 *
231 * @throws \CiviCRM_API3_Exception
232 */
233 public static function getElements($contribIds, $params, $contactIds) {
234 $pdfElements = [];
235
236 $pdfElements['contribIDs'] = implode(',', $contribIds);
237
238 $pdfElements['details'] = self::getDetails($pdfElements['contribIDs']);
239
240 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
241
242 $pdfElements['params'] = $params;
243
244 $pdfElements['createPdf'] = FALSE;
245 if (!empty($pdfElements['params']['output']) &&
246 ($pdfElements['params']['output'] === 'pdf_invoice' || $pdfElements['params']['output'] === 'pdf_receipt')
247 ) {
248 $pdfElements['createPdf'] = TRUE;
249 }
250
251 $excludeContactIds = [];
252 if (!$pdfElements['createPdf']) {
253 $contactDetails = civicrm_api3('Contact', 'get', [
254 'return' => ['email', 'do_not_email', 'is_deceased', 'on_hold'],
255 'id' => ['IN' => $contactIds],
256 'options' => ['limit' => 0],
257 ])['values'];
258 $pdfElements['suppressedEmails'] = 0;
259 $suppressedEmails = 0;
260 foreach ($contactDetails as $id => $values) {
261 if (empty($values['email']) ||
262 (empty($params['override_privacy']) && !empty($values['do_not_email']))
263 || !empty($values['is_deceased'])
264 || !empty($values['on_hold'])
265 ) {
266 $suppressedEmails++;
267 $pdfElements['suppressedEmails'] = $suppressedEmails;
268 $excludeContactIds[] = $values['contact_id'];
269 }
270 }
271 }
272 $pdfElements['excludeContactIds'] = $excludeContactIds;
273
274 return $pdfElements;
275 }
276
277 /**
278 * @param string $contributionIDs
279 *
280 * @return array
281 */
282 private static function getDetails($contributionIDs) {
283 if (empty($contributionIDs)) {
284 return [];
285 }
286 $query = "
287 SELECT c.id as contribution_id,
288 c.contact_id as contact_id ,
289 mp.membership_id as membership_id ,
290 pp.participant_id as participant_id ,
291 p.event_id as event_id
292 FROM civicrm_contribution c
293 LEFT JOIN civicrm_membership_payment mp ON mp.contribution_id = c.id
294 LEFT JOIN civicrm_participant_payment pp ON pp.contribution_id = c.id
295 LEFT JOIN civicrm_participant p ON pp.participant_id = p.id
296 WHERE c.id IN ( $contributionIDs )";
297
298 $rows = [];
299 $dao = CRM_Core_DAO::executeQuery($query);
300
301 while ($dao->fetch()) {
302 $rows[$dao->contribution_id]['component'] = $dao->participant_id ? 'event' : 'contribute';
303 $rows[$dao->contribution_id]['contact'] = $dao->contact_id;
304 if ($dao->membership_id) {
305 if (!array_key_exists('membership', $rows[$dao->contribution_id])) {
306 $rows[$dao->contribution_id]['membership'] = [];
307 }
308 $rows[$dao->contribution_id]['membership'][] = $dao->membership_id;
309 }
310 if ($dao->participant_id) {
311 $rows[$dao->contribution_id]['participant'] = $dao->participant_id;
312 }
313 if ($dao->event_id) {
314 $rows[$dao->contribution_id]['event'] = $dao->event_id;
315 }
316 }
317 return $rows;
318 }
319
320 }