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