Cleanup phpdoc comments
[civicrm-core.git] / CRM / Contribute / Form / Contribution / Main.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
bef9421f 37 * This class generates form components for processing a Contribution
6a488035
TO
38 *
39 */
40class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_ContributionBase {
41
42 /**
43 *Define default MembershipType Id
44 *
45 */
46 public $_defaultMemTypeId;
47
48 public $_relatedOrganizationFound;
49
78f0ee1b
CW
50 public $_onBehalfRequired = FALSE;
51 public $_onbehalf = FALSE;
6a488035
TO
52 public $_paymentProcessors;
53 protected $_defaults;
54
55 public $_membershipTypeValues;
56
57 public $_useForMember;
58
dde5a0ef 59 /**
100fef9d 60 * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl
dde5a0ef
EM
61 * @var array
62 */
63 public $_paymentFields = array();
64
e50ee6a3 65 protected $_paymentProcessorID;
7c8cc461 66 protected $_snippet;
6a488035
TO
67
68 /**
100fef9d 69 * Set variables up before form is built
6a488035
TO
70 *
71 * @return void
72 * @access public
73 */
74 public function preProcess() {
75 parent::preProcess();
76
7bf9cde2 77 self::preProcessPaymentOptions($this);
6a488035
TO
78
79 // Make the contributionPageID avilable to the template
80 $this->assign('contributionPageID', $this->_id);
81 $this->assign('isShare', CRM_Utils_Array::value('is_share', $this->_values));
82 $this->assign('isConfirmEnabled', CRM_Utils_Array::value('is_confirm_enabled', $this->_values));
83
84 // make sure we have right permission to edit this user
da8d9879 85 $csContactID = $this->getContactID();
6a488035
TO
86 $reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
87 $mainDisplay = CRM_Utils_Request::retrieve('_qf_Main_display', 'Boolean', CRM_Core_DAO::$_nullObject);
88
6a488035
TO
89 if ($reset) {
90 $this->assign('reset', $reset);
91 }
92
93 if ($mainDisplay) {
94 $this->assign('mainDisplay', $mainDisplay);
95 }
96
78f0ee1b
CW
97 // Possible values for 'is_for_organization':
98 // * 0 - org profile disabled
99 // * 1 - org profile optional
100 // * 2 - org profile required
6a488035 101 $this->_onbehalf = FALSE;
78f0ee1b
CW
102 if (!empty($this->_values['is_for_organization'])) {
103 if ($this->_values['is_for_organization'] == 2) {
104 $this->_onBehalfRequired = TRUE;
105 }
106 // Add organization profile if 1 of the following are true:
107 // If the org profile is required
108 if ($this->_onBehalfRequired ||
109 // Or we are building the form for the first time
110 empty($_POST) ||
111 // Or the user has submitted the form and checked the "On Behalf" checkbox
112 !empty($_POST['is_for_organization'])
6a488035 113 ) {
78f0ee1b
CW
114 $this->_onbehalf = TRUE;
115 CRM_Contribute_Form_Contribution_OnBehalfOf::preProcess($this);
6a488035
TO
116 }
117 }
78f0ee1b 118 $this->assign('onBehalfRequired', $this->_onBehalfRequired);
6a488035 119
8af73472 120 if ($this->_honor_block_is_active) {
9b10578b
TO
121 CRM_Contact_Form_ProfileContact::preprocess($this);
122 }
123
ab234a8a
CW
124 if ($this->_snippet) {
125 $this->assign('isOnBehalfCallback', CRM_Utils_Array::value('onbehalf', $_GET, FALSE));
126 return;
127 }
133e2c99 128
78f0ee1b 129 if (!empty($this->_pcpInfo['id']) && !empty($this->_pcpInfo['intro_text'])) {
6a488035
TO
130 $this->assign('intro_text', $this->_pcpInfo['intro_text']);
131 }
78f0ee1b 132 elseif (!empty($this->_values['intro_text'])) {
6a488035
TO
133 $this->assign('intro_text', $this->_values['intro_text']);
134 }
135
136 $qParams = "reset=1&amp;id={$this->_id}";
78f0ee1b 137 if ($pcpId = CRM_Utils_Array::value('pcp_id', $this->_pcpInfo)) {
6a488035
TO
138 $qParams .= "&amp;pcpId={$pcpId}";
139 }
78f0ee1b 140 $this->assign('qParams', $qParams);
6a488035 141
78f0ee1b 142 if (!empty($this->_values['footer_text'])) {
6a488035
TO
143 $this->assign('footer_text', $this->_values['footer_text']);
144 }
145
146 //CRM-5001
a7488080 147 if (!empty($this->_values['is_for_organization'])) {
6a488035 148 $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
9d7cae22
DG
149 $ufJoinParams = array(
150 'module' => 'onBehalf',
151 'entity_table' => 'civicrm_contribution_page',
152 'entity_id' => $this->_id,
153 );
154 $onBehalfProfileIDs = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
155 // getUFGroupIDs returns an array with the first item being the ID we need
156 $onBehalfProfileID = $onBehalfProfileIDs[0];
157 if ($onBehalfProfileID) {
158 $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($onBehalfProfileID);
6a488035
TO
159 foreach (array(
160 'Individual', 'Organization', 'Household') as $contactType) {
9d7cae22
DG
161 if (in_array($contactType, $onBehalfProfile) &&
162 (in_array('Membership', $onBehalfProfile) ||
163 in_array('Contribution', $onBehalfProfile)
6a488035
TO
164 )
165 ) {
166 CRM_Core_Error::fatal($msg);
167 }
168 }
169 }
170
171 if ($postID = CRM_Utils_Array::value('custom_post_id', $this->_values)) {
172 $postProfile = CRM_Core_BAO_UFGroup::profileGroups($postID);
173 foreach (array(
174 'Individual', 'Organization', 'Household') as $contactType) {
175 if (in_array($contactType, $postProfile) &&
176 (in_array('Membership', $postProfile) ||
177 in_array('Contribution', $postProfile)
178 )
179 ) {
180 CRM_Core_Error::fatal($msg);
181 }
182 }
183 }
184 }
6a488035
TO
185 }
186
186c9c17 187 /**
100fef9d 188 * Set the default values
186c9c17
EM
189 *
190 * @return void
191 * @access public
192 */
193 /**
194 *
195 */
6a488035 196 function setDefaultValues() {
6a488035 197 // check if the user is registered and we have a contact ID
da8d9879 198 $contactID = $this->getContactID();
6a488035 199
da8d9879 200 if (!empty($contactID)) {
6a488035
TO
201 $fields = array();
202 $removeCustomFieldTypes = array('Contribution', 'Membership');
203 $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields();
204
205 // remove component related fields
206 foreach ($this->_fields as $name => $dontCare) {
207 //don't set custom data Used for Contribution (CRM-1344)
208 if (substr($name, 0, 7) == 'custom_') {
209 $id = substr($name, 7);
210 if (!CRM_Core_BAO_CustomGroup::checkCustomField($id, $removeCustomFieldTypes)) {
211 continue;
212 }
213 // ignore component fields
214 }
215 elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) == 'membership_') || (substr($name, 0, 13) == 'contribution_')) {
216 continue;
217 }
218 $fields[$name] = 1;
219 }
fc60f30e
RN
220
221 if (!empty($fields)) {
472561f4 222 CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults);
fc60f30e
RN
223 }
224
9d665938 225 $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
226 $this->_defaults = array_merge($this->_defaults, $billingDefaults);
6a488035
TO
227 }
228
229 //set custom field defaults set by admin if value is not set
230 if (!empty($this->_fields)) {
231 //load default campaign from page.
232 if (array_key_exists('contribution_campaign_id', $this->_fields)) {
233 $this->_defaults['contribution_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values);
234 }
235
236 //set custom field defaults
237 foreach ($this->_fields as $name => $field) {
238 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
239 if (!isset($this->_defaults[$name])) {
240 CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults,
241 NULL, CRM_Profile_Form::MODE_REGISTER
242 );
243 }
244 }
245 }
246 }
247
6a488035
TO
248 // // hack to simplify credit card entry for testing
249 // $this->_defaults['credit_card_type'] = 'Visa';
250 // $this->_defaults['amount'] = 168;
251 // $this->_defaults['credit_card_number'] = '4111111111111111';
252 // $this->_defaults['cvv2'] = '000';
f0bf6c15 253 // $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
6a488035
TO
254
255 // // hack to simplify direct debit entry for testing
256 // $this->_defaults['account_holder'] = 'Max Müller';
257 // $this->_defaults['bank_account_number'] = '12345678';
258 // $this->_defaults['bank_identification_number'] = '12030000';
259 // $this->_defaults['bank_name'] = 'Bankname';
260
261 //build set default for pledge overdue payment.
a7488080 262 if (!empty($this->_values['pledge_id'])) {
133e2c99 263 //get all pledge payment records of current pledge id.
264 $pledgePayments = array();
265
266 //used to record completed pledge payment ids used later for honor default
267 $completedContributionIds = array();
268
269 $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($this->_values['pledge_id']);
6a488035 270
6a488035 271 $duePayment = FALSE;
133e2c99 272 foreach ($pledgePayments as $payId => $value) {
273 if ($value['status'] == 'Overdue') {
6a488035
TO
274 $this->_defaults['pledge_amount'][$payId] = 1;
275 }
133e2c99 276 elseif (!$duePayment && $value['status'] == 'Pending') {
6a488035
TO
277 $this->_defaults['pledge_amount'][$payId] = 1;
278 $duePayment = TRUE;
279 }
133e2c99 280 elseif ($value['status'] == 'Completed' && $value['contribution_id']) {
281 $completedContributionIds[] = $value['contribution_id'];
282 }
283 }
284
8af73472 285 if ($this->_honor_block_is_active && count($completedContributionIds)) {
133e2c99 286 $softCredit = array();
287 foreach ($completedContributionIds as $id) {
288 $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id);
289 }
290 if (isset($softCredit['soft_credit'])) {
291 $this->_defaults['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type'];
292
293 //since honoree profile fieldname of fields are prefixed with 'honor'
294 //we need to reformat the fieldname to append prefix during setting default values
295 CRM_Core_BAO_UFGroup::setProfileDefaults(
296 $softCredit['soft_credit'][1]['contact_id'],
297 CRM_Core_BAO_UFGroup::getFields($this->_honoreeProfileId),
298 $defaults
299 );
300 foreach ($defaults as $fieldName => $value) {
301 $this->_defaults['honor[' . $fieldName . ']'] = $value;
302 }
303 }
6a488035
TO
304 }
305 }
a7488080 306 elseif (!empty($this->_values['pledge_block_id'])) {
6a488035
TO
307 //set default to one time contribution.
308 $this->_defaults['is_pledge'] = 0;
309 }
310
311 // to process Custom data that are appended to URL
312 $getDefaults = CRM_Core_BAO_CustomGroup::extractGetParams($this, "'Contact', 'Individual', 'Contribution'");
03110609 313 $this->_defaults = array_merge($this->_defaults, $getDefaults);
6a488035
TO
314
315 $config = CRM_Core_Config::singleton();
316 // set default country from config if no country set
a7488080 317 if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) {
6a488035
TO
318 $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
319 }
320
321 // set default state/province from config if no state/province set
a7488080 322 if (empty($this->_defaults["billing_state_province_id-{$this->_bltID}"])) {
6a488035
TO
323 $this->_defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
324 }
325
6a488035 326 if ($this->_priceSetId) {
a1a68e64 327 if (($this->_useForMember && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) {
6a488035
TO
328 $selectedCurrentMemTypes = array();
329 foreach ($this->_priceSet['fields'] as $key => $val) {
330 foreach ($val['options'] as $keys => $values) {
331 $opMemTypeId = CRM_Utils_Array::value('membership_type_id', $values);
332 if ($opMemTypeId &&
333 in_array($opMemTypeId, $this->_currentMemberships) &&
334 !in_array($opMemTypeId, $selectedCurrentMemTypes)
335 ) {
336 if ($val['html_type'] == 'CheckBox') {
337 $this->_defaults["price_{$key}"][$keys] = 1;
338 }
339 else {
340 $this->_defaults["price_{$key}"] = $keys;
341 }
342 $selectedCurrentMemTypes[] = $values['membership_type_id'];
343 }
a7488080 344 elseif (!empty($values['is_default']) &&
6a488035
TO
345 !$opMemTypeId &&
346 (!isset($this->_defaults["price_{$key}"]) ||
347 ($val['html_type'] == 'CheckBox' && !isset($this->_defaults["price_{$key}"][$keys]))
348 )
349 ) {
350 if ($val['html_type'] == 'CheckBox') {
351 $this->_defaults["price_{$key}"][$keys] = 1;
352 }
353 else {
354 $this->_defaults["price_{$key}"] = $keys;
355 }
356 }
357 }
358 }
359 }
360 else {
9da8dc8c 361 CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $this->_defaults);
6a488035
TO
362 }
363 }
364
365 if (!empty($this->_paymentProcessors)) {
366 foreach ($this->_paymentProcessors as $pid => $value) {
a7488080 367 if (!empty($value['is_default'])) {
6a488035
TO
368 $this->_defaults['payment_processor'] = $pid;
369 }
370 }
371 }
372
373 return $this->_defaults;
374 }
375
376 /**
c490a46a 377 * Build the form object
6a488035 378 *
355ba699 379 * @return void
6a488035
TO
380 * @access public
381 */
382 public function buildQuickForm() {
4839c695
KJ
383 // build profiles first so that we can determine address fields etc
384 // and then show copy address checkbox
385 $this->buildCustom($this->_values['custom_pre_id'], 'customPre');
386 $this->buildCustom($this->_values['custom_post_id'], 'customPost');
387
7d613bb7 388 if (!empty($this->_fields) && !empty($this->_values['custom_pre_id'])) {
4839c695
KJ
389 $profileAddressFields = array();
390 foreach ($this->_fields as $key => $value) {
7d613bb7 391 CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, array('uf_group_id' => $this->_values['custom_pre_id']));
4839c695
KJ
392 }
393 $this->set('profileAddressFields', $profileAddressFields);
394 }
395
7bf9cde2 396 // Build payment processor form
67399793 397 if (empty($_GET['onbehalf'])) {
7c8cc461 398 CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
7bf9cde2 399 // Return if we are in an ajax callback
7c8cc461
CW
400 if ($this->_snippet) {
401 return;
402 }
6a488035
TO
403 }
404
405 $config = CRM_Core_Config::singleton();
78f0ee1b 406
6a488035 407 if ($this->_onbehalf) {
061a96be 408 CRM_Contribute_Form_Contribution_OnBehalfOf::buildQuickForm($this);
ab234a8a
CW
409 // Return if we are in an ajax callback
410 if ($this->_snippet) {
411 return;
412 }
6a488035
TO
413 }
414
415 $this->applyFilter('__ALL__', 'trim');
416 $this->add('text', "email-{$this->_bltID}",
417 ts('Email Address'),
418 array('size' => 30, 'maxlength' => 60, 'class' => 'email'),
419 TRUE
420 );
421 $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email');
6a488035 422 $pps = array();
67399793 423 //@todo - this should be replaced by a check as to whether billing fields are set
596bff78 424 $onlinePaymentProcessorEnabled = FALSE;
6a488035 425 if (!empty($this->_paymentProcessors)) {
596bff78 426 foreach ($this->_paymentProcessors as $key => $name) {
427 if($name['billing_mode'] == 1) {
428 $onlinePaymentProcessorEnabled = TRUE;
429 }
6a488035
TO
430 $pps[$key] = $name['name'];
431 }
432 }
a7488080 433 if (!empty($this->_values['is_pay_later'])) {
6a488035
TO
434 $pps[0] = $this->_values['pay_later_text'];
435 }
436
437 if (count($pps) > 1) {
438 $this->addRadio('payment_processor', ts('Payment Method'), $pps,
439 NULL, "&nbsp;", TRUE
440 );
441 }
442 elseif (!empty($pps)) {
443 $key = array_keys($pps);
444 $key = array_pop($key);
445 $this->addElement('hidden', 'payment_processor', $key);
446 if ($key === 0) {
447 $this->assign('is_pay_later', $this->_values['is_pay_later']);
448 $this->assign('pay_later_text', $this->_values['pay_later_text']);
449 }
450 }
596bff78 451
452 $contactID = $this->getContactID();
453 if($this->getContactID() === '0') {
454 $this->addCidZeroOptions($onlinePaymentProcessorEnabled);
455 }
6a488035
TO
456 //build pledge block.
457 $this->_useForMember = 0;
458 //don't build membership block when pledge_id is passed
a7488080 459 if (empty($this->_values['pledge_id'])) {
6a488035
TO
460 $this->_separateMembershipPayment = FALSE;
461 if (in_array('CiviMember', $config->enableComponents)) {
462 $isTest = 0;
463 if ($this->_action & CRM_Core_Action::PREVIEW) {
464 $isTest = 1;
465 }
466
467 if ($this->_priceSetId &&
468 (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet))
469 ) {
470 $this->_useForMember = 1;
471 $this->set('useForMember', $this->_useForMember);
472 }
473
474 $this->_separateMembershipPayment = CRM_Member_BAO_Membership::buildMembershipBlock($this,
475 $this->_id,
5b757295 476 $this->_membershipContactID,
6a488035 477 TRUE, NULL, FALSE,
5b757295 478 $isTest
6a488035
TO
479 );
480 }
481 $this->set('separateMembershipPayment', $this->_separateMembershipPayment);
482 }
483 $this->assign('useForMember', $this->_useForMember);
484 // If we configured price set for contribution page
485 // we are not allow membership signup as well as any
486 // other contribution amount field, CRM-5095
487 if (isset($this->_priceSetId) && $this->_priceSetId) {
488 $this->add('hidden', 'priceSetId', $this->_priceSetId);
489 // build price set form.
490 $this->set('priceSetId', $this->_priceSetId);
9da8dc8c 491 CRM_Price_BAO_PriceSet::buildPriceSet($this);
6a488035 492 if ($this->_values['is_monetary'] &&
8cc574cf 493 $this->_values['is_recur'] && empty($this->_values['pledge_id'])) {
6a488035
TO
494 self::buildRecur($this);
495 }
496 }
6a488035
TO
497
498 if ($this->_priceSetId) {
9da8dc8c 499 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
6a488035
TO
500 if ($is_quick_config) {
501 $this->_useForMember = 0;
502 $this->set('useForMember', $this->_useForMember);
503 }
504 }
505
506 if ($this->_values['is_for_organization']) {
507 $this->buildOnBehalfOrganization();
508 }
509
510 //we allow premium for pledge during pledge creation only.
a7488080 511 if (empty($this->_values['pledge_id'])) {
6a488035
TO
512 CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE);
513 }
514
133e2c99 515 //add honor block
8af73472 516 if ($this->_honor_block_is_active) {
133e2c99 517 $this->assign('honor_block_is_active', TRUE);
133e2c99 518
519 //build soft-credit section
520 CRM_Contribute_Form_SoftCredit::buildQuickForm($this);
521 //build honoree profile section
522 CRM_Contact_Form_ProfileContact::buildQuickForm($this);
6a488035
TO
523 }
524
525
526 //don't build pledge block when mid is passed
527 if (!$this->_mid) {
528 $config = CRM_Core_Config::singleton();
8cc574cf 529 if (in_array('CiviPledge', $config->enableComponents) && !empty($this->_values['pledge_block_id'])) {
6a488035
TO
530 CRM_Pledge_BAO_PledgeBlock::buildPledgeBlock($this);
531 }
532 }
533
6a488035
TO
534 //to create an cms user
535 if (!$this->_userID) {
536 $createCMSUser = FALSE;
537
538 if ($this->_values['custom_pre_id']) {
539 $profileID = $this->_values['custom_pre_id'];
540 $createCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user');
541 }
542
543 if (!$createCMSUser &&
544 $this->_values['custom_post_id']
545 ) {
546 if (!is_array($this->_values['custom_post_id'])) {
547 $profileIDs = array($this->_values['custom_post_id']);
548 }
549 else {
550 $profileIDs = $this->_values['custom_post_id'];
551 }
552 foreach ($profileIDs as $pid) {
553 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
554 $profileID = $pid;
555 $createCMSUser = TRUE;
556 break;
557 }
558 }
559 }
560
561 if ($createCMSUser) {
562 CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE);
563 }
564 }
565 if ($this->_pcpId) {
566 if ($pcpSupporter = CRM_PCP_BAO_PCP::displayName($this->_pcpId)) {
b942a32a 567 $pcp_supporter_text = ts('This contribution is being made thanks to the effort of <strong>%1</strong>, who supports our campaign.', array(1 => $pcpSupporter));
eea141c0
DG
568 // 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.
569 $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute');
570 if(!empty($text)) {
b942a32a 571 $pcp_supporter_text .= ts("You can support it as well - once you complete the donation, you will be able to create your own Personal Campaign Page!");
eea141c0
DG
572 }
573 $this->assign('pcpSupporterText', $pcp_supporter_text);
6a488035 574 }
31ed2806
JM
575 $prms = array('id' => $this->_pcpId);
576 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
577 if ($pcpInfo['is_honor_roll']) {
4d327c2d 578 $this->assign('isHonor', TRUE);
31ed2806
JM
579 $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL,
580 array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );")
581 );
582 $extraOption = array('onclick' => "return pcpAnonymous( );");
583 $elements = array();
584 $elements[] = &$this->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption);
585 $elements[] = &$this->createElement('radio', NULL, '', ts('List my contribution anonymously'), 1, $extraOption);
586 $this->addGroup($elements, 'pcp_is_anonymous', NULL, '&nbsp;&nbsp;&nbsp;');
f26a6b99
JM
587 $this->setDefaults(array('pcp_display_in_roll' => 1));
588 $this->setDefaults(array('pcp_is_anonymous' => 1));
31ed2806
JM
589
590 $this->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30));
591 $this->add('textarea', 'pcp_personal_note', ts('Personal Note'), array('style' => 'height: 3em; width: 40em;'));
592 }
6a488035
TO
593 }
594
595 //we have to load confirm contribution button in template
596 //when multiple payment processor as the user
597 //can toggle with payment processor selection
598 $billingModePaymentProcessors = 0;
599 if ( !empty( $this->_paymentProcessors ) ) {
600 foreach ($this->_paymentProcessors as $key => $values) {
601 if ($values['billing_mode'] == CRM_Core_Payment::BILLING_MODE_BUTTON) {
602 $billingModePaymentProcessors++;
603 }
604 }
605 }
606
607 if ($billingModePaymentProcessors && count($this->_paymentProcessors) == $billingModePaymentProcessors) {
608 $allAreBillingModeProcessors = TRUE;
609 } else {
610 $allAreBillingModeProcessors = FALSE;
611 }
612
613 if (!($allAreBillingModeProcessors && !$this->_values['is_pay_later'])) {
61189839
CW
614 $submitButton = array(
615 'type' => 'upload',
c0321a2d 616 'name' => CRM_Utils_Array::value('is_confirm_enabled', $this->_values) ? ts('Confirm Contribution') : ts('Contribute'),
61189839
CW
617 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
618 'isDefault' => TRUE,
6a488035 619 );
61189839
CW
620 // Add submit-once behavior when confirm page disabled
621 if (empty($this->_values['is_confirm_enabled'])) {
622 $submitButton['js'] = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');");
623 }
624 $this->addButtons(array($submitButton));
6a488035
TO
625 }
626
627 $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this);
6a488035
TO
628 }
629
6a488035 630 /**
100fef9d 631 * Build elements to enable pay on behalf of an organization.
6a488035
TO
632 *
633 * @access public
634 */
635 function buildOnBehalfOrganization() {
636 if ($this->_membershipContactID) {
637 $entityBlock = array('contact_id' => $this->_membershipContactID);
638 CRM_Core_BAO_Location::getValues($entityBlock, $this->_defaults);
639 }
640
78f0ee1b 641 if (!$this->_onBehalfRequired) {
6a488035
TO
642 $this->addElement('checkbox', 'is_for_organization',
643 $this->_values['for_organization'],
644 NULL, array('onclick' => "showOnBehalf( );")
645 );
646 }
6a488035
TO
647
648 $this->assign('is_for_organization', TRUE);
649 $this->assign('urlPath', 'civicrm/contribute/transact');
650 }
651
652 /**
100fef9d 653 * Build elements to collect information for recurring contributions
6a488035
TO
654 *
655 * @access public
03110609
EM
656 *
657 * @param CRM_Core_Form $form
6a488035 658 */
a5aef36c 659 public static function buildRecur(&$form) {
6a488035
TO
660 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur');
661 $className = get_class($form);
662
a5aef36c 663 $form->assign('is_recur_interval', CRM_Utils_Array::value('is_recur_interval', $form->_values));
664 $form->assign('is_recur_installments', CRM_Utils_Array::value('is_recur_installments', $form->_values));
6a488035
TO
665
666 $form->add('checkbox', 'is_recur', ts('I want to contribute this amount'), NULL);
667
a7488080 668 if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
6a488035
TO
669 $form->add('text', 'frequency_interval', ts('Every'), $attributes['frequency_interval']);
670 $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer');
671 }
672 else {
673 // make sure frequency_interval is submitted as 1 if given no choice to user.
674 $form->add('hidden', 'frequency_interval', 1);
675 }
676
677 $frUnits = CRM_Utils_Array::value('recur_frequency_unit', $form->_values);
678 if (empty($frUnits) &&
679 $className == 'CRM_Contribute_Form_Contribution'
680 ) {
681 $frUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR,
682 CRM_Core_OptionGroup::values('recur_frequency_units')
683 );
684 }
685
686 $unitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $frUnits);
687
688 // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit
689 if(sizeof($unitVals) == 1) {
a5aef36c 690 $form->assign('one_frequency_unit', true);
6a488035
TO
691 $unit = $unitVals[0];
692 $form->add('hidden', 'frequency_unit', $unit);
a7488080 693 if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
6a488035
TO
694 $unit .= "(s)";
695 }
a5aef36c 696 $form->assign('frequency_unit', $unit);
6a488035 697 } else {
a5aef36c 698 $form->assign('one_frequency_unit', false);
6a488035
TO
699 $units = array();
700 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
701 foreach ($unitVals as $key => $val) {
702 if (array_key_exists($val, $frequencyUnits)) {
703 $units[$val] = $frequencyUnits[$val];
a7488080 704 if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
6a488035
TO
705 $units[$val] = "{$frequencyUnits[$val]}(s)";
706 }
707 }
708 }
709 $frequencyUnit = &$form->add('select', 'frequency_unit', NULL, $units);
710 }
711
712
713 // FIXME: Ideally we should freeze select box if there is only
714 // one option but looks there is some problem /w QF freeze.
715 //if ( count( $units ) == 1 ) {
716 //$frequencyUnit->freeze( );
717 //}
718
719 $form->add('text', 'installments', ts('installments'),
720 $attributes['installments']
721 );
722 $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer');
723 }
724
725 /**
100fef9d 726 * Global form rule
6a488035 727 *
2a6da8d7
EM
728 * @param array $fields the input form values
729 * @param array $files the uploaded files if any
730 * @param $self
731 *
6a488035
TO
732 *
733 * @return true if no errors, else array of errors
734 * @access public
735 * @static
736 */
737 static function formRule($fields, $files, $self) {
738 $errors = array();
739 $amount = self::computeAmount($fields, $self);
740
8cc574cf 741 if ((!empty($fields['selectMembership']) &&
6a488035
TO
742 $fields['selectMembership'] != 'no_thanks'
743 ) ||
8cc574cf 744 (!empty($fields['priceSetId']) &&
6a488035
TO
745 $self->_useForMember
746 )
747 ) {
748 $lifeMember = CRM_Member_BAO_Membership::getAllContactMembership($self->_userID, FALSE, TRUE);
749
750 $membershipOrgDetails = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization();
751
752 $unallowedOrgs = array();
753 foreach (array_keys($lifeMember) as $memTypeId) {
754 $unallowedOrgs[] = $membershipOrgDetails[$memTypeId];
755 }
756 }
757
758 //check for atleast one pricefields should be selected
a7488080 759 if (!empty($fields['priceSetId'])) {
9da8dc8c 760 $priceField = new CRM_Price_DAO_PriceField();
6a488035
TO
761 $priceField->price_set_id = $fields['priceSetId'];
762 $priceField->orderBy('weight');
763 $priceField->find();
764
765 $check = array();
766 $membershipIsActive = TRUE;
767 $previousId = $otherAmount = FALSE;
768 while ($priceField->fetch()) {
769
770 if ($self->_quickConfig && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) {
771 $previousId = $priceField->id;
772 if ($priceField->name == 'membership_amount' && !$priceField->is_active ) {
773 $membershipIsActive = FALSE;
774 }
775 }
776 if ($priceField->name == 'other_amount') {
8cc574cf 777 if ($self->_quickConfig && empty($fields["price_{$priceField->id}"]) &&
6a488035
TO
778 array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"])) {
779 $otherAmount = $priceField->id;
780 }
781 elseif (!empty($fields["price_{$priceField->id}"])) {
ef88f444 782 $otherAmountVal = CRM_Utils_Rule::cleanMoney($fields["price_{$priceField->id}"]);
6a488035
TO
783 $min = CRM_Utils_Array::value('min_amount', $self->_values);
784 $max = CRM_Utils_Array::value('max_amount', $self->_values);
785 if ($min && $otherAmountVal < $min) {
786 $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1',
787 array(1 => $min)
788 );
789 }
790 if ($max && $otherAmountVal > $max) {
791 $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.',
792 array(1 => $max)
793 );
794 }
795 }
796 }
797 if (!empty($fields["price_{$priceField->id}"]) || ($previousId == $priceField->id && isset($fields["price_{$previousId}"])
798 && empty($fields["price_{$previousId}"]))) {
799 $check[] = $priceField->id;
800 }
801 }
802
b5a62499
PN
803 $currentMemberships = NULL;
804 if ($membershipIsActive) {
805 $is_test = $self->_mode != 'live' ? 1 : 0;
806 $memContactID = $self->_membershipContactID;
7f7fa13a
EM
807
808 // For anonymous user check using dedupe rule
b5a62499
PN
809 // if user has Cancelled Membership
810 if (!$memContactID) {
811 $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
812 $dedupeParams['check_permission'] = FALSE;
813 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');
814 // if we find more than one contact, use the first one
815 $memContactID = CRM_Utils_Array::value(0, $ids);
816 }
817 $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID,
818 $is_test
819 );
7f7fa13a 820
b5a62499
PN
821 $errorText = 'Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.';
822 foreach ($self->_values['fee'] as $fieldKey => $fieldValue) {
823 if ($fieldValue['html_type'] != 'Text' && CRM_Utils_Array::value('price_' . $fieldKey, $fields)) {
824 if (!is_array($fields['price_' . $fieldKey])) {
7f7fa13a 825 if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]])
3a96b63b
PN
826 && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships)) {
827 $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'])));
b5a62499
PN
828 }
829 }
830 else {
831 foreach ($fields['price_' . $fieldKey] as $key => $ignore) {
7f7fa13a 832 if (array_key_exists('membership_type_id', $fieldValue['options'][$key])
3a96b63b 833 && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships)) {
b5a62499
PN
834 $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$key]['membership_type_id'])));
835 }
836 }
837 }
838 }
839 }
840 }
7f7fa13a 841
9ce8bcfc 842 // CRM-12233
665e5ec7 843 if ($membershipIsActive && !$self->_membershipBlock['is_required']
9ce8bcfc
PN
844 && $self->_values['amount_block_is_active']) {
845 $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL;
846 foreach ($self->_values['fee'] as $fieldKey => $fieldValue) {
41f27e49 847 // if 'No thank you' membership is selected then set $membershipFieldId
9ce8bcfc
PN
848 if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array::value('price_' . $fieldKey, $fields) == 0) {
849 $membershipFieldId = $fieldKey;
850 }
851 elseif ($membershipFieldId) {
852 if ($fieldValue['name'] == 'other_amount') {
853 $otherFieldId = $fieldKey;
854 }
855 elseif ($fieldValue['name'] == 'contribution_amount') {
856 $contributionFieldId = $fieldKey;
857 }
665e5ec7 858
9ce8bcfc
PN
859 if (!$errorKey || CRM_Utils_Array::value('price_' . $contributionFieldId, $fields) == '0') {
860 $errorKey = $fieldKey;
861 }
862 }
863 }
41f27e49 864 // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error
8cc574cf 865 if ($membershipFieldId && !(CRM_Utils_Array::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) {
1cd7e100 866 $errors["price_{$errorKey}"] = ts('Additional Contribution is required.');
9ce8bcfc
PN
867 }
868 }
6a488035
TO
869 if (empty($check)) {
870 if ($self->_useForMember == 1 && $membershipIsActive) {
871 $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).');
872 }
873 else {
874 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
875 }
876 }
877 if($otherAmount && !empty($check)) {
878 $errors["price_{$otherAmount}"] = ts('Amount is required field.');
879 }
880
881 if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) {
882 $priceFieldIDS = array();
883 $priceFieldMemTypes = array();
884
885 foreach ($self->_priceSet['fields'] as $priceId => $value) {
8cc574cf 886 if (!empty($fields['price_' . $priceId]) || ($self->_quickConfig && $value['name'] == 'membership_amount' && empty($self->_membershipBlock['is_required']))) {
a7488080 887 if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) {
6a488035
TO
888 foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) {
889 if ($isSet) {
890 $priceFieldIDS[] = $priceFldVal;
891 }
892 }
893 }
8cc574cf 894 elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) {
6a488035
TO
895 // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering
896 // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577
897 $priceFieldIDS[] = $fields['price_' . $priceId];
898 }
899
a7488080 900 if (!empty($value['options'])) {
6a488035 901 foreach ($value['options'] as $val) {
88c6259e 902 if (!empty($val['membership_type_id']) && (
fb4aa4d9 903 ($fields['price_' . $priceId] == $val['id']) ||
904 (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']]))
905 )
906 ) {
6a488035
TO
907 $priceFieldMemTypes[] = $val['membership_type_id'];
908 }
909 }
910 }
911 }
912 }
913
914 if (!empty($lifeMember)) {
915 foreach ($priceFieldIDS as $priceFieldId) {
9da8dc8c 916 if (($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) &&
6a488035
TO
917 in_array($membershipOrgDetails[$id], $unallowedOrgs)
918 ) {
919 $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.');
920 break;
921 }
922 }
923 }
924
925 if (!empty($priceFieldIDS)) {
926 $ids = implode(',', $priceFieldIDS);
927
928 $priceFieldIDS['id'] = $fields['priceSetId'];
929 $self->set('memberPriceFieldIDS', $priceFieldIDS);
9da8dc8c 930 $count = CRM_Price_BAO_PriceSet::getMembershipCount($ids);
6a488035
TO
931 foreach ($count as $id => $occurance) {
932 if ($occurance > 1) {
933 $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.');
934 }
935 }
936 }
937
938 if (empty($priceFieldMemTypes)) {
939 $errors['_qf_default'] = ts('Please select at least one membership option.');
940 }
941 }
942
9da8dc8c 943 CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'],
6a488035
TO
944 $fields, $lineItem
945 );
665e5ec7 946
6a488035
TO
947 if ($fields['amount'] < 0) {
948 $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
949 }
950 $amount = $fields['amount'];
951 }
952
953 if (isset($fields['selectProduct']) &&
8c7f9d7b 954 $fields['selectProduct'] != 'no_thanks') {
6a488035
TO
955 $productDAO = new CRM_Contribute_DAO_Product();
956 $productDAO->id = $fields['selectProduct'];
957 $productDAO->find(TRUE);
958 $min_amount = $productDAO->min_contribution;
959
960 if ($amount < $min_amount) {
961 $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount)));
962 CRM_Core_Session::setStatus($errors['selectProduct']);
963 }
964 }
965
a7488080 966 if (!empty($fields['is_recur'])) {
6a488035
TO
967 if ($fields['frequency_interval'] <= 0) {
968 $errors['frequency_interval'] = ts('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).');
969 }
970 if ($fields['frequency_unit'] == '0') {
971 $errors['frequency_unit'] = ts('Please select a period (e.g. months, years ...) for how often you want to make this recurring contribution (EXAMPLE: Every 3 MONTHS).');
972 }
973 }
974
a7488080 975 if (!empty($fields['is_recur']) &&
6a488035
TO
976 CRM_Utils_Array::value('payment_processor', $fields) == 0) {
977 $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
978 }
979
a7488080 980 if (!empty($fields['is_for_organization']) &&
6a488035
TO
981 !property_exists($self, 'organizationName')
982 ) {
983
a7488080
CW
984 if (empty($fields['onbehalf']['organization_name'])) {
985 if (!empty($fields['org_option']) && !$fields['onbehalfof_id']) {
6a488035
TO
986 $errors['organization_id'] = ts('Please select an organization or enter a new one.');
987 }
a7488080 988 elseif (empty($fields['org_option'])) {
6a488035
TO
989 $errors['onbehalf']['organization_name'] = ts('Please enter the organization name.');
990 }
991 }
992
993 foreach ($fields['onbehalf'] as $key => $value) {
994 if (strstr($key, 'email')) {
995 $emailLocType = explode('-', $key);
996 }
997 }
a7488080 998 if (empty($fields['onbehalf']["email-{$emailLocType[1]}"])) {
6a488035
TO
999 $errors['onbehalf']["email-{$emailLocType[1]}"] = ts('Organization email is required.');
1000 }
1001 }
1002
1003 // validate PCP fields - if not anonymous, we need a nick name value
8cc574cf 1004 if ($self->_pcpId && !empty($fields['pcp_display_in_roll']) &&
6a488035
TO
1005 (CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0) &&
1006 CRM_Utils_Array::value('pcp_roll_nickname', $fields) == ''
1007 ) {
1008 $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
1009 }
1010
1011 // return if this is express mode
1012 $config = CRM_Core_Config::singleton();
f92fc7eb
CW
1013 if ($self->_paymentProcessor &&
1014 $self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON
1015 ) {
8cc574cf 1016 if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) ||
6a488035
TO
1017 CRM_Utils_Array::value($self->_expressButtonName, $fields)
1018 ) {
1019 return $errors;
1020 }
1021 }
1022
1023 //validate the pledge fields.
a7488080 1024 if (!empty($self->_values['pledge_block_id'])) {
6a488035 1025 //validation for pledge payment.
a7488080 1026 if (!empty($self->_values['pledge_id'])) {
6a488035
TO
1027 if (empty($fields['pledge_amount'])) {
1028 $errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
1029 }
1030 }
a7488080 1031 elseif (!empty($fields['is_pledge'])) {
6a488035 1032 if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == FALSE) {
917a9954 1033 $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.');
6a488035
TO
1034 }
1035 else {
1036 if (CRM_Utils_Array::value('pledge_installments', $fields) == NULL) {
1037 $errors['pledge_installments'] = ts('Pledge Installments is required field.');
1038 }
1039 elseif (CRM_Utils_array::value('pledge_installments', $fields) == 1) {
1040 $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.');
1041 }
1042 elseif (CRM_Utils_array::value('pledge_installments', $fields) == 0) {
1043 $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
1044 }
1045 }
1046
1047 //validation for Pledge Frequency Interval.
1048 if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == FALSE) {
1049 $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
1050 }
1051 else {
1052 if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == NULL) {
1053 $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
1054 }
1055 elseif (CRM_Utils_array::value('pledge_frequency_interval', $fields) == 0) {
1056 $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
1057 }
1058 }
1059 }
1060 }
1061
1062 // also return if paylater mode
8ae4d0d3 1063 if (CRM_Utils_Array::value('payment_processor', $fields) == 0 && $self->_isBillingAddressRequiredForPayLater == 0) {
6a488035
TO
1064 return empty($errors) ? TRUE : $errors;
1065 }
1066
1067 // if the user has chosen a free membership or the amount is less than zero
1068 // i.e. we skip calling the payment processor and hence dont need credit card
1069 // info
f92fc7eb 1070 if ((float) $amount <= 0.0) {
6a488035
TO
1071 return $errors;
1072 }
1073
7cb3d4f0
CW
1074 if (!empty($self->_paymentFields)) {
1075 CRM_Core_Form::validateMandatoryFields($self->_paymentFields, $fields, $errors);
6a488035 1076 }
7cb3d4f0 1077 CRM_Core_Payment_Form::validateCreditCard($fields, $errors);
6a488035 1078
6a488035
TO
1079 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
1080 if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
1081 $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
1082 if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) {
1083 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
1084 array(1 => ucwords(str_replace('_', " ", $greeting)))
1085 );
1086 }
1087 }
1088 }
1089
1090 return empty($errors) ? TRUE : $errors;
1091 }
1092
186c9c17 1093 /**
c490a46a
CW
1094 * @param array $params
1095 * @param CRM_Core_Form $form
186c9c17
EM
1096 *
1097 * @return int|mixed|null|string
1098 */
cde484fd 1099 public static function computeAmount(&$params, &$form) {
6a488035
TO
1100 $amount = NULL;
1101
1102 // first clean up the other amount field if present
1103 if (isset($params['amount_other'])) {
1104 $params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']);
1105 }
1106
8cc574cf 1107 if (CRM_Utils_Array::value('amount', $params) == 'amount_other_radio' || !empty($params['amount_other'])) {
6a488035
TO
1108 $amount = $params['amount_other'];
1109 }
1110 elseif (!empty($params['pledge_amount'])) {
1111 $amount = 0;
1112 foreach ($params['pledge_amount'] as $paymentId => $dontCare) {
1113 $amount += CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount');
1114 }
1115 }
1116 else {
a7488080 1117 if (!empty($form->_values['amount'])) {
6a488035
TO
1118 $amountID = CRM_Utils_Array::value('amount', $params);
1119
1120 if ($amountID) {
1121 $params['amount_level'] = CRM_Utils_Array::value('label', $form->_values[$amountID]);
1122 $amount = CRM_Utils_Array::value('value', $form->_values[$amountID]);
1123 }
1124 }
1125 }
1126 return $amount;
1127 }
1128
1129 /**
c490a46a 1130 * Process the form submission
6a488035
TO
1131 *
1132 * @access public
1133 *
355ba699 1134 * @return void
6a488035
TO
1135 */
1136 public function postProcess() {
1137 $config = CRM_Core_Config::singleton();
6a488035
TO
1138 // we first reset the confirm page so it accepts new values
1139 $this->controller->resetPage('Confirm');
1140
1141 // get the submitted form values.
1142 $params = $this->controller->exportValues($this->_name);
da8d9879 1143
4625312e
EM
1144 //carry campaign from profile.
1145 if (array_key_exists('contribution_campaign_id', $params)) {
1146 $params['campaign_id'] = $params['contribution_campaign_id'];
1147 }
1148
1149 if (!empty($params['onbehalfof_id'])) {
1150 $params['organization_id'] = $params['onbehalfof_id'];
1151 }
1152
1153 $params['currencyID'] = $config->defaultCurrency;
1154
a7488080 1155 if (!empty($params['priceSetId'])) {
9da8dc8c 1156 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
6a488035 1157 if ($is_quick_config) {
9da8dc8c 1158 $priceField = new CRM_Price_DAO_PriceField();
6a488035
TO
1159 $priceField->price_set_id = $params['priceSetId'];
1160 $priceField->orderBy('weight');
1161 $priceField->find();
1162
1d9d37c7 1163 $priceOptions = array();
6a488035 1164 while ($priceField->fetch()) {
1d9d37c7
FG
1165 CRM_Price_BAO_PriceFieldValue::getValues($priceField->id, $priceOptions);
1166 if ($selectedPriceOptionID = CRM_Utils_Array::value("price_{$priceField->id}", $params)) {
1167 switch ($priceField->name) {
1168 case 'membership_amount':
1169 $this->_params['selectMembership'] = $params['selectMembership'] = CRM_Utils_Array::value('membership_type_id', $priceOptions[$selectedPriceOptionID]);
1170 $this->set('selectMembership', $params['selectMembership']);
1171 if (CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) == 0) {
1172 $this->_values['amount'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]);
1173 }
1174 break;
1175
1176 case 'contribution_amount':
1177 $params['amount'] = $selectedPriceOptionID;
1178 $this->_values['amount'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]);
1179 $this->_values[$selectedPriceOptionID]['value'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]);
1180 $this->_values[$selectedPriceOptionID]['label'] = CRM_Utils_Array::value('label', $priceOptions[$selectedPriceOptionID]);
1181 $this->_values[$selectedPriceOptionID]['amount_id'] = CRM_Utils_Array::value('id', $priceOptions[$selectedPriceOptionID]);
1182 $this->_values[$selectedPriceOptionID]['weight'] = CRM_Utils_Array::value('weight', $priceOptions[$selectedPriceOptionID]);
1183 break;
1184
1185 case 'other_amount':
1186 $params['amount_other'] = $selectedPriceOptionID;
1187 break;
6a488035
TO
1188 }
1189 }
6a488035
TO
1190 }
1191 }
1192 }
1193
1194 if (($this->_values['is_pay_later'] &&
1195 empty($this->_paymentProcessor) &&
1196 !array_key_exists('hidden_processor', $params)) ||
7195b525 1197 (!empty($params['payment_processor']) && $params['payment_processor'] == 0)) {
6a488035
TO
1198 $params['is_pay_later'] = 1;
1199 }
1200 else {
1201 $params['is_pay_later'] = 0;
1202 }
1203
1204 $this->set('is_pay_later', $params['is_pay_later']);
1205 // assign pay later stuff
1206 $this->_params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE);
1207 $this->assign('is_pay_later', $params['is_pay_later']);
1208 if ($params['is_pay_later']) {
1209 $this->assign('pay_later_text', $this->_values['pay_later_text']);
1210 $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']);
1211 }
1212
8bff2456 1213 // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID
6a488035
TO
1214 $params['amount'] = self::computeAmount($params, $this);
1215 $params['separate_amount'] = $params['amount'];
1216 $memFee = NULL;
a7488080 1217 if (!empty($params['selectMembership'])) {
6a488035
TO
1218 if (!empty($this->_membershipTypeValues)) {
1219 $membershipTypeValues = $this->_membershipTypeValues[$params['selectMembership']];
1220 }
1221 else {
1222 $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this,
1223 $params['selectMembership']
1224 );
1225 }
1226 $memFee = $membershipTypeValues['minimum_fee'];
1227 if (!$params['amount'] && !$this->_separateMembershipPayment) {
1228 $params['amount'] = $memFee ? $memFee : 0;
1229 }
1230 }
e6d4f555 1231 //If the membership & contribution is used in contribution page & not separate payment
9ce8bcfc
PN
1232 $fieldId = $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL;
1233 $proceFieldAmount = 0;
8af73472 1234 if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment == 0) {
9da8dc8c 1235 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
6a488035
TO
1236 if ($is_quick_config) {
1237 foreach ($this->_priceSet['fields'] as $fieldKey => $fieldVal) {
8cc574cf 1238 if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey ])) {
6a488035
TO
1239 $fieldId = $fieldVal['id'];
1240 $fieldOption = $params['price_' . $fieldId];
9ce8bcfc 1241 $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount'];
6a488035
TO
1242 $memPresent = TRUE;
1243 }
1244 else {
a7488080 1245 if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' || $fieldVal['name'] == 'contribution_amount')) {
6a488035
TO
1246 $fieldId = $fieldVal['id'];
1247 if ($fieldVal['name'] == 'other_amount') {
9ce8bcfc 1248 $proceFieldAmount += $this->_submitValues['price_' . $fieldId];
6a488035 1249 }
abcf506a 1250 elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues['price_' . $fieldId] > 0) {
9ce8bcfc 1251 $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount'];
ce9024ca 1252 }
6a488035
TO
1253 unset($params['price_' . $fieldId]);
1254 break;
1255 }
1256 }
1257 }
1258 }
1259 }
ef88f444 1260
6a488035
TO
1261 if (!isset($params['amount_other'])) {
1262 $this->set('amount_level', CRM_Utils_Array::value('amount_level', $params));
1263 }
1264
1265 if ($priceSetId = CRM_Utils_Array::value('priceSetId', $params)) {
1266 $lineItem = array();
9da8dc8c 1267 $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config' );
6a488035 1268 if ( $is_quick_config ) {
ef88f444
PC
1269 foreach ( $this->_values['fee'] as $key => & $val ) {
1270 if ( $val['name'] == 'other_amount' && $val['html_type'] == 'Text' && array_key_exists( 'price_'.$key, $params ) ) {
1271 $params['price_'.$key] = CRM_Utils_Rule::cleanMoney($params['price_'.$key]); //Clean out any currency symbols
96cba2eb 1272 if ( $params['price_'.$key] != 0 ) {
ef88f444
PC
1273 foreach ( $val['options'] as $optionKey => & $options ) {
1274 $options['amount'] = CRM_Utils_Array::value( 'price_'.$key, $params );
1275 break;
1276 }
6a488035
TO
1277 }
1278 $params['price_'.$key] = 1;
1279 break;
1280 }
1281 }
1282 }
6a488035
TO
1283 $component = '';
1284 if ($this->_membershipBlock) {
1285 $component = 'membership';
1286 }
ef88f444 1287
9da8dc8c 1288 CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $component);
dc428161 1289 if ($params['tax_amount']) {
1290 $this->set('tax_amount', $params['tax_amount']);
1291 }
6a488035
TO
1292
1293 if ($proceFieldAmount) {
6a488035 1294 $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount;
c40e1ff4 1295 $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
dc428161 1296 if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) {
1297 $proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount'];
1298 }
6a488035
TO
1299 if (!$this->_membershipBlock['is_separate_payment']) {
1300 $params['amount'] = $proceFieldAmount; //require when separate membership not used
1301 }
1302 }
1303 $this->set('lineItem', $lineItem);
1304 }
1305
8cc574cf 1306 if ($this->_membershipBlock['is_separate_payment'] && !empty($params['separate_amount'])) {
6a488035
TO
1307 $this->set('amount', $params['separate_amount']);
1308 } else {
1309 $this->set('amount', $params['amount']);
1310 }
1311
1312 // generate and set an invoiceID for this transaction
1313 $invoiceID = md5(uniqid(rand(), TRUE));
1314 $this->set('invoiceID', $invoiceID);
1315
e6d4f555 1316 // required only if is_monetary and valid positive amount
6a488035
TO
1317 if ($this->_values['is_monetary'] &&
1318 is_array($this->_paymentProcessor) &&
1319 ((float ) $params['amount'] > 0.0 || $memFee > 0.0)
1320 ) {
1321
1322 // default mode is direct
1323 $this->set('contributeMode', 'direct');
1324
f92fc7eb
CW
1325 if ($this->_paymentProcessor &&
1326 $this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON
1327 ) {
6a488035
TO
1328 //get the button name
1329 $buttonName = $this->controller->getButtonName();
1330 if (in_array($buttonName,
1331 array($this->_expressButtonName, $this->_expressButtonName . '_x', $this->_expressButtonName . '_y')
8cc574cf 1332 ) && empty($params['is_pay_later'])) {
6a488035
TO
1333 $this->set('contributeMode', 'express');
1334
1335 $donateURL = CRM_Utils_System::url('civicrm/contribute', '_qf_Contribute_display=1');
1336 $params['cancelURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
1337 $params['returnURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Confirm_display=1&rfp=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
1338 $params['invoiceID'] = $invoiceID;
fc7063a4 1339 $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']);
6a488035
TO
1340
1341 //default action is Sale
1342 $params['payment_action'] = 'Sale';
1343
1344 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
1345 $token = $payment->setExpressCheckout($params);
1346 if (is_a($token, 'CRM_Core_Error')) {
1347 CRM_Core_Error::displaySessionError($token);
1348 CRM_Utils_System::redirect($params['cancelURL']);
1349 }
1350
1351 $this->set('token', $token);
1352
1353 $paymentURL = $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token";
1354 CRM_Utils_System::redirect($paymentURL);
1355 }
1356 }
f92fc7eb
CW
1357 elseif ($this->_paymentProcessor &&
1358 $this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_NOTIFY
1359 ) {
6a488035
TO
1360 $this->set('contributeMode', 'notify');
1361 }
1362 }
1363
1364 // should we skip the confirm page?
a7488080 1365 if (empty($this->_values['is_confirm_enabled'])) {
94fe3c35
DL
1366 // call the post process hook for the main page before we switch to confirm
1367 $this->postProcessHook();
1368
6a488035
TO
1369 // build the confirm page
1370 $confirmForm = &$this->controller->_pages['Confirm'];
1371 $confirmForm->preProcess();
1372 $confirmForm->buildQuickForm();
1373
1374 // the confirmation page is valid
1375 $data = &$this->controller->container();
1376 $data['valid']['Confirm'] = 1;
1377
1378 // confirm the contribution
94fe3c35
DL
1379 // mainProcess calls the hook also
1380 $confirmForm->mainProcess();
6a488035
TO
1381 $qfKey = $this->controller->_key;
1382
1383 // redirect to thank you page
1384 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE));
1385 }
ef88f444 1386
6a488035 1387 }
7bf9cde2
CW
1388
1389 /**
1390 * Handle Payment Processor switching
1391 * For contribution and event registration forms
cc789d46 1392 * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register $form
03110609 1393 * @param bool $noFees
7bf9cde2 1394 */
b6a469c5 1395 static function preProcessPaymentOptions(&$form, $noFees = FALSE) {
7bf9cde2 1396 $form->_snippet = CRM_Utils_Array::value('snippet', $_GET);
7bf9cde2 1397
b6a469c5 1398 $form->_paymentProcessors = $noFees ? array() : $form->get('paymentProcessors');
e50ee6a3 1399 $form->_paymentProcessorID = NULL;
b6a469c5 1400 if ($form->_paymentProcessors) {
f92fc7eb
CW
1401 // Fetch type during ajax request
1402 if (isset($_GET['type']) && $form->_snippet) {
67399793 1403 $form->_paymentProcessorID = CRM_Utils_Request::retrieve('type', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, 0);
754bdbd4 1404 }
f92fc7eb 1405 // Remember type during form post
afc675df 1406 elseif (!empty($form->_submitValues)) {
e50ee6a3
EM
1407 $form->_paymentProcessorID = CRM_Utils_Array::value('payment_processor', $form->_submitValues);
1408 $form->_paymentProcessor = CRM_Utils_Array::value($form->_paymentProcessorID, $form->_paymentProcessors);
1409 $form->set('type', $form->_paymentProcessorID);
f92fc7eb
CW
1410 $form->set('mode', $form->_mode);
1411 $form->set('paymentProcessor', $form->_paymentProcessor);
7bf9cde2
CW
1412 }
1413 // Set default payment processor
1414 else {
b6a469c5
CW
1415 foreach ($form->_paymentProcessors as $values) {
1416 if (!empty($values['is_default']) || count($form->_paymentProcessors) == 1) {
e50ee6a3 1417 $form->_paymentProcessorID = $values['id'];
f92fc7eb 1418 break;
7bf9cde2
CW
1419 }
1420 }
1421 }
e50ee6a3 1422 if ($form->_paymentProcessorID) {
7bf9cde2
CW
1423 CRM_Core_Payment_ProcessorForm::preProcess($form);
1424 }
67399793
EM
1425 else {
1426 $form->_paymentProcessor = array();
1427 }
7bf9cde2
CW
1428
1429 //get payPal express id and make it available to template
b6a469c5 1430 foreach ($form->_paymentProcessors as $ppId => $values) {
7bf9cde2
CW
1431 $payPalExpressId = ($values['payment_processor_type'] == 'PayPal_Express') ? $values['id'] : 0;
1432 $form->assign('payPalExpressId', $payPalExpressId);
1433 if ($payPalExpressId) {
1434 break;
1435 }
1436 }
bef9421f
CW
1437 if (!$form->_snippet) {
1438 // Add JS to show icons for the accepted credit cards
1439 $creditCardTypes = CRM_Core_Payment_Form::getCreditCardCSSNames();
1440 CRM_Core_Resources::singleton()
f3902ac6
CW
1441 ->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10)
1442 // workaround for CRM-13634
1443 // ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes)));
1444 ->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';');
bef9421f 1445 }
7bf9cde2 1446 }
e50ee6a3 1447 $form->assign('paymentProcessorID', $form->_paymentProcessorID);
7bf9cde2 1448 }
6a488035
TO
1449}
1450