various comment fixes
[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-2014 |
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-2014
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 *
107 * @return void
108 */
109 public function buildQuickForm() {
110
111 $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt',
112 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';")
113 );
114 $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt',
115 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'block';")
116 );
117 $this->addRule('output', ts('Selection required'), 'required');
118
119 $this->add('select', 'pdf_format_id', ts('Page Format'),
120 array(0 => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE)
121 );
122 $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE);
123 $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do no email / Do not mail)'), FALSE);
124
125 $this->addButtons(array(
126 array(
127 'type' => 'next',
128 'name' => ts('Process Receipt(s)'),
129 'isDefault' => TRUE,
130 ),
131 array(
132 'type' => 'back',
133 'name' => ts('Cancel'),
134 ),
135 )
136 );
137 }
138
139 /**
140 * Set default values
141 */
142 public function setDefaultValues() {
143 $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
144 return array('pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0);
145 }
146
147 /**
148 * Process the form after the input has been submitted and validated
149 *
150 *
151 * @return void
152 */
153 public function postProcess() {
154 // get all the details needed to generate a receipt
155 $message = array();
156 $template = CRM_Core_Smarty::singleton();
157
158 $params = $this->controller->exportValues($this->_name);
159 $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds);
160
161 foreach ($elements['details'] as $contribID => $detail) {
162 $input = $ids = $objects = array();
163
164 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
165 continue;
166 }
167
168 $input['component'] = $detail['component'];
169
170 $ids['contact'] = $detail['contact'];
171 $ids['contribution'] = $contribID;
172 $ids['contributionRecur'] = NULL;
173 $ids['contributionPage'] = NULL;
174 $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
175 $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
176 $ids['event'] = CRM_Utils_Array::value('event', $detail);
177
178 if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
179 CRM_Core_Error::fatal();
180 }
181
182 $contribution = &$objects['contribution'];
183
184 // set some fake input values so we can reuse IPN code
185 $input['amount'] = $contribution->total_amount;
186 $input['is_test'] = $contribution->is_test;
187 $input['fee_amount'] = $contribution->fee_amount;
188 $input['net_amount'] = $contribution->net_amount;
189 $input['trxn_id'] = $contribution->trxn_id;
190 $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
191
192 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
193 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
194
195 $values = array();
196 $mail = $elements['baseIPN']->sendMail($input, $ids, $objects, $values, FALSE, $elements['createPdf']);
197
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();
207 if (!empty($params['receipt_update'])) {
208 $objects['contribution']->receipt_date = date('Y-m-d H-i-s');
209 $objects['contribution']->save();
210 }
211 }
212
213 if ($elements['createPdf']) {
214 CRM_Utils_PDF_Utils::html2pdf($message,
215 'civicrmContributionReceipt.pdf',
216 FALSE,
217 $elements['params']['pdf_format_id']
218 );
219 CRM_Utils_System::civiExit();
220 }
221 else {
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']));
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 }
235
236 /**
237 * Declaration of common variables for Invoice and PDF
238 *
239 *
240 * @param array $contribIds
241 * Contribution Id.
242 * @param array $params
243 * Parameter for pdf or email invoices.
244 * @param array $contactIds
245 * Contact Id.
246 *
247 * @return array
248 * array of common elements
249 *
250 */
251 static public function getElements($contribIds, $params, $contactIds) {
252 $pdfElements = array();
253
254 $pdfElements['contribIDs'] = implode(',', $contribIds);
255
256 $pdfElements['details'] = CRM_Contribute_Form_Task_Status::getDetails($pdfElements['contribIDs']);
257
258 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
259
260 $pdfElements['params'] = $params;
261
262 $pdfElements['createPdf'] = FALSE;
263 if (!empty($pdfElements['params']['output']) &&
264 ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt")
265 ) {
266 $pdfElements['createPdf'] = TRUE;
267 }
268
269 $excludeContactIds = array();
270 if (!$pdfElements['createPdf']) {
271 $returnProperties = array(
272 'email' => 1,
273 'do_not_email' => 1,
274 'is_deceased' => 1,
275 'on_hold' => 1,
276 );
277
278 list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
279 $pdfElements['suppressedEmails'] = 0;
280 $suppressedEmails = 0;
281 foreach ($contactDetails as $id => $values) {
282 if (empty($values['email']) ||
283 (empty($params['override_privacy']) && !empty($values['do_not_email']))
284 || CRM_Utils_Array::value('is_deceased', $values)
285 || !empty($values['on_hold'])
286 ) {
287 $suppressedEmails++;
288 $pdfElements['suppressedEmails'] = $suppressedEmails;
289 $excludeContactIds[] = $values['contact_id'];
290 }
291 }
292 }
293 $pdfElements['excludeContactIds'] = $excludeContactIds;
294
295 return $pdfElements;
296 }
297
298 }