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