3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to email a group of
22 class CRM_Contribute_Form_Task_PDF
extends CRM_Contribute_Form_Task
{
25 * Are we operating in "single mode", i.e. updating the task of only
26 * one specific contribution?
30 public $_single = FALSE;
35 * Build all the data structures needed to build the form.
37 public function preProcess() {
39 // check that all the contribution ids have pending status
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.");
49 $this->assign('single', $this->_single
);
51 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
52 $urlParams = 'force=1';
53 if (CRM_Utils_Rule
::qfKey($qfKey)) {
54 $urlParams .= "&qfKey=$qfKey";
57 $url = CRM_Utils_System
::url('civicrm/contribute/search', $urlParams);
61 'title' => ts('Search Results'),
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();
74 * Build the form object.
76 public function buildQuickForm() {
78 $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt',
80 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';
81 document.getElementById('selectEmailFrom').style.display = 'block';",
84 $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt',
86 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'block';
87 document.getElementById('selectEmailFrom').style.display = 'none';",
90 $this->addRule('output', ts('Selection required'), 'required');
92 $this->add('select', 'pdf_format_id', ts('Page Format'),
93 [0 => ts('- default -')] + CRM_Core_BAO_PdfFormat
::getList(TRUE)
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);
98 $this->add('select', 'from_email_address', ts('From Email'), $this->_fromEmails
, FALSE);
103 'name' => ts('Process Receipt(s)'),
108 'name' => ts('Cancel'),
114 * Set default values.
116 public function setDefaultValues() {
117 $defaultFormat = CRM_Core_BAO_PdfFormat
::getDefaultValues();
118 return ['pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0];
122 * Process the form after the input has been submitted and validated.
124 public function postProcess() {
125 // get all the details needed to generate a receipt
127 $template = CRM_Core_Smarty
::singleton();
129 $params = $this->controller
->exportValues($this->_name
);
130 $elements = self
::getElements($this->_contributionIds
, $params, $this->_contactIds
);
132 foreach ($elements['details'] as $contribID => $detail) {
135 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
138 // @todo - CRM_Contribute_BAO_Contribution::sendMail re-does pretty much everything between here & when we call it.
139 $input['component'] = $detail['component'];
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;
149 $contribution = new CRM_Contribute_BAO_Contribution();
150 $contribution->id
= $contribID;
151 $contribution->fetch();
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
167 1 => [$contribution->trxn_id
, 'String'],
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]);
181 $mail = CRM_Contribute_BAO_Contribution
::sendMail($input, $ids, $contribID, $elements['createPdf']);
183 if (!empty($mail['html'])) {
184 $message[] = $mail['html'];
186 elseif (!empty($mail['body'])) {
187 $message[] = nl2br($mail['body']);
190 // reset template values before processing next transactions
191 $template->clearTemplateVars();
194 if ($elements['createPdf']) {
195 CRM_Utils_PDF_Utils
::html2pdf($message,
198 $elements['params']['pdf_format_id']
200 CRM_Utils_System
::civiExit();
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');
209 $status = ts('Your mail has been sent.');
210 $msgTitle = ts('Sent');
211 $msgType = 'success';
213 CRM_Core_Session
::setStatus($status, $msgTitle, $msgType);
218 * Declaration of common variables for Invoice and PDF.
221 * @param array $contribIds
223 * @param array $params
224 * Parameter for pdf or email invoices.
225 * @param array $contactIds
229 * array of common elements
231 * @throws \CiviCRM_API3_Exception
233 public static function getElements($contribIds, $params, $contactIds) {
236 $pdfElements['contribIDs'] = implode(',', $contribIds);
238 $pdfElements['details'] = self
::getDetails($pdfElements['contribIDs']);
240 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
242 $pdfElements['params'] = $params;
244 $pdfElements['createPdf'] = FALSE;
245 if (!empty($pdfElements['params']['output']) &&
246 ($pdfElements['params']['output'] === 'pdf_invoice' ||
$pdfElements['params']['output'] === 'pdf_receipt')
248 $pdfElements['createPdf'] = TRUE;
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],
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'])
267 $pdfElements['suppressedEmails'] = $suppressedEmails;
268 $excludeContactIds[] = $values['contact_id'];
272 $pdfElements['excludeContactIds'] = $excludeContactIds;
278 * @param string $contributionIDs
282 private static function getDetails($contributionIDs) {
283 if (empty($contributionIDs)) {
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 )";
299 $dao = CRM_Core_DAO
::executeQuery($query);
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'] = [];
308 $rows[$dao->contribution_id
]['membership'][] = $dao->membership_id
;
310 if ($dao->participant_id
) {
311 $rows[$dao->contribution_id
]['participant'] = $dao->participant_id
;
313 if ($dao->event_id
) {
314 $rows[$dao->contribution_id
]['event'] = $dao->event_id
;