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