Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-08-10-15-41-33
[civicrm-core.git] / CRM / Contribute / Form / Task / PDF.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
31 * @copyright CiviCRM LLC (c) 2004-2015
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 /**
51 * Build all the data structures needed to build the form.
52 *
53 * @return void
54 */
55 public function preProcess() {
56 $id = CRM_Utils_Request::retrieve('id', 'Positive',
57 $this, FALSE
58 );
59
60 if ($id) {
61 $this->_contributionIds = array($id);
62 $this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
63 $this->_single = TRUE;
64 $this->assign('totalSelectedContributions', 1);
65 }
66 else {
67 parent::preProcess();
68 }
69
70 // check that all the contribution ids have pending status
71 $query = "
72 SELECT count(*)
73 FROM civicrm_contribution
74 WHERE contribution_status_id != 1
75 AND {$this->_componentClause}";
76 $count = CRM_Core_DAO::singleValueQuery($query);
77 if ($count != 0) {
78 CRM_Core_Error::statusBounce("Please select only online contributions with Completed status.");
79 }
80
81 // we have all the contribution ids, so now we get the contact ids
82 parent::setContactIDs();
83 $this->assign('single', $this->_single);
84
85 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
86 $urlParams = 'force=1';
87 if (CRM_Utils_Rule::qfKey($qfKey)) {
88 $urlParams .= "&qfKey=$qfKey";
89 }
90
91 $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams);
92 $breadCrumb = array(
93 array(
94 'url' => $url,
95 'title' => ts('Search Results'),
96 ),
97 );
98
99 CRM_Utils_System::appendBreadCrumb($breadCrumb);
100 CRM_Utils_System::setTitle(ts('Print Contribution Receipts'));
101 }
102
103 /**
104 * Build the form object.
105 */
106 public function buildQuickForm() {
107
108 $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt',
109 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';")
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 );
119 $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE);
120 $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do no email / Do not mail)'), FALSE);
121
122 $this->addButtons(array(
123 array(
124 'type' => 'next',
125 'name' => ts('Process Receipt(s)'),
126 'isDefault' => TRUE,
127 ),
128 array(
129 'type' => 'back',
130 'name' => ts('Cancel'),
131 ),
132 )
133 );
134 }
135
136 /**
137 * Set default values.
138 */
139 public function setDefaultValues() {
140 $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
141 return array('pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0);
142 }
143
144 /**
145 * Process the form after the input has been submitted and validated.
146 *
147 *
148 * @return void
149 */
150 public function postProcess() {
151 // get all the details needed to generate a receipt
152 $message = array();
153 $template = CRM_Core_Smarty::singleton();
154
155 $params = $this->controller->exportValues($this->_name);
156 $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds);
157
158 foreach ($elements['details'] as $contribID => $detail) {
159 $input = $ids = $objects = array();
160
161 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
162 continue;
163 }
164
165 $input['component'] = $detail['component'];
166
167 $ids['contact'] = $detail['contact'];
168 $ids['contribution'] = $contribID;
169 $ids['contributionRecur'] = NULL;
170 $ids['contributionPage'] = NULL;
171 $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
172 $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
173 $ids['event'] = CRM_Utils_Array::value('event', $detail);
174
175 if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
176 CRM_Core_Error::fatal();
177 }
178
179 $contribution = &$objects['contribution'];
180
181 // set some fake input values so we can reuse IPN code
182 $input['amount'] = $contribution->total_amount;
183 $input['is_test'] = $contribution->is_test;
184 $input['fee_amount'] = $contribution->fee_amount;
185 $input['net_amount'] = $contribution->net_amount;
186 $input['trxn_id'] = $contribution->trxn_id;
187 $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
188
189 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
190 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
191
192 $values = array();
193 $mail = $elements['baseIPN']->sendMail($input, $ids, $objects, $values, FALSE, $elements['createPdf']);
194
195 if ($mail['html']) {
196 $message[] = $mail['html'];
197 }
198 else {
199 $message[] = nl2br($mail['body']);
200 }
201
202 // reset template values before processing next transactions
203 $template->clearTemplateVars();
204 if (!empty($params['receipt_update'])) {
205 $objects['contribution']->receipt_date = date('Y-m-d H-i-s');
206 $objects['contribution']->save();
207 }
208 }
209
210 if ($elements['createPdf']) {
211 CRM_Utils_PDF_Utils::html2pdf($message,
212 'civicrmContributionReceipt.pdf',
213 FALSE,
214 $elements['params']['pdf_format_id']
215 );
216 CRM_Utils_System::civiExit();
217 }
218 else {
219 if ($elements['suppressedEmails']) {
220 $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']));
221 $msgTitle = ts('Email Error');
222 $msgType = 'error';
223 }
224 else {
225 $status = ts('Your mail has been sent.');
226 $msgTitle = ts('Sent');
227 $msgType = 'success';
228 }
229 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
230 }
231 }
232
233 /**
234 * Declaration of common variables for Invoice and PDF.
235 *
236 *
237 * @param array $contribIds
238 * Contribution Id.
239 * @param array $params
240 * Parameter for pdf or email invoices.
241 * @param array $contactIds
242 * Contact Id.
243 *
244 * @return array
245 * array of common elements
246 *
247 */
248 static public function getElements($contribIds, $params, $contactIds) {
249 $pdfElements = array();
250
251 $pdfElements['contribIDs'] = implode(',', $contribIds);
252
253 $pdfElements['details'] = CRM_Contribute_Form_Task_Status::getDetails($pdfElements['contribIDs']);
254
255 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
256
257 $pdfElements['params'] = $params;
258
259 $pdfElements['createPdf'] = FALSE;
260 if (!empty($pdfElements['params']['output']) &&
261 ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt")
262 ) {
263 $pdfElements['createPdf'] = TRUE;
264 }
265
266 $excludeContactIds = array();
267 if (!$pdfElements['createPdf']) {
268 $returnProperties = array(
269 'email' => 1,
270 'do_not_email' => 1,
271 'is_deceased' => 1,
272 'on_hold' => 1,
273 );
274
275 list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
276 $pdfElements['suppressedEmails'] = 0;
277 $suppressedEmails = 0;
278 foreach ($contactDetails as $id => $values) {
279 if (empty($values['email']) ||
280 (empty($params['override_privacy']) && !empty($values['do_not_email']))
281 || CRM_Utils_Array::value('is_deceased', $values)
282 || !empty($values['on_hold'])
283 ) {
284 $suppressedEmails++;
285 $pdfElements['suppressedEmails'] = $suppressedEmails;
286 $excludeContactIds[] = $values['contact_id'];
287 }
288 }
289 }
290 $pdfElements['excludeContactIds'] = $excludeContactIds;
291
292 return $pdfElements;
293 }
294
295 }