65d4fb1fa9577e5dbf958dad75ea868c7b4a7237
[civicrm-core.git] / CRM / Contribute / Form / Contribution / ThankYou.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Form for thank-you / success page - 3rd step of online contribution process.
20 */
21 class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_ContributionBase {
22
23 /**
24 * Membership price set status.
25 * @var bool
26 */
27 public $_useForMember;
28
29 /**
30 * Tranxaaction Id of the current contribution
31 * @var string
32 */
33 public $_trxnId;
34
35 /**
36 * Set variables up before form is built.
37 */
38 public function preProcess() {
39 parent::preProcess();
40
41 $this->_params = $this->get('params');
42 $this->_lineItem = $this->get('lineItem');
43 $this->_useForMember = $this->get('useForMember');
44 $is_deductible = $this->get('is_deductible');
45 $this->assign('is_deductible', $is_deductible);
46 $this->assign('thankyou_title', CRM_Utils_Array::value('thankyou_title', $this->_values));
47 $this->assign('thankyou_text', CRM_Utils_Array::value('thankyou_text', $this->_values));
48 $this->assign('thankyou_footer', CRM_Utils_Array::value('thankyou_footer', $this->_values));
49 $this->assign('max_reminders', CRM_Utils_Array::value('max_reminders', $this->_values));
50 $this->assign('initial_reminder_day', CRM_Utils_Array::value('initial_reminder_day', $this->_values));
51 CRM_Utils_System::setTitle(CRM_Utils_Array::value('thankyou_title', $this->_values));
52 // Make the contributionPageID available to the template
53 $this->assign('contributionPageID', $this->_id);
54 $this->assign('isShare', $this->_values['is_share']);
55
56 $this->_params['is_pay_later'] = $this->get('is_pay_later');
57 $this->assign('is_pay_later', $this->_params['is_pay_later']);
58 if ($this->_params['is_pay_later']) {
59 $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']);
60 }
61 $this->assign('is_for_organization', CRM_Utils_Array::value('is_for_organization', $this->_params));
62 }
63
64 /**
65 * Overwrite action, since we are only showing elements in frozen mode
66 * no help display needed
67 *
68 * @return int
69 */
70 public function getAction() {
71 if ($this->_action & CRM_Core_Action::PREVIEW) {
72 return CRM_Core_Action::VIEW | CRM_Core_Action::PREVIEW;
73 }
74 else {
75 return CRM_Core_Action::VIEW;
76 }
77 }
78
79 /**
80 * Build the form object.
81 */
82 public function buildQuickForm() {
83 // FIXME: Some of this code is identical to Confirm.php and should be broken out into a shared function
84 $this->assignToTemplate();
85 $this->_ccid = $this->get('ccid');
86 $productID = $this->get('productID');
87 $option = $this->get('option');
88 $membershipTypeID = $this->get('membershipTypeID');
89 $this->assign('receiptFromEmail', CRM_Utils_Array::value('receipt_from_email', $this->_values));
90
91 if ($productID) {
92 CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, FALSE, $productID, $option);
93 }
94
95 $params = $this->_params;
96 $invoicing = CRM_Invoicing_Utils::isInvoicingEnabled();
97 // Make a copy of line items array to use for display only
98 $tplLineItems = $this->_lineItem;
99 if ($invoicing) {
100 $getTaxDetails = FALSE;
101 foreach ($this->_lineItem as $key => $value) {
102 foreach ($value as $k => $v) {
103 if (isset($v['tax_rate'])) {
104 if ($v['tax_rate'] != '') {
105 $getTaxDetails = TRUE;
106 // Cast to float to display without trailing zero decimals
107 $tplLineItems[$key][$k]['tax_rate'] = (float) $v['tax_rate'];
108 }
109 }
110 }
111 }
112 $this->assign('getTaxDetails', $getTaxDetails);
113 $this->assign('taxTerm', CRM_Invoicing_Utils::getTaxTerm());
114 $this->assign('totalTaxAmount', $params['tax_amount']);
115 }
116
117 if ($this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) {
118 $this->assign('lineItem', $tplLineItems);
119 }
120 else {
121 if (is_array($membershipTypeID)) {
122 $membershipTypeID = current($membershipTypeID);
123 }
124 $this->assign('is_quick_config', 1);
125 $this->_params['is_quick_config'] = 1;
126 }
127 $this->assign('priceSetID', $this->_priceSetId);
128 $this->assign('useForMember', $this->get('useForMember'));
129
130 if (!empty($this->_values['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) {
131 $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE);
132
133 $this->assign('soft_credit_type', $softCreditTypes[$params['soft_credit_type_id']]);
134 CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields($this, $params['honor']);
135
136 $fieldTypes = ['Contact'];
137 $fieldTypes[] = CRM_Core_BAO_UFGroup::getContactType($this->_values['honoree_profile_id']);
138 $this->buildCustom($this->_values['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes);
139 }
140
141 $qParams = "reset=1&amp;id={$this->_id}";
142 //pcp elements
143 if ($this->_pcpId) {
144 $qParams .= "&amp;pcpId={$this->_pcpId}";
145 $this->assign('pcpBlock', TRUE);
146 foreach ([
147 'pcp_display_in_roll',
148 'pcp_is_anonymous',
149 'pcp_roll_nickname',
150 'pcp_personal_note',
151 ] as $val) {
152 if (!empty($this->_params[$val])) {
153 $this->assign($val, $this->_params[$val]);
154 }
155 }
156 }
157
158 $this->assign('qParams', $qParams);
159
160 if ($membershipTypeID) {
161 $transactionID = $this->get('membership_trx_id');
162 $membershipAmount = $this->get('membership_amount');
163 $renewalMode = $this->get('renewal_mode');
164 $this->assign('membership_trx_id', $transactionID);
165 $this->assign('membership_amount', $membershipAmount);
166 $this->assign('renewal_mode', $renewalMode);
167
168 $this->buildMembershipBlock(
169 $this->_membershipContactID,
170 FALSE,
171 $membershipTypeID,
172 TRUE,
173 NULL
174 );
175
176 if (!empty($params['auto_renew'])) {
177 $this->assign('auto_renew', TRUE);
178 }
179 }
180
181 $this->_separateMembershipPayment = $this->get('separateMembershipPayment');
182 $this->assign("is_separate_payment", $this->_separateMembershipPayment);
183
184 if (empty($this->_ccid)) {
185 $this->buildCustom($this->_values['custom_pre_id'], 'customPre', TRUE);
186 $this->buildCustom($this->_values['custom_post_id'], 'customPost', TRUE);
187 }
188 if (!empty($this->_values['onbehalf_profile_id']) &&
189 !empty($params['onbehalf']) &&
190 ($this->_values['is_for_organization'] == 2 ||
191 !empty($params['is_for_organization'])
192 ) && empty($this->_ccid)
193 ) {
194 $fieldTypes = ['Contact', 'Organization'];
195 $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization');
196 $fieldTypes = array_merge($fieldTypes, $contactSubType);
197 if (is_array($this->_membershipBlock) && !empty($this->_membershipBlock)) {
198 $fieldTypes = array_merge($fieldTypes, ['Membership']);
199 }
200 else {
201 $fieldTypes = array_merge($fieldTypes, ['Contribution']);
202 }
203
204 $this->buildCustom($this->_values['onbehalf_profile_id'], 'onbehalfProfile', TRUE, 'onbehalf', $fieldTypes);
205 }
206
207 $this->_trxnId = CRM_Utils_Array::value('trxn_id', $this->_params);
208
209 $this->assign('trxn_id', $this->_trxnId);
210
211 $this->assign('receive_date',
212 CRM_Utils_Date::mysqlToIso(CRM_Utils_Array::value('receive_date', $this->_params))
213 );
214
215 $defaults = [];
216 $fields = [];
217 foreach ($this->_fields as $name => $dontCare) {
218 if ($name != 'onbehalf' || $name != 'honor') {
219 $fields[$name] = 1;
220 }
221 }
222 $fields['state_province'] = $fields['country'] = $fields['email'] = 1;
223 $contact = $this->_params = $this->controller->exportValues('Main');
224
225 foreach ($fields as $name => $dontCare) {
226 if (isset($contact[$name])) {
227 $defaults[$name] = $contact[$name];
228 if (substr($name, 0, 7) == 'custom_') {
229 $timeField = "{$name}_time";
230 if (isset($contact[$timeField])) {
231 $defaults[$timeField] = $contact[$timeField];
232 }
233 }
234 elseif (in_array($name, [
235 'addressee',
236 'email_greeting',
237 'postal_greeting',
238 ]) && !empty($contact[$name . '_custom'])
239 ) {
240 $defaults[$name . '_custom'] = $contact[$name . '_custom'];
241 }
242 }
243 }
244
245 $this->_submitValues = array_merge($this->_submitValues, $defaults);
246
247 $this->setDefaults($defaults);
248
249 $values['entity_id'] = $this->_id;
250 $values['entity_table'] = 'civicrm_contribution_page';
251
252 CRM_Friend_BAO_Friend::retrieve($values, $data);
253 $tellAFriend = FALSE;
254 if ($this->_pcpId) {
255 if ($this->_pcpBlock['is_tellfriend_enabled']) {
256 $this->assign('friendText', ts('Tell a Friend'));
257 $subUrl = "eid={$this->_pcpId}&blockId={$this->_pcpBlock['id']}&pcomponent=pcp";
258 $tellAFriend = TRUE;
259 }
260 }
261 elseif (!empty($data['is_active'])) {
262 $friendText = $data['title'];
263 $this->assign('friendText', $friendText);
264 $subUrl = "eid={$this->_id}&pcomponent=contribute";
265 $tellAFriend = TRUE;
266 }
267
268 if ($tellAFriend) {
269 if ($this->_action & CRM_Core_Action::PREVIEW) {
270 $url = CRM_Utils_System::url("civicrm/friend",
271 "reset=1&action=preview&{$subUrl}"
272 );
273 }
274 else {
275 $url = CRM_Utils_System::url("civicrm/friend",
276 "reset=1&{$subUrl}"
277 );
278 }
279 $this->assign('friendURL', $url);
280 }
281
282 $isPendingOutcome = TRUE;
283 try {
284 // A payment notification update could have come in at any time. Check at the last minute.
285 $contributionStatusID = civicrm_api3('Contribution', 'getvalue', [
286 'id' => CRM_Utils_Array::value('contributionID', $params),
287 'return' => 'contribution_status_id',
288 'is_test' => ($this->_mode == 'test') ? 1 : 0,
289 'invoice_id' => CRM_Utils_Array::value('invoiceID', $params),
290 ]);
291 if (CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contributionStatusID) === 'Pending'
292 && !empty($params['payment_processor_id'])
293 ) {
294 $isPendingOutcome = TRUE;
295 }
296 else {
297 $isPendingOutcome = FALSE;
298 }
299 }
300 catch (CiviCRM_API3_Exception $e) {
301
302 }
303 $this->assign('isPendingOutcome', $isPendingOutcome);
304
305 $this->freeze();
306
307 // can we blow away the session now to prevent hackery
308 // CRM-9491
309 $this->controller->reset();
310 }
311
312 }