commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / 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 $this->assign('single', $this->_single);
82
83 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
84 $urlParams = 'force=1';
85 if (CRM_Utils_Rule::qfKey($qfKey)) {
86 $urlParams .= "&qfKey=$qfKey";
87 }
88
89 $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams);
90 $breadCrumb = array(
91 array(
92 'url' => $url,
93 'title' => ts('Search Results'),
94 ),
95 );
96 CRM_Contact_Form_Task_EmailCommon ::preProcessFromAddress($this, FALSE);
97 // we have all the contribution ids, so now we get the contact ids
98 parent::setContactIDs();
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(
113 'onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';
114 document.getElementById('selectEmailFrom').style.display = 'block';")
115 );
116 $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt',
117 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'block';")
118 );
119 $this->addRule('output', ts('Selection required'), 'required');
120
121 $this->add('select', 'pdf_format_id', ts('Page Format'),
122 array(0 => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE)
123 );
124 $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE);
125 $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do no email / Do not mail)'), FALSE);
126
127 $this->add('select', 'fromEmailAddress', ts('From Email'), $this->_fromEmails, FALSE, array('class' => 'crm-select2 huge'));
128
129 $this->addButtons(array(
130 array(
131 'type' => 'next',
132 'name' => ts('Process Receipt(s)'),
133 'isDefault' => TRUE,
134 ),
135 array(
136 'type' => 'back',
137 'name' => ts('Cancel'),
138 ),
139 )
140 );
141 }
142
143 /**
144 * Set default values.
145 */
146 public function setDefaultValues() {
147 $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
148 return array('pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0);
149 }
150
151 /**
152 * Process the form after the input has been submitted and validated.
153 *
154 *
155 * @return void
156 */
157 public function postProcess() {
158 // get all the details needed to generate a receipt
159 $message = array();
160 $template = CRM_Core_Smarty::singleton();
161
162 $params = $this->controller->exportValues($this->_name);
163 $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds);
164
165 foreach ($elements['details'] as $contribID => $detail) {
166 $input = $ids = $objects = array();
167
168 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
169 continue;
170 }
171
172 $input['component'] = $detail['component'];
173
174 $ids['contact'] = $detail['contact'];
175 $ids['contribution'] = $contribID;
176 $ids['contributionRecur'] = NULL;
177 $ids['contributionPage'] = NULL;
178 $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
179 $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
180 $ids['event'] = CRM_Utils_Array::value('event', $detail);
181
182 if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
183 CRM_Core_Error::fatal();
184 }
185
186 $contribution = &$objects['contribution'];
187
188 // set some fake input values so we can reuse IPN code
189 $input['amount'] = $contribution->total_amount;
190 $input['is_test'] = $contribution->is_test;
191 $input['fee_amount'] = $contribution->fee_amount;
192 $input['net_amount'] = $contribution->net_amount;
193 $input['trxn_id'] = $contribution->trxn_id;
194 $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
195
196 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
197 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
198
199 $values = array();
200
201 if (isset($params['fromEmailAddress']) && !$elements['createPdf']) {
202 // CRM-19129 Allow useres the choice of From Email to send the receipt from.
203 $fromEmail = $params['fromEmailAddress'];
204 $from = CRM_Utils_Array::value($fromEmail, $this->_emails);
205 $fromDetails = explode(' <', $from);
206 $input['receipt_from_email'] = substr(trim($fromDetails[1]), 0, -1);
207 $input['receipt_from_name'] = str_replace('"', '', $fromDetails[0]);
208 }
209
210 $mail = $elements['baseIPN']->sendMail($input, $ids, $objects, $values, FALSE, $elements['createPdf']);
211
212 if ($mail['html']) {
213 $message[] = $mail['html'];
214 }
215 else {
216 $message[] = nl2br($mail['body']);
217 }
218
219 // reset template values before processing next transactions
220 $template->clearTemplateVars();
221 if (!empty($params['receipt_update'])) {
222 $objects['contribution']->receipt_date = date('Y-m-d H-i-s');
223 $objects['contribution']->save();
224 }
225 }
226
227 if ($elements['createPdf']) {
228 CRM_Utils_PDF_Utils::html2pdf($message,
229 'civicrmContributionReceipt.pdf',
230 FALSE,
231 $elements['params']['pdf_format_id']
232 );
233 CRM_Utils_System::civiExit();
234 }
235 else {
236 if ($elements['suppressedEmails']) {
237 $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']));
238 $msgTitle = ts('Email Error');
239 $msgType = 'error';
240 }
241 else {
242 $status = ts('Your mail has been sent.');
243 $msgTitle = ts('Sent');
244 $msgType = 'success';
245 }
246 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
247 }
248 }
249
250 /**
251 * Declaration of common variables for Invoice and PDF.
252 *
253 *
254 * @param array $contribIds
255 * Contribution Id.
256 * @param array $params
257 * Parameter for pdf or email invoices.
258 * @param array $contactIds
259 * Contact Id.
260 *
261 * @return array
262 * array of common elements
263 *
264 */
265 static public function getElements($contribIds, $params, $contactIds) {
266 $pdfElements = array();
267
268 $pdfElements['contribIDs'] = implode(',', $contribIds);
269
270 $pdfElements['details'] = CRM_Contribute_Form_Task_Status::getDetails($pdfElements['contribIDs']);
271
272 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
273
274 $pdfElements['params'] = $params;
275
276 $pdfElements['createPdf'] = FALSE;
277 if (!empty($pdfElements['params']['output']) &&
278 ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt")
279 ) {
280 $pdfElements['createPdf'] = TRUE;
281 }
282
283 $excludeContactIds = array();
284 if (!$pdfElements['createPdf']) {
285 $returnProperties = array(
286 'email' => 1,
287 'do_not_email' => 1,
288 'is_deceased' => 1,
289 'on_hold' => 1,
290 );
291
292 list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
293 $pdfElements['suppressedEmails'] = 0;
294 $suppressedEmails = 0;
295 foreach ($contactDetails as $id => $values) {
296 if (empty($values['email']) ||
297 (empty($params['override_privacy']) && !empty($values['do_not_email']))
298 || CRM_Utils_Array::value('is_deceased', $values)
299 || !empty($values['on_hold'])
300 ) {
301 $suppressedEmails++;
302 $pdfElements['suppressedEmails'] = $suppressedEmails;
303 $excludeContactIds[] = $values['contact_id'];
304 }
305 }
306 }
307 $pdfElements['excludeContactIds'] = $excludeContactIds;
308
309 return $pdfElements;
310 }
311
312 }