3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * This class provides the functionality to email a group of
38 class CRM_Contribute_Form_Task_PDF
extends CRM_Contribute_Form_Task
{
41 * Are we operating in "single mode", i.e. updating the task of only
42 * one specific contribution?
46 public $_single = FALSE;
51 * Build all the data structures needed to build the form.
55 public function preProcess() {
56 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
61 $this->_contributionIds
= array($id);
62 $this->_componentClause
= " civicrm_contribution.id IN ( $id ) ";
63 $this->_single
= TRUE;
64 $this->assign('totalSelectedContributions', 1);
70 // check that all the contribution ids have pending status
73 FROM civicrm_contribution
74 WHERE contribution_status_id != 1
75 AND {$this->_componentClause}";
76 $count = CRM_Core_DAO
::singleValueQuery($query);
78 CRM_Core_Error
::statusBounce("Please select only online contributions with Completed status.");
81 $this->assign('single', $this->_single
);
83 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
84 $urlParams = 'force=1';
85 if (CRM_Utils_Rule
::qfKey($qfKey)) {
86 $urlParams .= "&qfKey=$qfKey";
89 $url = CRM_Utils_System
::url('civicrm/contribute/search', $urlParams);
93 'title' => ts('Search Results'),
96 CRM_Contact_Form_Task_EmailCommon
::preProcessFromAddress($this, FALSE);
97 // we have all the contribution ids, so now we get the contact ids
98 parent
::setContactIDs();
99 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
100 CRM_Utils_System
::setTitle(ts('Print Contribution Receipts'));
104 * Build the form object.
109 public function buildQuickForm() {
111 $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt',
113 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';
114 document.getElementById('selectEmailFrom').style.display = 'block';")
116 $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt',
117 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'block';")
119 $this->addRule('output', ts('Selection required'), 'required');
121 $this->add('select', 'pdf_format_id', ts('Page Format'),
122 array(0 => ts('- default -')) + CRM_Core_BAO_PdfFormat
::getList(TRUE)
124 $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE);
125 $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do no email / Do not mail)'), FALSE);
127 $this->add('select', 'fromEmailAddress', ts('From Email'), $this->_fromEmails
, FALSE, array('class' => 'crm-select2 huge'));
129 $this->addButtons(array(
132 'name' => ts('Process Receipt(s)'),
137 'name' => ts('Cancel'),
144 * Set default values.
146 public function setDefaultValues() {
147 $defaultFormat = CRM_Core_BAO_PdfFormat
::getDefaultValues();
148 return array('pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0);
152 * Process the form after the input has been submitted and validated.
157 public function postProcess() {
158 // get all the details needed to generate a receipt
160 $template = CRM_Core_Smarty
::singleton();
162 $params = $this->controller
->exportValues($this->_name
);
163 $elements = self
::getElements($this->_contributionIds
, $params, $this->_contactIds
);
165 foreach ($elements['details'] as $contribID => $detail) {
166 $input = $ids = $objects = array();
168 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
172 $input['component'] = $detail['component'];
174 $ids['contact'] = $detail['contact'];
175 $ids['contribution'] = $contribID;
176 $ids['contributionRecur'] = NULL;
177 $ids['contributionPage'] = NULL;
178 $ids['membership'] = CRM_Utils_Array
::value('membership', $detail);
179 $ids['participant'] = CRM_Utils_Array
::value('participant', $detail);
180 $ids['event'] = CRM_Utils_Array
::value('event', $detail);
182 if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
183 CRM_Core_Error
::fatal();
186 $contribution = &$objects['contribution'];
188 // set some fake input values so we can reuse IPN code
189 $input['amount'] = $contribution->total_amount
;
190 $input['is_test'] = $contribution->is_test
;
191 $input['fee_amount'] = $contribution->fee_amount
;
192 $input['net_amount'] = $contribution->net_amount
;
193 $input['trxn_id'] = $contribution->trxn_id
;
194 $input['trxn_date'] = isset($contribution->trxn_date
) ?
$contribution->trxn_date
: NULL;
196 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
197 $objects['contribution']->receive_date
= CRM_Utils_Date
::isoToMysql($objects['contribution']->receive_date
);
201 if (isset($params['fromEmailAddress']) && !$elements['createPdf']) {
202 // CRM-19129 Allow useres the choice of From Email to send the receipt from.
203 $fromEmail = $params['fromEmailAddress'];
204 $from = CRM_Utils_Array
::value($fromEmail, $this->_emails
);
205 $fromDetails = explode(' <', $from);
206 $input['receipt_from_email'] = substr(trim($fromDetails[1]), 0, -1);
207 $input['receipt_from_name'] = str_replace('"', '', $fromDetails[0]);
210 $mail = $elements['baseIPN']->sendMail($input, $ids, $objects, $values, FALSE, $elements['createPdf']);
213 $message[] = $mail['html'];
216 $message[] = nl2br($mail['body']);
219 // reset template values before processing next transactions
220 $template->clearTemplateVars();
221 if (!empty($params['receipt_update'])) {
222 $objects['contribution']->receipt_date
= date('Y-m-d H-i-s');
223 $objects['contribution']->save();
227 if ($elements['createPdf']) {
228 CRM_Utils_PDF_Utils
::html2pdf($message,
229 'civicrmContributionReceipt.pdf',
231 $elements['params']['pdf_format_id']
233 CRM_Utils_System
::civiExit();
236 if ($elements['suppressedEmails']) {
237 $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).', array(1 => $elements['suppressedEmails']));
238 $msgTitle = ts('Email Error');
242 $status = ts('Your mail has been sent.');
243 $msgTitle = ts('Sent');
244 $msgType = 'success';
246 CRM_Core_Session
::setStatus($status, $msgTitle, $msgType);
251 * Declaration of common variables for Invoice and PDF.
254 * @param array $contribIds
256 * @param array $params
257 * Parameter for pdf or email invoices.
258 * @param array $contactIds
262 * array of common elements
265 static public function getElements($contribIds, $params, $contactIds) {
266 $pdfElements = array();
268 $pdfElements['contribIDs'] = implode(',', $contribIds);
270 $pdfElements['details'] = CRM_Contribute_Form_Task_Status
::getDetails($pdfElements['contribIDs']);
272 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
274 $pdfElements['params'] = $params;
276 $pdfElements['createPdf'] = FALSE;
277 if (!empty($pdfElements['params']['output']) &&
278 ($pdfElements['params']['output'] == "pdf_invoice" ||
$pdfElements['params']['output'] == "pdf_receipt")
280 $pdfElements['createPdf'] = TRUE;
283 $excludeContactIds = array();
284 if (!$pdfElements['createPdf']) {
285 $returnProperties = array(
292 list($contactDetails) = CRM_Utils_Token
::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
293 $pdfElements['suppressedEmails'] = 0;
294 $suppressedEmails = 0;
295 foreach ($contactDetails as $id => $values) {
296 if (empty($values['email']) ||
297 (empty($params['override_privacy']) && !empty($values['do_not_email']))
298 || CRM_Utils_Array
::value('is_deceased', $values)
299 ||
!empty($values['on_hold'])
302 $pdfElements['suppressedEmails'] = $suppressedEmails;
303 $excludeContactIds[] = $values['contact_id'];
307 $pdfElements['excludeContactIds'] = $excludeContactIds;