Merge pull request #11050 from eileenmcnaughton/cache
[civicrm-core.git] / CRM / Contribute / Form / ContributionBase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
32 */
33
34 /**
35 * This class generates form components for processing a contribution.
36 */
37 class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
38
39 /**
40 * The id of the contribution page that we are processing.
41 *
42 * @var int
43 */
44 public $_id;
45
46 /**
47 * The mode that we are in
48 *
49 * @var string
50 * @protect
51 */
52 public $_mode;
53
54 /**
55 * The contact id related to a membership
56 *
57 * @var int
58 */
59 public $_membershipContactID;
60
61 /**
62 * The values for the contribution db object
63 *
64 * @var array
65 */
66 public $_values;
67
68 /**
69 * The paymentProcessor attributes for this page
70 *
71 * @var array
72 */
73 public $_paymentProcessor;
74
75 public $_paymentObject = NULL;
76
77 /**
78 * The membership block for this page
79 *
80 * @var array
81 */
82 public $_membershipBlock = NULL;
83
84 /**
85 * Does this form support a separate membership payment
86 * @var bool
87 */
88 protected $_separateMembershipPayment;
89
90 /**
91 * The params submitted by the form and computed by the app
92 *
93 * @var array
94 */
95 public $_params = array();
96
97 /**
98 * The fields involved in this contribution page
99 *
100 * @var array
101 */
102 public $_fields = array();
103
104 /**
105 * The billing location id for this contribution page.
106 *
107 * @var int
108 */
109 public $_bltID;
110
111 /**
112 * Cache the amount to make things easier
113 *
114 * @var float
115 */
116 public $_amount;
117
118 /**
119 * Pcp id
120 *
121 * @var integer
122 */
123 public $_pcpId;
124
125 /**
126 * Pcp block
127 *
128 * @var array
129 */
130 public $_pcpBlock;
131
132 /**
133 * Pcp info
134 *
135 * @var array
136 */
137 public $_pcpInfo;
138
139 /**
140 * The contact id of the person for whom membership is being added or renewed based on the cid in the url,
141 * checksum, or session
142 * @var int
143 */
144 public $_contactID;
145
146 protected $_userID;
147
148 /**
149 * The Membership ID for membership renewal
150 *
151 * @var int
152 */
153 public $_membershipId;
154
155 /**
156 * Price Set ID, if the new price set method is used
157 *
158 * @var int
159 */
160 public $_priceSetId;
161
162 /**
163 * Array of fields for the price set
164 *
165 * @var array
166 */
167 public $_priceSet;
168
169 public $_action;
170
171 /**
172 * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
173 * @var string
174 */
175 public $_contributeMode;
176
177 /**
178 * Contribution page supports memberships
179 * @var boolean
180 */
181 public $_useForMember;
182
183 /**
184 * @deprecated
185 *
186 * @var
187 */
188 public $_isBillingAddressRequiredForPayLater;
189
190 /**
191 * Flag if email field exists in embedded profile
192 *
193 * @var bool
194 */
195 public $_emailExists = FALSE;
196
197 /**
198 * Is this a backoffice form
199 * (this will affect whether paypal express code is displayed)
200 * @var bool
201 */
202 public $isBackOffice = FALSE;
203
204 /**
205 * Payment instrument if for the transaction.
206 *
207 * This will generally be drawn from the payment processor and is ignored for
208 * front end forms.
209 *
210 * @var int
211 */
212 public $paymentInstrumentID;
213
214 /**
215 * Set variables up before form is built.
216 *
217 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
218 * @throws \Exception
219 */
220 public function preProcess() {
221
222 // current contribution page id
223 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
224 $this->_ccid = CRM_Utils_Request::retrieve('ccid', 'Positive', $this);
225 if (!$this->_id) {
226 // seems like the session is corrupted and/or we lost the id trail
227 // lets just bump this to a regular session error and redirect user to main page
228 $this->controller->invalidKeyRedirect();
229 }
230 $this->_emailExists = $this->get('emailExists');
231
232 // this was used prior to the cleverer this_>getContactID - unsure now
233 $this->_userID = CRM_Core_Session::singleton()->getLoggedInContactID();
234
235 $this->_contactID = $this->_membershipContactID = $this->getContactID();
236 $this->_mid = NULL;
237 if ($this->_contactID) {
238 $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
239 if ($this->_mid) {
240 $membership = new CRM_Member_DAO_Membership();
241 $membership->id = $this->_mid;
242
243 if ($membership->find(TRUE)) {
244 $this->_defaultMemTypeId = $membership->membership_type_id;
245 if ($membership->contact_id != $this->_contactID) {
246 $validMembership = FALSE;
247 $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization');
248 if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) {
249 $this->_membershipContactID = $membership->contact_id;
250 $this->assign('membershipContactID', $this->_membershipContactID);
251 $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']);
252 $validMembership = TRUE;
253 }
254 else {
255 $membershipType = new CRM_Member_BAO_MembershipType();
256 $membershipType->id = $membership->membership_type_id;
257 if ($membershipType->find(TRUE)) {
258 // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values.
259 // Convert to comma separated list.
260 $inheritedRelTypes = implode(CRM_Utils_Array::explodePadded($membershipType->relationship_type_id), ',');
261 $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id);
262 if (array_key_exists($membership->contact_id, $permContacts)) {
263 $this->_membershipContactID = $membership->contact_id;
264 $validMembership = TRUE;
265 }
266 }
267 }
268 if (!$validMembership) {
269 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');
270 }
271 }
272 }
273 else {
274 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');
275 }
276 unset($membership);
277 }
278 }
279
280 // we do not want to display recently viewed items, so turn off
281 $this->assign('displayRecent', FALSE);
282 // Contribution page values are cleared from session, so can't use normal Printer Friendly view.
283 // Use Browser Print instead.
284 $this->assign('browserPrint', TRUE);
285
286 // action
287 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
288 $this->assign('action', $this->_action);
289
290 // current mode
291 $this->_mode = ($this->_action == 1024) ? 'test' : 'live';
292
293 $this->_values = $this->get('values');
294 $this->_fields = $this->get('fields');
295 $this->_bltID = $this->get('bltID');
296 $this->_paymentProcessor = $this->get('paymentProcessor');
297 $this->_priceSetId = $this->get('priceSetId');
298 $this->_priceSet = $this->get('priceSet');
299
300 if (!$this->_values) {
301 // get all the values from the dao object
302 $this->_values = array();
303 $this->_fields = array();
304
305 CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values);
306 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
307 && !CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id']))
308 ) {
309 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
310 }
311 if (empty($this->_values['is_active'])) {
312 throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id);
313 }
314
315 $this->assignBillingType();
316
317 // check for is_monetary status
318 $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values);
319 $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values);
320 if (!empty($this->_ccid)) {
321 $this->_values['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution',
322 $this->_ccid,
323 'financial_type_id'
324 );
325 if ($isPayLater) {
326 $isPayLater = FALSE;
327 $this->_values['is_pay_later'] = FALSE;
328 }
329 }
330
331 if ($isMonetary) {
332 $this->_paymentProcessorIDs = array_filter(explode(
333 CRM_Core_DAO::VALUE_SEPARATOR,
334 CRM_Utils_Array::value('payment_processor', $this->_values)
335 ));
336
337 $this->assignPaymentProcessor($isPayLater);
338 }
339
340 // get price info
341 // CRM-5095
342 CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
343
344 // this avoids getting E_NOTICE errors in php
345 $setNullFields = array(
346 'amount_block_is_active',
347 'is_allow_other_amount',
348 'footer_text',
349 );
350 foreach ($setNullFields as $f) {
351 if (!isset($this->_values[$f])) {
352 $this->_values[$f] = NULL;
353 }
354 }
355
356 //check if Membership Block is enabled, if Membership Fields are included in profile
357 //get membership section for this contribution page
358 $this->_membershipBlock = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
359 $this->set('membershipBlock', $this->_membershipBlock);
360
361 if (!empty($this->_values['custom_pre_id'])) {
362 $preProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_pre_id']);
363 }
364
365 if (!empty($this->_values['custom_post_id'])) {
366 $postProfileType = CRM_Core_BAO_UFField::getProfileType($this->_values['custom_post_id']);
367 }
368
369 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
370 (isset($preProfileType) && $preProfileType == 'Membership')
371 ) &&
372 !$this->_membershipBlock['is_active']
373 ) {
374 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.'));
375 }
376
377 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id);
378
379 if ($pledgeBlock) {
380 $this->_values['pledge_block_id'] = CRM_Utils_Array::value('id', $pledgeBlock);
381 $this->_values['max_reminders'] = CRM_Utils_Array::value('max_reminders', $pledgeBlock);
382 $this->_values['initial_reminder_day'] = CRM_Utils_Array::value('initial_reminder_day', $pledgeBlock);
383 $this->_values['additional_reminder_day'] = CRM_Utils_Array::value('additional_reminder_day', $pledgeBlock);
384
385 //set pledge id in values
386 $pledgeId = CRM_Utils_Request::retrieve('pledgeId', 'Positive', $this);
387
388 //authenticate pledge user for pledge payment.
389 if ($pledgeId) {
390 $this->_values['pledge_id'] = $pledgeId;
391
392 //lets override w/ pledge campaign.
393 $this->_values['campaign_id'] = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge',
394 $pledgeId,
395 'campaign_id'
396 );
397 self::authenticatePledgeUser();
398 }
399 }
400 $this->set('values', $this->_values);
401 $this->set('fields', $this->_fields);
402 }
403
404 // Handle PCP
405 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
406 if ($pcpId) {
407 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
408 $this->_pcpId = $pcp['pcpId'];
409 $this->_pcpBlock = $pcp['pcpBlock'];
410 $this->_pcpInfo = $pcp['pcpInfo'];
411 }
412
413 // Link (button) for users to create their own Personal Campaign page
414 if ($linkText = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute')) {
415 $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign',
416 "action=add&reset=1&pageId={$this->_id}&component=contribute",
417 FALSE, NULL, TRUE
418 );
419 $this->assign('linkTextUrl', $linkTextUrl);
420 $this->assign('linkText', $linkText);
421 }
422
423 //set pledge block if block id is set
424 if (!empty($this->_values['pledge_block_id'])) {
425 $this->assign('pledgeBlock', TRUE);
426 }
427
428 // check if one of the (amount , membership) blocks is active or not.
429 $this->_membershipBlock = $this->get('membershipBlock');
430
431 if (!$this->_values['amount_block_is_active'] &&
432 !$this->_membershipBlock['is_active'] &&
433 !$this->_priceSetId
434 ) {
435 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.'));
436 }
437
438 if ($this->_values['amount_block_is_active']) {
439 $this->set('amount_block_is_active', $this->_values['amount_block_is_active']);
440 }
441
442 $this->_contributeMode = $this->get('contributeMode');
443 $this->assign('contributeMode', $this->_contributeMode);
444
445 //assigning is_monetary and is_email_receipt to template
446 $this->assign('is_monetary', $this->_values['is_monetary']);
447 $this->assign('is_email_receipt', $this->_values['is_email_receipt']);
448 $this->assign('bltID', $this->_bltID);
449
450 //assign cancelSubscription URL to templates
451 $this->assign('cancelSubscriptionUrl',
452 CRM_Utils_Array::value('cancelSubscriptionUrl', $this->_values)
453 );
454
455 // assigning title to template in case someone wants to use it, also setting CMS page title
456 if ($this->_pcpId) {
457 $this->assign('title', $this->_pcpInfo['title']);
458 CRM_Utils_System::setTitle($this->_pcpInfo['title']);
459 }
460 else {
461 $this->assign('title', $this->_values['title']);
462 CRM_Utils_System::setTitle($this->_values['title']);
463 }
464 $this->_defaults = array();
465
466 $this->_amount = $this->get('amount');
467
468 //CRM-6907
469 $config = CRM_Core_Config::singleton();
470 $config->defaultCurrency = CRM_Utils_Array::value('currency',
471 $this->_values,
472 $config->defaultCurrency
473 );
474
475 //lets allow user to override campaign.
476 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
477 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
478 $this->_values['campaign_id'] = $campID;
479 }
480
481 //do check for cancel recurring and clean db, CRM-7696
482 if (CRM_Utils_Request::retrieve('cancel', 'Boolean')) {
483 self::cancelRecurring();
484 }
485
486 // check if billing block is required for pay later
487 if (CRM_Utils_Array::value('is_pay_later', $this->_values)) {
488 $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values);
489 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
490 }
491 }
492
493 /**
494 * Set the default values.
495 */
496 public function setDefaultValues() {
497 return $this->_defaults;
498 }
499
500 /**
501 * Assign the minimal set of variables to the template.
502 */
503 public function assignToTemplate() {
504 $this->set('name', $this->assignBillingName($this->_params));
505
506 $this->assign('paymentProcessor', $this->_paymentProcessor);
507 $vars = array(
508 'amount',
509 'currencyID',
510 'credit_card_type',
511 'trxn_id',
512 'amount_level',
513 );
514
515 $config = CRM_Core_Config::singleton();
516 if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) {
517 $this->assign('is_recur_enabled', 1);
518 $vars = array_merge($vars, array(
519 'is_recur',
520 'frequency_interval',
521 'frequency_unit',
522 'installments',
523 ));
524 }
525
526 if (in_array('CiviPledge', $config->enableComponents) &&
527 CRM_Utils_Array::value('is_pledge', $this->_params) == 1
528 ) {
529 $this->assign('pledge_enabled', 1);
530
531 $vars = array_merge($vars, array(
532 'is_pledge',
533 'pledge_frequency_interval',
534 'pledge_frequency_unit',
535 'pledge_installments',
536 ));
537 }
538
539 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
540 // function to get correct amount level consistently. Remove setting of the amount level in
541 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
542 // to cover all variants.
543 if (isset($this->_params['amount_other']) || isset($this->_params['selectMembership'])) {
544 $this->_params['amount_level'] = '';
545 }
546
547 foreach ($vars as $v) {
548 if (isset($this->_params[$v])) {
549 if ($v == "amount" && $this->_params[$v] === 0) {
550 $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE);
551 }
552 $this->assign($v, $this->_params[$v]);
553 }
554 }
555
556 $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
557 $this->_params,
558 $this->_bltID
559 ));
560
561 if (!empty($this->_params['onbehalf_profile_id']) && !empty($this->_params['onbehalf'])) {
562 $this->assign('onBehalfName', $this->_params['organization_name']);
563 $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
564 $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
565 }
566
567 //fix for CRM-3767
568 $isMonetary = FALSE;
569 if ($this->_amount > 0.0) {
570 $isMonetary = TRUE;
571 }
572 elseif (!empty($this->_params['selectMembership'])) {
573 $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
574 if ($memFee > 0.0) {
575 $isMonetary = TRUE;
576 }
577 }
578
579 // The concept of contributeMode is deprecated.
580 // The payment processor object can provide info about the fields it shows.
581 if ($isMonetary) {
582 /** @var $paymentProcessorObject \CRM_Core_Payment */
583 $paymentProcessorObject = $this->_paymentProcessor['object'];
584 $paymentFields = $paymentProcessorObject->getPaymentFormFields();
585 foreach ($paymentFields as $index => $paymentField) {
586 if (!isset($this->_params[$paymentField])) {
587 unset($paymentFields[$index]);
588 continue;
589 }
590 if ($paymentField === 'credit_card_exp_date') {
591 $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $this->_params));
592 $date = CRM_Utils_Date::mysqlToIso($date);
593 $this->assign('credit_card_exp_date', $date);
594 }
595 elseif ($paymentField === 'credit_card_number') {
596 $this->assign('credit_card_number',
597 CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $this->_params))
598 );
599 }
600 else {
601 $this->assign($paymentField, $this->_params[$paymentField]);
602 }
603 }
604 $paymentFieldsetLabel = ts('%1 Information', array($paymentProcessorObject->getPaymentTypeLabel()));
605 if (empty($paymentFields)) {
606 $paymentFieldsetLabel = '';
607 }
608 $this->assign('paymentFieldsetLabel', $paymentFieldsetLabel);
609 $this->assign('paymentFields', $paymentFields);
610
611 }
612
613 $this->assign('email',
614 $this->controller->exportValue('Main', "email-{$this->_bltID}")
615 );
616
617 // also assign the receipt_text
618 if (isset($this->_values['receipt_text'])) {
619 $this->assign('receipt_text', $this->_values['receipt_text']);
620 }
621 }
622
623 /**
624 * Add the custom fields.
625 *
626 * @param int $id
627 * @param string $name
628 * @param bool $viewOnly
629 * @param null $profileContactType
630 * @param array $fieldTypes
631 */
632 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
633 if ($id) {
634 $contactID = $this->getContactID();
635
636 // we don't allow conflicting fields to be
637 // configured via profile - CRM 2100
638 $fieldsToIgnore = array(
639 'receive_date' => 1,
640 'trxn_id' => 1,
641 'invoice_id' => 1,
642 'net_amount' => 1,
643 'fee_amount' => 1,
644 'non_deductible_amount' => 1,
645 'total_amount' => 1,
646 'amount_level' => 1,
647 'contribution_status_id' => 1,
648 // @todo replace payment_instrument with payment instrument id.
649 // both are available now but the id field is the most consistent.
650 'payment_instrument' => 1,
651 'payment_instrument_id' => 1,
652 'contribution_check_number' => 1,
653 'financial_type' => 1,
654 );
655
656 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
657 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL
658 );
659
660 if ($fields) {
661 // determine if email exists in profile so we know if we need to manually insert CRM-2888, CRM-15067
662 foreach ($fields as $key => $field) {
663 if (substr($key, 0, 6) == 'email-' &&
664 !in_array($profileContactType, array('honor', 'onbehalf'))
665 ) {
666 $this->_emailExists = TRUE;
667 $this->set('emailExists', TRUE);
668 }
669 }
670
671 if (array_intersect_key($fields, $fieldsToIgnore)) {
672 $fields = array_diff_key($fields, $fieldsToIgnore);
673 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
674 }
675
676 //remove common fields only if profile is not configured for onbehalf/honor
677 if (!in_array($profileContactType, array('honor', 'onbehalf'))) {
678 $fields = array_diff_key($fields, $this->_fields);
679 }
680
681 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
682 $addCaptcha = FALSE;
683 // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page
684 $viewOnlyFileValues = empty($profileContactType) ? array() : array($profileContactType => array());
685 foreach ($fields as $key => $field) {
686 if ($viewOnly &&
687 isset($field['data_type']) &&
688 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
689 ) {
690 //retrieve file value from submitted values on basis of $profileContactType
691 $fileValue = CRM_Utils_Array::value($key, $this->_params);
692 if (!empty($profileContactType) && !empty($this->_params[$profileContactType])) {
693 $fileValue = CRM_Utils_Array::value($key, $this->_params[$profileContactType]);
694 }
695
696 if ($fileValue) {
697 $path = CRM_Utils_Array::value('name', $fileValue);
698 $fileType = CRM_Utils_Array::value('type', $fileValue);
699 $fileValue = CRM_Utils_File::getFileURL($path, $fileType);
700 }
701
702 // format custom file value fetched from submitted value
703 if ($profileContactType) {
704 $viewOnlyFileValues[$profileContactType][$key] = $fileValue;
705 }
706 else {
707 $viewOnlyFileValues[$key] = $fileValue;
708 }
709
710 // On viewOnly use-case (as in online contribution Confirm page) we no longer need to set
711 // required property because being required file is already uploaded while registration
712 $field['is_required'] = FALSE;
713 }
714 if ($profileContactType) {
715 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
716 if ($profileContactType == 'honor') {
717 $honoreeNamefields = array(
718 'prefix_id',
719 'first_name',
720 'last_name',
721 'suffix_id',
722 'organization_name',
723 'household_name',
724 );
725 if (in_array($field['name'], $honoreeNamefields)) {
726 unset($fields[$field['name']]);
727 continue;
728 }
729 }
730 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
731 CRM_Core_BAO_UFGroup::buildProfile(
732 $this,
733 $field,
734 CRM_Profile_Form::MODE_CREATE,
735 $contactID,
736 TRUE,
737 $profileContactType
738 );
739 $this->_fields[$profileContactType][$key] = $field;
740 }
741 else {
742 unset($fields[$key]);
743 }
744 }
745 else {
746 CRM_Core_BAO_UFGroup::buildProfile(
747 $this,
748 $field,
749 CRM_Profile_Form::MODE_CREATE,
750 $contactID,
751 TRUE
752 );
753 $this->_fields[$key] = $field;
754 }
755 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
756 if ($field['add_captcha'] && !$this->_userID) {
757 $addCaptcha = TRUE;
758 }
759 }
760
761 $this->assign($name, $fields);
762
763 if ($profileContactType && count($viewOnlyFileValues[$profileContactType])) {
764 $this->assign('viewOnlyPrefixFileValues', $viewOnlyFileValues);
765 }
766 elseif (count($viewOnlyFileValues)) {
767 $this->assign('viewOnlyFileValues', $viewOnlyFileValues);
768 }
769
770 if ($addCaptcha && !$viewOnly) {
771 $captcha = CRM_Utils_ReCAPTCHA::singleton();
772 $captcha->add($this);
773 $this->assign('isCaptcha', TRUE);
774 }
775 }
776 }
777 }
778
779 /**
780 * Add onbehalf/honoree profile fields and native module fields.
781 *
782 * @param int $id
783 * @param CRM_Core_Form $form
784 */
785 public function buildComponentForm($id, $form) {
786 if (empty($id)) {
787 return;
788 }
789
790 $contactID = $this->getContactID();
791
792 foreach (array('soft_credit', 'on_behalf') as $module) {
793 if ($module == 'soft_credit') {
794 if (empty($form->_values['honoree_profile_id'])) {
795 continue;
796 }
797
798 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['honoree_profile_id'], 'is_active')) {
799 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.'));
800 }
801
802 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']);
803 $requiredProfileFields = array(
804 'Individual' => array('first_name', 'last_name'),
805 'Organization' => array('organization_name', 'email'),
806 'Household' => array('household_name', 'email'),
807 );
808 $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_values['honoree_profile_id'], $requiredProfileFields[$profileContactType]);
809 if (!$validProfile) {
810 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.'));
811 }
812
813 foreach (array('honor_block_title', 'honor_block_text') as $name) {
814 $form->assign($name, $form->_values[$name]);
815 }
816
817 $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE);
818
819 // radio button for Honor Type
820 foreach ($form->_values['soft_credit_types'] as $value) {
821 $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value);
822 }
823 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE);
824
825 $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields(
826 $this->_values['honoree_profile_id'], FALSE,
827 NULL, NULL,
828 NULL, FALSE,
829 NULL, TRUE,
830 NULL, CRM_Core_Permission::CREATE
831 );
832 $form->assign('honoreeProfileFields', $honoreeProfileFields);
833
834 // add the form elements
835 foreach ($honoreeProfileFields as $name => $field) {
836 // If soft credit type is not chosen then make omit requiredness from honoree profile fields
837 if (count($form->_submitValues) &&
838 empty($form->_submitValues['soft_credit_type_id']) &&
839 !empty($field['is_required'])
840 ) {
841 $field['is_required'] = FALSE;
842 }
843 CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, NULL, FALSE, FALSE, NULL, 'honor');
844 }
845 }
846 else {
847 if (empty($form->_values['onbehalf_profile_id'])) {
848 continue;
849 }
850
851 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values['onbehalf_profile_id'], 'is_active')) {
852 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.'));
853 }
854
855 $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id);
856 if (empty($member['is_active'])) {
857 $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
858 $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($form->_values['onbehalf_profile_id']);
859 foreach (
860 array(
861 'Individual',
862 'Organization',
863 'Household',
864 ) as $contactType
865 ) {
866 if (in_array($contactType, $onBehalfProfile) &&
867 (in_array('Membership', $onBehalfProfile) ||
868 in_array('Contribution', $onBehalfProfile)
869 )
870 ) {
871 CRM_Core_Error::fatal($msg);
872 }
873 }
874 }
875
876 if ($contactID) {
877 // retrieve all permissioned organizations of contact $contactID
878 $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization');
879
880 if (count($organizations)) {
881 // Related org url - pass checksum if needed
882 $args = array(
883 'ufId' => $form->_values['onbehalf_profile_id'],
884 'cid' => '',
885 );
886 if (!empty($_GET['cs'])) {
887 $args = array(
888 'ufId' => $form->_values['onbehalf_profile_id'],
889 'uid' => $this->_contactID,
890 'cs' => $_GET['cs'],
891 'cid' => '',
892 );
893 }
894 $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE);
895 $form->assign('locDataURL', $locDataURL);
896 }
897 if (count($organizations) > 0) {
898 $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations));
899
900 $orgOptions = array(
901 0 => ts('Select an existing organization'),
902 1 => ts('Enter a new organization'),
903 );
904 $form->addRadio('org_option', ts('options'), $orgOptions);
905 $form->setDefaults(array('org_option' => 0));
906 }
907 }
908
909 $form->assign('fieldSetTitle', ts(CRM_Core_BAO_UFGroup::getTitle($form->_values['onbehalf_profile_id'])));
910
911 if (CRM_Utils_Array::value('is_for_organization', $form->_values)) {
912 if ($form->_values['is_for_organization'] == 2) {
913 $form->assign('onBehalfRequired', TRUE);
914 }
915 else {
916 $form->addElement('checkbox', 'is_for_organization',
917 $form->_values['for_organization'],
918 NULL
919 );
920 }
921 }
922
923 $profileFields = CRM_Core_BAO_UFGroup::getFields(
924 $form->_values['onbehalf_profile_id'],
925 FALSE, CRM_Core_Action::VIEW, NULL,
926 NULL, FALSE, NULL, FALSE, NULL,
927 CRM_Core_Permission::CREATE, NULL
928 );
929
930 $form->assign('onBehalfOfFields', $profileFields);
931 if (!empty($form->_submitValues['onbehalf'])) {
932 if (!empty($form->_submitValues['onbehalfof_id'])) {
933 $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']);
934 }
935 $form->assign('submittedOnBehalfInfo', json_encode(str_replace('"', '\"', $form->_submitValues['onbehalf']), JSON_HEX_APOS));
936 }
937
938 $fieldTypes = array('Contact', 'Organization');
939 if (!empty($form->_membershipBlock)) {
940 $fieldTypes = array_merge($fieldTypes, array('Membership'));
941 }
942 $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization');
943 $fieldTypes = array_merge($fieldTypes, $contactSubType);
944
945 foreach ($profileFields as $name => $field) {
946 if (in_array($field['field_type'], $fieldTypes)) {
947 list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2);
948 if (in_array($prefixName, array('organization_name', 'email')) && empty($field['is_required'])) {
949 $field['is_required'] = 1;
950 }
951 if (count($form->_submitValues) &&
952 empty($form->_submitValues['is_for_organization']) &&
953 $form->_values['is_for_organization'] == 1 &&
954 !empty($field['is_required'])
955 ) {
956 $field['is_required'] = FALSE;
957 }
958 CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf', NULL, 'onbehalf');
959 }
960 }
961 }
962 }
963
964 }
965
966 /**
967 * Check template file exists.
968 *
969 * @param string $suffix
970 *
971 * @return null|string
972 */
973 public function checkTemplateFileExists($suffix = NULL) {
974 if ($this->_id) {
975 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
976 $template = CRM_Core_Form::getTemplate();
977 if ($template->template_exists($templateFile)) {
978 return $templateFile;
979 }
980 }
981 return NULL;
982 }
983
984 /**
985 * Use the form name to create the tpl file name.
986 *
987 * @return string
988 */
989 public function getTemplateFileName() {
990 $fileName = $this->checkTemplateFileExists();
991 return $fileName ? $fileName : parent::getTemplateFileName();
992 }
993
994 /**
995 * Add the extra.tpl in.
996 *
997 * Default extra tpl file basically just replaces .tpl with .extra.tpl
998 * i.e. we do not override - why isn't this done at the CRM_Core_Form level?
999 *
1000 * @return string
1001 */
1002 public function overrideExtraTemplateFileName() {
1003 $fileName = $this->checkTemplateFileExists('extra.');
1004 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
1005 }
1006
1007 /**
1008 * Authenticate pledge user during online payment.
1009 */
1010 public function authenticatePledgeUser() {
1011 //get the userChecksum and contact id
1012 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
1013 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
1014
1015 //get pledge status and contact id
1016 $pledgeValues = array();
1017 $pledgeParams = array('id' => $this->_values['pledge_id']);
1018 $returnProperties = array('contact_id', 'status_id');
1019 CRM_Core_DAO::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
1020
1021 //get all status
1022 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
1023 $validStatus = array(
1024 array_search('Pending', $allStatus),
1025 array_search('In Progress', $allStatus),
1026 array_search('Overdue', $allStatus),
1027 );
1028
1029 $validUser = FALSE;
1030 if ($this->_userID &&
1031 $this->_userID == $pledgeValues['contact_id']
1032 ) {
1033 //check for authenticated user.
1034 $validUser = TRUE;
1035 }
1036 elseif ($userChecksum && $pledgeValues['contact_id']) {
1037 //check for anonymous user.
1038 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($pledgeValues['contact_id'], $userChecksum);
1039
1040 //make sure cid is same as pledge contact id
1041 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
1042 $validUser = FALSE;
1043 }
1044 }
1045
1046 if (!$validUser) {
1047 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."));
1048 }
1049
1050 //check for valid pledge status.
1051 if (!in_array($pledgeValues['status_id'], $validStatus)) {
1052 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))));
1053 }
1054 }
1055
1056 /**
1057 * Cancel recurring contributions.
1058 *
1059 * In case user cancel recurring contribution,
1060 * When we get the control back from payment gate way
1061 * lets delete the recurring and related contribution.
1062 */
1063 public function cancelRecurring() {
1064 $isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean');
1065 if ($isCancel) {
1066 $isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean');
1067 $recurId = CRM_Utils_Request::retrieve('recurId', 'Positive');
1068 //clean db for recurring contribution.
1069 if ($isRecur && $recurId) {
1070 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
1071 }
1072 $contribId = CRM_Utils_Request::retrieve('contribId', 'Positive');
1073 if ($contribId) {
1074 CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
1075 }
1076 }
1077 }
1078
1079 /**
1080 * Build Membership Block in Contribution Pages.
1081 *
1082 * @param int $cid
1083 * Contact checked for having a current membership for a particular membership.
1084 * @param bool $isContributionMainPage
1085 * Is this the main page? If so add form input fields.
1086 * (or better yet don't have this functionality in a function shared with forms that don't share it).
1087 * @param int|array $selectedMembershipTypeID
1088 * Selected membership id.
1089 * @param bool $thankPage
1090 * Thank you page.
1091 * @param null $isTest
1092 *
1093 * @return bool
1094 * Is this a separate membership payment
1095 */
1096 protected function buildMembershipBlock(
1097 $cid,
1098 $isContributionMainPage = FALSE,
1099 $selectedMembershipTypeID = NULL,
1100 $thankPage = FALSE,
1101 $isTest = NULL
1102 ) {
1103
1104 $separateMembershipPayment = FALSE;
1105 if ($this->_membershipBlock) {
1106 $this->_currentMemberships = array();
1107
1108 $membershipTypeIds = $membershipTypes = $radio = array();
1109 $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE;
1110
1111 $allowAutoRenewMembership = $autoRenewOption = FALSE;
1112 $autoRenewMembershipTypeOptions = array();
1113
1114 $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock);
1115
1116 if ($membershipPriceset) {
1117 foreach ($this->_priceSet['fields'] as $pField) {
1118 if (empty($pField['options'])) {
1119 continue;
1120 }
1121 foreach ($pField['options'] as $opId => $opValues) {
1122 if (empty($opValues['membership_type_id'])) {
1123 continue;
1124 }
1125 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
1126 }
1127 }
1128 }
1129 elseif (!empty($this->_membershipBlock['membership_types'])) {
1130 $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']);
1131 }
1132
1133 if (!empty($membershipTypeIds)) {
1134 //set status message if wrong membershipType is included in membershipBlock
1135 if (isset($this->_mid) && !$membershipPriceset) {
1136 $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
1137 $this->_mid,
1138 'membership_type_id'
1139 );
1140 if (!in_array($membershipTypeID, $membershipTypeIds)) {
1141 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');
1142 }
1143 }
1144
1145 $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds);
1146 $this->_membershipTypeValues = $membershipTypeValues;
1147 $endDate = NULL;
1148 foreach ($membershipTypeIds as $value) {
1149 $memType = $membershipTypeValues[$value];
1150 if ($selectedMembershipTypeID != NULL) {
1151 if ($memType['id'] == $selectedMembershipTypeID) {
1152 $this->assign('minimum_fee',
1153 CRM_Utils_Array::value('minimum_fee', $memType)
1154 );
1155 $this->assign('membership_name', $memType['name']);
1156 if (!$thankPage && $cid) {
1157 $membership = new CRM_Member_DAO_Membership();
1158 $membership->contact_id = $cid;
1159 $membership->membership_type_id = $memType['id'];
1160 if ($membership->find(TRUE)) {
1161 $this->assign('renewal_mode', TRUE);
1162 $memType['current_membership'] = $membership->end_date;
1163 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
1164 }
1165 }
1166 $membershipTypes[] = $memType;
1167 }
1168 }
1169 elseif ($memType['is_active']) {
1170 $javascriptMethod = NULL;
1171 $allowAutoRenewOpt = (int) $memType['auto_renew'];
1172 if (is_array($this->_paymentProcessors)) {
1173 foreach ($this->_paymentProcessors as $id => $val) {
1174 if ($id && !$val['is_recur']) {
1175 $allowAutoRenewOpt = 0;
1176 continue;
1177 }
1178 }
1179 }
1180
1181 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
1182 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));;
1183
1184 if ($allowAutoRenewOpt) {
1185 $allowAutoRenewMembership = TRUE;
1186 }
1187
1188 //add membership type.
1189 $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL,
1190 $memType['id'], $javascriptMethod
1191 );
1192 if ($cid) {
1193 $membership = new CRM_Member_DAO_Membership();
1194 $membership->contact_id = $cid;
1195 $membership->membership_type_id = $memType['id'];
1196
1197 //show current membership, skip pending and cancelled membership records,
1198 //because we take first membership record id for renewal
1199 $membership->whereAdd('status_id != 5 AND status_id !=6');
1200
1201 if (!is_null($isTest)) {
1202 $membership->is_test = $isTest;
1203 }
1204
1205 //CRM-4297
1206 $membership->orderBy('end_date DESC');
1207
1208 if ($membership->find(TRUE)) {
1209 if (!$membership->end_date) {
1210 unset($radio[$memType['id']]);
1211 $this->assign('islifetime', TRUE);
1212 continue;
1213 }
1214 $this->assign('renewal_mode', TRUE);
1215 $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id;
1216 $memType['current_membership'] = $membership->end_date;
1217 if (!$endDate) {
1218 $endDate = $memType['current_membership'];
1219 $this->_defaultMemTypeId = $memType['id'];
1220 }
1221 if ($memType['current_membership'] < $endDate) {
1222 $endDate = $memType['current_membership'];
1223 $this->_defaultMemTypeId = $memType['id'];
1224 }
1225 }
1226 }
1227 $membershipTypes[] = $memType;
1228 }
1229 }
1230 }
1231
1232 $this->assign('membershipBlock', $this->_membershipBlock);
1233 $this->assign('showRadio', $isContributionMainPage);
1234 $this->assign('membershipTypes', $membershipTypes);
1235 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
1236 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
1237 //give preference to user submitted auto_renew value.
1238 $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE;
1239 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
1240
1241 // Assign autorenew option (0:hide,1:optional,2:required) so we can use it in confirmation etc.
1242 $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId);
1243 //$selectedMembershipTypeID is retrieved as an array for membership priceset if multiple
1244 //options for different organisation is selected on the contribution page.
1245 if (is_numeric($selectedMembershipTypeID) && isset($membershipTypeValues[$selectedMembershipTypeID]['auto_renew'])) {
1246 $this->assign('autoRenewOption', $membershipTypeValues[$selectedMembershipTypeID]['auto_renew']);
1247 }
1248 else {
1249 $this->assign('autoRenewOption', $autoRenewOption);
1250 }
1251
1252 if ($isContributionMainPage) {
1253 if (!$membershipPriceset) {
1254 if (!$this->_membershipBlock['is_required']) {
1255 $this->assign('showRadioNoThanks', TRUE);
1256 $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
1257 $this->addGroup($radio, 'selectMembership', NULL);
1258 }
1259 elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
1260 $temp = array_keys($radio);
1261 $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
1262 $this->assign('singleMembership', TRUE);
1263 $this->assign('showRadio', FALSE);
1264 }
1265 else {
1266 $this->addGroup($radio, 'selectMembership', NULL);
1267 }
1268
1269 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
1270 }
1271
1272 if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) {
1273 if ($autoRenewOption == 2) {
1274 $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.'));
1275 }
1276 else {
1277 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
1278 }
1279 }
1280
1281 }
1282 }
1283
1284 return $separateMembershipPayment;
1285 }
1286
1287 /**
1288 * Determine if recurring parameters need to be added to the form parameters.
1289 *
1290 * - is_recur
1291 * - frequency_interval
1292 * - frequency_unit
1293 *
1294 * For membership this is based on the membership type.
1295 *
1296 * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing.
1297 *
1298 * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout.
1299 */
1300 protected function setRecurringMembershipParams() {
1301 $selectedMembershipTypeID = CRM_Utils_Array::value('selectMembership', $this->_params);
1302 if ($selectedMembershipTypeID) {
1303 // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore
1304 // 'selectMembership' and calculate from the price_x fields so we have one method that always works
1305 // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix
1306 // this with an else (calculate for price set).
1307 $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_priceSetId);
1308 if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required'])
1309 || (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) &&
1310 !empty($this->_params['is_recur']))
1311 ) {
1312 $this->_params['auto_renew'] = TRUE;
1313 }
1314 }
1315 if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId']))
1316 && !empty($this->_paymentProcessor['is_recur']) &&
1317 CRM_Utils_Array::value('auto_renew', $this->_params)
1318 && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval'])
1319 ) {
1320
1321 $this->_params['is_recur'] = $this->_values['is_recur'] = 1;
1322 // check if price set is not quick config
1323 if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) {
1324 list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']);
1325 }
1326 else {
1327 // FIXME: set interval and unit based on selected membership type
1328 $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
1329 $this->_params['selectMembership'], 'duration_interval'
1330 );
1331 $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
1332 $this->_params['selectMembership'], 'duration_unit'
1333 );
1334 }
1335 }
1336 }
1337
1338 }