Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | +--------------------------------------------------------------------+ | |
006389de | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
d90a1a52 | 35 | * This class generates form components for processing a Contribution. |
6a488035 TO |
36 | */ |
37 | class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_ContributionBase { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Define default MembershipType Id. |
6a488035 TO |
41 | */ |
42 | public $_defaultMemTypeId; | |
43 | ||
6a488035 | 44 | public $_paymentProcessors; |
6a488035 TO |
45 | |
46 | public $_membershipTypeValues; | |
47 | ||
48 | public $_useForMember; | |
49 | ||
dde5a0ef | 50 | /** |
100fef9d | 51 | * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl |
dde5a0ef EM |
52 | * @var array |
53 | */ | |
54 | public $_paymentFields = array(); | |
55 | ||
e50ee6a3 | 56 | protected $_paymentProcessorID; |
7c8cc461 | 57 | protected $_snippet; |
6a488035 TO |
58 | |
59 | /** | |
fe482240 | 60 | * Set variables up before form is built. |
6a488035 TO |
61 | */ |
62 | public function preProcess() { | |
63 | parent::preProcess(); | |
64 | ||
42e3a033 EM |
65 | $this->_paymentProcessors = $this->get('paymentProcessors'); |
66 | $this->preProcessPaymentOptions(); | |
6a488035 | 67 | |
58466af1 | 68 | if (!empty($this->_ccid)) { |
69 | $payment = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_ccid, 'contribution'); | |
70 | //bounce if the contribution is not pending. | |
71 | if (empty($payment['balance'])) { | |
72 | CRM_Core_Error::statusBounce(ts("Returning since contribution has already been handled.")); | |
73 | } | |
74 | if (!empty($payment['total'])) { | |
75 | $this->_pendingAmount = $payment['total']; | |
76 | $this->assign('pendingAmount', $this->_pendingAmount); | |
77 | } | |
78 | $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_ccid); | |
79 | foreach (array_keys($lineItems) as $id) { | |
80 | $lineItems[$id]['id'] = $id; | |
81 | } | |
82 | $itemId = key($lineItems); | |
83 | if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) { | |
84 | $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id'); | |
85 | } | |
86 | ||
87 | if (!empty($lineItems[$itemId]['price_field_id'])) { | |
88 | $this->_lineItem[$this->_priceSetId] = $lineItems; | |
89 | } | |
90 | $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); | |
91 | $this->assign('lineItem', $this->_lineItem); | |
92 | $this->assign('is_quick_config', $isQuickConfig); | |
93 | $this->assign('priceSetID', $this->_priceSetId); | |
94 | } | |
95 | ||
b44e3f84 | 96 | // Make the contributionPageID available to the template |
6a488035 | 97 | $this->assign('contributionPageID', $this->_id); |
58466af1 | 98 | $this->assign('ccid', $this->_ccid); |
6a488035 TO |
99 | $this->assign('isShare', CRM_Utils_Array::value('is_share', $this->_values)); |
100 | $this->assign('isConfirmEnabled', CRM_Utils_Array::value('is_confirm_enabled', $this->_values)); | |
101 | ||
a3d827a7 | 102 | $this->assign('reset', CRM_Utils_Request::retrieve('reset', 'Boolean')); |
d90a1a52 EM |
103 | $this->assign('mainDisplay', CRM_Utils_Request::retrieve('_qf_Main_display', 'Boolean', |
104 | CRM_Core_DAO::$_nullObject)); | |
6a488035 | 105 | |
78f0ee1b | 106 | if (!empty($this->_pcpInfo['id']) && !empty($this->_pcpInfo['intro_text'])) { |
6a488035 TO |
107 | $this->assign('intro_text', $this->_pcpInfo['intro_text']); |
108 | } | |
78f0ee1b | 109 | elseif (!empty($this->_values['intro_text'])) { |
6a488035 TO |
110 | $this->assign('intro_text', $this->_values['intro_text']); |
111 | } | |
112 | ||
113 | $qParams = "reset=1&id={$this->_id}"; | |
78f0ee1b | 114 | if ($pcpId = CRM_Utils_Array::value('pcp_id', $this->_pcpInfo)) { |
6a488035 TO |
115 | $qParams .= "&pcpId={$pcpId}"; |
116 | } | |
78f0ee1b | 117 | $this->assign('qParams', $qParams); |
6a488035 | 118 | |
78f0ee1b | 119 | if (!empty($this->_values['footer_text'])) { |
6a488035 TO |
120 | $this->assign('footer_text', $this->_values['footer_text']); |
121 | } | |
6a488035 TO |
122 | } |
123 | ||
186c9c17 | 124 | /** |
fe482240 | 125 | * Set the default values. |
186c9c17 | 126 | */ |
00be9182 | 127 | public function setDefaultValues() { |
6a488035 | 128 | // check if the user is registered and we have a contact ID |
da8d9879 | 129 | $contactID = $this->getContactID(); |
6a488035 | 130 | |
da8d9879 | 131 | if (!empty($contactID)) { |
6a488035 TO |
132 | $fields = array(); |
133 | $removeCustomFieldTypes = array('Contribution', 'Membership'); | |
134 | $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields(); | |
135 | ||
136 | // remove component related fields | |
137 | foreach ($this->_fields as $name => $dontCare) { | |
138 | //don't set custom data Used for Contribution (CRM-1344) | |
139 | if (substr($name, 0, 7) == 'custom_') { | |
140 | $id = substr($name, 7); | |
141 | if (!CRM_Core_BAO_CustomGroup::checkCustomField($id, $removeCustomFieldTypes)) { | |
142 | continue; | |
143 | } | |
144 | // ignore component fields | |
145 | } | |
146 | elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) == 'membership_') || (substr($name, 0, 13) == 'contribution_')) { | |
147 | continue; | |
148 | } | |
149 | $fields[$name] = 1; | |
150 | } | |
fc60f30e RN |
151 | |
152 | if (!empty($fields)) { | |
472561f4 | 153 | CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults); |
fc60f30e RN |
154 | } |
155 | ||
9d665938 | 156 | $billingDefaults = $this->getProfileDefaults('Billing', $contactID); |
157 | $this->_defaults = array_merge($this->_defaults, $billingDefaults); | |
6a488035 | 158 | } |
58466af1 | 159 | if (!empty($this->_ccid) && !empty($this->_pendingAmount)) { |
fe552de9 | 160 | $this->_defaults['total_amount'] = CRM_Utils_Money::format($this->_pendingAmount, NULL, '%a'); |
58466af1 | 161 | } |
6a488035 | 162 | |
aefd7f6b EM |
163 | /* |
164 | * hack to simplify credit card entry for testing | |
165 | * | |
166 | * $this->_defaults['credit_card_type'] = 'Visa'; | |
167 | * $this->_defaults['amount'] = 168; | |
168 | * $this->_defaults['credit_card_number'] = '4111111111111111'; | |
169 | * $this->_defaults['cvv2'] = '000'; | |
170 | * $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05'); | |
171 | * // hack to simplify direct debit entry for testing | |
172 | * $this->_defaults['account_holder'] = 'Max Müller'; | |
173 | * $this->_defaults['bank_account_number'] = '12345678'; | |
174 | * $this->_defaults['bank_identification_number'] = '12030000'; | |
175 | * $this->_defaults['bank_name'] = 'Bankname'; | |
176 | */ | |
6a488035 TO |
177 | |
178 | //build set default for pledge overdue payment. | |
a7488080 | 179 | if (!empty($this->_values['pledge_id'])) { |
133e2c99 | 180 | //used to record completed pledge payment ids used later for honor default |
181 | $completedContributionIds = array(); | |
133e2c99 | 182 | $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($this->_values['pledge_id']); |
6a488035 | 183 | |
b8887609 | 184 | $paymentAmount = 0; |
6a488035 | 185 | $duePayment = FALSE; |
133e2c99 | 186 | foreach ($pledgePayments as $payId => $value) { |
187 | if ($value['status'] == 'Overdue') { | |
6a488035 | 188 | $this->_defaults['pledge_amount'][$payId] = 1; |
b8887609 | 189 | $paymentAmount += $value['scheduled_amount']; |
6a488035 | 190 | } |
133e2c99 | 191 | elseif (!$duePayment && $value['status'] == 'Pending') { |
6a488035 | 192 | $this->_defaults['pledge_amount'][$payId] = 1; |
b8887609 | 193 | $paymentAmount += $value['scheduled_amount']; |
6a488035 TO |
194 | $duePayment = TRUE; |
195 | } | |
133e2c99 | 196 | elseif ($value['status'] == 'Completed' && $value['contribution_id']) { |
197 | $completedContributionIds[] = $value['contribution_id']; | |
198 | } | |
199 | } | |
b8887609 | 200 | $this->_defaults['price_' . $this->_priceSetId] = $paymentAmount; |
133e2c99 | 201 | |
4779abb3 | 202 | if (count($completedContributionIds)) { |
133e2c99 | 203 | $softCredit = array(); |
204 | foreach ($completedContributionIds as $id) { | |
205 | $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id); | |
206 | } | |
207 | if (isset($softCredit['soft_credit'])) { | |
208 | $this->_defaults['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type']; | |
209 | ||
210 | //since honoree profile fieldname of fields are prefixed with 'honor' | |
211 | //we need to reformat the fieldname to append prefix during setting default values | |
212 | CRM_Core_BAO_UFGroup::setProfileDefaults( | |
213 | $softCredit['soft_credit'][1]['contact_id'], | |
214 | CRM_Core_BAO_UFGroup::getFields($this->_honoreeProfileId), | |
215 | $defaults | |
216 | ); | |
217 | foreach ($defaults as $fieldName => $value) { | |
218 | $this->_defaults['honor[' . $fieldName . ']'] = $value; | |
219 | } | |
220 | } | |
6a488035 TO |
221 | } |
222 | } | |
a7488080 | 223 | elseif (!empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
224 | //set default to one time contribution. |
225 | $this->_defaults['is_pledge'] = 0; | |
226 | } | |
227 | ||
228 | // to process Custom data that are appended to URL | |
229 | $getDefaults = CRM_Core_BAO_CustomGroup::extractGetParams($this, "'Contact', 'Individual', 'Contribution'"); | |
03110609 | 230 | $this->_defaults = array_merge($this->_defaults, $getDefaults); |
6a488035 TO |
231 | |
232 | $config = CRM_Core_Config::singleton(); | |
233 | // set default country from config if no country set | |
a7488080 | 234 | if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) { |
6a488035 TO |
235 | $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
236 | } | |
237 | ||
238 | // set default state/province from config if no state/province set | |
a7488080 | 239 | if (empty($this->_defaults["billing_state_province_id-{$this->_bltID}"])) { |
6a488035 TO |
240 | $this->_defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
241 | } | |
242 | ||
c432c016 | 243 | $entityId = $memtypeID = NULL; |
6a488035 | 244 | if ($this->_priceSetId) { |
a1a68e64 | 245 | if (($this->_useForMember && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) { |
6a488035 TO |
246 | $selectedCurrentMemTypes = array(); |
247 | foreach ($this->_priceSet['fields'] as $key => $val) { | |
248 | foreach ($val['options'] as $keys => $values) { | |
249 | $opMemTypeId = CRM_Utils_Array::value('membership_type_id', $values); | |
352c43f7 | 250 | $priceFieldName = 'price_' . $values['price_field_id']; |
034b5cb6 KJ |
251 | $priceFieldValue = CRM_Price_BAO_PriceSet::getPriceFieldValueFromURL($this, $priceFieldName); |
252 | if (!empty($priceFieldValue)) { | |
253 | CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $this->_defaults); | |
254 | // break here to prevent overwriting of default due to 'is_default' | |
255 | // option configuration or setting of current membership or | |
256 | // membership for related organization. | |
257 | // The value sent via URL get's higher priority. | |
258 | break; | |
259 | } | |
260 | elseif ($opMemTypeId && | |
6a488035 TO |
261 | in_array($opMemTypeId, $this->_currentMemberships) && |
262 | !in_array($opMemTypeId, $selectedCurrentMemTypes) | |
263 | ) { | |
034b5cb6 | 264 | CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults); |
c432c016 | 265 | $memtypeID = $selectedCurrentMemTypes[] = $values['membership_type_id']; |
6a488035 | 266 | } |
a7488080 | 267 | elseif (!empty($values['is_default']) && |
6a488035 | 268 | !$opMemTypeId && |
034b5cb6 KJ |
269 | (!isset($this->_defaults[$priceFieldName]) || |
270 | ($val['html_type'] == 'CheckBox' && | |
271 | !isset($this->_defaults[$priceFieldName][$keys])) | |
0cb9abc9 KJ |
272 | )) { |
273 | CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults); | |
c432c016 | 274 | $memtypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_defaults[$priceFieldName], 'membership_type_id'); |
6a488035 TO |
275 | } |
276 | } | |
277 | } | |
a97d770e | 278 | $entityId = CRM_Utils_Array::value('id', CRM_Member_BAO_Membership::getContactMembership($contactID, $memtypeID, NULL)); |
6a488035 TO |
279 | } |
280 | else { | |
9da8dc8c | 281 | CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $this->_defaults); |
6a488035 TO |
282 | } |
283 | } | |
284 | ||
a97d770e | 285 | //set custom field defaults set by admin if value is not set |
286 | if (!empty($this->_fields)) { | |
287 | //load default campaign from page. | |
288 | if (array_key_exists('contribution_campaign_id', $this->_fields)) { | |
289 | $this->_defaults['contribution_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values); | |
290 | } | |
291 | ||
292 | //set custom field defaults | |
293 | foreach ($this->_fields as $name => $field) { | |
294 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
295 | if (!isset($this->_defaults[$name])) { | |
296 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults, | |
297 | $entityId, CRM_Profile_Form::MODE_REGISTER | |
298 | ); | |
299 | } | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
6a488035 TO |
304 | if (!empty($this->_paymentProcessors)) { |
305 | foreach ($this->_paymentProcessors as $pid => $value) { | |
a7488080 | 306 | if (!empty($value['is_default'])) { |
e02d7e96 | 307 | $this->_defaults['payment_processor_id'] = $pid; |
6a488035 TO |
308 | } |
309 | } | |
310 | } | |
311 | ||
312 | return $this->_defaults; | |
313 | } | |
314 | ||
315 | /** | |
fe482240 | 316 | * Build the form object. |
6a488035 TO |
317 | */ |
318 | public function buildQuickForm() { | |
4839c695 KJ |
319 | // build profiles first so that we can determine address fields etc |
320 | // and then show copy address checkbox | |
2f44fc96 | 321 | if (empty($this->_ccid)) { |
322 | $this->buildCustom($this->_values['custom_pre_id'], 'customPre'); | |
323 | $this->buildCustom($this->_values['custom_post_id'], 'customPost'); | |
4839c695 | 324 | |
2f44fc96 | 325 | // CRM-18399: used by template to pass pre profile id as a url arg |
326 | $this->assign('custom_pre_id', $this->_values['custom_pre_id']); | |
d38c288e | 327 | |
2f44fc96 | 328 | $this->buildComponentForm($this->_id, $this); |
329 | } | |
bcb8cc84 | 330 | |
7bf9cde2 | 331 | // Build payment processor form |
6cc679d2 | 332 | CRM_Core_Payment_ProcessorForm::buildQuickForm($this); |
6a488035 TO |
333 | |
334 | $config = CRM_Core_Config::singleton(); | |
78f0ee1b | 335 | |
37326fa1 DG |
336 | $contactID = $this->getContactID(); |
337 | if ($contactID) { | |
338 | $this->assign('contact_id', $contactID); | |
f498a273 | 339 | $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID)); |
37326fa1 DG |
340 | } |
341 | ||
6a488035 | 342 | $this->applyFilter('__ALL__', 'trim'); |
58466af1 | 343 | if (empty($this->_ccid)) { |
344 | $this->add('text', "email-{$this->_bltID}", | |
345 | ts('Email Address'), | |
346 | array('size' => 30, 'maxlength' => 60, 'class' => 'email'), | |
347 | TRUE | |
348 | ); | |
349 | $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email'); | |
350 | } | |
351 | else { | |
352 | $this->addElement('hidden', "email-{$this->_bltID}", 1); | |
353 | $this->add('text', 'total_amount', ts('Total Amount'), array('readonly' => TRUE), FALSE); | |
58466af1 | 354 | } |
6a488035 | 355 | $pps = array(); |
67399793 | 356 | //@todo - this should be replaced by a check as to whether billing fields are set |
596bff78 | 357 | $onlinePaymentProcessorEnabled = FALSE; |
6a488035 | 358 | if (!empty($this->_paymentProcessors)) { |
596bff78 | 359 | foreach ($this->_paymentProcessors as $key => $name) { |
22e263ad | 360 | if ($name['billing_mode'] == 1) { |
596bff78 | 361 | $onlinePaymentProcessorEnabled = TRUE; |
362 | } | |
6a488035 TO |
363 | $pps[$key] = $name['name']; |
364 | } | |
365 | } | |
a7488080 | 366 | if (!empty($this->_values['is_pay_later'])) { |
6a488035 TO |
367 | $pps[0] = $this->_values['pay_later_text']; |
368 | } | |
369 | ||
370 | if (count($pps) > 1) { | |
e02d7e96 | 371 | $this->addRadio('payment_processor_id', ts('Payment Method'), $pps, |
ca3c9210 | 372 | NULL, " " |
6a488035 TO |
373 | ); |
374 | } | |
375 | elseif (!empty($pps)) { | |
376 | $key = array_keys($pps); | |
377 | $key = array_pop($key); | |
e02d7e96 | 378 | $this->addElement('hidden', 'payment_processor_id', $key); |
6a488035 TO |
379 | if ($key === 0) { |
380 | $this->assign('is_pay_later', $this->_values['is_pay_later']); | |
381 | $this->assign('pay_later_text', $this->_values['pay_later_text']); | |
382 | } | |
383 | } | |
596bff78 | 384 | |
385 | $contactID = $this->getContactID(); | |
aa288d3f | 386 | if ($this->getContactID() === 0) { |
596bff78 | 387 | $this->addCidZeroOptions($onlinePaymentProcessorEnabled); |
388 | } | |
aa288d3f | 389 | |
6a488035 TO |
390 | //build pledge block. |
391 | $this->_useForMember = 0; | |
392 | //don't build membership block when pledge_id is passed | |
58466af1 | 393 | if (empty($this->_values['pledge_id']) && empty($this->_ccid)) { |
6a488035 TO |
394 | $this->_separateMembershipPayment = FALSE; |
395 | if (in_array('CiviMember', $config->enableComponents)) { | |
396 | $isTest = 0; | |
397 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
398 | $isTest = 1; | |
399 | } | |
400 | ||
401 | if ($this->_priceSetId && | |
402 | (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet)) | |
403 | ) { | |
404 | $this->_useForMember = 1; | |
405 | $this->set('useForMember', $this->_useForMember); | |
406 | } | |
407 | ||
42e3a033 | 408 | $this->_separateMembershipPayment = $this->buildMembershipBlock( |
5b757295 | 409 | $this->_membershipContactID, |
6a488035 | 410 | TRUE, NULL, FALSE, |
5b757295 | 411 | $isTest |
6a488035 TO |
412 | ); |
413 | } | |
414 | $this->set('separateMembershipPayment', $this->_separateMembershipPayment); | |
415 | } | |
416 | $this->assign('useForMember', $this->_useForMember); | |
417 | // If we configured price set for contribution page | |
418 | // we are not allow membership signup as well as any | |
419 | // other contribution amount field, CRM-5095 | |
420 | if (isset($this->_priceSetId) && $this->_priceSetId) { | |
421 | $this->add('hidden', 'priceSetId', $this->_priceSetId); | |
422 | // build price set form. | |
423 | $this->set('priceSetId', $this->_priceSetId); | |
58466af1 | 424 | if (empty($this->_ccid)) { |
425 | CRM_Price_BAO_PriceSet::buildPriceSet($this); | |
426 | } | |
6a488035 | 427 | if ($this->_values['is_monetary'] && |
353ffa53 TO |
428 | $this->_values['is_recur'] && empty($this->_values['pledge_id']) |
429 | ) { | |
6a488035 TO |
430 | self::buildRecur($this); |
431 | } | |
432 | } | |
6a488035 | 433 | |
58466af1 | 434 | if ($this->_priceSetId && empty($this->_ccid)) { |
9da8dc8c | 435 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
6a488035 TO |
436 | if ($is_quick_config) { |
437 | $this->_useForMember = 0; | |
438 | $this->set('useForMember', $this->_useForMember); | |
439 | } | |
440 | } | |
441 | ||
6a488035 | 442 | //we allow premium for pledge during pledge creation only. |
58466af1 | 443 | if (empty($this->_values['pledge_id']) && empty($this->_ccid)) { |
6a488035 TO |
444 | CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE); |
445 | } | |
446 | ||
6a488035 | 447 | //don't build pledge block when mid is passed |
58466af1 | 448 | if (!$this->_mid && empty($this->_ccid)) { |
6a488035 | 449 | $config = CRM_Core_Config::singleton(); |
8cc574cf | 450 | if (in_array('CiviPledge', $config->enableComponents) && !empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
451 | CRM_Pledge_BAO_PledgeBlock::buildPledgeBlock($this); |
452 | } | |
453 | } | |
454 | ||
6a488035 | 455 | //to create an cms user |
58466af1 | 456 | if (!$this->_contactID && empty($this->_ccid)) { |
6a488035 TO |
457 | $createCMSUser = FALSE; |
458 | ||
459 | if ($this->_values['custom_pre_id']) { | |
460 | $profileID = $this->_values['custom_pre_id']; | |
461 | $createCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user'); | |
462 | } | |
463 | ||
464 | if (!$createCMSUser && | |
465 | $this->_values['custom_post_id'] | |
466 | ) { | |
467 | if (!is_array($this->_values['custom_post_id'])) { | |
468 | $profileIDs = array($this->_values['custom_post_id']); | |
469 | } | |
470 | else { | |
471 | $profileIDs = $this->_values['custom_post_id']; | |
472 | } | |
473 | foreach ($profileIDs as $pid) { | |
474 | if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) { | |
475 | $profileID = $pid; | |
476 | $createCMSUser = TRUE; | |
477 | break; | |
478 | } | |
479 | } | |
480 | } | |
481 | ||
482 | if ($createCMSUser) { | |
483 | CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE); | |
484 | } | |
485 | } | |
58466af1 | 486 | if ($this->_pcpId && empty($this->_ccid)) { |
6a488035 | 487 | if ($pcpSupporter = CRM_PCP_BAO_PCP::displayName($this->_pcpId)) { |
b942a32a | 488 | $pcp_supporter_text = ts('This contribution is being made thanks to the effort of <strong>%1</strong>, who supports our campaign.', array(1 => $pcpSupporter)); |
eea141c0 DG |
489 | // Only tell people that can also create a PCP if the contribution page has a non-empty value in the "Create Personal Campaign Page link" field. |
490 | $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute'); | |
22e263ad | 491 | if (!empty($text)) { |
b942a32a | 492 | $pcp_supporter_text .= ts("You can support it as well - once you complete the donation, you will be able to create your own Personal Campaign Page!"); |
eea141c0 DG |
493 | } |
494 | $this->assign('pcpSupporterText', $pcp_supporter_text); | |
6a488035 | 495 | } |
31ed2806 JM |
496 | $prms = array('id' => $this->_pcpId); |
497 | CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo); | |
498 | if ($pcpInfo['is_honor_roll']) { | |
4d327c2d | 499 | $this->assign('isHonor', TRUE); |
31ed2806 JM |
500 | $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL, |
501 | array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );") | |
502 | ); | |
503 | $extraOption = array('onclick' => "return pcpAnonymous( );"); | |
353ffa53 TO |
504 | $elements = array(); |
505 | $elements[] = &$this->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption); | |
506 | $elements[] = &$this->createElement('radio', NULL, '', ts('List my contribution anonymously'), 1, $extraOption); | |
31ed2806 | 507 | $this->addGroup($elements, 'pcp_is_anonymous', NULL, ' '); |
31ed2806 JM |
508 | |
509 | $this->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30)); | |
510 | $this->add('textarea', 'pcp_personal_note', ts('Personal Note'), array('style' => 'height: 3em; width: 40em;')); | |
511 | } | |
6a488035 | 512 | } |
58466af1 | 513 | if (empty($this->_values['fee']) && empty($this->_ccid)) { |
e1a89dfd E |
514 | CRM_Core_Error::fatal(ts('This page does not have any price fields configured or you may not have permission for them. Please contact the site administrator for more details.')); |
515 | } | |
6a488035 TO |
516 | |
517 | //we have to load confirm contribution button in template | |
518 | //when multiple payment processor as the user | |
519 | //can toggle with payment processor selection | |
520 | $billingModePaymentProcessors = 0; | |
481a74f4 | 521 | if (!empty($this->_paymentProcessors)) { |
6a488035 TO |
522 | foreach ($this->_paymentProcessors as $key => $values) { |
523 | if ($values['billing_mode'] == CRM_Core_Payment::BILLING_MODE_BUTTON) { | |
524 | $billingModePaymentProcessors++; | |
525 | } | |
526 | } | |
527 | } | |
528 | ||
529 | if ($billingModePaymentProcessors && count($this->_paymentProcessors) == $billingModePaymentProcessors) { | |
530 | $allAreBillingModeProcessors = TRUE; | |
0db6c3e1 TO |
531 | } |
532 | else { | |
6a488035 TO |
533 | $allAreBillingModeProcessors = FALSE; |
534 | } | |
535 | ||
536 | if (!($allAreBillingModeProcessors && !$this->_values['is_pay_later'])) { | |
61189839 CW |
537 | $submitButton = array( |
538 | 'type' => 'upload', | |
c0321a2d | 539 | 'name' => CRM_Utils_Array::value('is_confirm_enabled', $this->_values) ? ts('Confirm Contribution') : ts('Contribute'), |
61189839 CW |
540 | 'spacing' => ' ', |
541 | 'isDefault' => TRUE, | |
6a488035 | 542 | ); |
61189839 CW |
543 | // Add submit-once behavior when confirm page disabled |
544 | if (empty($this->_values['is_confirm_enabled'])) { | |
545 | $submitButton['js'] = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"); | |
546 | } | |
fe552de9 | 547 | //change button name for updating contribution |
548 | if (!empty($this->_ccid)) { | |
549 | $submitButton['name'] = ts('Confirm Payment'); | |
550 | } | |
61189839 | 551 | $this->addButtons(array($submitButton)); |
6a488035 TO |
552 | } |
553 | ||
554 | $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this); | |
6a488035 TO |
555 | } |
556 | ||
6a488035 | 557 | /** |
fe482240 | 558 | * Build elements to collect information for recurring contributions. |
6a488035 | 559 | * |
03110609 EM |
560 | * |
561 | * @param CRM_Core_Form $form | |
6a488035 | 562 | */ |
a5aef36c | 563 | public static function buildRecur(&$form) { |
6a488035 TO |
564 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur'); |
565 | $className = get_class($form); | |
566 | ||
a5aef36c | 567 | $form->assign('is_recur_interval', CRM_Utils_Array::value('is_recur_interval', $form->_values)); |
568 | $form->assign('is_recur_installments', CRM_Utils_Array::value('is_recur_installments', $form->_values)); | |
6a488035 TO |
569 | |
570 | $form->add('checkbox', 'is_recur', ts('I want to contribute this amount'), NULL); | |
571 | ||
a7488080 | 572 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
6a488035 TO |
573 | $form->add('text', 'frequency_interval', ts('Every'), $attributes['frequency_interval']); |
574 | $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer'); | |
575 | } | |
576 | else { | |
577 | // make sure frequency_interval is submitted as 1 if given no choice to user. | |
578 | $form->add('hidden', 'frequency_interval', 1); | |
579 | } | |
580 | ||
581 | $frUnits = CRM_Utils_Array::value('recur_frequency_unit', $form->_values); | |
582 | if (empty($frUnits) && | |
583 | $className == 'CRM_Contribute_Form_Contribution' | |
584 | ) { | |
585 | $frUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
586 | CRM_Core_OptionGroup::values('recur_frequency_units') | |
587 | ); | |
588 | } | |
589 | ||
590 | $unitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $frUnits); | |
591 | ||
592 | // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit | |
317fceb4 | 593 | if (count($unitVals) == 1) { |
874c9be7 | 594 | $form->assign('one_frequency_unit', TRUE); |
6a488035 TO |
595 | $unit = $unitVals[0]; |
596 | $form->add('hidden', 'frequency_unit', $unit); | |
a7488080 | 597 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
6a488035 TO |
598 | $unit .= "(s)"; |
599 | } | |
a5aef36c | 600 | $form->assign('frequency_unit', $unit); |
0db6c3e1 TO |
601 | } |
602 | else { | |
874c9be7 | 603 | $form->assign('one_frequency_unit', FALSE); |
6a488035 | 604 | $units = array(); |
728ebde6 | 605 | $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, TRUE); |
6a488035 TO |
606 | foreach ($unitVals as $key => $val) { |
607 | if (array_key_exists($val, $frequencyUnits)) { | |
608 | $units[$val] = $frequencyUnits[$val]; | |
a7488080 | 609 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
6a488035 TO |
610 | $units[$val] = "{$frequencyUnits[$val]}(s)"; |
611 | } | |
612 | } | |
613 | } | |
614 | $frequencyUnit = &$form->add('select', 'frequency_unit', NULL, $units); | |
615 | } | |
616 | ||
6a488035 TO |
617 | // FIXME: Ideally we should freeze select box if there is only |
618 | // one option but looks there is some problem /w QF freeze. | |
619 | //if ( count( $units ) == 1 ) { | |
620 | //$frequencyUnit->freeze( ); | |
621 | //} | |
622 | ||
623 | $form->add('text', 'installments', ts('installments'), | |
624 | $attributes['installments'] | |
625 | ); | |
626 | $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer'); | |
627 | } | |
628 | ||
629 | /** | |
fe482240 | 630 | * Global form rule. |
6a488035 | 631 | * |
014c4014 TO |
632 | * @param array $fields |
633 | * The input form values. | |
634 | * @param array $files | |
635 | * The uploaded files if any. | |
d90a1a52 | 636 | * @param CRM_Core_Form $self |
2a6da8d7 | 637 | * |
72b3a70c CW |
638 | * @return bool|array |
639 | * true if no errors, else array of errors | |
6a488035 | 640 | */ |
00be9182 | 641 | public static function formRule($fields, $files, $self) { |
6a488035 | 642 | $errors = array(); |
aefd7f6b | 643 | $amount = self::computeAmount($fields, $self->_values); |
fb357d6e | 644 | if (CRM_Utils_Array::value('auto_renew', $fields) && |
645 | CRM_Utils_Array::value('payment_processor_id', $fields) == 0 | |
646 | ) { | |
647 | $errors['auto_renew'] = ts('You cannot have auto-renewal on if you are paying later.'); | |
648 | } | |
6a488035 | 649 | |
8cc574cf | 650 | if ((!empty($fields['selectMembership']) && |
6a488035 TO |
651 | $fields['selectMembership'] != 'no_thanks' |
652 | ) || | |
8cc574cf | 653 | (!empty($fields['priceSetId']) && |
6a488035 TO |
654 | $self->_useForMember |
655 | ) | |
656 | ) { | |
352c43f7 | 657 | $isTest = ($self->_action & CRM_Core_Action::PREVIEW) ? TRUE : FALSE; |
658 | $lifeMember = CRM_Member_BAO_Membership::getAllContactMembership($self->_membershipContactID, $isTest, TRUE); | |
6a488035 TO |
659 | |
660 | $membershipOrgDetails = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization(); | |
661 | ||
662 | $unallowedOrgs = array(); | |
663 | foreach (array_keys($lifeMember) as $memTypeId) { | |
664 | $unallowedOrgs[] = $membershipOrgDetails[$memTypeId]; | |
665 | } | |
666 | } | |
667 | ||
668 | //check for atleast one pricefields should be selected | |
58466af1 | 669 | if (!empty($fields['priceSetId']) && empty($self->_ccid)) { |
9da8dc8c | 670 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
671 | $priceField->price_set_id = $fields['priceSetId']; |
672 | $priceField->orderBy('weight'); | |
673 | $priceField->find(); | |
674 | ||
675 | $check = array(); | |
676 | $membershipIsActive = TRUE; | |
677 | $previousId = $otherAmount = FALSE; | |
678 | while ($priceField->fetch()) { | |
679 | ||
680 | if ($self->_quickConfig && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) { | |
681 | $previousId = $priceField->id; | |
481a74f4 | 682 | if ($priceField->name == 'membership_amount' && !$priceField->is_active) { |
6a488035 TO |
683 | $membershipIsActive = FALSE; |
684 | } | |
685 | } | |
686 | if ($priceField->name == 'other_amount') { | |
8cc574cf | 687 | if ($self->_quickConfig && empty($fields["price_{$priceField->id}"]) && |
353ffa53 TO |
688 | array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"]) |
689 | ) { | |
6a488035 TO |
690 | $otherAmount = $priceField->id; |
691 | } | |
692 | elseif (!empty($fields["price_{$priceField->id}"])) { | |
ef88f444 | 693 | $otherAmountVal = CRM_Utils_Rule::cleanMoney($fields["price_{$priceField->id}"]); |
353ffa53 TO |
694 | $min = CRM_Utils_Array::value('min_amount', $self->_values); |
695 | $max = CRM_Utils_Array::value('max_amount', $self->_values); | |
6a488035 TO |
696 | if ($min && $otherAmountVal < $min) { |
697 | $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1', | |
353ffa53 | 698 | array(1 => $min) |
6a488035 TO |
699 | ); |
700 | } | |
701 | if ($max && $otherAmountVal > $max) { | |
702 | $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.', | |
353ffa53 | 703 | array(1 => $max) |
6a488035 TO |
704 | ); |
705 | } | |
706 | } | |
707 | } | |
708 | if (!empty($fields["price_{$priceField->id}"]) || ($previousId == $priceField->id && isset($fields["price_{$previousId}"]) | |
353ffa53 TO |
709 | && empty($fields["price_{$previousId}"])) |
710 | ) { | |
6a488035 TO |
711 | $check[] = $priceField->id; |
712 | } | |
713 | } | |
714 | ||
b5a62499 PN |
715 | $currentMemberships = NULL; |
716 | if ($membershipIsActive) { | |
717 | $is_test = $self->_mode != 'live' ? 1 : 0; | |
718 | $memContactID = $self->_membershipContactID; | |
7f7fa13a EM |
719 | |
720 | // For anonymous user check using dedupe rule | |
b5a62499 PN |
721 | // if user has Cancelled Membership |
722 | if (!$memContactID) { | |
723 | $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual'); | |
724 | $dedupeParams['check_permission'] = FALSE; | |
725 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
726 | // if we find more than one contact, use the first one | |
727 | $memContactID = CRM_Utils_Array::value(0, $ids); | |
728 | } | |
729 | $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID, | |
730 | $is_test | |
731 | ); | |
7f7fa13a | 732 | |
b5a62499 PN |
733 | $errorText = 'Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.'; |
734 | foreach ($self->_values['fee'] as $fieldKey => $fieldValue) { | |
735 | if ($fieldValue['html_type'] != 'Text' && CRM_Utils_Array::value('price_' . $fieldKey, $fields)) { | |
ca3c9210 | 736 | if (!is_array($fields['price_' . $fieldKey]) && isset($fieldValue['options'][$fields['price_' . $fieldKey]])) { |
7f7fa13a | 737 | if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]]) |
353ffa53 TO |
738 | && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships) |
739 | ) { | |
3a96b63b | 740 | $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id']))); |
b5a62499 PN |
741 | } |
742 | } | |
743 | else { | |
ca3c9210 | 744 | if (is_array($fields['price_' . $fieldKey])) { |
745 | foreach (array_keys($fields['price_' . $fieldKey]) as $key) { | |
746 | if (array_key_exists('membership_type_id', $fieldValue['options'][$key]) | |
747 | && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships) | |
748 | ) { | |
749 | $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$key]['membership_type_id']))); | |
750 | } | |
b5a62499 PN |
751 | } |
752 | } | |
753 | } | |
754 | } | |
755 | } | |
756 | } | |
7f7fa13a | 757 | |
9ce8bcfc | 758 | // CRM-12233 |
665e5ec7 | 759 | if ($membershipIsActive && !$self->_membershipBlock['is_required'] |
353ffa53 TO |
760 | && $self->_values['amount_block_is_active'] |
761 | ) { | |
9ce8bcfc PN |
762 | $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL; |
763 | foreach ($self->_values['fee'] as $fieldKey => $fieldValue) { | |
41f27e49 | 764 | // if 'No thank you' membership is selected then set $membershipFieldId |
9ce8bcfc PN |
765 | if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array::value('price_' . $fieldKey, $fields) == 0) { |
766 | $membershipFieldId = $fieldKey; | |
767 | } | |
768 | elseif ($membershipFieldId) { | |
769 | if ($fieldValue['name'] == 'other_amount') { | |
770 | $otherFieldId = $fieldKey; | |
771 | } | |
772 | elseif ($fieldValue['name'] == 'contribution_amount') { | |
773 | $contributionFieldId = $fieldKey; | |
774 | } | |
665e5ec7 | 775 | |
9ce8bcfc PN |
776 | if (!$errorKey || CRM_Utils_Array::value('price_' . $contributionFieldId, $fields) == '0') { |
777 | $errorKey = $fieldKey; | |
778 | } | |
779 | } | |
780 | } | |
41f27e49 | 781 | // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error |
8cc574cf | 782 | if ($membershipFieldId && !(CRM_Utils_Array::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) { |
1cd7e100 | 783 | $errors["price_{$errorKey}"] = ts('Additional Contribution is required.'); |
9ce8bcfc PN |
784 | } |
785 | } | |
58466af1 | 786 | if (empty($check) && empty($self->_ccid)) { |
6a488035 TO |
787 | if ($self->_useForMember == 1 && $membershipIsActive) { |
788 | $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).'); | |
789 | } | |
790 | else { | |
791 | $errors['_qf_default'] = ts('Select at least one option from Contribution(s).'); | |
792 | } | |
793 | } | |
22e263ad | 794 | if ($otherAmount && !empty($check)) { |
6a488035 TO |
795 | $errors["price_{$otherAmount}"] = ts('Amount is required field.'); |
796 | } | |
797 | ||
798 | if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) { | |
799 | $priceFieldIDS = array(); | |
800 | $priceFieldMemTypes = array(); | |
801 | ||
802 | foreach ($self->_priceSet['fields'] as $priceId => $value) { | |
8cc574cf | 803 | if (!empty($fields['price_' . $priceId]) || ($self->_quickConfig && $value['name'] == 'membership_amount' && empty($self->_membershipBlock['is_required']))) { |
a7488080 | 804 | if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) { |
6a488035 TO |
805 | foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) { |
806 | if ($isSet) { | |
807 | $priceFieldIDS[] = $priceFldVal; | |
808 | } | |
809 | } | |
810 | } | |
8cc574cf | 811 | elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) { |
6a488035 TO |
812 | // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering |
813 | // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577 | |
814 | $priceFieldIDS[] = $fields['price_' . $priceId]; | |
815 | } | |
816 | ||
a7488080 | 817 | if (!empty($value['options'])) { |
6a488035 | 818 | foreach ($value['options'] as $val) { |
88c6259e | 819 | if (!empty($val['membership_type_id']) && ( |
fb4aa4d9 | 820 | ($fields['price_' . $priceId] == $val['id']) || |
821 | (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']])) | |
822 | ) | |
823 | ) { | |
6a488035 TO |
824 | $priceFieldMemTypes[] = $val['membership_type_id']; |
825 | } | |
826 | } | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
831 | if (!empty($lifeMember)) { | |
832 | foreach ($priceFieldIDS as $priceFieldId) { | |
9da8dc8c | 833 | if (($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) && |
6a488035 TO |
834 | in_array($membershipOrgDetails[$id], $unallowedOrgs) |
835 | ) { | |
836 | $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.'); | |
837 | break; | |
838 | } | |
839 | } | |
840 | } | |
841 | ||
842 | if (!empty($priceFieldIDS)) { | |
843 | $ids = implode(',', $priceFieldIDS); | |
844 | ||
845 | $priceFieldIDS['id'] = $fields['priceSetId']; | |
846 | $self->set('memberPriceFieldIDS', $priceFieldIDS); | |
9da8dc8c | 847 | $count = CRM_Price_BAO_PriceSet::getMembershipCount($ids); |
b44e3f84 DS |
848 | foreach ($count as $id => $occurrence) { |
849 | if ($occurrence > 1) { | |
6a488035 TO |
850 | $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity. Contact the site administrator if you need assistance.'); |
851 | } | |
852 | } | |
853 | } | |
854 | ||
0d791f3b | 855 | if (empty($priceFieldMemTypes) && $self->_membershipBlock['is_required'] == 1) { |
6a488035 TO |
856 | $errors['_qf_default'] = ts('Please select at least one membership option.'); |
857 | } | |
858 | } | |
859 | ||
9da8dc8c | 860 | CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], |
6a488035 TO |
861 | $fields, $lineItem |
862 | ); | |
665e5ec7 | 863 | |
601c7a24 | 864 | $minAmt = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount'); |
6a488035 TO |
865 | if ($fields['amount'] < 0) { |
866 | $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly'); | |
867 | } | |
601c7a24 | 868 | elseif (!empty($minAmt) && $fields['amount'] < $minAmt) { |
869 | $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Contribution(s).', array( | |
870 | 1 => CRM_Utils_Money::format($minAmt), | |
871 | )); | |
872 | } | |
873 | ||
6a488035 TO |
874 | $amount = $fields['amount']; |
875 | } | |
876 | ||
877 | if (isset($fields['selectProduct']) && | |
353ffa53 TO |
878 | $fields['selectProduct'] != 'no_thanks' |
879 | ) { | |
6a488035 TO |
880 | $productDAO = new CRM_Contribute_DAO_Product(); |
881 | $productDAO->id = $fields['selectProduct']; | |
882 | $productDAO->find(TRUE); | |
883 | $min_amount = $productDAO->min_contribution; | |
884 | ||
885 | if ($amount < $min_amount) { | |
886 | $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount))); | |
887 | CRM_Core_Session::setStatus($errors['selectProduct']); | |
888 | } | |
889 | } | |
890 | ||
577d1236 WA |
891 | //CRM-16285 - Function to handle validation errors on form, for recurring contribution field. |
892 | CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors); | |
6a488035 | 893 | |
a7488080 | 894 | if (!empty($fields['is_recur']) && |
e02d7e96 | 895 | CRM_Utils_Array::value('payment_processor_id', $fields) == 0 |
353ffa53 | 896 | ) { |
6a488035 TO |
897 | $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.'); |
898 | } | |
899 | ||
6a488035 | 900 | // validate PCP fields - if not anonymous, we need a nick name value |
8cc574cf | 901 | if ($self->_pcpId && !empty($fields['pcp_display_in_roll']) && |
6a488035 TO |
902 | (CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0) && |
903 | CRM_Utils_Array::value('pcp_roll_nickname', $fields) == '' | |
904 | ) { | |
905 | $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.'); | |
906 | } | |
907 | ||
908 | // return if this is express mode | |
909 | $config = CRM_Core_Config::singleton(); | |
f92fc7eb CW |
910 | if ($self->_paymentProcessor && |
911 | $self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON | |
912 | ) { | |
8cc574cf | 913 | if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) || |
6a488035 TO |
914 | CRM_Utils_Array::value($self->_expressButtonName, $fields) |
915 | ) { | |
916 | return $errors; | |
917 | } | |
918 | } | |
919 | ||
920 | //validate the pledge fields. | |
a7488080 | 921 | if (!empty($self->_values['pledge_block_id'])) { |
6a488035 | 922 | //validation for pledge payment. |
a7488080 | 923 | if (!empty($self->_values['pledge_id'])) { |
6a488035 TO |
924 | if (empty($fields['pledge_amount'])) { |
925 | $errors['pledge_amount'] = ts('At least one payment option needs to be checked.'); | |
926 | } | |
927 | } | |
a7488080 | 928 | elseif (!empty($fields['is_pledge'])) { |
6a488035 | 929 | if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == FALSE) { |
917a9954 | 930 | $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.'); |
6a488035 TO |
931 | } |
932 | else { | |
933 | if (CRM_Utils_Array::value('pledge_installments', $fields) == NULL) { | |
934 | $errors['pledge_installments'] = ts('Pledge Installments is required field.'); | |
935 | } | |
35522279 | 936 | elseif (CRM_Utils_Array::value('pledge_installments', $fields) == 1) { |
6a488035 TO |
937 | $errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.'); |
938 | } | |
35522279 | 939 | elseif (CRM_Utils_Array::value('pledge_installments', $fields) == 0) { |
6a488035 TO |
940 | $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.'); |
941 | } | |
942 | } | |
943 | ||
944 | //validation for Pledge Frequency Interval. | |
945 | if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == FALSE) { | |
946 | $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.'); | |
947 | } | |
948 | else { | |
949 | if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == NULL) { | |
950 | $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.'); | |
951 | } | |
35522279 | 952 | elseif (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == 0) { |
6a488035 TO |
953 | $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0'); |
954 | } | |
955 | } | |
956 | } | |
957 | } | |
958 | ||
6a488035 | 959 | // if the user has chosen a free membership or the amount is less than zero |
1d1fee72 | 960 | // i.e. we don't need to validate payment related fields or profiles. |
f92fc7eb | 961 | if ((float) $amount <= 0.0) { |
6a488035 TO |
962 | return $errors; |
963 | } | |
964 | ||
986905f5 | 965 | if (CRM_Utils_Array::value('payment_processor_id', $fields) == NULL) { |
966 | $errors['payment_processor_id'] = ts('Payment Method is a required field.'); | |
ca3c9210 | 967 | } |
968 | else { | |
969 | CRM_Core_Payment_Form::validatePaymentInstrument( | |
970 | $fields['payment_processor_id'], | |
971 | $fields, | |
972 | $errors, | |
973 | (!$self->_isBillingAddressRequiredForPayLater ? NULL : 'billing') | |
974 | ); | |
975 | } | |
6a488035 | 976 | |
6a488035 TO |
977 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { |
978 | if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { | |
979 | $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); | |
980 | if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) { | |
981 | $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', | |
353ffa53 | 982 | array(1 => ucwords(str_replace('_', " ", $greeting))) |
6a488035 TO |
983 | ); |
984 | } | |
985 | } | |
986 | } | |
987 | ||
988 | return empty($errors) ? TRUE : $errors; | |
989 | } | |
990 | ||
186c9c17 | 991 | /** |
aefd7f6b EM |
992 | * Compute amount to be paid. |
993 | * | |
c490a46a | 994 | * @param array $params |
aefd7f6b | 995 | * @param array $formValues |
186c9c17 EM |
996 | * |
997 | * @return int|mixed|null|string | |
998 | */ | |
aefd7f6b | 999 | public static function computeAmount($params, $formValues) { |
99b93cf8 | 1000 | $amount = 0; |
aefd7f6b | 1001 | // First clean up the other amount field if present. |
6a488035 TO |
1002 | if (isset($params['amount_other'])) { |
1003 | $params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']); | |
1004 | } | |
1005 | ||
8cc574cf | 1006 | if (CRM_Utils_Array::value('amount', $params) == 'amount_other_radio' || !empty($params['amount_other'])) { |
6a488035 TO |
1007 | $amount = $params['amount_other']; |
1008 | } | |
1009 | elseif (!empty($params['pledge_amount'])) { | |
6a488035 TO |
1010 | foreach ($params['pledge_amount'] as $paymentId => $dontCare) { |
1011 | $amount += CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount'); | |
1012 | } | |
1013 | } | |
1014 | else { | |
aefd7f6b | 1015 | if (!empty($formValues['amount'])) { |
6a488035 TO |
1016 | $amountID = CRM_Utils_Array::value('amount', $params); |
1017 | ||
1018 | if ($amountID) { | |
c039f658 | 1019 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
1020 | // function to get correct amount level consistently. Remove setting of the amount level in | |
1021 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest | |
1022 | // to cover all variants. | |
aefd7f6b EM |
1023 | $params['amount_level'] = CRM_Utils_Array::value('label', $formValues[$amountID]); |
1024 | $amount = CRM_Utils_Array::value('value', $formValues[$amountID]); | |
6a488035 TO |
1025 | } |
1026 | } | |
1027 | } | |
1028 | return $amount; | |
1029 | } | |
1030 | ||
1031 | /** | |
fe482240 | 1032 | * Process the form submission. |
6a488035 TO |
1033 | */ |
1034 | public function postProcess() { | |
6a488035 TO |
1035 | // we first reset the confirm page so it accepts new values |
1036 | $this->controller->resetPage('Confirm'); | |
1037 | ||
1038 | // get the submitted form values. | |
1039 | $params = $this->controller->exportValues($this->_name); | |
8cb12ff1 | 1040 | $this->submit($params); |
1041 | ||
1042 | if (empty($this->_values['is_confirm_enabled'])) { | |
1043 | $this->skipToThankYouPage(); | |
1044 | } | |
1045 | ||
1046 | } | |
da8d9879 | 1047 | |
8cb12ff1 | 1048 | /** |
1049 | * Submit function. | |
1050 | * | |
1051 | * This is the guts of the postProcess made also accessible to the test suite. | |
1052 | * | |
1053 | * @param array $params | |
1054 | * Submitted values. | |
1055 | */ | |
1056 | public function submit($params) { | |
4625312e EM |
1057 | //carry campaign from profile. |
1058 | if (array_key_exists('contribution_campaign_id', $params)) { | |
1059 | $params['campaign_id'] = $params['contribution_campaign_id']; | |
1060 | } | |
1061 | ||
8cb12ff1 | 1062 | $params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; |
4625312e | 1063 | |
65e172a3 | 1064 | // @todo refactor this & leverage it from the unit tests. |
a7488080 | 1065 | if (!empty($params['priceSetId'])) { |
9da8dc8c | 1066 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
6a488035 | 1067 | if ($is_quick_config) { |
9da8dc8c | 1068 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
1069 | $priceField->price_set_id = $params['priceSetId']; |
1070 | $priceField->orderBy('weight'); | |
1071 | $priceField->find(); | |
1072 | ||
1d9d37c7 | 1073 | $priceOptions = array(); |
6a488035 | 1074 | while ($priceField->fetch()) { |
1d9d37c7 | 1075 | CRM_Price_BAO_PriceFieldValue::getValues($priceField->id, $priceOptions); |
ca3c9210 | 1076 | if (($selectedPriceOptionID = CRM_Utils_Array::value("price_{$priceField->id}", $params)) != FALSE && $selectedPriceOptionID > 0) { |
1d9d37c7 FG |
1077 | switch ($priceField->name) { |
1078 | case 'membership_amount': | |
1079 | $this->_params['selectMembership'] = $params['selectMembership'] = CRM_Utils_Array::value('membership_type_id', $priceOptions[$selectedPriceOptionID]); | |
1080 | $this->set('selectMembership', $params['selectMembership']); | |
1d9d37c7 | 1081 | |
d71d328a | 1082 | case 'contribution_amount': |
1d9d37c7 | 1083 | $params['amount'] = $selectedPriceOptionID; |
19c22a58 | 1084 | if ($priceField->name == 'contribution_amount' || |
1085 | ($priceField->name == 'membership_amount' && | |
1086 | CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) == 0) | |
1087 | ) { | |
1088 | $this->_values['amount'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]); | |
1089 | } | |
1d9d37c7 FG |
1090 | $this->_values[$selectedPriceOptionID]['value'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]); |
1091 | $this->_values[$selectedPriceOptionID]['label'] = CRM_Utils_Array::value('label', $priceOptions[$selectedPriceOptionID]); | |
1092 | $this->_values[$selectedPriceOptionID]['amount_id'] = CRM_Utils_Array::value('id', $priceOptions[$selectedPriceOptionID]); | |
1093 | $this->_values[$selectedPriceOptionID]['weight'] = CRM_Utils_Array::value('weight', $priceOptions[$selectedPriceOptionID]); | |
1094 | break; | |
1095 | ||
1096 | case 'other_amount': | |
1097 | $params['amount_other'] = $selectedPriceOptionID; | |
1098 | break; | |
6a488035 TO |
1099 | } |
1100 | } | |
6a488035 TO |
1101 | } |
1102 | } | |
1103 | } | |
58466af1 | 1104 | |
1105 | if (!empty($this->_ccid) && !empty($this->_pendingAmount)) { | |
1106 | $params['amount'] = $this->_pendingAmount; | |
1107 | } | |
1108 | else { | |
1109 | // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID | |
1110 | $params['amount'] = self::computeAmount($params, $this->_values); | |
1111 | } | |
6a488035 | 1112 | |
6a488035 TO |
1113 | $params['separate_amount'] = $params['amount']; |
1114 | $memFee = NULL; | |
a7488080 | 1115 | if (!empty($params['selectMembership'])) { |
8cb12ff1 | 1116 | if (empty($this->_membershipTypeValues)) { |
1117 | $this->_membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, | |
1a00ea3c | 1118 | (array) $params['selectMembership'] |
6a488035 TO |
1119 | ); |
1120 | } | |
8cb12ff1 | 1121 | $membershipTypeValues = $this->_membershipTypeValues[$params['selectMembership']]; |
6a488035 TO |
1122 | $memFee = $membershipTypeValues['minimum_fee']; |
1123 | if (!$params['amount'] && !$this->_separateMembershipPayment) { | |
1124 | $params['amount'] = $memFee ? $memFee : 0; | |
1125 | } | |
1126 | } | |
e6d4f555 | 1127 | //If the membership & contribution is used in contribution page & not separate payment |
65e172a3 | 1128 | $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL; |
9ce8bcfc | 1129 | $proceFieldAmount = 0; |
8af73472 | 1130 | if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment == 0) { |
9da8dc8c | 1131 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
6a488035 TO |
1132 | if ($is_quick_config) { |
1133 | foreach ($this->_priceSet['fields'] as $fieldKey => $fieldVal) { | |
874c9be7 | 1134 | if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey])) { |
353ffa53 | 1135 | $fieldId = $fieldVal['id']; |
6a488035 | 1136 | $fieldOption = $params['price_' . $fieldId]; |
9ce8bcfc | 1137 | $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount']; |
353ffa53 | 1138 | $memPresent = TRUE; |
6a488035 TO |
1139 | } |
1140 | else { | |
a7488080 | 1141 | if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' || $fieldVal['name'] == 'contribution_amount')) { |
6a488035 TO |
1142 | $fieldId = $fieldVal['id']; |
1143 | if ($fieldVal['name'] == 'other_amount') { | |
9ce8bcfc | 1144 | $proceFieldAmount += $this->_submitValues['price_' . $fieldId]; |
6a488035 | 1145 | } |
abcf506a | 1146 | elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues['price_' . $fieldId] > 0) { |
9ce8bcfc | 1147 | $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount']; |
ce9024ca | 1148 | } |
6a488035 TO |
1149 | unset($params['price_' . $fieldId]); |
1150 | break; | |
1151 | } | |
1152 | } | |
1153 | } | |
1154 | } | |
1155 | } | |
874c9be7 | 1156 | |
6a488035 TO |
1157 | if (!isset($params['amount_other'])) { |
1158 | $this->set('amount_level', CRM_Utils_Array::value('amount_level', $params)); | |
1159 | } | |
1160 | ||
2f44fc96 | 1161 | if (!empty($this->_ccid)) { |
58466af1 | 1162 | $this->set('lineItem', $this->_lineItem); |
1163 | } | |
1164 | elseif ($priceSetId = CRM_Utils_Array::value('priceSetId', $params)) { | |
6a488035 | 1165 | $lineItem = array(); |
481a74f4 TO |
1166 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); |
1167 | if ($is_quick_config) { | |
1168 | foreach ($this->_values['fee'] as $key => & $val) { | |
387fd5aa | 1169 | if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && !empty($params['price_' . $key])) { |
aefd7f6b EM |
1170 | // Clean out any currency symbols. |
1171 | $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]); | |
353ffa53 | 1172 | if ($params['price_' . $key] != 0) { |
481a74f4 | 1173 | foreach ($val['options'] as $optionKey => & $options) { |
353ffa53 | 1174 | $options['amount'] = CRM_Utils_Array::value('price_' . $key, $params); |
ef88f444 PC |
1175 | break; |
1176 | } | |
6a488035 | 1177 | } |
92fcb95f | 1178 | $params['price_' . $key] = 1; |
6a488035 TO |
1179 | break; |
1180 | } | |
1181 | } | |
1182 | } | |
6a488035 TO |
1183 | $component = ''; |
1184 | if ($this->_membershipBlock) { | |
1185 | $component = 'membership'; | |
1186 | } | |
ef88f444 | 1187 | |
9da8dc8c | 1188 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $component); |
dc428161 | 1189 | if ($params['tax_amount']) { |
1190 | $this->set('tax_amount', $params['tax_amount']); | |
1191 | } | |
6a488035 TO |
1192 | |
1193 | if ($proceFieldAmount) { | |
6a488035 | 1194 | $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount; |
c40e1ff4 | 1195 | $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount; |
dc428161 | 1196 | if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) { |
1197 | $proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount']; | |
1198 | } | |
6a488035 | 1199 | if (!$this->_membershipBlock['is_separate_payment']) { |
aefd7f6b EM |
1200 | //require when separate membership not used |
1201 | $params['amount'] = $proceFieldAmount; | |
6a488035 TO |
1202 | } |
1203 | } | |
1204 | $this->set('lineItem', $lineItem); | |
1205 | } | |
1206 | ||
3a19e759 | 1207 | if ($params['amount'] != 0 && (($this->_values['is_pay_later'] && |
1208 | empty($this->_paymentProcessor) && | |
1209 | !array_key_exists('hidden_processor', $params)) || | |
36f45674 | 1210 | (CRM_Utils_Array::value('payment_processor_id', $params) == 0)) |
3a19e759 | 1211 | ) { |
1212 | $params['is_pay_later'] = 1; | |
1213 | } | |
1214 | else { | |
1215 | $params['is_pay_later'] = 0; | |
1216 | } | |
1217 | ||
1218 | // Would be nice to someday understand the point of this set. | |
1219 | $this->set('is_pay_later', $params['is_pay_later']); | |
1220 | // assign pay later stuff | |
1221 | $this->_params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE); | |
1222 | $this->assign('is_pay_later', $params['is_pay_later']); | |
1223 | if ($params['is_pay_later']) { | |
1224 | $this->assign('pay_later_text', $this->_values['pay_later_text']); | |
1225 | $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']); | |
1226 | } | |
1227 | ||
8cc574cf | 1228 | if ($this->_membershipBlock['is_separate_payment'] && !empty($params['separate_amount'])) { |
6a488035 | 1229 | $this->set('amount', $params['separate_amount']); |
0db6c3e1 TO |
1230 | } |
1231 | else { | |
6a488035 TO |
1232 | $this->set('amount', $params['amount']); |
1233 | } | |
1234 | ||
1235 | // generate and set an invoiceID for this transaction | |
1236 | $invoiceID = md5(uniqid(rand(), TRUE)); | |
1237 | $this->set('invoiceID', $invoiceID); | |
3910048c | 1238 | $params['invoiceID'] = $invoiceID; |
8cb12ff1 | 1239 | $params['description'] = ts('Online Contribution') . ': ' . ((!empty($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title'])); |
ec022878 | 1240 | $params['button'] = $this->controller->getButtonName(); |
e6d4f555 | 1241 | // required only if is_monetary and valid positive amount |
8cb12ff1 | 1242 | // @todo it seems impossible for $memFee to be greater than 0 & $params['amount'] not to |
1243 | // be & by requiring $memFee down here we make it harder to do a sensible refactoring of the function | |
1244 | // above (ie. extract the amount in a small function). | |
6a488035 | 1245 | if ($this->_values['is_monetary'] && |
1d1fee72 | 1246 | !empty($this->_paymentProcessor) && |
6a488035 TO |
1247 | ((float ) $params['amount'] > 0.0 || $memFee > 0.0) |
1248 | ) { | |
0f2b049e | 1249 | // The concept of contributeMode is deprecated - as should be the 'is_monetary' setting. |
3910048c | 1250 | $this->setContributeMode(); |
90102a32 EM |
1251 | // Really this setting of $this->_params & params within it should be done earlier on in the function |
1252 | // probably the values determined here should be reused in confirm postProcess as there is no opportunity to alter anything | |
1253 | // on the confirm page. However as we are dealing with a stable release we go as close to where it is used | |
1254 | // as possible. | |
1255 | // In general the form has a lack of clarity of the logic of why things are set on the form in some cases & | |
1256 | // the logic around when $this->_params is used compared to other params arrays. | |
1257 | $this->_params = array_merge($params, $this->_params); | |
1258 | $this->setRecurringMembershipParams(); | |
f92fc7eb | 1259 | if ($this->_paymentProcessor && |
10cdf4cb | 1260 | $this->_paymentProcessor['object']->supports('preApproval') |
f92fc7eb | 1261 | ) { |
90102a32 | 1262 | $this->handlePreApproval($this->_params); |
6a488035 | 1263 | } |
38b66756 | 1264 | } |
3910048c | 1265 | } |
6a488035 | 1266 | |
3910048c EM |
1267 | /** |
1268 | * Assign the billing mode to the template. | |
1269 | * | |
1270 | * This is required for legacy support for contributeMode in templates. | |
1271 | * | |
1272 | * The goal is to remove this parameter & use more relevant parameters. | |
1273 | */ | |
1274 | protected function setContributeMode() { | |
1275 | switch ($this->_paymentProcessor['billing_mode']) { | |
1276 | case CRM_Core_Payment::BILLING_MODE_FORM: | |
1277 | $this->set('contributeMode', 'direct'); | |
1278 | break; | |
6a488035 | 1279 | |
3910048c EM |
1280 | case CRM_Core_Payment::BILLING_MODE_BUTTON: |
1281 | $this->set('contributeMode', 'express'); | |
1282 | break; | |
6a488035 | 1283 | |
3910048c EM |
1284 | case CRM_Core_Payment::BILLING_MODE_NOTIFY: |
1285 | $this->set('contributeMode', 'notify'); | |
1286 | break; | |
6a488035 | 1287 | } |
874c9be7 | 1288 | |
6a488035 | 1289 | } |
7bf9cde2 | 1290 | |
3910048c EM |
1291 | /** |
1292 | * Process confirm function and pass browser to the thank you page. | |
1293 | */ | |
1294 | protected function skipToThankYouPage() { | |
1295 | // call the post process hook for the main page before we switch to confirm | |
1296 | $this->postProcessHook(); | |
1297 | ||
1298 | // build the confirm page | |
1299 | $confirmForm = &$this->controller->_pages['Confirm']; | |
1300 | $confirmForm->preProcess(); | |
1301 | $confirmForm->buildQuickForm(); | |
1302 | ||
1303 | // the confirmation page is valid | |
1304 | $data = &$this->controller->container(); | |
1305 | $data['valid']['Confirm'] = 1; | |
1306 | ||
1307 | // confirm the contribution | |
1308 | // mainProcess calls the hook also | |
1309 | $confirmForm->mainProcess(); | |
1310 | $qfKey = $this->controller->_key; | |
1311 | ||
1312 | // redirect to thank you page | |
1313 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE)); | |
aefd7f6b EM |
1314 | } |
1315 | ||
8cb12ff1 | 1316 | /** |
1317 | * Function for unit tests on the postProcess function. | |
1318 | * | |
1319 | * @param array $params | |
1320 | */ | |
1321 | public function testSubmit($params) { | |
1322 | $_SERVER['REQUEST_METHOD'] = 'GET'; | |
1323 | $this->controller = new CRM_Contribute_Controller_Contribution(); | |
1324 | $this->submit($params); | |
1325 | } | |
1326 | ||
6a488035 | 1327 | } |