CRM-15070 Fix for defualt value.
[civicrm-core.git] / CRM / Contribute / Form / Task / PDF.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 *
355ba699 150 * @return void
6a488035
TO
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) {
8cc574cf
CW
182 if (empty($values['email']) || !empty($values['do_not_email']) ||
183 CRM_Utils_Array::value('is_deceased', $values) || !empty($values['on_hold'])) {
6a488035
TO
184 $suppressedEmails++;
185 $excludeContactIds[] = $values['contact_id'];
186 }
187 }
188 }
189
190 foreach ($details as $contribID => $detail) {
191 $input = $ids = $objects = array();
192
193 if (in_array($detail['contact'], $excludeContactIds)) {
194 continue;
195 }
196
197 $input['component'] = $detail['component'];
198
199 $ids['contact'] = $detail['contact'];
200 $ids['contribution'] = $contribID;
201 $ids['contributionRecur'] = NULL;
202 $ids['contributionPage'] = NULL;
203 $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
204 $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
205 $ids['event'] = CRM_Utils_Array::value('event', $detail);
206
207 if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
208 CRM_Core_Error::fatal();
209 }
210
211 $contribution = &$objects['contribution'];
212 // CRM_Core_Error::debug('o',$objects);
213
214
215 // set some fake input values so we can reuse IPN code
216 $input['amount'] = $contribution->total_amount;
217 $input['is_test'] = $contribution->is_test;
218 $input['fee_amount'] = $contribution->fee_amount;
219 $input['net_amount'] = $contribution->net_amount;
220 $input['trxn_id'] = $contribution->trxn_id;
221 $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
222
665e5ec7 223 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
6a488035
TO
224 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
225
226 // CRM_Core_Error::debug('input',$input);
227
228 $values = array();
229 $mail = $baseIPN->sendMail($input, $ids, $objects, $values, FALSE, $createPdf);
230
231 if ($mail['html']) {
232 $message[] = $mail['html'];
233 }
234 else {
235 $message[] = nl2br($mail['body']);
236 }
237
238 // reset template values before processing next transactions
239 $template->clearTemplateVars();
240 }
241 if ($createPdf) {
242 CRM_Utils_PDF_Utils::html2pdf($message,
243 'civicrmContributionReceipt.pdf',
244 FALSE,
245 $params['pdf_format_id']
246 );
247 CRM_Utils_System::civiExit();
248 }
249 else {
250 if ($suppressedEmails) {
251 $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));
252 $msgTitle = ts('Email Error');
253 $msgType = 'error';
254 }
255 else {
256 $status = ts('Your mail has been sent.');
257 $msgTitle = ts('Sent');
258 $msgType = 'success';
259 }
260 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
261 }
262 }
263}
264