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