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