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