Merge pull request #6254 from yashodha/CRM-16853.fixes
[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;
1b9f9ca3 76
6a488035
TO
77 public $_paymentObject = NULL;
78
79 /**
80 * The membership block for this page
81 *
82 * @var array
6a488035
TO
83 */
84 public $_membershipBlock = NULL;
85
f64a217a
EM
86 /**
87 * Does this form support a separate membership payment
88 * @var bool
89 */
90 protected $_separateMembershipPayment;
6a488035 91 /**
100fef9d 92 * The default values for the form
6a488035
TO
93 *
94 * @var array
6a488035
TO
95 */
96 protected $_defaults;
97
98 /**
99 * The params submitted by the form and computed by the app
100 *
101 * @var array
6a488035
TO
102 */
103 public $_params;
104
105 /**
106 * The fields involved in this contribution page
107 *
108 * @var array
6a488035 109 */
532ee86f 110 public $_fields = array();
6a488035
TO
111
112 /**
f92d1e2a 113 * The billing location id for this contribution page.
6a488035
TO
114 *
115 * @var int
6a488035
TO
116 */
117 public $_bltID;
118
119 /**
120 * Cache the amount to make things easier
121 *
122 * @var float
6a488035
TO
123 */
124 public $_amount;
125
126 /**
100fef9d 127 * Pcp id
6a488035
TO
128 *
129 * @var integer
6a488035
TO
130 */
131 public $_pcpId;
132
133 /**
100fef9d 134 * Pcp block
6a488035
TO
135 *
136 * @var array
6a488035
TO
137 */
138 public $_pcpBlock;
139
140 /**
100fef9d 141 * Pcp info
6a488035
TO
142 *
143 * @var array
6a488035
TO
144 */
145 public $_pcpInfo;
146
5b757295 147 /**
148 * The contact id of the person for whom membership is being added or renewed based on the cid in the url,
149 * checksum, or session
0e5e0c2e 150 * @var int
5b757295 151 */
0e5e0c2e 152 public $_contactID;
5b757295 153
6a488035
TO
154 protected $_userID;
155
156 /**
100fef9d 157 * The Membership ID for membership renewal
6a488035
TO
158 *
159 * @var int
6a488035
TO
160 */
161 public $_membershipId;
162
163 /**
164 * Price Set ID, if the new price set method is used
165 *
166 * @var int
6a488035
TO
167 */
168 public $_priceSetId;
169
170 /**
171 * Array of fields for the price set
172 *
173 * @var array
6a488035
TO
174 */
175 public $_priceSet;
176
177 public $_action;
178
874c9be7 179 /**
353ffa53
TO
180 * Is honor block is enabled for this contribution?
181 *
182 * @var boolean
183 */
8af73472 184 public $_honor_block_is_active = FALSE;
185
dbddfb08
EM
186 /**
187 * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
188 * @var string
189 */
190 public $_contributeMode;
191
192 /**
100fef9d 193 * Contribution page supports memberships
dbddfb08
EM
194 * @var boolean
195 */
196 public $_useForMember;
8ae4d0d3 197
198 public $_isBillingAddressRequiredForPayLater;
353ffa53 199
6a488035 200 /**
fe482240 201 * Set variables up before form is built.
6a488035 202 *
7fe37828
EM
203 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
204 * @throws \Exception
6a488035
TO
205 */
206 public function preProcess() {
6a488035
TO
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
a6c15c46 217 $this->_userID = CRM_Core_Session::singleton()->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
6a488035 312 if ($isMonetary &&
8cc574cf 313 (!$isPayLater || !empty($this->_values['payment_processor']))
6a488035 314 ) {
a6c15c46
EM
315 $this->_paymentProcessorIDs = explode(
316 CRM_Core_DAO::VALUE_SEPARATOR,
317 CRM_Utils_Array::value('payment_processor', $this->_values)
1b9f9ca3 318 );
f92d1e2a 319
1b9f9ca3 320 $this->assignPaymentProcessor();
6a488035
TO
321 }
322
323 // get price info
324 // CRM-5095
9da8dc8c 325 CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
6a488035
TO
326
327 // this avoids getting E_NOTICE errors in php
328 $setNullFields = array(
329 'amount_block_is_active',
6a488035
TO
330 'is_allow_other_amount',
331 'footer_text',
332 );
333 foreach ($setNullFields as $f) {
334 if (!isset($this->_values[$f])) {
335 $this->_values[$f] = NULL;
336 }
337 }
338
339 //check if Membership Block is enabled, if Membership Fields are included in profile
340 //get membership section for this contribution page
341 $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
342 $this->set('membershipBlock', $this->_membershipBlock);
343
344 if ($this->_values['custom_pre_id']) {
345 $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']);
346 }
347
348 if ($this->_values['custom_post_id']) {
349 $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']);
350 }
351
352 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
353 (isset($preProfileType) && $preProfileType == 'Membership')
354 ) &&
355 !$this->_membershipBlock['is_active']
356 ) {
357 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.'));
358 }
359
360 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id);
361
362 if ($pledgeBlock) {
363 $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock);
364 $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock);
365 $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock);
366 $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock);
367
368 //set pledge id in values
369 $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this);
370
371 //authenticate pledge user for pledge payment.
372 if ($pledgeId) {
373 $this->_values['pledge_id'] = $pledgeId;
374
375 //lets override w/ pledge campaign.
376 $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge',
377 $pledgeId,
378 'campaign_id'
379 );
380 self::authenticatePledgeUser();
381 }
382 }
383 $this->set('values', $this->_values);
384 $this->set('fields', $this->_fields);
385 }
386
387 // Handle PCP
388 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
389 if ($pcpId) {
353ffa53
TO
390 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
391 $this->_pcpId = $pcp['pcpId'];
6a488035 392 $this->_pcpBlock = $pcp['pcpBlock'];
353ffa53 393 $this->_pcpInfo = $pcp['pcpInfo'];
6a488035
TO
394 }
395
396 // Link (button) for users to create their own Personal Campaign page
397 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) {
398 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
399 "action=add&reset=1&pageId={$this->_id}&component=contribute",
400 FALSE, NULL, TRUE
401 );
402 $this->assign('linkTextUrl', $linkTextUrl);
403 $this->assign('linkText', $linkText);
404 }
405
406 //set pledge block if block id is set
a7488080 407 if (!empty($this->_values['pledge_block_id'])) {
6a488035
TO
408 $this->assign('pledgeBlock', TRUE);
409 }
410
f92d1e2a 411 // check if one of the (amount , membership) blocks is active or not.
6a488035
TO
412 $this->_membershipBlock = $this->get('membershipBlock');
413
414 if (!$this->_values['amount_block_is_active'] &&
415 !$this->_membershipBlock['is_active'] &&
416 !$this->_priceSetId
417 ) {
418 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.'));
419 }
420
421 if ($this->_values['amount_block_is_active']) {
422 $this->set('amount_block_is_active', $this->_values['amount_block_is_active']);
423 }
424
425 $this->_contributeMode = $this->get('contributeMode');
426 $this->assign('contributeMode', $this->_contributeMode);
427
428 //assigning is_monetary and is_email_receipt to template
429 $this->assign('is_monetary', $this->_values['is_monetary']);
430 $this->assign('is_email_receipt', $this->_values['is_email_receipt']);
431 $this->assign('bltID', $this->_bltID);
432
433 //assign cancelSubscription URL to templates
434 $this->assign('cancelSubscriptionUrl',
435 CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values)
436 );
437
438 // assigning title to template in case someone wants to use it, also setting CMS page title
439 if ($this->_pcpId) {
440 $this->assign('title', $this->_pcpInfo['title']);
441 CRM_Utils_System::setTitle($this->_pcpInfo['title']);
442 }
443 else {
444 $this->assign('title', $this->_values['title']);
445 CRM_Utils_System::setTitle($this->_values['title']);
446 }
447 $this->_defaults = array();
448
449 $this->_amount = $this->get('amount');
450
451 //CRM-6907
452 $config = CRM_Core_Config::singleton();
453 $config->defaultCurrency = CRM_Utils_Array::value('currency',
454 $this->_values,
455 $config->defaultCurrency
456 );
457
458 //lets allow user to override campaign.
459 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
460 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
461 $this->_values['campaign_id'] = $campID;
462 }
463
464 //do check for cancel recurring and clean db, CRM-7696
465 if (CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject)) {
466 self::cancelRecurring();
467 }
8ae4d0d3 468
469 // check if billing block is required for pay later
470 if (CRM_Utils_Array::value('is_pay_later', $this->_values)) {
471 $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values);
472 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
473 }
6a488035
TO
474 }
475
476 /**
fe482240 477 * Set the default values.
6a488035 478 */
00be9182 479 public function setDefaultValues() {
6a488035
TO
480 return $this->_defaults;
481 }
482
483 /**
fe482240 484 * Assign the minimal set of variables to the template.
6a488035 485 */
00be9182 486 public function assignToTemplate() {
6a488035 487 $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
a7488080 488 if (!empty($this->_params['billing_middle_name'])) {
6a488035
TO
489 $name .= " {$this->_params['billing_middle_name']}";
490 }
491 $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params);
492 $name = trim($name);
493 $this->assign('billingName', $name);
494 $this->set('name', $name);
495
496 $this->assign('paymentProcessor', $this->_paymentProcessor);
497 $vars = array(
353ffa53
TO
498 'amount',
499 'currencyID',
500 'credit_card_type',
501 'trxn_id',
502 'amount_level',
6a488035
TO
503 );
504
505 $config = CRM_Core_Config::singleton();
8cc574cf 506 if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) {
6a488035
TO
507 $this->assign('is_recur_enabled', 1);
508 $vars = array_merge($vars, array(
353ffa53
TO
509 'is_recur',
510 'frequency_interval',
511 'frequency_unit',
512 'installments',
513 ));
6a488035
TO
514 }
515
516 if (in_array('CiviPledge', $config->enableComponents) &&
517 CRM_Utils_Array::value('is_pledge', $this->_params) == 1
518 ) {
519 $this->assign('pledge_enabled', 1);
520
521 $vars = array_merge($vars, array(
522 'is_pledge',
353ffa53
TO
523 'pledge_frequency_interval',
524 'pledge_frequency_unit',
525 'pledge_installments',
526 ));
6a488035
TO
527 }
528
529 if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) {
530 $this->_params['amount_level'] = '';
531 }
532
533 foreach ($vars as $v) {
3fb990f4 534 if (isset($this->_params[$v])) {
735fe42d
PJ
535 if ($v == "amount" && $this->_params[$v] === 0) {
536 $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE);
3fb990f4 537 }
6a488035
TO
538 $this->assign($v, $this->_params[$v]);
539 }
540 }
541
542 // assign the address formatted up for display
543 $addressParts = array(
544 "street_address-{$this->_bltID}",
545 "city-{$this->_bltID}",
546 "postal_code-{$this->_bltID}",
547 "state_province-{$this->_bltID}",
548 "country-{$this->_bltID}",
549 );
550
551 $addressFields = array();
552 foreach ($addressParts as $part) {
553 list($n, $id) = explode('-', $part);
554 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params);
555 }
556
557 $this->assign('address', CRM_Utils_Address::format($addressFields));
558
a7488080 559 if (!empty($this->_params['hidden_onbehalf_profile'])) {
6a488035
TO
560 $this->assign('onBehalfName', $this->_params['organization_name']);
561 $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
562 $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
563 }
564
565 //fix for CRM-3767
566 $assignCCInfo = FALSE;
567 if ($this->_amount > 0.0) {
568 $assignCCInfo = TRUE;
569 }
a7488080 570 elseif (!empty($this->_params['selectMembership'])) {
6a488035
TO
571 $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
572 if ($memFee > 0.0) {
573 $assignCCInfo = TRUE;
574 }
575 }
576
577 if ($this->_contributeMode == 'direct' && $assignCCInfo) {
f92fc7eb
CW
578 if ($this->_paymentProcessor &&
579 $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
580 ) {
6a488035
TO
581 $this->assign('account_holder', $this->_params['account_holder']);
582 $this->assign('bank_identification_number', $this->_params['bank_identification_number']);
583 $this->assign('bank_name', $this->_params['bank_name']);
584 $this->assign('bank_account_number', $this->_params['bank_account_number']);
585 }
586 else {
587 $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params));
588 $date = CRM_Utils_Date::mysqlToIso($date);
589 $this->assign('credit_card_exp_date', $date);
590 $this->assign('credit_card_number',
353ffa53 591 CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params))
6a488035
TO
592 );
593 }
594 }
595
596 $this->assign('email',
597 $this->controller->exportValue('Main', "email-{$this->_bltID}")
598 );
599
600 // also assign the receipt_text
601 if (isset($this->_values['receipt_text'])) {
602 $this->assign('receipt_text', $this->_values['receipt_text']);
603 }
604 }
605
606 /**
fe482240 607 * Add the custom fields.
6a488035 608 *
100fef9d
CW
609 * @param int $id
610 * @param string $name
f4aaa82a
EM
611 * @param bool $viewOnly
612 * @param null $profileContactType
f92d1e2a 613 * @param array $fieldTypes
6a488035 614 */
00be9182 615 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
6a488035 616 if ($id) {
da8d9879 617 $contactID = $this->getContactID();
6a488035
TO
618
619 // we don't allow conflicting fields to be
620 // configured via profile - CRM 2100
621 $fieldsToIgnore = array(
622 'receive_date' => 1,
623 'trxn_id' => 1,
624 'invoice_id' => 1,
625 'net_amount' => 1,
626 'fee_amount' => 1,
627 'non_deductible_amount' => 1,
628 'total_amount' => 1,
629 'amount_level' => 1,
630 'contribution_status_id' => 1,
631 'payment_instrument' => 1,
632 'check_number' => 1,
633 'financial_type' => 1,
634 );
635
636 $fields = NULL;
637 if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
638 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
639 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
640 );
641 }
642 else {
643 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
644 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
645 );
646 }
647
648 if ($fields) {
649 // unset any email-* fields since we already collect it, CRM-2888
650 foreach (array_keys($fields) as $fieldName) {
8af73472 651 if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') {
6a488035
TO
652 unset($fields[$fieldName]);
653 }
654 }
655
656 if (array_intersect_key($fields, $fieldsToIgnore)) {
657 $fields = array_diff_key($fields, $fieldsToIgnore);
658 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
659 }
660
661 $fields = array_diff_assoc($fields, $this->_fields);
662
663 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
664 $addCaptcha = FALSE;
665 foreach ($fields as $key => $field) {
666 if ($viewOnly &&
667 isset($field['data_type']) &&
668 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
669 ) {
670 // ignore file upload fields
671 continue;
672 }
673
133e2c99 674 if ($profileContactType) {
675 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
353ffa53
TO
676 $honoreeNamefields = array(
677 'prefix_id',
678 'first_name',
679 'last_name',
680 'suffix_id',
681 'organization_name',
389bcebf 682 'household_name',
353ffa53 683 );
133e2c99 684 if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) {
685 unset($fields[$field['name']]);
686 continue;
687 }
6a488035
TO
688 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
689 CRM_Core_BAO_UFGroup::buildProfile(
690 $this,
691 $field,
692 CRM_Profile_Form::MODE_CREATE,
693 $contactID,
133e2c99 694 TRUE,
695 $profileContactType
6a488035 696 );
133e2c99 697 $this->_fields[$profileContactType][$key] = $field;
6a488035
TO
698 }
699 else {
700 unset($fields[$key]);
701 }
702 }
703 else {
704 CRM_Core_BAO_UFGroup::buildProfile(
705 $this,
706 $field,
707 CRM_Profile_Form::MODE_CREATE,
708 $contactID,
709 TRUE
710 );
711 $this->_fields[$key] = $field;
712 }
71fc6ea4
DG
713 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
714 if ($field['add_captcha'] && !$this->_userID) {
6a488035
TO
715 $addCaptcha = TRUE;
716 }
717 }
718
719 $this->assign($name, $fields);
720
6a488035
TO
721 if ($addCaptcha && !$viewOnly) {
722 $captcha = CRM_Utils_ReCAPTCHA::singleton();
723 $captcha->add($this);
724 $this->assign('isCaptcha', TRUE);
725 }
726 }
727 }
728 }
729
f4aaa82a 730 /**
fe482240 731 * Check template file exists.
f92d1e2a
EM
732 *
733 * @param string $suffix
f4aaa82a
EM
734 *
735 * @return null|string
736 */
00be9182 737 public function checkTemplateFileExists($suffix = NULL) {
6a488035
TO
738 if ($this->_id) {
739 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
740 $template = CRM_Core_Form::getTemplate();
741 if ($template->template_exists($templateFile)) {
742 return $templateFile;
743 }
744 }
745 return NULL;
746 }
747
186c9c17 748 /**
fe482240 749 * Use the form name to create the tpl file name.
186c9c17
EM
750 *
751 * @return string
186c9c17 752 */
00be9182 753 public function getTemplateFileName() {
6a488035
TO
754 $fileName = $this->checkTemplateFileExists();
755 return $fileName ? $fileName : parent::getTemplateFileName();
756 }
757
186c9c17 758 /**
f92d1e2a
EM
759 * Add the extra.tpl in.
760 *
186c9c17 761 * Default extra tpl file basically just replaces .tpl with .extra.tpl
f92d1e2a 762 * i.e. we do not override - why isn't this done at the CRM_Core_Form level?
186c9c17
EM
763 *
764 * @return string
186c9c17 765 */
00be9182 766 public function overrideExtraTemplateFileName() {
6a488035
TO
767 $fileName = $this->checkTemplateFileExists('extra.');
768 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
769 }
770
771 /**
100fef9d 772 * Authenticate pledge user during online payment.
6a488035
TO
773 */
774 public function authenticatePledgeUser() {
775 //get the userChecksum and contact id
776 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
777 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
778
779 //get pledge status and contact id
353ffa53
TO
780 $pledgeValues = array();
781 $pledgeParams = array('id' => $this->_values['pledge_id']);
6a488035
TO
782 $returnProperties = array('contact_id', 'status_id');
783 CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
784
785 //get all status
786 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
353ffa53
TO
787 $validStatus = array(
788 array_search('Pending', $allStatus),
6a488035
TO
789 array_search('In Progress', $allStatus),
790 array_search('Overdue', $allStatus),
791 );
792
793 $validUser = FALSE;
794 if ($this->_userID &&
795 $this->_userID == $pledgeValues['contact_id']
796 ) {
797 //check for authenticated user.
798 $validUser = TRUE;
799 }
800 elseif ($userChecksum && $pledgeValues['contact_id']) {
801 //check for anonymous user.
802 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum);
803
804 //make sure cid is same as pledge contact id
805 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
806 $validUser = FALSE;
807 }
808 }
809
810 if (!$validUser) {
811 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."));
812 }
813
814 //check for valid pledge status.
815 if (!in_array($pledgeValues['status_id'], $validStatus)) {
816 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))));
817 }
818 }
819
820 /**
f92d1e2a
EM
821 * Cancel recurring contributions.
822 *
6a488035
TO
823 * In case user cancel recurring contribution,
824 * When we get the control back from payment gate way
825 * lets delete the recurring and related contribution.
389bcebf 826 */
6a488035
TO
827 public function cancelRecurring() {
828 $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject);
829 if ($isCancel) {
830 $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject);
831 $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject);
832 //clean db for recurring contribution.
833 if ($isRecur && $recurId) {
834 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
835 }
836 $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject);
837 if ($contribId) {
838 CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
839 }
840 }
841 }
96025800 842
42e3a033
EM
843 /**
844 * Build Membership Block in Contribution Pages.
845 *
42e3a033
EM
846 * @param int $cid
847 * Contact checked for having a current membership for a particular membership.
a46bfec1
EM
848 * @param bool $isContributionMainPage
849 * Is this the main page? If so add form input fields.
850 * (or better yet don't have this functionality in a function shared with forms that don't share it).
42e3a033
EM
851 * @param int $selectedMembershipTypeID
852 * Selected membership id.
853 * @param bool $thankPage
854 * Thank you page.
855 * @param null $isTest
856 *
857 * @return bool
858 * Is this a separate membership payment
859 */
860 protected function buildMembershipBlock(
861 $cid,
a46bfec1 862 $isContributionMainPage = FALSE,
42e3a033
EM
863 $selectedMembershipTypeID = NULL,
864 $thankPage = FALSE,
865 $isTest = NULL
866 ) {
867
868 $separateMembershipPayment = FALSE;
869 if ($this->_membershipBlock) {
870 $this->_currentMemberships = array();
871
42e3a033
EM
872 $membershipTypeIds = $membershipTypes = $radio = array();
873 $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE;
874
875 $allowAutoRenewMembership = $autoRenewOption = FALSE;
876 $autoRenewMembershipTypeOptions = array();
877
a46bfec1 878 $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock);
42e3a033
EM
879
880 if ($membershipPriceset) {
881 foreach ($this->_priceSet['fields'] as $pField) {
882 if (empty($pField['options'])) {
883 continue;
884 }
885 foreach ($pField['options'] as $opId => $opValues) {
886 if (empty($opValues['membership_type_id'])) {
887 continue;
888 }
889 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
890 }
891 }
892 }
a46bfec1
EM
893 elseif (!empty($this->_membershipBlock['membership_types'])) {
894 $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']);
42e3a033
EM
895 }
896
897 if (!empty($membershipTypeIds)) {
898 //set status message if wrong membershipType is included in membershipBlock
899 if (isset($this->_mid) && !$membershipPriceset) {
900 $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
901 $this->_mid,
902 'membership_type_id'
903 );
904 if (!in_array($membershipTypeID, $membershipTypeIds)) {
905 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('Invalid Membership'), 'error');
906 }
907 }
908
4c7b8a7d 909 $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds);
42e3a033
EM
910 $this->_membershipTypeValues = $membershipTypeValues;
911 $endDate = NULL;
912 foreach ($membershipTypeIds as $value) {
913 $memType = $membershipTypeValues[$value];
914 if ($selectedMembershipTypeID != NULL) {
915 if ($memType['id'] == $selectedMembershipTypeID) {
916 $this->assign('minimum_fee',
917 CRM_Utils_Array::value('minimum_fee', $memType)
918 );
919 $this->assign('membership_name', $memType['name']);
920 if (!$thankPage && $cid) {
921 $membership = new CRM_Member_DAO_Membership();
922 $membership->contact_id = $cid;
923 $membership->membership_type_id = $memType['id'];
924 if ($membership->find(TRUE)) {
925 $this->assign('renewal_mode', TRUE);
926 $memType['current_membership'] = $membership->end_date;
927 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
928 }
929 }
930 $membershipTypes[] = $memType;
931 }
932 }
933 elseif ($memType['is_active']) {
934 $javascriptMethod = NULL;
935 $allowAutoRenewOpt = 1;
936 if (is_array($this->_paymentProcessors)) {
937 foreach ($this->_paymentProcessors as $id => $val) {
938 if (!$val['is_recur']) {
939 $allowAutoRenewOpt = 0;
940 continue;
941 }
942 }
943 }
944
945 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
946 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));;
947
948 if ($allowAutoRenewOpt) {
949 $allowAutoRenewMembership = TRUE;
950 }
951
952 //add membership type.
953 $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL,
954 $memType['id'], $javascriptMethod
955 );
956 if ($cid) {
957 $membership = new CRM_Member_DAO_Membership();
958 $membership->contact_id = $cid;
959 $membership->membership_type_id = $memType['id'];
960
961 //show current membership, skip pending and cancelled membership records,
962 //because we take first membership record id for renewal
963 $membership->whereAdd('status_id != 5 AND status_id !=6');
964
965 if (!is_null($isTest)) {
966 $membership->is_test = $isTest;
967 }
968
969 //CRM-4297
970 $membership->orderBy('end_date DESC');
971
972 if ($membership->find(TRUE)) {
973 if (!$membership->end_date) {
974 unset($radio[$memType['id']]);
975 $this->assign('islifetime', TRUE);
976 continue;
977 }
978 $this->assign('renewal_mode', TRUE);
979 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
980 $memType['current_membership'] = $membership->end_date;
981 if (!$endDate) {
982 $endDate = $memType['current_membership'];
983 $this->_defaultMemTypeId = $memType['id'];
984 }
985 if ($memType['current_membership'] < $endDate) {
986 $endDate = $memType['current_membership'];
987 $this->_defaultMemTypeId = $memType['id'];
988 }
989 }
990 }
991 $membershipTypes[] = $memType;
992 }
993 }
994 }
995
a46bfec1
EM
996 $this->assign('membershipBlock', $this->_membershipBlock);
997 $this->assign('showRadio', $isContributionMainPage);
998 $this->assign('membershipTypes', $membershipTypes);
999 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
1000 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
1001 //give preference to user submitted auto_renew value.
1002 $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE;
1003 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
1004
1005 if ($isContributionMainPage) {
42e3a033 1006 if (!$membershipPriceset) {
a46bfec1 1007 if (!$this->_membershipBlock['is_required']) {
42e3a033
EM
1008 $this->assign('showRadioNoThanks', TRUE);
1009 $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
1010 $this->addGroup($radio, 'selectMembership', NULL);
1011 }
a46bfec1 1012 elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
42e3a033
EM
1013 $temp = array_keys($radio);
1014 $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
1015 $this->assign('singleMembership', TRUE);
1016 $this->assign('showRadio', FALSE);
1017 }
1018 else {
1019 $this->addGroup($radio, 'selectMembership', NULL);
1020 }
1021
1022 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
1023 }
1024 else {
1025 $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId);
1026 $this->assign('autoRenewOption', $autoRenewOption);
1027 }
1028
1029 if (!$this->_values['is_pay_later'] && is_array($this->_paymentProcessors) && ($allowAutoRenewMembership || $autoRenewOption)) {
1030 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
1031 }
1032
1033 }
42e3a033
EM
1034 }
1035
1036 return $separateMembershipPayment;
1037 }
1038
6a488035 1039}