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