--ICM-17, added condition to check if ACL-FT is turned off or on
[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
dbddfb08
EM
173 /**
174 * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
175 * @var string
176 */
177 public $_contributeMode;
178
179 /**
100fef9d 180 * Contribution page supports memberships
dbddfb08
EM
181 * @var boolean
182 */
183 public $_useForMember;
8ae4d0d3 184
185 public $_isBillingAddressRequiredForPayLater;
353ffa53 186
6a488035 187 /**
fe482240 188 * Set variables up before form is built.
6a488035 189 *
7fe37828
EM
190 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
191 * @throws \Exception
6a488035
TO
192 */
193 public function preProcess() {
6a488035
TO
194
195 // current contribution page id
196 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
197 if (!$this->_id) {
d420cf02
DL
198 // seems like the session is corrupted and/or we lost the id trail
199 // lets just bump this to a regular session error and redirect user to main page
200 $this->controller->invalidKeyRedirect();
6a488035 201 }
d420cf02 202
5b757295 203 // this was used prior to the cleverer this_>getContactID - unsure now
a6c15c46 204 $this->_userID = CRM_Core_Session::singleton()->get('userID');
5b757295 205
206 $this->_contactID = $this->_membershipContactID = $this->getContactID();
6a488035 207 $this->_mid = NULL;
5b757295 208 if ($this->_contactID) {
6a488035
TO
209 $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
210 if ($this->_mid) {
211 $membership = new CRM_Member_DAO_Membership();
212 $membership->id = $this->_mid;
213
214 if ($membership->find(TRUE)) {
215 $this->_defaultMemTypeId = $membership->membership_type_id;
5b757295 216 if ($membership->contact_id != $this->_contactID) {
6fe8deba 217 $validMembership = FALSE;
33260076 218 $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization');
219 if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) {
6fe8deba
DS
220 $this->_membershipContactID = $membership->contact_id;
221 $this->assign('membershipContactID', $this->_membershipContactID);
33260076 222 $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']);
6fe8deba 223 $validMembership = TRUE;
0db6c3e1
TO
224 }
225 else {
51e89def
DS
226 $membershipType = new CRM_Member_BAO_MembershipType();
227 $membershipType->id = $membership->membership_type_id;
228 if ($membershipType->find(TRUE)) {
f9f0eff9 229 // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values.
f92d1e2a 230 // Convert to comma separated list.
f9f0eff9 231 $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ',');
51e89def
DS
232 $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id);
233 if (array_key_exists($membership->contact_id, $permContacts)) {
234 $this->_membershipContactID = $membership->contact_id;
6fe8deba 235 $validMembership = TRUE;
51e89def
DS
236 }
237 }
6a488035 238 }
6fe8deba
DS
239 if (!$validMembership) {
240 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');
241 }
6a488035
TO
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);
0799e1c4
E
277 if (!CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id']))) {
278 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
279 }
a7488080 280 if (empty($this->_values['is_active'])) {
4b57bc9f 281 throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id);
6a488035
TO
282 }
283
8345c9d3 284 $this->assignBillingType();
6a488035
TO
285
286 // check for is_monetary status
287 $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values);
288 $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values);
289
6a488035 290 if ($isMonetary &&
8cc574cf 291 (!$isPayLater || !empty($this->_values['payment_processor']))
6a488035 292 ) {
a6c15c46
EM
293 $this->_paymentProcessorIDs = explode(
294 CRM_Core_DAO::VALUE_SEPARATOR,
295 CRM_Utils_Array::value('payment_processor', $this->_values)
1b9f9ca3 296 );
f92d1e2a 297
1b9f9ca3 298 $this->assignPaymentProcessor();
6a488035
TO
299 }
300
301 // get price info
302 // CRM-5095
9da8dc8c 303 CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
6a488035
TO
304
305 // this avoids getting E_NOTICE errors in php
306 $setNullFields = array(
307 'amount_block_is_active',
6a488035
TO
308 'is_allow_other_amount',
309 'footer_text',
310 );
311 foreach ($setNullFields as $f) {
312 if (!isset($this->_values[$f])) {
313 $this->_values[$f] = NULL;
314 }
315 }
316
317 //check if Membership Block is enabled, if Membership Fields are included in profile
318 //get membership section for this contribution page
319 $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
320 $this->set('membershipBlock', $this->_membershipBlock);
321
4779abb3 322 if (!empty($this->_values['custom_pre_id'])) {
6a488035
TO
323 $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']);
324 }
325
4779abb3 326 if (!empty($this->_values['custom_post_id'])) {
6a488035
TO
327 $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']);
328 }
329
330 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
331 (isset($preProfileType) && $preProfileType == 'Membership')
332 ) &&
333 !$this->_membershipBlock['is_active']
334 ) {
335 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.'));
336 }
337
338 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id);
339
340 if ($pledgeBlock) {
341 $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock);
342 $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock);
343 $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock);
344 $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock);
345
346 //set pledge id in values
347 $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this);
348
349 //authenticate pledge user for pledge payment.
350 if ($pledgeId) {
351 $this->_values['pledge_id'] = $pledgeId;
352
353 //lets override w/ pledge campaign.
354 $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge',
355 $pledgeId,
356 'campaign_id'
357 );
358 self::authenticatePledgeUser();
359 }
360 }
361 $this->set('values', $this->_values);
362 $this->set('fields', $this->_fields);
363 }
364
365 // Handle PCP
366 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
367 if ($pcpId) {
353ffa53
TO
368 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
369 $this->_pcpId = $pcp['pcpId'];
6a488035 370 $this->_pcpBlock = $pcp['pcpBlock'];
353ffa53 371 $this->_pcpInfo = $pcp['pcpInfo'];
6a488035
TO
372 }
373
374 // Link (button) for users to create their own Personal Campaign page
375 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) {
376 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
377 "action=add&reset=1&pageId={$this->_id}&component=contribute",
378 FALSE, NULL, TRUE
379 );
380 $this->assign('linkTextUrl', $linkTextUrl);
381 $this->assign('linkText', $linkText);
382 }
383
384 //set pledge block if block id is set
a7488080 385 if (!empty($this->_values['pledge_block_id'])) {
6a488035
TO
386 $this->assign('pledgeBlock', TRUE);
387 }
388
f92d1e2a 389 // check if one of the (amount , membership) blocks is active or not.
6a488035
TO
390 $this->_membershipBlock = $this->get('membershipBlock');
391
392 if (!$this->_values['amount_block_is_active'] &&
393 !$this->_membershipBlock['is_active'] &&
394 !$this->_priceSetId
395 ) {
396 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.'));
397 }
398
399 if ($this->_values['amount_block_is_active']) {
400 $this->set('amount_block_is_active', $this->_values['amount_block_is_active']);
401 }
402
403 $this->_contributeMode = $this->get('contributeMode');
404 $this->assign('contributeMode', $this->_contributeMode);
405
406 //assigning is_monetary and is_email_receipt to template
407 $this->assign('is_monetary', $this->_values['is_monetary']);
408 $this->assign('is_email_receipt', $this->_values['is_email_receipt']);
409 $this->assign('bltID', $this->_bltID);
410
411 //assign cancelSubscription URL to templates
412 $this->assign('cancelSubscriptionUrl',
413 CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values)
414 );
415
416 // assigning title to template in case someone wants to use it, also setting CMS page title
417 if ($this->_pcpId) {
418 $this->assign('title', $this->_pcpInfo['title']);
419 CRM_Utils_System::setTitle($this->_pcpInfo['title']);
420 }
421 else {
422 $this->assign('title', $this->_values['title']);
423 CRM_Utils_System::setTitle($this->_values['title']);
424 }
425 $this->_defaults = array();
426
427 $this->_amount = $this->get('amount');
428
429 //CRM-6907
430 $config = CRM_Core_Config::singleton();
431 $config->defaultCurrency = CRM_Utils_Array::value('currency',
432 $this->_values,
433 $config->defaultCurrency
434 );
435
436 //lets allow user to override campaign.
437 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
438 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
439 $this->_values['campaign_id'] = $campID;
440 }
441
442 //do check for cancel recurring and clean db, CRM-7696
443 if (CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject)) {
444 self::cancelRecurring();
445 }
8ae4d0d3 446
447 // check if billing block is required for pay later
448 if (CRM_Utils_Array::value('is_pay_later', $this->_values)) {
449 $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values);
450 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
451 }
6a488035
TO
452 }
453
454 /**
fe482240 455 * Set the default values.
6a488035 456 */
00be9182 457 public function setDefaultValues() {
6a488035
TO
458 return $this->_defaults;
459 }
460
461 /**
fe482240 462 * Assign the minimal set of variables to the template.
6a488035 463 */
00be9182 464 public function assignToTemplate() {
6a488035 465 $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
a7488080 466 if (!empty($this->_params['billing_middle_name'])) {
6a488035
TO
467 $name .= " {$this->_params['billing_middle_name']}";
468 }
469 $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params);
470 $name = trim($name);
471 $this->assign('billingName', $name);
472 $this->set('name', $name);
473
474 $this->assign('paymentProcessor', $this->_paymentProcessor);
475 $vars = array(
353ffa53
TO
476 'amount',
477 'currencyID',
478 'credit_card_type',
479 'trxn_id',
480 'amount_level',
6a488035
TO
481 );
482
483 $config = CRM_Core_Config::singleton();
8cc574cf 484 if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) {
6a488035
TO
485 $this->assign('is_recur_enabled', 1);
486 $vars = array_merge($vars, array(
353ffa53
TO
487 'is_recur',
488 'frequency_interval',
489 'frequency_unit',
490 'installments',
491 ));
6a488035
TO
492 }
493
494 if (in_array('CiviPledge', $config->enableComponents) &&
495 CRM_Utils_Array::value('is_pledge', $this->_params) == 1
496 ) {
497 $this->assign('pledge_enabled', 1);
498
499 $vars = array_merge($vars, array(
500 'is_pledge',
353ffa53
TO
501 'pledge_frequency_interval',
502 'pledge_frequency_unit',
503 'pledge_installments',
504 ));
6a488035
TO
505 }
506
507 if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) {
508 $this->_params['amount_level'] = '';
509 }
510
511 foreach ($vars as $v) {
3fb990f4 512 if (isset($this->_params[$v])) {
735fe42d
PJ
513 if ($v == "amount" && $this->_params[$v] === 0) {
514 $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE);
3fb990f4 515 }
6a488035
TO
516 $this->assign($v, $this->_params[$v]);
517 }
518 }
519
520 // assign the address formatted up for display
521 $addressParts = array(
522 "street_address-{$this->_bltID}",
523 "city-{$this->_bltID}",
524 "postal_code-{$this->_bltID}",
525 "state_province-{$this->_bltID}",
526 "country-{$this->_bltID}",
527 );
528
529 $addressFields = array();
530 foreach ($addressParts as $part) {
531 list($n, $id) = explode('-', $part);
532 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $this->_params);
533 }
534
535 $this->assign('address', CRM_Utils_Address::format($addressFields));
536
cb804cd9 537 if (!empty($this->_params['onbehalf_profile_id']) && !empty($this->_params['onbehalf'])) {
6a488035
TO
538 $this->assign('onBehalfName', $this->_params['organization_name']);
539 $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
540 $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
541 }
542
543 //fix for CRM-3767
544 $assignCCInfo = FALSE;
545 if ($this->_amount > 0.0) {
546 $assignCCInfo = TRUE;
547 }
a7488080 548 elseif (!empty($this->_params['selectMembership'])) {
6a488035
TO
549 $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
550 if ($memFee > 0.0) {
551 $assignCCInfo = TRUE;
552 }
553 }
554
555 if ($this->_contributeMode == 'direct' && $assignCCInfo) {
f92fc7eb
CW
556 if ($this->_paymentProcessor &&
557 $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
558 ) {
6a488035
TO
559 $this->assign('account_holder', $this->_params['account_holder']);
560 $this->assign('bank_identification_number', $this->_params['bank_identification_number']);
561 $this->assign('bank_name', $this->_params['bank_name']);
562 $this->assign('bank_account_number', $this->_params['bank_account_number']);
563 }
564 else {
565 $date = CRM_Utils_Date::format(CRM_Utils_array::value('credit_card_exp_date', $this->_params));
566 $date = CRM_Utils_Date::mysqlToIso($date);
567 $this->assign('credit_card_exp_date', $date);
568 $this->assign('credit_card_number',
353ffa53 569 CRM_Utils_System::mungeCreditCard(CRM_Utils_array::value('credit_card_number', $this->_params))
6a488035
TO
570 );
571 }
572 }
573
574 $this->assign('email',
575 $this->controller->exportValue('Main', "email-{$this->_bltID}")
576 );
577
578 // also assign the receipt_text
579 if (isset($this->_values['receipt_text'])) {
580 $this->assign('receipt_text', $this->_values['receipt_text']);
581 }
582 }
583
584 /**
fe482240 585 * Add the custom fields.
6a488035 586 *
100fef9d
CW
587 * @param int $id
588 * @param string $name
f4aaa82a
EM
589 * @param bool $viewOnly
590 * @param null $profileContactType
f92d1e2a 591 * @param array $fieldTypes
6a488035 592 */
00be9182 593 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
6a488035 594 if ($id) {
da8d9879 595 $contactID = $this->getContactID();
6a488035
TO
596
597 // we don't allow conflicting fields to be
598 // configured via profile - CRM 2100
599 $fieldsToIgnore = array(
600 'receive_date' => 1,
601 'trxn_id' => 1,
602 'invoice_id' => 1,
603 'net_amount' => 1,
604 'fee_amount' => 1,
605 'non_deductible_amount' => 1,
606 'total_amount' => 1,
607 'amount_level' => 1,
608 'contribution_status_id' => 1,
609 'payment_instrument' => 1,
610 'check_number' => 1,
611 'financial_type' => 1,
612 );
613
614 $fields = NULL;
615 if ($contactID && CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
616 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
617 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
618 );
619 }
620 else {
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
626 if ($fields) {
627 // unset any email-* fields since we already collect it, CRM-2888
628 foreach (array_keys($fields) as $fieldName) {
c043358f 629 if (substr($fieldName, 0, 6) == 'email-' && !in_array($profileContactType, array('honor', 'onbehalf'))) {
6a488035
TO
630 unset($fields[$fieldName]);
631 }
632 }
633
634 if (array_intersect_key($fields, $fieldsToIgnore)) {
635 $fields = array_diff_key($fields, $fieldsToIgnore);
636 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
637 }
638
639 $fields = array_diff_assoc($fields, $this->_fields);
640
641 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
642 $addCaptcha = FALSE;
643 foreach ($fields as $key => $field) {
644 if ($viewOnly &&
645 isset($field['data_type']) &&
646 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
647 ) {
648 // ignore file upload fields
649 continue;
650 }
651
133e2c99 652 if ($profileContactType) {
653 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
c043358f 654 if ($profileContactType == 'honor') {
655 $honoreeNamefields = array(
656 'prefix_id',
657 'first_name',
658 'last_name',
659 'suffix_id',
660 'organization_name',
661 'household_name',
662 );
663 if (in_array($field['name'], $honoreeNamefields)) {
664 unset($fields[$field['name']]);
665 continue;
666 }
133e2c99 667 }
6a488035
TO
668 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
669 CRM_Core_BAO_UFGroup::buildProfile(
670 $this,
671 $field,
672 CRM_Profile_Form::MODE_CREATE,
673 $contactID,
133e2c99 674 TRUE,
675 $profileContactType
6a488035 676 );
133e2c99 677 $this->_fields[$profileContactType][$key] = $field;
6a488035
TO
678 }
679 else {
680 unset($fields[$key]);
681 }
682 }
683 else {
684 CRM_Core_BAO_UFGroup::buildProfile(
685 $this,
686 $field,
687 CRM_Profile_Form::MODE_CREATE,
688 $contactID,
689 TRUE
690 );
691 $this->_fields[$key] = $field;
692 }
71fc6ea4
DG
693 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
694 if ($field['add_captcha'] && !$this->_userID) {
6a488035
TO
695 $addCaptcha = TRUE;
696 }
697 }
698
699 $this->assign($name, $fields);
700
6a488035
TO
701 if ($addCaptcha && !$viewOnly) {
702 $captcha = CRM_Utils_ReCAPTCHA::singleton();
703 $captcha->add($this);
704 $this->assign('isCaptcha', TRUE);
705 }
706 }
707 }
708 }
709
4779abb3 710 /**
711 * Add onbehalf/honoree profile fields and native module fields.
712 *
713 * @param int $id
714 * @param CRM_Core_Form $form
715 */
bcb8cc84 716 public function buildComponentForm($id, $form) {
717 if (empty($id)) {
718 return;
719 }
720
721 $contactID = $this->getContactID();
722
723 foreach (array('soft_credit', 'on_behalf') as $module) {
bcb8cc84 724 if ($module == 'soft_credit') {
4779abb3 725 if (empty($form->_values['honoree_profile_id'])) {
726 continue;
727 }
bcb8cc84 728
4779abb3 729 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['honoree_profile_id'], 'is_active')) {
bcb8cc84 730 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.'));
731 }
732
4779abb3 733 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']);
bcb8cc84 734 $requiredProfileFields = array(
735 'Individual' => array('first_name', 'last_name'),
736 'Organization' => array('organization_name', 'email'),
737 'Household' => array('household_name', 'email'),
738 );
4779abb3 739 $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_values['honoree_profile_id'], $requiredProfileFields[$profileContactType]);
bcb8cc84 740 if (!$validProfile) {
741 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.'));
742 }
743
cb804cd9 744 foreach (array('honor_block_title', 'honor_block_text') as $name) {
4779abb3 745 $form->assign($name, $form->_values[$name]);
cb804cd9 746 }
747
748 $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE);
749
750 // radio button for Honor Type
4779abb3 751 foreach ($form->_values['soft_credit_types'] as $value) {
cb804cd9 752 $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value);
753 }
754 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE);
755
4779abb3 756 $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields(
757 $this->_values['honoree_profile_id'], FALSE,
758 NULL, NULL,
759 NULL, FALSE,
760 NULL, TRUE,
761 NULL, CRM_Core_Permission::CREATE
cb804cd9 762 );
cb804cd9 763 $form->assign('honoreeProfileFields', $honoreeProfileFields);
764
765 // add the form elements
766 foreach ($honoreeProfileFields as $name => $field) {
767 // If soft credit type is not chosen then make omit requiredness from honoree profile fields
768 if (count($form->_submitValues) &&
769 empty($form->_submitValues['soft_credit_type_id']) &&
770 !empty($field['is_required'])
771 ) {
772 $field['is_required'] = FALSE;
773 }
c043358f 774 CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, 'honor');
cb804cd9 775 }
bcb8cc84 776 }
777 else {
4779abb3 778 if (empty($form->_values['onbehalf_profile_id'])) {
779 continue;
780 }
bcb8cc84 781
4779abb3 782 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['onbehalf_profile_id'], 'is_active')) {
bcb8cc84 783 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.'));
784 }
785
786 $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id);
787 if (empty($member['is_active'])) {
788 $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
4779abb3 789 $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($form->_values['onbehalf_profile_id']);
bcb8cc84 790 foreach (array(
791 'Individual',
792 'Organization',
793 'Household',
794 ) as $contactType) {
795 if (in_array($contactType, $onBehalfProfile) &&
796 (in_array('Membership', $onBehalfProfile) ||
797 in_array('Contribution', $onBehalfProfile)
798 )
799 ) {
800 CRM_Core_Error::fatal($msg);
801 }
802 }
803 }
804
c043358f 805 if ($contactID) {
33260076 806 // retrieve all permissioned organizations of contact $contactID
807 $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization');
c043358f 808
33260076 809 if (count($organizations)) {
c043358f 810 // Related org url - pass checksum if needed
f23093b2 811 $args = array(
4779abb3 812 'ufId' => $form->_values['onbehalf_profile_id'],
f23093b2 813 'cid' => '',
814 );
c043358f 815 if (!empty($_GET['cs'])) {
816 $args = array(
4779abb3 817 'ufId' => $form->_values['onbehalf_profile_id'],
c043358f 818 'uid' => $this->_contactID,
819 'cs' => $_GET['cs'],
820 'cid' => '',
821 );
822 }
823 $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE);
824 $form->assign('locDataURL', $locDataURL);
825 }
33260076 826 if (count($organizations) > 0) {
827 $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations));
c043358f 828
829 $orgOptions = array(
830 0 => ts('Select an existing organization'),
831 1 => ts('Enter a new organization'),
832 );
833 $form->addRadio('org_option', ts('options'), $orgOptions);
834 $form->setDefaults(array('org_option' => 0));
835 }
bcb8cc84 836 }
837
c043358f 838 $form->assign('fieldSetTitle', ts('Organization Details'));
c043358f 839
4779abb3 840 if (CRM_Utils_Array::value('is_for_organization', $form->_values)) {
841 if ($form->_values['is_for_organization'] == 2) {
c043358f 842 $form->assign('onBehalfRequired', TRUE);
bcb8cc84 843 }
844 else {
845 $form->addElement('checkbox', 'is_for_organization',
846 $form->_values['for_organization'],
847 NULL
848 );
849 }
850 }
c043358f 851
4779abb3 852 $profileFields = CRM_Core_BAO_UFGroup::getFields(
853 $form->_values['onbehalf_profile_id'],
854 FALSE, CRM_Core_Action::VIEW, NULL,
855 NULL, FALSE, NULL, FALSE, NULL,
856 CRM_Core_Permission::CREATE, NULL
c043358f 857 );
4779abb3 858
c043358f 859 $form->assign('onBehalfOfFields', $profileFields);
860 if (!empty($form->_submitValues['onbehalf'])) {
861 if (!empty($form->_submitValues['onbehalfof_id'])) {
862 $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']);
863 }
864 $form->assign('submittedOnBehalfInfo', json_encode($form->_submitValues['onbehalf']));
865 }
866
867 $fieldTypes = array('Contact', 'Organization');
868 $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization');
869 $fieldTypes = array_merge($fieldTypes, $contactSubType);
870
871 foreach ($profileFields as $name => $field) {
872 if (in_array($field['field_type'], $fieldTypes)) {
873 list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2);
874 if (in_array($prefixName, array('organization_name', 'email')) && empty($field['is_required'])) {
875 $field['is_required'] = 1;
876 }
877 if (count($form->_submitValues) &&
878 empty($form->_submitValues['is_for_organization']) &&
4779abb3 879 $form->_values['is_for_organization'] == 1 &&
c043358f 880 !empty($field['is_required'])
881 ) {
882 $field['is_required'] = FALSE;
883 }
4779abb3 884 CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf', NULL, 'onbehalf');
c043358f 885 }
886 }
bcb8cc84 887 }
888 }
889
890 }
891
f4aaa82a 892 /**
fe482240 893 * Check template file exists.
f92d1e2a
EM
894 *
895 * @param string $suffix
f4aaa82a
EM
896 *
897 * @return null|string
898 */
00be9182 899 public function checkTemplateFileExists($suffix = NULL) {
6a488035
TO
900 if ($this->_id) {
901 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
902 $template = CRM_Core_Form::getTemplate();
903 if ($template->template_exists($templateFile)) {
904 return $templateFile;
905 }
906 }
907 return NULL;
908 }
909
186c9c17 910 /**
fe482240 911 * Use the form name to create the tpl file name.
186c9c17
EM
912 *
913 * @return string
186c9c17 914 */
00be9182 915 public function getTemplateFileName() {
6a488035
TO
916 $fileName = $this->checkTemplateFileExists();
917 return $fileName ? $fileName : parent::getTemplateFileName();
918 }
919
186c9c17 920 /**
f92d1e2a
EM
921 * Add the extra.tpl in.
922 *
186c9c17 923 * Default extra tpl file basically just replaces .tpl with .extra.tpl
f92d1e2a 924 * i.e. we do not override - why isn't this done at the CRM_Core_Form level?
186c9c17
EM
925 *
926 * @return string
186c9c17 927 */
00be9182 928 public function overrideExtraTemplateFileName() {
6a488035
TO
929 $fileName = $this->checkTemplateFileExists('extra.');
930 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
931 }
932
933 /**
100fef9d 934 * Authenticate pledge user during online payment.
6a488035
TO
935 */
936 public function authenticatePledgeUser() {
937 //get the userChecksum and contact id
938 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
939 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
940
941 //get pledge status and contact id
353ffa53
TO
942 $pledgeValues = array();
943 $pledgeParams = array('id' => $this->_values['pledge_id']);
6a488035
TO
944 $returnProperties = array('contact_id', 'status_id');
945 CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
946
947 //get all status
948 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
353ffa53
TO
949 $validStatus = array(
950 array_search('Pending', $allStatus),
6a488035
TO
951 array_search('In Progress', $allStatus),
952 array_search('Overdue', $allStatus),
953 );
954
955 $validUser = FALSE;
956 if ($this->_userID &&
957 $this->_userID == $pledgeValues['contact_id']
958 ) {
959 //check for authenticated user.
960 $validUser = TRUE;
961 }
962 elseif ($userChecksum && $pledgeValues['contact_id']) {
963 //check for anonymous user.
964 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum);
965
966 //make sure cid is same as pledge contact id
967 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
968 $validUser = FALSE;
969 }
970 }
971
972 if (!$validUser) {
973 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."));
974 }
975
976 //check for valid pledge status.
977 if (!in_array($pledgeValues['status_id'], $validStatus)) {
978 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))));
979 }
980 }
981
982 /**
f92d1e2a
EM
983 * Cancel recurring contributions.
984 *
6a488035
TO
985 * In case user cancel recurring contribution,
986 * When we get the control back from payment gate way
987 * lets delete the recurring and related contribution.
389bcebf 988 */
6a488035
TO
989 public function cancelRecurring() {
990 $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject);
991 if ($isCancel) {
992 $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject);
993 $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject);
994 //clean db for recurring contribution.
995 if ($isRecur && $recurId) {
996 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
997 }
998 $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject);
999 if ($contribId) {
1000 CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
1001 }
1002 }
1003 }
96025800 1004
42e3a033
EM
1005 /**
1006 * Build Membership Block in Contribution Pages.
1007 *
42e3a033
EM
1008 * @param int $cid
1009 * Contact checked for having a current membership for a particular membership.
a46bfec1
EM
1010 * @param bool $isContributionMainPage
1011 * Is this the main page? If so add form input fields.
1012 * (or better yet don't have this functionality in a function shared with forms that don't share it).
42e3a033
EM
1013 * @param int $selectedMembershipTypeID
1014 * Selected membership id.
1015 * @param bool $thankPage
1016 * Thank you page.
1017 * @param null $isTest
1018 *
1019 * @return bool
1020 * Is this a separate membership payment
1021 */
1022 protected function buildMembershipBlock(
1023 $cid,
a46bfec1 1024 $isContributionMainPage = FALSE,
42e3a033
EM
1025 $selectedMembershipTypeID = NULL,
1026 $thankPage = FALSE,
1027 $isTest = NULL
1028 ) {
1029
1030 $separateMembershipPayment = FALSE;
1031 if ($this->_membershipBlock) {
1032 $this->_currentMemberships = array();
1033
42e3a033
EM
1034 $membershipTypeIds = $membershipTypes = $radio = array();
1035 $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE;
1036
1037 $allowAutoRenewMembership = $autoRenewOption = FALSE;
1038 $autoRenewMembershipTypeOptions = array();
1039
a46bfec1 1040 $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock);
42e3a033
EM
1041
1042 if ($membershipPriceset) {
1043 foreach ($this->_priceSet['fields'] as $pField) {
1044 if (empty($pField['options'])) {
1045 continue;
1046 }
1047 foreach ($pField['options'] as $opId => $opValues) {
1048 if (empty($opValues['membership_type_id'])) {
1049 continue;
1050 }
1051 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
1052 }
1053 }
1054 }
a46bfec1
EM
1055 elseif (!empty($this->_membershipBlock['membership_types'])) {
1056 $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']);
42e3a033
EM
1057 }
1058
1059 if (!empty($membershipTypeIds)) {
1060 //set status message if wrong membershipType is included in membershipBlock
1061 if (isset($this->_mid) && !$membershipPriceset) {
1062 $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
1063 $this->_mid,
1064 'membership_type_id'
1065 );
1066 if (!in_array($membershipTypeID, $membershipTypeIds)) {
1067 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');
1068 }
1069 }
1070
4c7b8a7d 1071 $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds);
42e3a033
EM
1072 $this->_membershipTypeValues = $membershipTypeValues;
1073 $endDate = NULL;
1074 foreach ($membershipTypeIds as $value) {
1075 $memType = $membershipTypeValues[$value];
1076 if ($selectedMembershipTypeID != NULL) {
1077 if ($memType['id'] == $selectedMembershipTypeID) {
1078 $this->assign('minimum_fee',
1079 CRM_Utils_Array::value('minimum_fee', $memType)
1080 );
1081 $this->assign('membership_name', $memType['name']);
1082 if (!$thankPage && $cid) {
1083 $membership = new CRM_Member_DAO_Membership();
1084 $membership->contact_id = $cid;
1085 $membership->membership_type_id = $memType['id'];
1086 if ($membership->find(TRUE)) {
1087 $this->assign('renewal_mode', TRUE);
1088 $memType['current_membership'] = $membership->end_date;
1089 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
1090 }
1091 }
1092 $membershipTypes[] = $memType;
1093 }
1094 }
1095 elseif ($memType['is_active']) {
1096 $javascriptMethod = NULL;
08a4ce4e 1097 $allowAutoRenewOpt = (int) $memType['auto_renew'];
42e3a033
EM
1098 if (is_array($this->_paymentProcessors)) {
1099 foreach ($this->_paymentProcessors as $id => $val) {
1100 if (!$val['is_recur']) {
1101 $allowAutoRenewOpt = 0;
1102 continue;
1103 }
1104 }
1105 }
1106
1107 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
1108 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));;
1109
1110 if ($allowAutoRenewOpt) {
1111 $allowAutoRenewMembership = TRUE;
1112 }
1113
1114 //add membership type.
1115 $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL,
1116 $memType['id'], $javascriptMethod
1117 );
1118 if ($cid) {
1119 $membership = new CRM_Member_DAO_Membership();
1120 $membership->contact_id = $cid;
1121 $membership->membership_type_id = $memType['id'];
1122
1123 //show current membership, skip pending and cancelled membership records,
1124 //because we take first membership record id for renewal
1125 $membership->whereAdd('status_id != 5 AND status_id !=6');
1126
1127 if (!is_null($isTest)) {
1128 $membership->is_test = $isTest;
1129 }
1130
1131 //CRM-4297
1132 $membership->orderBy('end_date DESC');
1133
1134 if ($membership->find(TRUE)) {
1135 if (!$membership->end_date) {
1136 unset($radio[$memType['id']]);
1137 $this->assign('islifetime', TRUE);
1138 continue;
1139 }
1140 $this->assign('renewal_mode', TRUE);
1141 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
1142 $memType['current_membership'] = $membership->end_date;
1143 if (!$endDate) {
1144 $endDate = $memType['current_membership'];
1145 $this->_defaultMemTypeId = $memType['id'];
1146 }
1147 if ($memType['current_membership'] < $endDate) {
1148 $endDate = $memType['current_membership'];
1149 $this->_defaultMemTypeId = $memType['id'];
1150 }
1151 }
1152 }
1153 $membershipTypes[] = $memType;
1154 }
1155 }
1156 }
1157
a46bfec1
EM
1158 $this->assign('membershipBlock', $this->_membershipBlock);
1159 $this->assign('showRadio', $isContributionMainPage);
1160 $this->assign('membershipTypes', $membershipTypes);
1161 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
1162 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
1163 //give preference to user submitted auto_renew value.
1164 $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE;
1165 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
1166
1167 if ($isContributionMainPage) {
42e3a033 1168 if (!$membershipPriceset) {
a46bfec1 1169 if (!$this->_membershipBlock['is_required']) {
42e3a033
EM
1170 $this->assign('showRadioNoThanks', TRUE);
1171 $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
1172 $this->addGroup($radio, 'selectMembership', NULL);
1173 }
a46bfec1 1174 elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
42e3a033
EM
1175 $temp = array_keys($radio);
1176 $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
1177 $this->assign('singleMembership', TRUE);
1178 $this->assign('showRadio', FALSE);
1179 }
1180 else {
1181 $this->addGroup($radio, 'selectMembership', NULL);
1182 }
1183
1184 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
1185 }
1186 else {
1187 $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId);
1188 $this->assign('autoRenewOption', $autoRenewOption);
1189 }
1190
1191 if (!$this->_values['is_pay_later'] && is_array($this->_paymentProcessors) && ($allowAutoRenewMembership || $autoRenewOption)) {
1192 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
1193 }
1194
1195 }
42e3a033
EM
1196 }
1197
1198 return $separateMembershipPayment;
1199 }
1200
90102a32
EM
1201 /**
1202 * Determine if recurring parameters need to be added to the form parameters.
1203 * - is_recur
1204 * - frequency_interval
1205 * - frequency_unit
1206 *
1207 * For membership this is based on the membership type.
1208 *
1209 * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing.
1210 *
1211 * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout.
1212 */
1213 protected function setRecurringMembershipParams() {
fd359255
EM
1214 if (!empty($this->_params['priceSetId']) && !empty($this->_params['selectMembership'])) {
1215 // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore
1216 // 'selectMembership' and calculate from the price_x fields so we have one method that always works
1217 // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix
1218 // this with an else (calculate for price set).
1219 $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_params['priceSetId']);
1220 if (in_array($this->_params['selectMembership'], $membershipTypes['autorenew'])) {
1221 $this->_params['auto_renew'] = TRUE;
1222 }
1223 }
90102a32
EM
1224 if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId'])) && !empty($this->_paymentProcessor['is_recur']) &&
1225 CRM_Utils_Array::value('auto_renew', $this->_params) && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval'])
1226 ) {
1227
1228 $this->_params['is_recur'] = $this->_values['is_recur'] = 1;
1229 // check if price set is not quick config
1230 if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) {
1231 list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']);
1232 }
1233 else {
1234 // FIXME: set interval and unit based on selected membership type
1235 $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
1236 $this->_params['selectMembership'], 'duration_interval'
1237 );
1238 $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
1239 $this->_params['selectMembership'], 'duration_unit'
1240 );
1241 }
1242 }
1243 }
1244
6a488035 1245}