Merge pull request #1176 from pratik-joshi/CRM-13025
[civicrm-core.git] / CRM / Contribute / Form / ContributionBase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
155 protected $_userID;
156
157 /**
158 * the Membership ID for membership renewal
159 *
160 * @var int
161 * @public
162 */
163 public $_membershipId;
164
165 /**
166 * Price Set ID, if the new price set method is used
167 *
168 * @var int
169 * @protected
170 */
171 public $_priceSetId;
172
173 /**
174 * Array of fields for the price set
175 *
176 * @var array
177 * @protected
178 */
179 public $_priceSet;
180
181 public $_action;
182
183 /**
184 * Function to set variables up before form is built
185 *
186 * @return void
187 * @access public
188 */
189 public function preProcess() {
190 $config = CRM_Core_Config::singleton();
191 $session = CRM_Core_Session::singleton();
192
193 // current contribution page id
194 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
195 if (!$this->_id) {
196 $pastContributionID = $session->get('pastContributionID');
197 if (!$pastContributionID) {
198 CRM_Core_Error::fatal(ts('We can\'t load the requested web page due to an incomplete link. This can be caused by using your browser\'s Back button or by using an incomplete or invalid link.'));
199 }
200 else {
201 CRM_Core_Error::fatal(ts('An error occurred during form submission. This page requires form data to be submitted for processing and no form data was submitted or processed. We are sorry for any inconvience. Please click <a href=\'%1\'>here</a> to visit the contribution page and re-start the contribution process.', array(1 => CRM_Utils_System::url('civicrm/contribute/transact', 'reset=1&id=' . $pastContributionID))));
202 }
203 }
204 else {
205 $session->set('pastContributionID', $this->_id);
206 }
207
208 $this->_userID = $session->get('userID');
209 $this->_mid = NULL;
210 if ($this->_userID) {
211 $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
212 if ($this->_mid) {
213 $membership = new CRM_Member_DAO_Membership();
214 $membership->id = $this->_mid;
215
216 if ($membership->find(TRUE)) {
217 $this->_defaultMemTypeId = $membership->membership_type_id;
218 if ($membership->contact_id != $this->_userID) {
219 $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($this->_userID);
220 if (array_key_exists($membership->contact_id, $employers)) {
221 $this->_membershipContactID = $membership->contact_id;
222 $this->assign('membershipContactID', $this->_membershipContactID);
223 $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']);
224 }
225 else {
226 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');
227 }
228 }
229 }
230 else {
231 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');
232 }
233 unset($membership);
234 }
235 }
236
237 // we do not want to display recently viewed items, so turn off
238 $this->assign('displayRecent', FALSE);
239 // Contribution page values are cleared from session, so can't use normal Printer Friendly view.
240 // Use Browser Print instead.
241 $this->assign('browserPrint', TRUE);
242
243 // action
244 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
245 $this->assign('action', $this->_action);
246
247 // current mode
248 $this->_mode = ($this->_action == 1024) ? 'test' : 'live';
249
250 $this->_values = $this->get('values');
251 $this->_fields = $this->get('fields');
252 $this->_bltID = $this->get('bltID');
253 $this->_paymentProcessor = $this->get('paymentProcessor');
254 $this->_priceSetId = $this->get('priceSetId');
255 $this->_priceSet = $this->get('priceSet');
256
257 if (!$this->_values) {
258 // get all the values from the dao object
259 $this->_values = array();
260 $this->_fields = array();
261
262 CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values);
263
264 // check if form is active
265 if (!CRM_Utils_Array::value('is_active', $this->_values)) {
266 // form is inactive, die a fatal death
267 CRM_Core_Error::fatal(ts('The page you requested is currently unavailable.'));
268 }
269
270 // also check for billing informatin
271 // get the billing location type
b2b0530a 272 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
273 // CRM-8108 remove ts around Billing location type
274 //$this->_bltID = array_search( ts('Billing'), $locationTypes );
275 $this->_bltID = array_search('Billing', $locationTypes);
276 if (!$this->_bltID) {
277 CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
278 }
279 $this->set('bltID', $this->_bltID);
280
281 // check for is_monetary status
282 $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values);
283 $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values);
284
285 //FIXME: to support multiple payment processors
286 if ($isMonetary &&
287 (!$isPayLater || CRM_Utils_Array::value('payment_processor', $this->_values))
288 ) {
289 $ppID = CRM_Utils_Array::value('payment_processor', $this->_values);
290 if (!$ppID) {
291 CRM_Core_Error::fatal(ts('A payment processor must be selected for this contribution page (contact the site administrator for assistance).'));
292 }
293
294 $ppIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID);
295 $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($ppIds, $this->_mode);
296
297 $this->set('paymentProcessors', $this->_paymentProcessors);
298
299 //set default payment processor
300 if (!empty($this->_paymentProcessors) && empty($this->_paymentProcessor)) {
301 foreach ($this->_paymentProcessors as $ppId => $values) {
302 if ($values['is_default'] == 1 || (count($this->_paymentProcessors) == 1)) {
303 $defaultProcessorId = $ppId;
304 break;
305 }
306 }
307 }
308
309 if (isset($defaultProcessorId)) {
310 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($defaultProcessorId, $this->_mode);
311 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor);
312 }
313
314 if (!CRM_Utils_System::isNull($this->_paymentProcessors)) {
315 foreach ($this->_paymentProcessors as $eachPaymentProcessor) {
316 // check selected payment processor is active
317 if (empty($eachPaymentProcessor)) {
318 CRM_Core_Error::fatal(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
319 }
320
321 // ensure that processor has a valid config
322 $this->_paymentObject = &CRM_Core_Payment::singleton($this->_mode, $eachPaymentProcessor, $this);
323 $error = $this->_paymentObject->checkConfig();
324 if (!empty($error)) {
325 CRM_Core_Error::fatal($error);
326 }
327 }
328 }
329 }
330
331 // get price info
332 // CRM-5095
9da8dc8c 333 CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
6a488035
TO
334
335 // this avoids getting E_NOTICE errors in php
336 $setNullFields = array(
337 'amount_block_is_active',
338 'honor_block_is_active',
339 'is_allow_other_amount',
340 'footer_text',
341 );
342 foreach ($setNullFields as $f) {
343 if (!isset($this->_values[$f])) {
344 $this->_values[$f] = NULL;
345 }
346 }
347
348 //check if Membership Block is enabled, if Membership Fields are included in profile
349 //get membership section for this contribution page
350 $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
351 $this->set('membershipBlock', $this->_membershipBlock);
352
353 if ($this->_values['custom_pre_id']) {
354 $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']);
355 }
356
357 if ($this->_values['custom_post_id']) {
358 $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']);
359 }
360
361 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
362 (isset($preProfileType) && $preProfileType == 'Membership')
363 ) &&
364 !$this->_membershipBlock['is_active']
365 ) {
366 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.'));
367 }
368
369 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id);
370
371 if ($pledgeBlock) {
372 $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock);
373 $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock);
374 $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock);
375 $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock);
376
377 //set pledge id in values
378 $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this);
379
380 //authenticate pledge user for pledge payment.
381 if ($pledgeId) {
382 $this->_values['pledge_id'] = $pledgeId;
383
384 //lets override w/ pledge campaign.
385 $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge',
386 $pledgeId,
387 'campaign_id'
388 );
389 self::authenticatePledgeUser();
390 }
391 }
392 $this->set('values', $this->_values);
393 $this->set('fields', $this->_fields);
394 }
395
396 // Handle PCP
397 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
398 if ($pcpId) {
399 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
400 $this->_pcpId = $pcp['pcpId'];
401 $this->_pcpBlock = $pcp['pcpBlock'];
402 $this->_pcpInfo = $pcp['pcpInfo'];
403 }
404
405 // Link (button) for users to create their own Personal Campaign page
406 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) {
407 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
408 "action=add&reset=1&pageId={$this->_id}&component=contribute",
409 FALSE, NULL, TRUE
410 );
411 $this->assign('linkTextUrl', $linkTextUrl);
412 $this->assign('linkText', $linkText);
413 }
414
415 //set pledge block if block id is set
416 if (CRM_Utils_Array::value('pledge_block_id', $this->_values)) {
417 $this->assign('pledgeBlock', TRUE);
418 }
419
420 // check if one of the (amount , membership) bloks is active or not
421 $this->_membershipBlock = $this->get('membershipBlock');
422
423 if (!$this->_values['amount_block_is_active'] &&
424 !$this->_membershipBlock['is_active'] &&
425 !$this->_priceSetId
426 ) {
427 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.'));
428 }
429
430 if ($this->_values['amount_block_is_active']) {
431 $this->set('amount_block_is_active', $this->_values['amount_block_is_active']);
432 }
433
434 $this->_contributeMode = $this->get('contributeMode');
435 $this->assign('contributeMode', $this->_contributeMode);
436
437 //assigning is_monetary and is_email_receipt to template
438 $this->assign('is_monetary', $this->_values['is_monetary']);
439 $this->assign('is_email_receipt', $this->_values['is_email_receipt']);
440 $this->assign('bltID', $this->_bltID);
441
442 //assign cancelSubscription URL to templates
443 $this->assign('cancelSubscriptionUrl',
444 CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values)
445 );
446
447 // assigning title to template in case someone wants to use it, also setting CMS page title
448 if ($this->_pcpId) {
449 $this->assign('title', $this->_pcpInfo['title']);
450 CRM_Utils_System::setTitle($this->_pcpInfo['title']);
451 }
452 else {
453 $this->assign('title', $this->_values['title']);
454 CRM_Utils_System::setTitle($this->_values['title']);
455 }
456 $this->_defaults = array();
457
458 $this->_amount = $this->get('amount');
459
460 //CRM-6907
461 $config = CRM_Core_Config::singleton();
462 $config->defaultCurrency = CRM_Utils_Array::value('currency',
463 $this->_values,
464 $config->defaultCurrency
465 );
466
467 //lets allow user to override campaign.
468 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
469 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
470 $this->_values['campaign_id'] = $campID;
471 }
472
473 //do check for cancel recurring and clean db, CRM-7696
474 if (CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject)) {
475 self::cancelRecurring();
476 }
477 }
478
479 /**
480 * set the default values
481 *
482 * @return void
483 * @access public
484 */
485 function setDefaultValues() {
486 return $this->_defaults;
487 }
488
489 /**
490 * assign the minimal set of variables to the template
491 *
492 * @return void
493 * @access public
494 */
495 function assignToTemplate() {
496 $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
497 if (CRM_Utils_Array::value('billing_middle_name', $this->_params)) {
498 $name .= " {$this->_params['billing_middle_name']}";
499 }
500 $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params);
501 $name = trim($name);
502 $this->assign('billingName', $name);
503 $this->set('name', $name);
504
505 $this->assign('paymentProcessor', $this->_paymentProcessor);
506 $vars = array(
507 'amount', 'currencyID',
508 'credit_card_type', 'trxn_id', 'amount_level',
509 );
510
511 $config = CRM_Core_Config::singleton();
512 if (isset($this->_values['is_recur']) &&
f92fc7eb 513 CRM_Utils_Array::value('is_recur', $this->_paymentProcessor)
6a488035
TO
514 ) {
515 $this->assign('is_recur_enabled', 1);
516 $vars = array_merge($vars, array(
517 'is_recur', 'frequency_interval', 'frequency_unit',
518 'installments',
519 ));
520 }
521
522 if (in_array('CiviPledge', $config->enableComponents) &&
523 CRM_Utils_Array::value('is_pledge', $this->_params) == 1
524 ) {
525 $this->assign('pledge_enabled', 1);
526
527 $vars = array_merge($vars, array(
528 'is_pledge',
529 'pledge_frequency_interval',
530 'pledge_frequency_unit',
531 'pledge_installments',
532 ));
533 }
534
535 if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) {
536 $this->_params['amount_level'] = '';
537 }
538
539 foreach ($vars as $v) {
3fb990f4 540 if (isset($this->_params[$v])) {
6a488035
TO
541 if ($v == 'frequency_unit' || $v == 'pledge_frequency_unit') {
542 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
543 if (array_key_exists($this->_params[$v], $frequencyUnits)) {
544 $this->_params[$v] = $frequencyUnits[$this->_params[$v]];
545 }
546 }
3fb990f4
RN
547 if ($v == "amount") {
548 $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], ' ');
549 }
6a488035
TO
550 $this->assign($v, $this->_params[$v]);
551 }
552 }
553
554 // assign the address formatted up for display
555 $addressParts = array(
556 "street_address-{$this->_bltID}",
557 "city-{$this->_bltID}",
558 "postal_code-{$this->_bltID}",
559 "state_province-{$this->_bltID}",
560 "country-{$this->_bltID}",
561 );
562
563 $addressFields = array();
564 foreach ($addressParts as $part) {
565 list($n, $id) = explode('-', $part);
566 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params);
567 }
568
569 $this->assign('address', CRM_Utils_Address::format($addressFields));
570
571 if (CRM_Utils_Array::value('hidden_onbehalf_profile', $this->_params)) {
572 $this->assign('onBehalfName', $this->_params['organization_name']);
573 $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
574 $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
575 }
576
577 //fix for CRM-3767
578 $assignCCInfo = FALSE;
579 if ($this->_amount > 0.0) {
580 $assignCCInfo = TRUE;
581 }
582 elseif (CRM_Utils_array::value('selectMembership', $this->_params)) {
583 $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
584 if ($memFee > 0.0) {
585 $assignCCInfo = TRUE;
586 }
587 }
588
589 if ($this->_contributeMode == 'direct' && $assignCCInfo) {
f92fc7eb
CW
590 if ($this->_paymentProcessor &&
591 $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
592 ) {
6a488035
TO
593 $this->assign('payment_type', $this->_paymentProcessor['payment_type']);
594 $this->assign('account_holder', $this->_params['account_holder']);
595 $this->assign('bank_identification_number', $this->_params['bank_identification_number']);
596 $this->assign('bank_name', $this->_params['bank_name']);
597 $this->assign('bank_account_number', $this->_params['bank_account_number']);
598 }
599 else {
600 $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params));
601 $date = CRM_Utils_Date::mysqlToIso($date);
602 $this->assign('credit_card_exp_date', $date);
603 $this->assign('credit_card_number',
604 CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params))
605 );
606 }
607 }
608
609 $this->assign('email',
610 $this->controller->exportValue('Main', "email-{$this->_bltID}")
611 );
612
613 // also assign the receipt_text
614 if (isset($this->_values['receipt_text'])) {
615 $this->assign('receipt_text', $this->_values['receipt_text']);
616 }
617 }
618
619 /**
620 * Function to add the custom fields
621 *
622 * @return None
623 * @access public
624 */
625 function buildCustom($id, $name, $viewOnly = FALSE, $onBehalf = FALSE, $fieldTypes = NULL) {
626 $stateCountryMap = array();
627
628 if ($id) {
629 $contactID = $this->_userID;
630
631 // we don't allow conflicting fields to be
632 // configured via profile - CRM 2100
633 $fieldsToIgnore = array(
634 'receive_date' => 1,
635 'trxn_id' => 1,
636 'invoice_id' => 1,
637 'net_amount' => 1,
638 'fee_amount' => 1,
639 'non_deductible_amount' => 1,
640 'total_amount' => 1,
641 'amount_level' => 1,
642 'contribution_status_id' => 1,
643 'payment_instrument' => 1,
644 'check_number' => 1,
645 'financial_type' => 1,
646 );
647
648 $fields = NULL;
649 if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
650 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
651 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
652 );
653 }
654 else {
655 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
656 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
657 );
658 }
659
660 if ($fields) {
661 // unset any email-* fields since we already collect it, CRM-2888
662 foreach (array_keys($fields) as $fieldName) {
663 if (substr($fieldName, 0, 6) == 'email-') {
664 unset($fields[$fieldName]);
665 }
666 }
667
668 if (array_intersect_key($fields, $fieldsToIgnore)) {
669 $fields = array_diff_key($fields, $fieldsToIgnore);
670 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
671 }
672
673 $fields = array_diff_assoc($fields, $this->_fields);
674
675 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
676 $addCaptcha = FALSE;
677 foreach ($fields as $key => $field) {
678 if ($viewOnly &&
679 isset($field['data_type']) &&
680 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
681 ) {
682 // ignore file upload fields
683 continue;
684 }
685
686 list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
687 if ($prefixName == 'state_province' || $prefixName == 'country' || $prefixName == 'county') {
688 if (!array_key_exists($index, $stateCountryMap)) {
689 $stateCountryMap[$index] = array();
690 }
691 $stateCountryMap[$index][$prefixName] = $key;
692 }
693
694 if ($onBehalf) {
695 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
696 CRM_Core_BAO_UFGroup::buildProfile(
697 $this,
698 $field,
699 CRM_Profile_Form::MODE_CREATE,
700 $contactID,
701 TRUE
702 );
703 $this->_fields['onbehalf'][$key] = $field;
704 }
705 else {
706 unset($fields[$key]);
707 }
708 }
709 else {
710 CRM_Core_BAO_UFGroup::buildProfile(
711 $this,
712 $field,
713 CRM_Profile_Form::MODE_CREATE,
714 $contactID,
715 TRUE
716 );
717 $this->_fields[$key] = $field;
718 }
719 if ($field['add_captcha']) {
720 $addCaptcha = TRUE;
721 }
722 }
723
724 $this->assign($name, $fields);
725
726 CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
727
728 if ($addCaptcha && !$viewOnly) {
729 $captcha = CRM_Utils_ReCAPTCHA::singleton();
730 $captcha->add($this);
731 $this->assign('isCaptcha', TRUE);
732 }
733 }
734 }
735 }
736
737 function checkTemplateFileExists($suffix = NULL) {
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
748 function getTemplateFileName() {
749 $fileName = $this->checkTemplateFileExists();
750 return $fileName ? $fileName : parent::getTemplateFileName();
751 }
752
753 function overrideExtraTemplateFileName() {
754 $fileName = $this->checkTemplateFileExists('extra.');
755 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
756 }
757
758 /**
759 * Function to authenticate pledge user during online payment.
760 *
761 * @access public
762 *
763 * @return None
764 */
765 public function authenticatePledgeUser() {
766 //get the userChecksum and contact id
767 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
768 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
769
770 //get pledge status and contact id
771 $pledgeValues = array();
772 $pledgeParams = array('id' => $this->_values['pledge_id']);
773 $returnProperties = array('contact_id', 'status_id');
774 CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
775
776 //get all status
777 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
778 $validStatus = array(array_search('Pending', $allStatus),
779 array_search('In Progress', $allStatus),
780 array_search('Overdue', $allStatus),
781 );
782
783 $validUser = FALSE;
784 if ($this->_userID &&
785 $this->_userID == $pledgeValues['contact_id']
786 ) {
787 //check for authenticated user.
788 $validUser = TRUE;
789 }
790 elseif ($userChecksum && $pledgeValues['contact_id']) {
791 //check for anonymous user.
792 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum);
793
794 //make sure cid is same as pledge contact id
795 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
796 $validUser = FALSE;
797 }
798 }
799
800 if (!$validUser) {
801 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."));
802 }
803
804 //check for valid pledge status.
805 if (!in_array($pledgeValues['status_id'], $validStatus)) {
806 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))));
807 }
808 }
809
810 /**
811 * In case user cancel recurring contribution,
812 * When we get the control back from payment gate way
813 * lets delete the recurring and related contribution.
814 *
815 **/
816 public function cancelRecurring() {
817 $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject);
818 if ($isCancel) {
819 $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject);
820 $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject);
821 //clean db for recurring contribution.
822 if ($isRecur && $recurId) {
823 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
824 }
825 $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject);
826 if ($contribId) {
827 CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
828 }
829 }
830 }
831}
832