Merge branch '4.6' into 'master'
[civicrm-core.git] / CRM / Contribute / Form / Task / PDF.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
35 * This class provides the functionality to email a group of
36 * contacts.
37 */
38class 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.
6a488035
TO
52 *
53 * @return void
95ea96be 54 */
389bcebf 55 public function preProcess() {
6a488035
TO
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 = "
72SELECT count(*)
73FROM civicrm_contribution
74WHERE contribution_status_id != 1
75AND {$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(
874c9be7 93 array(
353ffa53 94 'url' => $url,
6a488035 95 'title' => ts('Search Results'),
389bcebf 96 ),
353ffa53 97 );
6a488035
TO
98
99 CRM_Utils_System::appendBreadCrumb($breadCrumb);
100 CRM_Utils_System::setTitle(ts('Print Contribution Receipts'));
101 }
102
103 /**
fe482240 104 * Build the form object.
6a488035
TO
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 );
10708045
EM
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);
6a488035
TO
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 /**
fe482240 137 * Set default values.
6a488035 138 */
00be9182 139 public function setDefaultValues() {
6a488035 140 $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
10708045 141 return array('pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0);
6a488035
TO
142 }
143
144 /**
fe482240 145 * Process the form after the input has been submitted and validated.
6a488035 146 *
6a488035 147 *
355ba699 148 * @return void
6a488035
TO
149 */
150 public function postProcess() {
151 // get all the details needed to generate a receipt
2826a42e
RK
152 $message = array();
153 $template = CRM_Core_Smarty::singleton();
6a488035 154
6efffa5d
PB
155 $params = $this->controller->exportValues($this->_name);
156 $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds);
6a488035 157
2826a42e 158 foreach ($elements['details'] as $contribID => $detail) {
6a488035
TO
159 $input = $ids = $objects = array();
160
3a1261ac 161 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
6a488035
TO
162 continue;
163 }
164
165 $input['component'] = $detail['component'];
166
167 $ids['contact'] = $detail['contact'];
2826a42e 168 $ids['contribution'] = $contribID;
6a488035
TO
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
3a1261ac 175 if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
6a488035
TO
176 CRM_Core_Error::fatal();
177 }
178
179 $contribution = &$objects['contribution'];
6a488035
TO
180
181 // set some fake input values so we can reuse IPN code
353ffa53
TO
182 $input['amount'] = $contribution->total_amount;
183 $input['is_test'] = $contribution->is_test;
6a488035
TO
184 $input['fee_amount'] = $contribution->fee_amount;
185 $input['net_amount'] = $contribution->net_amount;
353ffa53
TO
186 $input['trxn_id'] = $contribution->trxn_id;
187 $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
6a488035 188
665e5ec7 189 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
6a488035
TO
190 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
191
6a488035 192 $values = array();
3a1261ac 193 $mail = $elements['baseIPN']->sendMail($input, $ids, $objects, $values, FALSE, $elements['createPdf']);
6a488035
TO
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();
10708045
EM
204 if (!empty($params['receipt_update'])) {
205 $objects['contribution']->receipt_date = date('Y-m-d H-i-s');
206 $objects['contribution']->save();
207 }
6a488035 208 }
3a1261ac
RK
209
210 if ($elements['createPdf']) {
6a488035
TO
211 CRM_Utils_PDF_Utils::html2pdf($message,
212 'civicrmContributionReceipt.pdf',
213 FALSE,
3a1261ac 214 $elements['params']['pdf_format_id']
6a488035
TO
215 );
216 CRM_Utils_System::civiExit();
217 }
218 else {
3a1261ac
RK
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']));
6a488035
TO
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 }
3a1261ac
RK
232
233 /**
fe482240 234 * Declaration of common variables for Invoice and PDF.
3a1261ac 235 *
3a1261ac 236 *
014c4014
TO
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.
6efffa5d 243 *
a6c01b45
CW
244 * @return array
245 * array of common elements
2826a42e 246 *
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']) &&
353ffa53
TO
261 ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt")
262 ) {
3a1261ac
RK
263 $pdfElements['createPdf'] = TRUE;
264 }
265
266 $excludeContactIds = array();
267 if (!$pdfElements['createPdf']) {
268 $returnProperties = array(
353ffa53
TO
269 'email' => 1,
270 'do_not_email' => 1,
271 'is_deceased' => 1,
272 'on_hold' => 1,
273 );
3a1261ac 274
6efffa5d 275 list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
3a1261ac 276 $pdfElements['suppressedEmails'] = 0;
830b3e83 277 $suppressedEmails = 0;
3a1261ac 278 foreach ($contactDetails as $id => $values) {
10708045
EM
279 if (empty($values['email']) ||
280 (empty($params['override_privacy']) && !empty($values['do_not_email']))
281 || CRM_Utils_Array::value('is_deceased', $values)
353ffa53
TO
282 || !empty($values['on_hold'])
283 ) {
874c9be7
TO
284 $suppressedEmails++;
285 $pdfElements['suppressedEmails'] = $suppressedEmails;
286 $excludeContactIds[] = $values['contact_id'];
3a1261ac
RK
287 }
288 }
289 }
290 $pdfElements['excludeContactIds'] = $excludeContactIds;
2826a42e 291
3a1261ac
RK
292 return $pdfElements;
293 }
96025800 294
6a488035 295}