Merge pull request #857 from colemanw/is_test
[civicrm-core.git] / CRM / Contribute / Form / Task / PDF.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 *
33 */
34
35/**
36 * This class provides the functionality to email a group of
37 * contacts.
38 */
39class 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 /**
52 * build all the data structures needed to build the form
53 *
54 * @return void
55 * @access public
56 */ function preProcess() {
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 = "
73SELECT count(*)
74FROM civicrm_contribution
75WHERE contribution_status_id != 1
76AND {$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 /**
103 * Build the form
104 *
105 * @access public
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
123 $this->addButtons(array(
124 array(
125 'type' => 'next',
126 'name' => ts('Process Receipt(s)'),
127 'isDefault' => TRUE,
128 ),
129 array(
130 'type' => 'back',
131 'name' => ts('Cancel'),
132 ),
133 )
134 );
135 }
136
137 /**
138 * Set default values
139 */
140 function setDefaultValues() {
141 $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
142 return array('pdf_format_id' => $defaultFormat['id']);
143 }
144
145 /**
146 * process the form after the input has been submitted and validated
147 *
148 * @access public
149 *
150 * @return None
151 */
152 public function postProcess() {
153 // get all the details needed to generate a receipt
154 $contribIDs = implode(',', $this->_contributionIds);
155
156 $details = CRM_Contribute_Form_Task_Status::getDetails($contribIDs);
157
158 $baseIPN = new CRM_Core_Payment_BaseIPN();
159
160 $message = array();
161 $template = CRM_Core_Smarty::singleton();
162
163 $params = $this->controller->exportValues($this->_name);
164
165 $createPdf = FALSE;
166 if ($params['output'] == "pdf_receipt") {
167 $createPdf = TRUE;
168 }
169
170 $excludeContactIds = array();
171 if (!$createPdf) {
172 $returnProperties = array(
173 'email' => 1,
174 'do_not_email' => 1,
175 'is_deceased' => 1,
176 'on_hold' => 1,
177 );
178
179 list($contactDetails) = CRM_Utils_Token::getTokenDetails($this->_contactIds, $returnProperties, FALSE, FALSE);
180 $suppressedEmails = 0;
181 foreach ($contactDetails as $id => $values) {
182 if (empty($values['email']) ||
183 CRM_Utils_Array::value('do_not_email', $values) ||
184 CRM_Utils_Array::value('is_deceased', $values) ||
185 CRM_Utils_Array::value('on_hold', $values)
186 ) {
187 $suppressedEmails++;
188 $excludeContactIds[] = $values['contact_id'];
189 }
190 }
191 }
192
193 foreach ($details as $contribID => $detail) {
194 $input = $ids = $objects = array();
195
196 if (in_array($detail['contact'], $excludeContactIds)) {
197 continue;
198 }
199
200 $input['component'] = $detail['component'];
201
202 $ids['contact'] = $detail['contact'];
203 $ids['contribution'] = $contribID;
204 $ids['contributionRecur'] = NULL;
205 $ids['contributionPage'] = NULL;
206 $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
207 $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
208 $ids['event'] = CRM_Utils_Array::value('event', $detail);
209
210 if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
211 CRM_Core_Error::fatal();
212 }
213
214 $contribution = &$objects['contribution'];
215 // CRM_Core_Error::debug('o',$objects);
216
217
218 // set some fake input values so we can reuse IPN code
219 $input['amount'] = $contribution->total_amount;
220 $input['is_test'] = $contribution->is_test;
221 $input['fee_amount'] = $contribution->fee_amount;
222 $input['net_amount'] = $contribution->net_amount;
223 $input['trxn_id'] = $contribution->trxn_id;
224 $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
225
226 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
227 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
228
229 // CRM_Core_Error::debug('input',$input);
230
231 $values = array();
232 $mail = $baseIPN->sendMail($input, $ids, $objects, $values, FALSE, $createPdf);
233
234 if ($mail['html']) {
235 $message[] = $mail['html'];
236 }
237 else {
238 $message[] = nl2br($mail['body']);
239 }
240
241 // reset template values before processing next transactions
242 $template->clearTemplateVars();
243 }
244 if ($createPdf) {
245 CRM_Utils_PDF_Utils::html2pdf($message,
246 'civicrmContributionReceipt.pdf',
247 FALSE,
248 $params['pdf_format_id']
249 );
250 CRM_Utils_System::civiExit();
251 }
252 else {
253 if ($suppressedEmails) {
254 $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 => $suppressedEmails));
255 $msgTitle = ts('Email Error');
256 $msgType = 'error';
257 }
258 else {
259 $status = ts('Your mail has been sent.');
260 $msgTitle = ts('Sent');
261 $msgType = 'success';
262 }
263 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
264 }
265 }
266}
267