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