Fix onbehalf processing via snippet
[civicrm-core.git] / CRM / Contribute / Form / ContributionBase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
e3155fe2
EM
34use Civi\Payment\System;
35
6a488035 36/**
f92d1e2a 37 * This class generates form components for processing a contribution.
6a488035
TO
38 */
39class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
40
41 /**
f92d1e2a 42 * The id of the contribution page that we are processing.
6a488035
TO
43 *
44 * @var int
6a488035
TO
45 */
46 public $_id;
47
48 /**
100fef9d 49 * The mode that we are in
6a488035
TO
50 *
51 * @var string
52 * @protect
53 */
54 public $_mode;
55
56 /**
100fef9d 57 * The contact id related to a membership
6a488035
TO
58 *
59 * @var int
6a488035
TO
60 */
61 public $_membershipContactID;
62
63 /**
100fef9d 64 * The values for the contribution db object
6a488035
TO
65 *
66 * @var array
6a488035
TO
67 */
68 public $_values;
69
70 /**
100fef9d 71 * The paymentProcessor attributes for this page
6a488035
TO
72 *
73 * @var array
6a488035
TO
74 */
75 public $_paymentProcessor;
1b9f9ca3 76
6a488035
TO
77 public $_paymentObject = NULL;
78
79 /**
80 * The membership block for this page
81 *
82 * @var array
6a488035
TO
83 */
84 public $_membershipBlock = NULL;
85
f64a217a
EM
86 /**
87 * Does this form support a separate membership payment
88 * @var bool
89 */
90 protected $_separateMembershipPayment;
6a488035
TO
91
92 /**
93 * The params submitted by the form and computed by the app
94 *
95 * @var array
6a488035 96 */
90102a32 97 public $_params = array();
6a488035
TO
98
99 /**
100 * The fields involved in this contribution page
101 *
102 * @var array
6a488035 103 */
532ee86f 104 public $_fields = array();
6a488035
TO
105
106 /**
f92d1e2a 107 * The billing location id for this contribution page.
6a488035
TO
108 *
109 * @var int
6a488035
TO
110 */
111 public $_bltID;
112
113 /**
114 * Cache the amount to make things easier
115 *
116 * @var float
6a488035
TO
117 */
118 public $_amount;
119
120 /**
100fef9d 121 * Pcp id
6a488035
TO
122 *
123 * @var integer
6a488035
TO
124 */
125 public $_pcpId;
126
127 /**
100fef9d 128 * Pcp block
6a488035
TO
129 *
130 * @var array
6a488035
TO
131 */
132 public $_pcpBlock;
133
134 /**
100fef9d 135 * Pcp info
6a488035
TO
136 *
137 * @var array
6a488035
TO
138 */
139 public $_pcpInfo;
140
5b757295 141 /**
142 * The contact id of the person for whom membership is being added or renewed based on the cid in the url,
143 * checksum, or session
0e5e0c2e 144 * @var int
5b757295 145 */
0e5e0c2e 146 public $_contactID;
5b757295 147
6a488035
TO
148 protected $_userID;
149
150 /**
100fef9d 151 * The Membership ID for membership renewal
6a488035
TO
152 *
153 * @var int
6a488035
TO
154 */
155 public $_membershipId;
156
157 /**
158 * Price Set ID, if the new price set method is used
159 *
160 * @var int
6a488035
TO
161 */
162 public $_priceSetId;
163
164 /**
165 * Array of fields for the price set
166 *
167 * @var array
6a488035
TO
168 */
169 public $_priceSet;
170
171 public $_action;
172
874c9be7 173 /**
353ffa53
TO
174 * Is honor block is enabled for this contribution?
175 *
176 * @var boolean
177 */
8af73472 178 public $_honor_block_is_active = FALSE;
179
dbddfb08
EM
180 /**
181 * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
182 * @var string
183 */
184 public $_contributeMode;
185
186 /**
100fef9d 187 * Contribution page supports memberships
dbddfb08
EM
188 * @var boolean
189 */
190 public $_useForMember;
8ae4d0d3 191
192 public $_isBillingAddressRequiredForPayLater;
353ffa53 193
6a488035 194 /**
fe482240 195 * Set variables up before form is built.
6a488035 196 *
7fe37828
EM
197 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
198 * @throws \Exception
6a488035
TO
199 */
200 public function preProcess() {
6a488035
TO
201
202 // current contribution page id
203 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
204 if (!$this->_id) {
d420cf02
DL
205 // seems like the session is corrupted and/or we lost the id trail
206 // lets just bump this to a regular session error and redirect user to main page
207 $this->controller->invalidKeyRedirect();
6a488035 208 }
d420cf02 209
5b757295 210 // this was used prior to the cleverer this_>getContactID - unsure now
a6c15c46 211 $this->_userID = CRM_Core_Session::singleton()->get('userID');
5b757295 212
213 $this->_contactID = $this->_membershipContactID = $this->getContactID();
6a488035 214 $this->_mid = NULL;
5b757295 215 if ($this->_contactID) {
6a488035
TO
216 $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
217 if ($this->_mid) {
218 $membership = new CRM_Member_DAO_Membership();
219 $membership->id = $this->_mid;
220
221 if ($membership->find(TRUE)) {
222 $this->_defaultMemTypeId = $membership->membership_type_id;
5b757295 223 if ($membership->contact_id != $this->_contactID) {
6fe8deba 224 $validMembership = FALSE;
6a488035 225 $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($this->_userID);
6fe8deba
DS
226 if (!empty($employers) && array_key_exists($membership->contact_id, $employers)) {
227 $this->_membershipContactID = $membership->contact_id;
228 $this->assign('membershipContactID', $this->_membershipContactID);
229 $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']);
230 $validMembership = TRUE;
0db6c3e1
TO
231 }
232 else {
51e89def
DS
233 $membershipType = new CRM_Member_BAO_MembershipType();
234 $membershipType->id = $membership->membership_type_id;
235 if ($membershipType->find(TRUE)) {
f9f0eff9 236 // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values.
f92d1e2a 237 // Convert to comma separated list.
f9f0eff9 238 $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ',');
51e89def
DS
239 $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id);
240 if (array_key_exists($membership->contact_id, $permContacts)) {
241 $this->_membershipContactID = $membership->contact_id;
6fe8deba 242 $validMembership = TRUE;
51e89def
DS
243 }
244 }
6a488035 245 }
6fe8deba
DS
246 if (!$validMembership) {
247 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');
248 }
6a488035
TO
249 }
250 }
251 else {
252 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');
253 }
254 unset($membership);
255 }
256 }
257
258 // we do not want to display recently viewed items, so turn off
259 $this->assign('displayRecent', FALSE);
260 // Contribution page values are cleared from session, so can't use normal Printer Friendly view.
261 // Use Browser Print instead.
262 $this->assign('browserPrint', TRUE);
263
264 // action
265 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
266 $this->assign('action', $this->_action);
267
268 // current mode
269 $this->_mode = ($this->_action == 1024) ? 'test' : 'live';
270
271 $this->_values = $this->get('values');
272 $this->_fields = $this->get('fields');
273 $this->_bltID = $this->get('bltID');
274 $this->_paymentProcessor = $this->get('paymentProcessor');
275 $this->_priceSetId = $this->get('priceSetId');
276 $this->_priceSet = $this->get('priceSet');
277
278 if (!$this->_values) {
279 // get all the values from the dao object
280 $this->_values = array();
281 $this->_fields = array();
282
283 CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values);
284
a7488080 285 if (empty($this->_values['is_active'])) {
4b57bc9f 286 throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id);
6a488035
TO
287 }
288
8345c9d3 289 $this->assignBillingType();
6a488035
TO
290
291 // check for is_monetary status
292 $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values);
293 $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values);
294
6a488035 295 if ($isMonetary &&
8cc574cf 296 (!$isPayLater || !empty($this->_values['payment_processor']))
6a488035 297 ) {
a6c15c46
EM
298 $this->_paymentProcessorIDs = explode(
299 CRM_Core_DAO::VALUE_SEPARATOR,
300 CRM_Utils_Array::value('payment_processor', $this->_values)
1b9f9ca3 301 );
f92d1e2a 302
1b9f9ca3 303 $this->assignPaymentProcessor();
6a488035
TO
304 }
305
306 // get price info
307 // CRM-5095
9da8dc8c 308 CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
6a488035
TO
309
310 // this avoids getting E_NOTICE errors in php
311 $setNullFields = array(
312 'amount_block_is_active',
6a488035
TO
313 'is_allow_other_amount',
314 'footer_text',
315 );
316 foreach ($setNullFields as $f) {
317 if (!isset($this->_values[$f])) {
318 $this->_values[$f] = NULL;
319 }
320 }
321
322 //check if Membership Block is enabled, if Membership Fields are included in profile
323 //get membership section for this contribution page
324 $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
325 $this->set('membershipBlock', $this->_membershipBlock);
326
327 if ($this->_values['custom_pre_id']) {
328 $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']);
329 }
330
331 if ($this->_values['custom_post_id']) {
332 $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']);
333 }
334
335 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
336 (isset($preProfileType) && $preProfileType == 'Membership')
337 ) &&
338 !$this->_membershipBlock['is_active']
339 ) {
340 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.'));
341 }
342
343 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id);
344
345 if ($pledgeBlock) {
346 $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock);
347 $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock);
348 $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock);
349 $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock);
350
351 //set pledge id in values
352 $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this);
353
354 //authenticate pledge user for pledge payment.
355 if ($pledgeId) {
356 $this->_values['pledge_id'] = $pledgeId;
357
358 //lets override w/ pledge campaign.
359 $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge',
360 $pledgeId,
361 'campaign_id'
362 );
363 self::authenticatePledgeUser();
364 }
365 }
366 $this->set('values', $this->_values);
367 $this->set('fields', $this->_fields);
368 }
369
370 // Handle PCP
371 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
372 if ($pcpId) {
353ffa53
TO
373 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
374 $this->_pcpId = $pcp['pcpId'];
6a488035 375 $this->_pcpBlock = $pcp['pcpBlock'];
353ffa53 376 $this->_pcpInfo = $pcp['pcpInfo'];
6a488035
TO
377 }
378
379 // Link (button) for users to create their own Personal Campaign page
380 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) {
381 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
382 "action=add&reset=1&pageId={$this->_id}&component=contribute",
383 FALSE, NULL, TRUE
384 );
385 $this->assign('linkTextUrl', $linkTextUrl);
386 $this->assign('linkText', $linkText);
387 }
388
389 //set pledge block if block id is set
a7488080 390 if (!empty($this->_values['pledge_block_id'])) {
6a488035
TO
391 $this->assign('pledgeBlock', TRUE);
392 }
393
f92d1e2a 394 // check if one of the (amount , membership) blocks is active or not.
6a488035
TO
395 $this->_membershipBlock = $this->get('membershipBlock');
396
397 if (!$this->_values['amount_block_is_active'] &&
398 !$this->_membershipBlock['is_active'] &&
399 !$this->_priceSetId
400 ) {
401 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.'));
402 }
403
404 if ($this->_values['amount_block_is_active']) {
405 $this->set('amount_block_is_active', $this->_values['amount_block_is_active']);
406 }
407
408 $this->_contributeMode = $this->get('contributeMode');
409 $this->assign('contributeMode', $this->_contributeMode);
410
411 //assigning is_monetary and is_email_receipt to template
412 $this->assign('is_monetary', $this->_values['is_monetary']);
413 $this->assign('is_email_receipt', $this->_values['is_email_receipt']);
414 $this->assign('bltID', $this->_bltID);
415
416 //assign cancelSubscription URL to templates
417 $this->assign('cancelSubscriptionUrl',
418 CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values)
419 );
420
421 // assigning title to template in case someone wants to use it, also setting CMS page title
422 if ($this->_pcpId) {
423 $this->assign('title', $this->_pcpInfo['title']);
424 CRM_Utils_System::setTitle($this->_pcpInfo['title']);
425 }
426 else {
427 $this->assign('title', $this->_values['title']);
428 CRM_Utils_System::setTitle($this->_values['title']);
429 }
430 $this->_defaults = array();
431
432 $this->_amount = $this->get('amount');
433
434 //CRM-6907
435 $config = CRM_Core_Config::singleton();
436 $config->defaultCurrency = CRM_Utils_Array::value('currency',
437 $this->_values,
438 $config->defaultCurrency
439 );
440
441 //lets allow user to override campaign.
442 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
443 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
444 $this->_values['campaign_id'] = $campID;
445 }
446
447 //do check for cancel recurring and clean db, CRM-7696
448 if (CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject)) {
449 self::cancelRecurring();
450 }
8ae4d0d3 451
452 // check if billing block is required for pay later
453 if (CRM_Utils_Array::value('is_pay_later', $this->_values)) {
454 $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values);
455 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
456 }
6a488035
TO
457 }
458
459 /**
fe482240 460 * Set the default values.
6a488035 461 */
00be9182 462 public function setDefaultValues() {
6a488035
TO
463 return $this->_defaults;
464 }
465
466 /**
fe482240 467 * Assign the minimal set of variables to the template.
6a488035 468 */
00be9182 469 public function assignToTemplate() {
6a488035 470 $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
a7488080 471 if (!empty($this->_params['billing_middle_name'])) {
6a488035
TO
472 $name .= " {$this->_params['billing_middle_name']}";
473 }
474 $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params);
475 $name = trim($name);
476 $this->assign('billingName', $name);
477 $this->set('name', $name);
478
479 $this->assign('paymentProcessor', $this->_paymentProcessor);
480 $vars = array(
353ffa53
TO
481 'amount',
482 'currencyID',
483 'credit_card_type',
484 'trxn_id',
485 'amount_level',
6a488035
TO
486 );
487
488 $config = CRM_Core_Config::singleton();
8cc574cf 489 if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) {
6a488035
TO
490 $this->assign('is_recur_enabled', 1);
491 $vars = array_merge($vars, array(
353ffa53
TO
492 'is_recur',
493 'frequency_interval',
494 'frequency_unit',
495 'installments',
496 ));
6a488035
TO
497 }
498
499 if (in_array('CiviPledge', $config->enableComponents) &&
500 CRM_Utils_Array::value('is_pledge', $this->_params) == 1
501 ) {
502 $this->assign('pledge_enabled', 1);
503
504 $vars = array_merge($vars, array(
505 'is_pledge',
353ffa53
TO
506 'pledge_frequency_interval',
507 'pledge_frequency_unit',
508 'pledge_installments',
509 ));
6a488035
TO
510 }
511
512 if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) {
513 $this->_params['amount_level'] = '';
514 }
515
516 foreach ($vars as $v) {
3fb990f4 517 if (isset($this->_params[$v])) {
735fe42d
PJ
518 if ($v == "amount" && $this->_params[$v] === 0) {
519 $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE);
3fb990f4 520 }
6a488035
TO
521 $this->assign($v, $this->_params[$v]);
522 }
523 }
524
525 // assign the address formatted up for display
526 $addressParts = array(
527 "street_address-{$this->_bltID}",
528 "city-{$this->_bltID}",
529 "postal_code-{$this->_bltID}",
530 "state_province-{$this->_bltID}",
531 "country-{$this->_bltID}",
532 );
533
534 $addressFields = array();
535 foreach ($addressParts as $part) {
536 list($n, $id) = explode('-', $part);
537 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params);
538 }
539
540 $this->assign('address', CRM_Utils_Address::format($addressFields));
541
e63910c5 542 if (!empty($this->_params['onbehalf_profile_id'])) {
6a488035
TO
543 $this->assign('onBehalfName', $this->_params['organization_name']);
544 $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
545 $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
546 }
547
548 //fix for CRM-3767
549 $assignCCInfo = FALSE;
550 if ($this->_amount > 0.0) {
551 $assignCCInfo = TRUE;
552 }
a7488080 553 elseif (!empty($this->_params['selectMembership'])) {
6a488035
TO
554 $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
555 if ($memFee > 0.0) {
556 $assignCCInfo = TRUE;
557 }
558 }
559
560 if ($this->_contributeMode == 'direct' && $assignCCInfo) {
f92fc7eb
CW
561 if ($this->_paymentProcessor &&
562 $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
563 ) {
6a488035
TO
564 $this->assign('account_holder', $this->_params['account_holder']);
565 $this->assign('bank_identification_number', $this->_params['bank_identification_number']);
566 $this->assign('bank_name', $this->_params['bank_name']);
567 $this->assign('bank_account_number', $this->_params['bank_account_number']);
568 }
569 else {
570 $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params));
571 $date = CRM_Utils_Date::mysqlToIso($date);
572 $this->assign('credit_card_exp_date', $date);
573 $this->assign('credit_card_number',
353ffa53 574 CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params))
6a488035
TO
575 );
576 }
577 }
578
579 $this->assign('email',
580 $this->controller->exportValue('Main', "email-{$this->_bltID}")
581 );
582
583 // also assign the receipt_text
584 if (isset($this->_values['receipt_text'])) {
585 $this->assign('receipt_text', $this->_values['receipt_text']);
586 }
587 }
588
589 /**
fe482240 590 * Add the custom fields.
6a488035 591 *
100fef9d
CW
592 * @param int $id
593 * @param string $name
f4aaa82a
EM
594 * @param bool $viewOnly
595 * @param null $profileContactType
f92d1e2a 596 * @param array $fieldTypes
6a488035 597 */
00be9182 598 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
6a488035 599 if ($id) {
da8d9879 600 $contactID = $this->getContactID();
6a488035
TO
601
602 // we don't allow conflicting fields to be
603 // configured via profile - CRM 2100
604 $fieldsToIgnore = array(
605 'receive_date' => 1,
606 'trxn_id' => 1,
607 'invoice_id' => 1,
608 'net_amount' => 1,
609 'fee_amount' => 1,
610 'non_deductible_amount' => 1,
611 'total_amount' => 1,
612 'amount_level' => 1,
613 'contribution_status_id' => 1,
614 'payment_instrument' => 1,
615 'check_number' => 1,
616 'financial_type' => 1,
617 );
618
619 $fields = NULL;
620 if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
621 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
622 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
623 );
624 }
625 else {
626 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
627 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
628 );
629 }
630
631 if ($fields) {
632 // unset any email-* fields since we already collect it, CRM-2888
633 foreach (array_keys($fields) as $fieldName) {
8af73472 634 if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') {
6a488035
TO
635 unset($fields[$fieldName]);
636 }
637 }
638
639 if (array_intersect_key($fields, $fieldsToIgnore)) {
640 $fields = array_diff_key($fields, $fieldsToIgnore);
641 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
642 }
643
644 $fields = array_diff_assoc($fields, $this->_fields);
645
646 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
647 $addCaptcha = FALSE;
648 foreach ($fields as $key => $field) {
649 if ($viewOnly &&
650 isset($field['data_type']) &&
651 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
652 ) {
653 // ignore file upload fields
654 continue;
655 }
656
133e2c99 657 if ($profileContactType) {
658 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
353ffa53
TO
659 $honoreeNamefields = array(
660 'prefix_id',
661 'first_name',
662 'last_name',
663 'suffix_id',
664 'organization_name',
389bcebf 665 'household_name',
353ffa53 666 );
133e2c99 667 if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) {
668 unset($fields[$field['name']]);
669 continue;
670 }
6a488035
TO
671 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
672 CRM_Core_BAO_UFGroup::buildProfile(
673 $this,
674 $field,
675 CRM_Profile_Form::MODE_CREATE,
676 $contactID,
133e2c99 677 TRUE,
678 $profileContactType
6a488035 679 );
133e2c99 680 $this->_fields[$profileContactType][$key] = $field;
6a488035
TO
681 }
682 else {
683 unset($fields[$key]);
684 }
685 }
686 else {
687 CRM_Core_BAO_UFGroup::buildProfile(
688 $this,
689 $field,
690 CRM_Profile_Form::MODE_CREATE,
691 $contactID,
692 TRUE
693 );
694 $this->_fields[$key] = $field;
695 }
71fc6ea4
DG
696 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
697 if ($field['add_captcha'] && !$this->_userID) {
6a488035
TO
698 $addCaptcha = TRUE;
699 }
700 }
701
702 $this->assign($name, $fields);
703
6a488035
TO
704 if ($addCaptcha && !$viewOnly) {
705 $captcha = CRM_Utils_ReCAPTCHA::singleton();
706 $captcha->add($this);
707 $this->assign('isCaptcha', TRUE);
708 }
709 }
710 }
711 }
712
bcb8cc84 713
714 public function buildComponentForm($id, $form) {
715 if (empty($id)) {
716 return;
717 }
718
719 $contactID = $this->getContactID();
720
721 foreach (array('soft_credit', 'on_behalf') as $module) {
722 $ufJoinParams = array(
723 'module' => $module,
724 'entity_table' => 'civicrm_contribution_page',
725 'entity_id' => $id,
726 );
727
728 $ufJoin = new CRM_Core_DAO_UFJoin();
729 $ufJoin->copyValues($ufJoinParams);
730 $ufJoin->find(TRUE);
731 if (!$ufJoin->is_active) {
732 continue;
733 }
734
735 if ($module == 'soft_credit') {
736 $form->_honoreeProfileId = $ufJoin->uf_group_id;
737 $form->_honor_block_is_active = $ufJoin->is_active;
738
739 if (!$form->_honoreeProfileId ||
740 !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_honoreeProfileId, 'is_active')
741 ) {
742 CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the selected honoree profile is either disabled or not found.'));
743 }
744
745 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_honoreeProfileId);
746 $requiredProfileFields = array(
747 'Individual' => array('first_name', 'last_name'),
748 'Organization' => array('organization_name', 'email'),
749 'Household' => array('household_name', 'email'),
750 );
751 $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_honoreeProfileId, $requiredProfileFields[$profileContactType]);
752 if (!$validProfile) {
753 CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of honoree and the required fields of the selected honoree profile are disabled or doesn\'t exist.'));
754 }
755
756 //build soft-credit section
757 CRM_Contribute_Form_SoftCredit::buildQuickForm($form);
758 }
759 else {
760 $params = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoin->module_data, TRUE, 'on_behalf');
761 $form->_values = array_merge($params, $form->_values);
762
763 $onBehalfProfileId = $ufJoin->uf_group_id;
764
765 if (!$onBehalfProfileId ||
766 !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $onBehalfProfileId, 'is_active')
767 ) {
768 CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of an organization and the selected onbehalf profile is either disabled or not found.'));
769 }
770
771 $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id);
772 if (empty($member['is_active'])) {
773 $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
774 $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($onBehalfProfileId);
775 foreach (array(
776 'Individual',
777 'Organization',
778 'Household',
779 ) as $contactType) {
780 if (in_array($contactType, $onBehalfProfile) &&
781 (in_array('Membership', $onBehalfProfile) ||
782 in_array('Contribution', $onBehalfProfile)
783 )
784 ) {
785 CRM_Core_Error::fatal($msg);
786 }
787 }
788 }
789
6cc679d2 790 $form->addElement('hidden', 'onbehalf_profile_id', $onBehalfProfileId);
791 // TODO: submitted values of on-behalf snippet aren't carried to successive form,
792 // so this is hackish fix for now to carry the submitted on-behalf profile values in json format
793 if (!empty($form->_submitValues['onbehalf'])) {
794 $form->addElement('hidden', 'onbehalf_values', json_encode($form->_submitValues['onbehalf']));
bcb8cc84 795 }
796
797 if (CRM_Utils_Array::value('is_for_organization', $params)) {
798 if ($params['is_for_organization'] == 2) {
799 $this->assign('onBehalfRequired', TRUE);
800 }
801 else {
802 $form->addElement('checkbox', 'is_for_organization',
803 $form->_values['for_organization'],
804 NULL
805 );
806 }
807 }
808 $form->assign('onBehalfprofileId', $onBehalfProfileId);
809 }
810 }
811
812 }
813
f4aaa82a 814 /**
fe482240 815 * Check template file exists.
f92d1e2a
EM
816 *
817 * @param string $suffix
f4aaa82a
EM
818 *
819 * @return null|string
820 */
00be9182 821 public function checkTemplateFileExists($suffix = NULL) {
6a488035
TO
822 if ($this->_id) {
823 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
824 $template = CRM_Core_Form::getTemplate();
825 if ($template->template_exists($templateFile)) {
826 return $templateFile;
827 }
828 }
829 return NULL;
830 }
831
186c9c17 832 /**
fe482240 833 * Use the form name to create the tpl file name.
186c9c17
EM
834 *
835 * @return string
186c9c17 836 */
00be9182 837 public function getTemplateFileName() {
6a488035
TO
838 $fileName = $this->checkTemplateFileExists();
839 return $fileName ? $fileName : parent::getTemplateFileName();
840 }
841
186c9c17 842 /**
f92d1e2a
EM
843 * Add the extra.tpl in.
844 *
186c9c17 845 * Default extra tpl file basically just replaces .tpl with .extra.tpl
f92d1e2a 846 * i.e. we do not override - why isn't this done at the CRM_Core_Form level?
186c9c17
EM
847 *
848 * @return string
186c9c17 849 */
00be9182 850 public function overrideExtraTemplateFileName() {
6a488035
TO
851 $fileName = $this->checkTemplateFileExists('extra.');
852 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
853 }
854
855 /**
100fef9d 856 * Authenticate pledge user during online payment.
6a488035
TO
857 */
858 public function authenticatePledgeUser() {
859 //get the userChecksum and contact id
860 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
861 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
862
863 //get pledge status and contact id
353ffa53
TO
864 $pledgeValues = array();
865 $pledgeParams = array('id' => $this->_values['pledge_id']);
6a488035
TO
866 $returnProperties = array('contact_id', 'status_id');
867 CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
868
869 //get all status
870 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
353ffa53
TO
871 $validStatus = array(
872 array_search('Pending', $allStatus),
6a488035
TO
873 array_search('In Progress', $allStatus),
874 array_search('Overdue', $allStatus),
875 );
876
877 $validUser = FALSE;
878 if ($this->_userID &&
879 $this->_userID == $pledgeValues['contact_id']
880 ) {
881 //check for authenticated user.
882 $validUser = TRUE;
883 }
884 elseif ($userChecksum && $pledgeValues['contact_id']) {
885 //check for anonymous user.
886 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum);
887
888 //make sure cid is same as pledge contact id
889 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
890 $validUser = FALSE;
891 }
892 }
893
894 if (!$validUser) {
895 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."));
896 }
897
898 //check for valid pledge status.
899 if (!in_array($pledgeValues['status_id'], $validStatus)) {
900 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))));
901 }
902 }
903
904 /**
f92d1e2a
EM
905 * Cancel recurring contributions.
906 *
6a488035
TO
907 * In case user cancel recurring contribution,
908 * When we get the control back from payment gate way
909 * lets delete the recurring and related contribution.
389bcebf 910 */
6a488035
TO
911 public function cancelRecurring() {
912 $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject);
913 if ($isCancel) {
914 $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject);
915 $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject);
916 //clean db for recurring contribution.
917 if ($isRecur && $recurId) {
918 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
919 }
920 $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject);
921 if ($contribId) {
922 CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
923 }
924 }
925 }
96025800 926
42e3a033
EM
927 /**
928 * Build Membership Block in Contribution Pages.
929 *
42e3a033
EM
930 * @param int $cid
931 * Contact checked for having a current membership for a particular membership.
a46bfec1
EM
932 * @param bool $isContributionMainPage
933 * Is this the main page? If so add form input fields.
934 * (or better yet don't have this functionality in a function shared with forms that don't share it).
42e3a033
EM
935 * @param int $selectedMembershipTypeID
936 * Selected membership id.
937 * @param bool $thankPage
938 * Thank you page.
939 * @param null $isTest
940 *
941 * @return bool
942 * Is this a separate membership payment
943 */
944 protected function buildMembershipBlock(
945 $cid,
a46bfec1 946 $isContributionMainPage = FALSE,
42e3a033
EM
947 $selectedMembershipTypeID = NULL,
948 $thankPage = FALSE,
949 $isTest = NULL
950 ) {
951
952 $separateMembershipPayment = FALSE;
953 if ($this->_membershipBlock) {
954 $this->_currentMemberships = array();
955
42e3a033
EM
956 $membershipTypeIds = $membershipTypes = $radio = array();
957 $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE;
958
959 $allowAutoRenewMembership = $autoRenewOption = FALSE;
960 $autoRenewMembershipTypeOptions = array();
961
a46bfec1 962 $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock);
42e3a033
EM
963
964 if ($membershipPriceset) {
965 foreach ($this->_priceSet['fields'] as $pField) {
966 if (empty($pField['options'])) {
967 continue;
968 }
969 foreach ($pField['options'] as $opId => $opValues) {
970 if (empty($opValues['membership_type_id'])) {
971 continue;
972 }
973 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
974 }
975 }
976 }
a46bfec1
EM
977 elseif (!empty($this->_membershipBlock['membership_types'])) {
978 $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']);
42e3a033
EM
979 }
980
981 if (!empty($membershipTypeIds)) {
982 //set status message if wrong membershipType is included in membershipBlock
983 if (isset($this->_mid) && !$membershipPriceset) {
984 $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
985 $this->_mid,
986 'membership_type_id'
987 );
988 if (!in_array($membershipTypeID, $membershipTypeIds)) {
989 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('Invalid Membership'), 'error');
990 }
991 }
992
4c7b8a7d 993 $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds);
42e3a033
EM
994 $this->_membershipTypeValues = $membershipTypeValues;
995 $endDate = NULL;
996 foreach ($membershipTypeIds as $value) {
997 $memType = $membershipTypeValues[$value];
998 if ($selectedMembershipTypeID != NULL) {
999 if ($memType['id'] == $selectedMembershipTypeID) {
1000 $this->assign('minimum_fee',
1001 CRM_Utils_Array::value('minimum_fee', $memType)
1002 );
1003 $this->assign('membership_name', $memType['name']);
1004 if (!$thankPage && $cid) {
1005 $membership = new CRM_Member_DAO_Membership();
1006 $membership->contact_id = $cid;
1007 $membership->membership_type_id = $memType['id'];
1008 if ($membership->find(TRUE)) {
1009 $this->assign('renewal_mode', TRUE);
1010 $memType['current_membership'] = $membership->end_date;
1011 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
1012 }
1013 }
1014 $membershipTypes[] = $memType;
1015 }
1016 }
1017 elseif ($memType['is_active']) {
1018 $javascriptMethod = NULL;
08a4ce4e 1019 $allowAutoRenewOpt = (int) $memType['auto_renew'];
42e3a033
EM
1020 if (is_array($this->_paymentProcessors)) {
1021 foreach ($this->_paymentProcessors as $id => $val) {
1022 if (!$val['is_recur']) {
1023 $allowAutoRenewOpt = 0;
1024 continue;
1025 }
1026 }
1027 }
1028
1029 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
1030 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));;
1031
1032 if ($allowAutoRenewOpt) {
1033 $allowAutoRenewMembership = TRUE;
1034 }
1035
1036 //add membership type.
1037 $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL,
1038 $memType['id'], $javascriptMethod
1039 );
1040 if ($cid) {
1041 $membership = new CRM_Member_DAO_Membership();
1042 $membership->contact_id = $cid;
1043 $membership->membership_type_id = $memType['id'];
1044
1045 //show current membership, skip pending and cancelled membership records,
1046 //because we take first membership record id for renewal
1047 $membership->whereAdd('status_id != 5 AND status_id !=6');
1048
1049 if (!is_null($isTest)) {
1050 $membership->is_test = $isTest;
1051 }
1052
1053 //CRM-4297
1054 $membership->orderBy('end_date DESC');
1055
1056 if ($membership->find(TRUE)) {
1057 if (!$membership->end_date) {
1058 unset($radio[$memType['id']]);
1059 $this->assign('islifetime', TRUE);
1060 continue;
1061 }
1062 $this->assign('renewal_mode', TRUE);
1063 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
1064 $memType['current_membership'] = $membership->end_date;
1065 if (!$endDate) {
1066 $endDate = $memType['current_membership'];
1067 $this->_defaultMemTypeId = $memType['id'];
1068 }
1069 if ($memType['current_membership'] < $endDate) {
1070 $endDate = $memType['current_membership'];
1071 $this->_defaultMemTypeId = $memType['id'];
1072 }
1073 }
1074 }
1075 $membershipTypes[] = $memType;
1076 }
1077 }
1078 }
1079
a46bfec1
EM
1080 $this->assign('membershipBlock', $this->_membershipBlock);
1081 $this->assign('showRadio', $isContributionMainPage);
1082 $this->assign('membershipTypes', $membershipTypes);
1083 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
1084 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
1085 //give preference to user submitted auto_renew value.
1086 $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE;
1087 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
1088
1089 if ($isContributionMainPage) {
42e3a033 1090 if (!$membershipPriceset) {
a46bfec1 1091 if (!$this->_membershipBlock['is_required']) {
42e3a033
EM
1092 $this->assign('showRadioNoThanks', TRUE);
1093 $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
1094 $this->addGroup($radio, 'selectMembership', NULL);
1095 }
a46bfec1 1096 elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
42e3a033
EM
1097 $temp = array_keys($radio);
1098 $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
1099 $this->assign('singleMembership', TRUE);
1100 $this->assign('showRadio', FALSE);
1101 }
1102 else {
1103 $this->addGroup($radio, 'selectMembership', NULL);
1104 }
1105
1106 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
1107 }
1108 else {
1109 $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId);
1110 $this->assign('autoRenewOption', $autoRenewOption);
1111 }
1112
1113 if (!$this->_values['is_pay_later'] && is_array($this->_paymentProcessors) && ($allowAutoRenewMembership || $autoRenewOption)) {
1114 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
1115 }
1116
1117 }
42e3a033
EM
1118 }
1119
1120 return $separateMembershipPayment;
1121 }
1122
90102a32
EM
1123 /**
1124 * Determine if recurring parameters need to be added to the form parameters.
1125 * - is_recur
1126 * - frequency_interval
1127 * - frequency_unit
1128 *
1129 * For membership this is based on the membership type.
1130 *
1131 * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing.
1132 *
1133 * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout.
1134 */
1135 protected function setRecurringMembershipParams() {
fd359255
EM
1136 if (!empty($this->_params['priceSetId']) && !empty($this->_params['selectMembership'])) {
1137 // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore
1138 // 'selectMembership' and calculate from the price_x fields so we have one method that always works
1139 // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix
1140 // this with an else (calculate for price set).
1141 $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_params['priceSetId']);
1142 if (in_array($this->_params['selectMembership'], $membershipTypes['autorenew'])) {
1143 $this->_params['auto_renew'] = TRUE;
1144 }
1145 }
90102a32
EM
1146 if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId'])) && !empty($this->_paymentProcessor['is_recur']) &&
1147 CRM_Utils_Array::value('auto_renew', $this->_params) && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval'])
1148 ) {
1149
1150 $this->_params['is_recur'] = $this->_values['is_recur'] = 1;
1151 // check if price set is not quick config
1152 if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) {
1153 list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']);
1154 }
1155 else {
1156 // FIXME: set interval and unit based on selected membership type
1157 $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
1158 $this->_params['selectMembership'], 'duration_interval'
1159 );
1160 $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
1161 $this->_params['selectMembership'], 'duration_unit'
1162 );
1163 }
1164 }
1165 }
1166
6a488035 1167}