commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Contribute / Form / ContributionBase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for processing a contribution
38 *
39 */
40 class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
41
42 /**
43 * The id of the contribution page that we are processsing
44 *
45 * @var int
46 */
47 public $_id;
48
49 /**
50 * The mode that we are in
51 *
52 * @var string
53 * @protect
54 */
55 public $_mode;
56
57 /**
58 * The contact id related to a membership
59 *
60 * @var int
61 */
62 public $_membershipContactID;
63
64 /**
65 * The values for the contribution db object
66 *
67 * @var array
68 */
69 public $_values;
70
71 /**
72 * The paymentProcessor attributes for this page
73 *
74 * @var array
75 */
76 public $_paymentProcessor;
77 public $_paymentObject = NULL;
78
79 /**
80 * The membership block for this page
81 *
82 * @var array
83 */
84 public $_membershipBlock = NULL;
85
86 /**
87 * Does this form support a separate membership payment
88 * @var bool
89 */
90 protected $_separateMembershipPayment;
91 /**
92 * The default values for the form
93 *
94 * @var array
95 */
96 protected $_defaults;
97
98 /**
99 * The params submitted by the form and computed by the app
100 *
101 * @var array
102 */
103 public $_params = array();
104
105 /**
106 * The fields involved in this contribution page
107 *
108 * @var array
109 */
110 public $_fields = array();
111
112 /**
113 * The billing location id for this contribiution page
114 *
115 * @var int
116 */
117 public $_bltID;
118
119 /**
120 * Cache the amount to make things easier
121 *
122 * @var float
123 */
124 public $_amount;
125
126 /**
127 * Pcp id
128 *
129 * @var integer
130 */
131 public $_pcpId;
132
133 /**
134 * Pcp block
135 *
136 * @var array
137 */
138 public $_pcpBlock;
139
140 /**
141 * Pcp info
142 *
143 * @var array
144 */
145 public $_pcpInfo;
146
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
150 * @var int
151 */
152 public $_contactID;
153
154 protected $_userID;
155
156 /**
157 * The Membership ID for membership renewal
158 *
159 * @var int
160 */
161 public $_membershipId;
162
163 /**
164 * Price Set ID, if the new price set method is used
165 *
166 * @var int
167 */
168 public $_priceSetId;
169
170 /**
171 * Array of fields for the price set
172 *
173 * @var array
174 */
175 public $_priceSet;
176
177 public $_action;
178
179 /**
180 * Is honor block is enabled for this contribution?
181 *
182 * @var boolean
183 */
184 public $_honor_block_is_active = FALSE;
185
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 /**
193 * Contribution page supports memberships
194 * @var boolean
195 */
196 public $_useForMember;
197
198 public $_isBillingAddressRequiredForPayLater;
199
200 /**
201 * Set variables up before form is built.
202 *
203 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
204 * @throws \Exception
205 */
206 public function preProcess() {
207
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) {
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();
217 }
218
219 // this was used prior to the cleverer this_>getContactID - unsure now
220 $this->_userID = $session->get('userID');
221
222 //Check if honor block is enabled for current contribution
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;
232
233 $this->_contactID = $this->_membershipContactID = $this->getContactID();
234 $this->_mid = NULL;
235 if ($this->_contactID) {
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;
243 if ($membership->contact_id != $this->_contactID) {
244 $validMembership = FALSE;
245 $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($this->_userID);
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;
251 }
252 else {
253 $membershipType = new CRM_Member_BAO_MembershipType();
254 $membershipType->id = $membership->membership_type_id;
255 if ($membershipType->find(TRUE)) {
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), ',');
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;
262 $validMembership = TRUE;
263 }
264 }
265 }
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 }
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
305 if (empty($this->_values['is_active'])) {
306 throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id);
307 }
308
309 // also check for billing informatin
310 // get the billing location type
311 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
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 &&
326 (!$isPayLater || !empty($this->_values['payment_processor']))
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
372 CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
373
374 // this avoids getting E_NOTICE errors in php
375 $setNullFields = array(
376 'amount_block_is_active',
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) {
437 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
438 $this->_pcpId = $pcp['pcpId'];
439 $this->_pcpBlock = $pcp['pcpBlock'];
440 $this->_pcpInfo = $pcp['pcpInfo'];
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
454 if (!empty($this->_values['pledge_block_id'])) {
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 }
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 }
521 }
522
523 /**
524 * Set the default values.
525 *
526 * @return void
527 */
528 public function setDefaultValues() {
529 return $this->_defaults;
530 }
531
532 /**
533 * Assign the minimal set of variables to the template.
534 *
535 * @return void
536 */
537 public function assignToTemplate() {
538 $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
539 if (!empty($this->_params['billing_middle_name'])) {
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(
549 'amount',
550 'currencyID',
551 'credit_card_type',
552 'trxn_id',
553 'amount_level',
554 );
555
556 $config = CRM_Core_Config::singleton();
557 if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) {
558 $this->assign('is_recur_enabled', 1);
559 $vars = array_merge($vars, array(
560 'is_recur',
561 'frequency_interval',
562 'frequency_unit',
563 'installments',
564 ));
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',
574 'pledge_frequency_interval',
575 'pledge_frequency_unit',
576 'pledge_installments',
577 ));
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) {
585 if (isset($this->_params[$v])) {
586 if ($v == "amount" && $this->_params[$v] === 0) {
587 $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE);
588 }
589 $this->assign($v, $this->_params[$v]);
590 }
591 }
592
593 // assign the address formatted up for display
594 $addressParts = array(
595 "street_address-{$this->_bltID}",
596 "city-{$this->_bltID}",
597 "postal_code-{$this->_bltID}",
598 "state_province-{$this->_bltID}",
599 "country-{$this->_bltID}",
600 );
601
602 $addressFields = array();
603 foreach ($addressParts as $part) {
604 list($n, $id) = explode('-', $part);
605 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params);
606 }
607
608 $this->assign('address', CRM_Utils_Address::format($addressFields));
609
610 if (!empty($this->_params['hidden_onbehalf_profile'])) {
611 $this->assign('onBehalfName', $this->_params['organization_name']);
612 $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
613 $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
614 }
615
616 //fix for CRM-3767
617 $assignCCInfo = FALSE;
618 if ($this->_amount > 0.0) {
619 $assignCCInfo = TRUE;
620 }
621 elseif (!empty($this->_params['selectMembership'])) {
622 $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
623 if ($memFee > 0.0) {
624 $assignCCInfo = TRUE;
625 }
626 }
627
628 if ($this->_contributeMode == 'direct' && $assignCCInfo) {
629 if ($this->_paymentProcessor &&
630 $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
631 ) {
632 $this->assign('account_holder', $this->_params['account_holder']);
633 $this->assign('bank_identification_number', $this->_params['bank_identification_number']);
634 $this->assign('bank_name', $this->_params['bank_name']);
635 $this->assign('bank_account_number', $this->_params['bank_account_number']);
636 }
637 else {
638 $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params));
639 $date = CRM_Utils_Date::mysqlToIso($date);
640 $this->assign('credit_card_exp_date', $date);
641 $this->assign('credit_card_number',
642 CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params))
643 );
644 }
645 }
646
647 $this->assign('email',
648 $this->controller->exportValue('Main', "email-{$this->_bltID}")
649 );
650
651 // also assign the receipt_text
652 if (isset($this->_values['receipt_text'])) {
653 $this->assign('receipt_text', $this->_values['receipt_text']);
654 }
655 }
656
657 /**
658 * Add the custom fields.
659 *
660 * @param int $id
661 * @param string $name
662 * @param bool $viewOnly
663 * @param null $profileContactType
664 * @param null $fieldTypes
665 *
666 * @return void
667 */
668 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
669 if ($id) {
670 $contactID = $this->getContactID();
671
672 // we don't allow conflicting fields to be
673 // configured via profile - CRM 2100
674 $fieldsToIgnore = array(
675 'receive_date' => 1,
676 'trxn_id' => 1,
677 'invoice_id' => 1,
678 'net_amount' => 1,
679 'fee_amount' => 1,
680 'non_deductible_amount' => 1,
681 'total_amount' => 1,
682 'amount_level' => 1,
683 'contribution_status_id' => 1,
684 'payment_instrument' => 1,
685 'check_number' => 1,
686 'financial_type' => 1,
687 );
688
689 $fields = NULL;
690 if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
691 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
692 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
693 );
694 }
695 else {
696 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
697 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
698 );
699 }
700
701 if ($fields) {
702 // unset any email-* fields since we already collect it, CRM-2888
703 foreach (array_keys($fields) as $fieldName) {
704 if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') {
705 unset($fields[$fieldName]);
706 }
707 }
708
709 if (array_intersect_key($fields, $fieldsToIgnore)) {
710 $fields = array_diff_key($fields, $fieldsToIgnore);
711 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
712 }
713
714 $fields = array_diff_assoc($fields, $this->_fields);
715
716 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
717 $addCaptcha = FALSE;
718 $viewOnlyFileValues = empty($profileContactType) ? array() : array($profileContactType => array());
719 // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page
720 foreach ($fields as $key => $field) {
721 if ($viewOnly &&
722 isset($field['data_type']) &&
723 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
724 ) {
725 //retrieve file value from submitted values on basis of $profileContactType
726 $fileValue = empty($profileContactType) ? CRM_Utils_Array::value($key, $this->_params) : CRM_Utils_Array::value(sprintf('%s[%s]', $profileContactType, $key), $this->_params);
727
728 if ($fileValue) {
729 $path = CRM_Utils_Array::value('name', $fileValue);
730 $fileType = CRM_Utils_Array::value('type', $fileValue);
731 $fileValue = CRM_Utils_File::getFileURL($path, $fileType);
732 }
733
734 // format custom file value fetched from submitted value
735 if ($profileContactType) {
736 $viewOnlyFileValues[$profileContactType][$key] = $fileValue;
737 }
738 else {
739 $viewOnlyFileValues[$key] = $fileValue;
740 }
741 }
742
743 if ($profileContactType) {
744 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
745 $honoreeNamefields = array(
746 'prefix_id',
747 'first_name',
748 'last_name',
749 'suffix_id',
750 'organization_name',
751 'household_name',
752 );
753 if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) {
754 unset($fields[$field['name']]);
755 continue;
756 }
757 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
758 CRM_Core_BAO_UFGroup::buildProfile(
759 $this,
760 $field,
761 CRM_Profile_Form::MODE_CREATE,
762 $contactID,
763 TRUE,
764 $profileContactType
765 );
766 $this->_fields[$profileContactType][$key] = $field;
767 }
768 else {
769 unset($fields[$key]);
770 }
771 }
772 else {
773 CRM_Core_BAO_UFGroup::buildProfile(
774 $this,
775 $field,
776 CRM_Profile_Form::MODE_CREATE,
777 $contactID,
778 TRUE
779 );
780 $this->_fields[$key] = $field;
781 }
782 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
783 if ($field['add_captcha'] && !$this->_userID) {
784 $addCaptcha = TRUE;
785 }
786 }
787
788 $this->assign($name, $fields);
789
790 if ($profileContactType && count($viewOnlyFileValues[$profileContactType])) {
791 $this->assign('viewOnlyPrefixFileValues', $viewOnlyFileValues);
792 }
793 elseif (count($viewOnlyFileValues)) {
794 $this->assign('viewOnlyFileValues', $viewOnlyFileValues);
795 }
796
797 if ($addCaptcha && !$viewOnly) {
798 $captcha = CRM_Utils_ReCAPTCHA::singleton();
799 $captcha->add($this);
800 $this->assign('isCaptcha', TRUE);
801 }
802 }
803 }
804 }
805
806 /**
807 * Check template file exists.
808 * @param null $suffix
809 *
810 * @return null|string
811 */
812 public function checkTemplateFileExists($suffix = NULL) {
813 if ($this->_id) {
814 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
815 $template = CRM_Core_Form::getTemplate();
816 if ($template->template_exists($templateFile)) {
817 return $templateFile;
818 }
819 }
820 return NULL;
821 }
822
823 /**
824 * Use the form name to create the tpl file name.
825 *
826 * @return string
827 */
828 /**
829 * @return string
830 */
831 public function getTemplateFileName() {
832 $fileName = $this->checkTemplateFileExists();
833 return $fileName ? $fileName : parent::getTemplateFileName();
834 }
835
836 /**
837 * Default extra tpl file basically just replaces .tpl with .extra.tpl
838 * i.e. we dont override
839 *
840 * @return string
841 */
842 /**
843 * @return string
844 */
845 public function overrideExtraTemplateFileName() {
846 $fileName = $this->checkTemplateFileExists('extra.');
847 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
848 }
849
850 /**
851 * Authenticate pledge user during online payment.
852 *
853 *
854 * @return void
855 */
856 public function authenticatePledgeUser() {
857 //get the userChecksum and contact id
858 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
859 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
860
861 //get pledge status and contact id
862 $pledgeValues = array();
863 $pledgeParams = array('id' => $this->_values['pledge_id']);
864 $returnProperties = array('contact_id', 'status_id');
865 CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
866
867 //get all status
868 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
869 $validStatus = array(
870 array_search('Pending', $allStatus),
871 array_search('In Progress', $allStatus),
872 array_search('Overdue', $allStatus),
873 );
874
875 $validUser = FALSE;
876 if ($this->_userID &&
877 $this->_userID == $pledgeValues['contact_id']
878 ) {
879 //check for authenticated user.
880 $validUser = TRUE;
881 }
882 elseif ($userChecksum && $pledgeValues['contact_id']) {
883 //check for anonymous user.
884 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum);
885
886 //make sure cid is same as pledge contact id
887 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
888 $validUser = FALSE;
889 }
890 }
891
892 if (!$validUser) {
893 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."));
894 }
895
896 //check for valid pledge status.
897 if (!in_array($pledgeValues['status_id'], $validStatus)) {
898 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))));
899 }
900 }
901
902 /**
903 * In case user cancel recurring contribution,
904 * When we get the control back from payment gate way
905 * lets delete the recurring and related contribution.
906 *
907 */
908 public function cancelRecurring() {
909 $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject);
910 if ($isCancel) {
911 $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject);
912 $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject);
913 //clean db for recurring contribution.
914 if ($isRecur && $recurId) {
915 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
916 }
917 $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject);
918 if ($contribId) {
919 CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
920 }
921 }
922 }
923
924 }