Merge pull request #1814 from dlobo/MiscFixes
[civicrm-core.git] / CRM / Contribute / Form / ContributionBase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for processing a ontribution
38 *
39 */
40class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
41
42 /**
43 * the id of the contribution page that we are proceessing
44 *
45 * @var int
46 * @public
47 */
48 public $_id;
49
50 /**
51 * the mode that we are in
52 *
53 * @var string
54 * @protect
55 */
56 public $_mode;
57
58 /**
59 * the contact id related to a membership
60 *
61 * @var int
62 * @public
63 */
64 public $_membershipContactID;
65
66 /**
67 * the values for the contribution db object
68 *
69 * @var array
70 * @protected
71 */
72 public $_values;
73
74 /**
75 * the paymentProcessor attributes for this page
76 *
77 * @var array
78 * @protected
79 */
80 public $_paymentProcessor;
81 public $_paymentObject = NULL;
82
83 /**
84 * The membership block for this page
85 *
86 * @var array
87 * @protected
88 */
89 public $_membershipBlock = NULL;
90
91 /**
92 * the default values for the form
93 *
94 * @var array
95 * @protected
96 */
97 protected $_defaults;
98
99 /**
100 * The params submitted by the form and computed by the app
101 *
102 * @var array
103 * @public
104 */
105 public $_params;
106
107 /**
108 * The fields involved in this contribution page
109 *
110 * @var array
111 * @public
112 */
113 public $_fields;
114
115 /**
116 * The billing location id for this contribiution page
117 *
118 * @var int
119 * @protected
120 */
121 public $_bltID;
122
123 /**
124 * Cache the amount to make things easier
125 *
126 * @var float
127 * @public
128 */
129 public $_amount;
130
131 /**
132 * pcp id
133 *
134 * @var integer
135 * @public
136 */
137 public $_pcpId;
138
139 /**
140 * pcp block
141 *
142 * @var array
143 * @public
144 */
145 public $_pcpBlock;
146
147 /**
148 * pcp info
149 *
150 * @var array
151 * @public
152 */
153 public $_pcpInfo;
154
5b757295 155 /**
156 * The contact id of the person for whom membership is being added or renewed based on the cid in the url,
157 * checksum, or session
158 * @var unknown_type
159 */
160 protected $_contactID;
161
6a488035
TO
162 protected $_userID;
163
164 /**
165 * the Membership ID for membership renewal
166 *
167 * @var int
168 * @public
169 */
170 public $_membershipId;
171
172 /**
173 * Price Set ID, if the new price set method is used
174 *
175 * @var int
176 * @protected
177 */
178 public $_priceSetId;
179
180 /**
181 * Array of fields for the price set
182 *
183 * @var array
184 * @protected
185 */
186 public $_priceSet;
187
188 public $_action;
189
190 /**
191 * Function to set variables up before form is built
192 *
193 * @return void
194 * @access public
195 */
196 public function preProcess() {
197 $config = CRM_Core_Config::singleton();
198 $session = CRM_Core_Session::singleton();
199
200 // current contribution page id
201 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
202 if (!$this->_id) {
203 $pastContributionID = $session->get('pastContributionID');
204 if (!$pastContributionID) {
205 CRM_Core_Error::fatal(ts('We can\'t load the requested web page due to an incomplete link. This can be caused by using your browser\'s Back button or by using an incomplete or invalid link.'));
206 }
207 else {
208 CRM_Core_Error::fatal(ts('An error occurred during form submission. This page requires form data to be submitted for processing and no form data was submitted or processed. We are sorry for any inconvience. Please click <a href=\'%1\'>here</a> to visit the contribution page and re-start the contribution process.', array(1 => CRM_Utils_System::url('civicrm/contribute/transact', 'reset=1&id=' . $pastContributionID))));
209 }
210 }
211 else {
212 $session->set('pastContributionID', $this->_id);
213 }
5b757295 214 // this was used prior to the cleverer this_>getContactID - unsure now
6a488035 215 $this->_userID = $session->get('userID');
5b757295 216
217 $this->_contactID = $this->_membershipContactID = $this->getContactID();
6a488035 218 $this->_mid = NULL;
5b757295 219 if ($this->_contactID) {
6a488035
TO
220 $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
221 if ($this->_mid) {
222 $membership = new CRM_Member_DAO_Membership();
223 $membership->id = $this->_mid;
224
225 if ($membership->find(TRUE)) {
226 $this->_defaultMemTypeId = $membership->membership_type_id;
5b757295 227 if ($membership->contact_id != $this->_contactID) {
6fe8deba 228 $validMembership = FALSE;
6a488035 229 $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($this->_userID);
6fe8deba
DS
230 if (!empty($employers) && array_key_exists($membership->contact_id, $employers)) {
231 $this->_membershipContactID = $membership->contact_id;
232 $this->assign('membershipContactID', $this->_membershipContactID);
233 $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']);
234 $validMembership = TRUE;
235 } else {
51e89def
DS
236 $membershipType = new CRM_Member_BAO_MembershipType();
237 $membershipType->id = $membership->membership_type_id;
238 if ($membershipType->find(TRUE)) {
239 $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id);
240 if (array_key_exists($membership->contact_id, $permContacts)) {
241 $this->_membershipContactID = $membership->contact_id;
6fe8deba 242 $validMembership = TRUE;
51e89def
DS
243 }
244 }
6a488035 245 }
6fe8deba
DS
246 if (!$validMembership) {
247 CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert');
248 }
6a488035
TO
249 }
250 }
251 else {
252 CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert');
253 }
254 unset($membership);
255 }
256 }
257
258 // we do not want to display recently viewed items, so turn off
259 $this->assign('displayRecent', FALSE);
260 // Contribution page values are cleared from session, so can't use normal Printer Friendly view.
261 // Use Browser Print instead.
262 $this->assign('browserPrint', TRUE);
263
264 // action
265 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
266 $this->assign('action', $this->_action);
267
268 // current mode
269 $this->_mode = ($this->_action == 1024) ? 'test' : 'live';
270
271 $this->_values = $this->get('values');
272 $this->_fields = $this->get('fields');
273 $this->_bltID = $this->get('bltID');
274 $this->_paymentProcessor = $this->get('paymentProcessor');
275 $this->_priceSetId = $this->get('priceSetId');
276 $this->_priceSet = $this->get('priceSet');
277
278 if (!$this->_values) {
279 // get all the values from the dao object
280 $this->_values = array();
281 $this->_fields = array();
282
283 CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values);
284
285 // check if form is active
286 if (!CRM_Utils_Array::value('is_active', $this->_values)) {
287 // form is inactive, die a fatal death
288 CRM_Core_Error::fatal(ts('The page you requested is currently unavailable.'));
289 }
290
291 // also check for billing informatin
292 // get the billing location type
b2b0530a 293 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
294 // CRM-8108 remove ts around Billing location type
295 //$this->_bltID = array_search( ts('Billing'), $locationTypes );
296 $this->_bltID = array_search('Billing', $locationTypes);
297 if (!$this->_bltID) {
298 CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
299 }
300 $this->set('bltID', $this->_bltID);
301
302 // check for is_monetary status
303 $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values);
304 $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values);
305
306 //FIXME: to support multiple payment processors
307 if ($isMonetary &&
308 (!$isPayLater || CRM_Utils_Array::value('payment_processor', $this->_values))
309 ) {
310 $ppID = CRM_Utils_Array::value('payment_processor', $this->_values);
311 if (!$ppID) {
312 CRM_Core_Error::fatal(ts('A payment processor must be selected for this contribution page (contact the site administrator for assistance).'));
313 }
314
315 $ppIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID);
316 $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($ppIds, $this->_mode);
317
318 $this->set('paymentProcessors', $this->_paymentProcessors);
319
320 //set default payment processor
321 if (!empty($this->_paymentProcessors) && empty($this->_paymentProcessor)) {
322 foreach ($this->_paymentProcessors as $ppId => $values) {
323 if ($values['is_default'] == 1 || (count($this->_paymentProcessors) == 1)) {
324 $defaultProcessorId = $ppId;
325 break;
326 }
327 }
328 }
329
330 if (isset($defaultProcessorId)) {
331 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($defaultProcessorId, $this->_mode);
332 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor);
333 }
334
335 if (!CRM_Utils_System::isNull($this->_paymentProcessors)) {
336 foreach ($this->_paymentProcessors as $eachPaymentProcessor) {
337 // check selected payment processor is active
338 if (empty($eachPaymentProcessor)) {
339 CRM_Core_Error::fatal(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
340 }
341
342 // ensure that processor has a valid config
343 $this->_paymentObject = &CRM_Core_Payment::singleton($this->_mode, $eachPaymentProcessor, $this);
344 $error = $this->_paymentObject->checkConfig();
345 if (!empty($error)) {
346 CRM_Core_Error::fatal($error);
347 }
348 }
349 }
350 }
351
352 // get price info
353 // CRM-5095
9da8dc8c 354 CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
6a488035
TO
355
356 // this avoids getting E_NOTICE errors in php
357 $setNullFields = array(
358 'amount_block_is_active',
359 'honor_block_is_active',
360 'is_allow_other_amount',
361 'footer_text',
362 );
363 foreach ($setNullFields as $f) {
364 if (!isset($this->_values[$f])) {
365 $this->_values[$f] = NULL;
366 }
367 }
368
369 //check if Membership Block is enabled, if Membership Fields are included in profile
370 //get membership section for this contribution page
371 $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
372 $this->set('membershipBlock', $this->_membershipBlock);
373
374 if ($this->_values['custom_pre_id']) {
375 $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']);
376 }
377
378 if ($this->_values['custom_post_id']) {
379 $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']);
380 }
381
382 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
383 (isset($preProfileType) && $preProfileType == 'Membership')
384 ) &&
385 !$this->_membershipBlock['is_active']
386 ) {
387 CRM_Core_Error::fatal(ts('This page includes a Profile with Membership fields - but the Membership Block is NOT enabled. Please notify the site administrator.'));
388 }
389
390 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id);
391
392 if ($pledgeBlock) {
393 $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock);
394 $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock);
395 $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock);
396 $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock);
397
398 //set pledge id in values
399 $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this);
400
401 //authenticate pledge user for pledge payment.
402 if ($pledgeId) {
403 $this->_values['pledge_id'] = $pledgeId;
404
405 //lets override w/ pledge campaign.
406 $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge',
407 $pledgeId,
408 'campaign_id'
409 );
410 self::authenticatePledgeUser();
411 }
412 }
413 $this->set('values', $this->_values);
414 $this->set('fields', $this->_fields);
415 }
416
417 // Handle PCP
418 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
419 if ($pcpId) {
420 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
421 $this->_pcpId = $pcp['pcpId'];
422 $this->_pcpBlock = $pcp['pcpBlock'];
423 $this->_pcpInfo = $pcp['pcpInfo'];
424 }
425
426 // Link (button) for users to create their own Personal Campaign page
427 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) {
428 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
429 "action=add&reset=1&pageId={$this->_id}&component=contribute",
430 FALSE, NULL, TRUE
431 );
432 $this->assign('linkTextUrl', $linkTextUrl);
433 $this->assign('linkText', $linkText);
434 }
435
436 //set pledge block if block id is set
437 if (CRM_Utils_Array::value('pledge_block_id', $this->_values)) {
438 $this->assign('pledgeBlock', TRUE);
439 }
440
441 // check if one of the (amount , membership) bloks is active or not
442 $this->_membershipBlock = $this->get('membershipBlock');
443
444 if (!$this->_values['amount_block_is_active'] &&
445 !$this->_membershipBlock['is_active'] &&
446 !$this->_priceSetId
447 ) {
448 CRM_Core_Error::fatal(ts('The requested online contribution page is missing a required Contribution Amount section or Membership section or Price Set. Please check with the site administrator for assistance.'));
449 }
450
451 if ($this->_values['amount_block_is_active']) {
452 $this->set('amount_block_is_active', $this->_values['amount_block_is_active']);
453 }
454
455 $this->_contributeMode = $this->get('contributeMode');
456 $this->assign('contributeMode', $this->_contributeMode);
457
458 //assigning is_monetary and is_email_receipt to template
459 $this->assign('is_monetary', $this->_values['is_monetary']);
460 $this->assign('is_email_receipt', $this->_values['is_email_receipt']);
461 $this->assign('bltID', $this->_bltID);
462
463 //assign cancelSubscription URL to templates
464 $this->assign('cancelSubscriptionUrl',
465 CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values)
466 );
467
468 // assigning title to template in case someone wants to use it, also setting CMS page title
469 if ($this->_pcpId) {
470 $this->assign('title', $this->_pcpInfo['title']);
471 CRM_Utils_System::setTitle($this->_pcpInfo['title']);
472 }
473 else {
474 $this->assign('title', $this->_values['title']);
475 CRM_Utils_System::setTitle($this->_values['title']);
476 }
477 $this->_defaults = array();
478
479 $this->_amount = $this->get('amount');
480
481 //CRM-6907
482 $config = CRM_Core_Config::singleton();
483 $config->defaultCurrency = CRM_Utils_Array::value('currency',
484 $this->_values,
485 $config->defaultCurrency
486 );
487
488 //lets allow user to override campaign.
489 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
490 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
491 $this->_values['campaign_id'] = $campID;
492 }
493
494 //do check for cancel recurring and clean db, CRM-7696
495 if (CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject)) {
496 self::cancelRecurring();
497 }
498 }
499
500 /**
501 * set the default values
502 *
503 * @return void
504 * @access public
505 */
506 function setDefaultValues() {
507 return $this->_defaults;
508 }
509
510 /**
511 * assign the minimal set of variables to the template
512 *
513 * @return void
514 * @access public
515 */
516 function assignToTemplate() {
517 $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
518 if (CRM_Utils_Array::value('billing_middle_name', $this->_params)) {
519 $name .= " {$this->_params['billing_middle_name']}";
520 }
521 $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params);
522 $name = trim($name);
523 $this->assign('billingName', $name);
524 $this->set('name', $name);
525
526 $this->assign('paymentProcessor', $this->_paymentProcessor);
527 $vars = array(
528 'amount', 'currencyID',
529 'credit_card_type', 'trxn_id', 'amount_level',
530 );
531
532 $config = CRM_Core_Config::singleton();
533 if (isset($this->_values['is_recur']) &&
f92fc7eb 534 CRM_Utils_Array::value('is_recur', $this->_paymentProcessor)
6a488035
TO
535 ) {
536 $this->assign('is_recur_enabled', 1);
537 $vars = array_merge($vars, array(
538 'is_recur', 'frequency_interval', 'frequency_unit',
539 'installments',
540 ));
541 }
542
543 if (in_array('CiviPledge', $config->enableComponents) &&
544 CRM_Utils_Array::value('is_pledge', $this->_params) == 1
545 ) {
546 $this->assign('pledge_enabled', 1);
547
548 $vars = array_merge($vars, array(
549 'is_pledge',
550 'pledge_frequency_interval',
551 'pledge_frequency_unit',
552 'pledge_installments',
553 ));
554 }
555
556 if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) {
557 $this->_params['amount_level'] = '';
558 }
559
560 foreach ($vars as $v) {
3fb990f4 561 if (isset($this->_params[$v])) {
6a488035
TO
562 if ($v == 'frequency_unit' || $v == 'pledge_frequency_unit') {
563 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
564 if (array_key_exists($this->_params[$v], $frequencyUnits)) {
565 $this->_params[$v] = $frequencyUnits[$this->_params[$v]];
566 }
567 }
735fe42d
PJ
568 if ($v == "amount" && $this->_params[$v] === 0) {
569 $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE);
3fb990f4 570 }
6a488035
TO
571 $this->assign($v, $this->_params[$v]);
572 }
573 }
574
575 // assign the address formatted up for display
576 $addressParts = array(
577 "street_address-{$this->_bltID}",
578 "city-{$this->_bltID}",
579 "postal_code-{$this->_bltID}",
580 "state_province-{$this->_bltID}",
581 "country-{$this->_bltID}",
582 );
583
584 $addressFields = array();
585 foreach ($addressParts as $part) {
586 list($n, $id) = explode('-', $part);
587 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params);
588 }
589
590 $this->assign('address', CRM_Utils_Address::format($addressFields));
591
592 if (CRM_Utils_Array::value('hidden_onbehalf_profile', $this->_params)) {
593 $this->assign('onBehalfName', $this->_params['organization_name']);
594 $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
595 $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
596 }
597
598 //fix for CRM-3767
599 $assignCCInfo = FALSE;
600 if ($this->_amount > 0.0) {
601 $assignCCInfo = TRUE;
602 }
603 elseif (CRM_Utils_array::value('selectMembership', $this->_params)) {
604 $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
605 if ($memFee > 0.0) {
606 $assignCCInfo = TRUE;
607 }
608 }
609
610 if ($this->_contributeMode == 'direct' && $assignCCInfo) {
f92fc7eb
CW
611 if ($this->_paymentProcessor &&
612 $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
613 ) {
6a488035
TO
614 $this->assign('payment_type', $this->_paymentProcessor['payment_type']);
615 $this->assign('account_holder', $this->_params['account_holder']);
616 $this->assign('bank_identification_number', $this->_params['bank_identification_number']);
617 $this->assign('bank_name', $this->_params['bank_name']);
618 $this->assign('bank_account_number', $this->_params['bank_account_number']);
619 }
620 else {
621 $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params));
622 $date = CRM_Utils_Date::mysqlToIso($date);
623 $this->assign('credit_card_exp_date', $date);
624 $this->assign('credit_card_number',
625 CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params))
626 );
627 }
628 }
629
630 $this->assign('email',
631 $this->controller->exportValue('Main', "email-{$this->_bltID}")
632 );
633
634 // also assign the receipt_text
635 if (isset($this->_values['receipt_text'])) {
636 $this->assign('receipt_text', $this->_values['receipt_text']);
637 }
638 }
639
640 /**
641 * Function to add the custom fields
642 *
643 * @return None
644 * @access public
645 */
646 function buildCustom($id, $name, $viewOnly = FALSE, $onBehalf = FALSE, $fieldTypes = NULL) {
647 $stateCountryMap = array();
648
649 if ($id) {
da8d9879 650 $contactID = $this->getContactID();
6a488035
TO
651
652 // we don't allow conflicting fields to be
653 // configured via profile - CRM 2100
654 $fieldsToIgnore = array(
655 'receive_date' => 1,
656 'trxn_id' => 1,
657 'invoice_id' => 1,
658 'net_amount' => 1,
659 'fee_amount' => 1,
660 'non_deductible_amount' => 1,
661 'total_amount' => 1,
662 'amount_level' => 1,
663 'contribution_status_id' => 1,
664 'payment_instrument' => 1,
665 'check_number' => 1,
666 'financial_type' => 1,
667 );
668
669 $fields = NULL;
670 if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
671 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
672 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
673 );
674 }
675 else {
676 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
677 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
678 );
679 }
680
681 if ($fields) {
682 // unset any email-* fields since we already collect it, CRM-2888
683 foreach (array_keys($fields) as $fieldName) {
684 if (substr($fieldName, 0, 6) == 'email-') {
685 unset($fields[$fieldName]);
686 }
687 }
688
689 if (array_intersect_key($fields, $fieldsToIgnore)) {
690 $fields = array_diff_key($fields, $fieldsToIgnore);
691 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
692 }
693
694 $fields = array_diff_assoc($fields, $this->_fields);
695
696 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
697 $addCaptcha = FALSE;
698 foreach ($fields as $key => $field) {
699 if ($viewOnly &&
700 isset($field['data_type']) &&
701 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
702 ) {
703 // ignore file upload fields
704 continue;
705 }
706
707 list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
708 if ($prefixName == 'state_province' || $prefixName == 'country' || $prefixName == 'county') {
709 if (!array_key_exists($index, $stateCountryMap)) {
710 $stateCountryMap[$index] = array();
711 }
712 $stateCountryMap[$index][$prefixName] = $key;
713 }
714
715 if ($onBehalf) {
716 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
717 CRM_Core_BAO_UFGroup::buildProfile(
718 $this,
719 $field,
720 CRM_Profile_Form::MODE_CREATE,
721 $contactID,
722 TRUE
723 );
724 $this->_fields['onbehalf'][$key] = $field;
725 }
726 else {
727 unset($fields[$key]);
728 }
729 }
730 else {
731 CRM_Core_BAO_UFGroup::buildProfile(
732 $this,
733 $field,
734 CRM_Profile_Form::MODE_CREATE,
735 $contactID,
736 TRUE
737 );
738 $this->_fields[$key] = $field;
739 }
71fc6ea4
DG
740 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
741 if ($field['add_captcha'] && !$this->_userID) {
6a488035
TO
742 $addCaptcha = TRUE;
743 }
744 }
745
746 $this->assign($name, $fields);
747
748 CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
749
750 if ($addCaptcha && !$viewOnly) {
751 $captcha = CRM_Utils_ReCAPTCHA::singleton();
752 $captcha->add($this);
753 $this->assign('isCaptcha', TRUE);
754 }
755 }
756 }
757 }
758
759 function checkTemplateFileExists($suffix = NULL) {
760 if ($this->_id) {
761 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
762 $template = CRM_Core_Form::getTemplate();
763 if ($template->template_exists($templateFile)) {
764 return $templateFile;
765 }
766 }
767 return NULL;
768 }
769
770 function getTemplateFileName() {
771 $fileName = $this->checkTemplateFileExists();
772 return $fileName ? $fileName : parent::getTemplateFileName();
773 }
774
775 function overrideExtraTemplateFileName() {
776 $fileName = $this->checkTemplateFileExists('extra.');
777 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
778 }
779
780 /**
781 * Function to authenticate pledge user during online payment.
782 *
783 * @access public
784 *
785 * @return None
786 */
787 public function authenticatePledgeUser() {
788 //get the userChecksum and contact id
789 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
790 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
791
792 //get pledge status and contact id
793 $pledgeValues = array();
794 $pledgeParams = array('id' => $this->_values['pledge_id']);
795 $returnProperties = array('contact_id', 'status_id');
796 CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
797
798 //get all status
799 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
800 $validStatus = array(array_search('Pending', $allStatus),
801 array_search('In Progress', $allStatus),
802 array_search('Overdue', $allStatus),
803 );
804
805 $validUser = FALSE;
806 if ($this->_userID &&
807 $this->_userID == $pledgeValues['contact_id']
808 ) {
809 //check for authenticated user.
810 $validUser = TRUE;
811 }
812 elseif ($userChecksum && $pledgeValues['contact_id']) {
813 //check for anonymous user.
814 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum);
815
816 //make sure cid is same as pledge contact id
817 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
818 $validUser = FALSE;
819 }
820 }
821
822 if (!$validUser) {
823 CRM_Core_Error::fatal(ts("Oops. It looks like you have an incorrect or incomplete link (URL). Please make sure you've copied the entire link, and try again. Contact the site administrator if this error persists."));
824 }
825
826 //check for valid pledge status.
827 if (!in_array($pledgeValues['status_id'], $validStatus)) {
828 CRM_Core_Error::fatal(ts('Oops. You cannot make a payment for this pledge - pledge status is %1.', array(1 => CRM_Utils_Array::value($pledgeValues['status_id'], $allStatus))));
829 }
830 }
831
832 /**
833 * In case user cancel recurring contribution,
834 * When we get the control back from payment gate way
835 * lets delete the recurring and related contribution.
836 *
837 **/
838 public function cancelRecurring() {
839 $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject);
840 if ($isCancel) {
841 $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject);
842 $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject);
843 //clean db for recurring contribution.
844 if ($isRecur && $recurId) {
845 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
846 }
847 $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject);
848 if ($contribId) {
849 CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
850 }
851 }
852 }
853}
854