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