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