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