3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class generates form components for processing a contribution.
37 class CRM_Contribute_Form_ContributionBase
extends CRM_Core_Form
{
40 * The id of the contribution page that we are processing.
47 * The mode that we are in
55 * The contact id related to a membership
59 public $_membershipContactID;
62 * The values for the contribution db object
69 * The paymentProcessor attributes for this page
73 public $_paymentProcessor;
75 public $_paymentObject = NULL;
78 * The membership block for this page
82 public $_membershipBlock = NULL;
85 * Does this form support a separate membership payment
88 protected $_separateMembershipPayment;
91 * The params submitted by the form and computed by the app
95 public $_params = array();
98 * The fields involved in this contribution page
102 public $_fields = array();
105 * The billing location id for this contribution page.
112 * Cache the amount to make things easier
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
149 * The Membership ID for membership renewal
153 public $_membershipId;
156 * Price Set ID, if the new price set method is used
163 * Array of fields for the price set
172 * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
175 public $_contributeMode;
178 * Contribution page supports memberships
181 public $_useForMember;
188 public $_isBillingAddressRequiredForPayLater;
191 * Flag if email field exists in embedded profile
195 public $_emailExists = FALSE;
198 * Is this a backoffice form
199 * (this will affect whether paypal express code is displayed)
202 public $isBackOffice = FALSE;
205 * Payment instrument if for the transaction.
207 * This will generally be drawn from the payment processor and is ignored for
212 public $paymentInstrumentID;
215 * Set variables up before form is built.
217 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
220 public function preProcess() {
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);
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();
230 $this->_emailExists
= $this->get('emailExists');
232 // this was used prior to the cleverer this_>getContactID - unsure now
233 $this->_userID
= CRM_Core_Session
::singleton()->getLoggedInContactID();
235 $this->_contactID
= $this->_membershipContactID
= $this->getContactID();
237 if ($this->_contactID
) {
238 $this->_mid
= CRM_Utils_Request
::retrieve('mid', 'Positive', $this);
240 $membership = new CRM_Member_DAO_Membership();
241 $membership->id
= $this->_mid
;
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;
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;
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');
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');
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);
287 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
288 $this->assign('action', $this->_action
);
291 $this->_mode
= ($this->_action
== 1024) ?
'test' : 'live';
293 $this->_values
= $this->get('values');
294 $this->_fields
= $this->get('fields');
295 $this->_bltID
= $this->get('bltID');
296 $this->_paymentProcessor
= $this->get('paymentProcessor');
298 $this->_priceSetId
= $this->get('priceSetId');
299 $this->_priceSet
= $this->get('priceSet');
301 if (!$this->_values
) {
302 // get all the values from the dao object
303 $this->_values
= array();
304 $this->_fields
= array();
306 CRM_Contribute_BAO_ContributionPage
::setValues($this->_id
, $this->_values
);
307 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()
308 && !CRM_Core_Permission
::check('add contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($this->_values
['financial_type_id']))
310 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
312 if (empty($this->_values
['is_active'])) {
313 throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id
);
316 $this->assignBillingType();
318 // check for is_monetary status
319 $isMonetary = CRM_Utils_Array
::value('is_monetary', $this->_values
);
320 $isPayLater = CRM_Utils_Array
::value('is_pay_later', $this->_values
);
321 if (!empty($this->_ccid
)) {
322 $this->_values
['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution',
328 $this->_values
['is_pay_later'] = FALSE;
333 $this->_paymentProcessorIDs
= array_filter(explode(
334 CRM_Core_DAO
::VALUE_SEPARATOR
,
335 CRM_Utils_Array
::value('payment_processor', $this->_values
)
338 $this->assignPaymentProcessor($isPayLater);
343 CRM_Price_BAO_PriceSet
::initSet($this, $this->_id
, 'civicrm_contribution_page');
345 // this avoids getting E_NOTICE errors in php
346 $setNullFields = array(
347 'amount_block_is_active',
348 'is_allow_other_amount',
351 foreach ($setNullFields as $f) {
352 if (!isset($this->_values
[$f])) {
353 $this->_values
[$f] = NULL;
357 //check if Membership Block is enabled, if Membership Fields are included in profile
358 //get membership section for this contribution page
359 $this->_membershipBlock
= CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
360 $this->set('membershipBlock', $this->_membershipBlock
);
362 if (!empty($this->_values
['custom_pre_id'])) {
363 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($this->_values
['custom_pre_id']);
366 if (!empty($this->_values
['custom_post_id'])) {
367 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($this->_values
['custom_post_id']);
370 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
371 (isset($preProfileType) && $preProfileType == 'Membership')
373 !$this->_membershipBlock
['is_active']
375 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.'));
378 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock
::getPledgeBlock($this->_id
);
381 $this->_values
['pledge_block_id'] = CRM_Utils_Array
::value('id', $pledgeBlock);
382 $this->_values
['max_reminders'] = CRM_Utils_Array
::value('max_reminders', $pledgeBlock);
383 $this->_values
['initial_reminder_day'] = CRM_Utils_Array
::value('initial_reminder_day', $pledgeBlock);
384 $this->_values
['additional_reminder_day'] = CRM_Utils_Array
::value('additional_reminder_day', $pledgeBlock);
386 //set pledge id in values
387 $pledgeId = CRM_Utils_Request
::retrieve('pledgeId', 'Positive', $this);
389 //authenticate pledge user for pledge payment.
391 $this->_values
['pledge_id'] = $pledgeId;
393 //lets override w/ pledge campaign.
394 $this->_values
['campaign_id'] = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge',
398 self
::authenticatePledgeUser();
401 $this->set('values', $this->_values
);
402 $this->set('fields', $this->_fields
);
406 $pcpId = CRM_Utils_Request
::retrieve('pcpId', 'Positive', $this);
408 $pcp = CRM_PCP_BAO_PCP
::handlePcp($pcpId, 'contribute', $this->_values
);
409 $this->_pcpId
= $pcp['pcpId'];
410 $this->_pcpBlock
= $pcp['pcpBlock'];
411 $this->_pcpInfo
= $pcp['pcpInfo'];
414 // Link (button) for users to create their own Personal Campaign page
415 if ($linkText = CRM_PCP_BAO_PCP
::getPcpBlockStatus($this->_id
, 'contribute')) {
416 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
417 "action=add&reset=1&pageId={$this->_id}&component=contribute",
420 $this->assign('linkTextUrl', $linkTextUrl);
421 $this->assign('linkText', $linkText);
424 //set pledge block if block id is set
425 if (!empty($this->_values
['pledge_block_id'])) {
426 $this->assign('pledgeBlock', TRUE);
429 // check if one of the (amount , membership) blocks is active or not.
430 $this->_membershipBlock
= $this->get('membershipBlock');
432 if (!$this->_values
['amount_block_is_active'] &&
433 !$this->_membershipBlock
['is_active'] &&
436 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.'));
439 if ($this->_values
['amount_block_is_active']) {
440 $this->set('amount_block_is_active', $this->_values
['amount_block_is_active']);
443 $this->_contributeMode
= $this->get('contributeMode');
444 $this->assign('contributeMode', $this->_contributeMode
);
446 //assigning is_monetary and is_email_receipt to template
447 $this->assign('is_monetary', $this->_values
['is_monetary']);
448 $this->assign('is_email_receipt', $this->_values
['is_email_receipt']);
449 $this->assign('bltID', $this->_bltID
);
451 //assign cancelSubscription URL to templates
452 $this->assign('cancelSubscriptionUrl',
453 CRM_Utils_Array
::value('cancelSubscriptionUrl', $this->_values
)
456 // assigning title to template in case someone wants to use it, also setting CMS page title
458 $this->assign('title', $this->_pcpInfo
['title']);
459 CRM_Utils_System
::setTitle($this->_pcpInfo
['title']);
462 $this->assign('title', $this->_values
['title']);
463 CRM_Utils_System
::setTitle($this->_values
['title']);
465 $this->_defaults
= array();
467 $this->_amount
= $this->get('amount');
470 $config = CRM_Core_Config
::singleton();
471 $config->defaultCurrency
= CRM_Utils_Array
::value('currency',
473 $config->defaultCurrency
476 //lets allow user to override campaign.
477 $campID = CRM_Utils_Request
::retrieve('campID', 'Positive', $this);
478 if ($campID && CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
479 $this->_values
['campaign_id'] = $campID;
482 //do check for cancel recurring and clean db, CRM-7696
483 if (CRM_Utils_Request
::retrieve('cancel', 'Boolean')) {
484 self
::cancelRecurring();
487 // check if billing block is required for pay later
488 if (CRM_Utils_Array
::value('is_pay_later', $this->_values
)) {
489 $this->_isBillingAddressRequiredForPayLater
= CRM_Utils_Array
::value('is_billing_required', $this->_values
);
490 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater
);
495 * Set the default values.
497 public function setDefaultValues() {
498 return $this->_defaults
;
502 * Assign the minimal set of variables to the template.
504 public function assignToTemplate() {
505 $this->set('name', $this->assignBillingName($this->_params
));
507 $this->assign('paymentProcessor', $this->_paymentProcessor
);
516 $config = CRM_Core_Config
::singleton();
517 if (isset($this->_values
['is_recur']) && !empty($this->_paymentProcessor
['is_recur'])) {
518 $this->assign('is_recur_enabled', 1);
519 $vars = array_merge($vars, array(
521 'frequency_interval',
527 if (in_array('CiviPledge', $config->enableComponents
) &&
528 CRM_Utils_Array
::value('is_pledge', $this->_params
) == 1
530 $this->assign('pledge_enabled', 1);
532 $vars = array_merge($vars, array(
534 'pledge_frequency_interval',
535 'pledge_frequency_unit',
536 'pledge_installments',
540 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
541 // function to get correct amount level consistently. Remove setting of the amount level in
542 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
543 // to cover all variants.
544 if (isset($this->_params
['amount_other']) ||
isset($this->_params
['selectMembership'])) {
545 $this->_params
['amount_level'] = '';
548 foreach ($vars as $v) {
549 if (isset($this->_params
[$v])) {
550 if ($v == "amount" && $this->_params
[$v] === 0) {
551 $this->_params
[$v] = CRM_Utils_Money
::format($this->_params
[$v], NULL, NULL, TRUE);
553 $this->assign($v, $this->_params
[$v]);
557 $this->assign('address', CRM_Utils_Address
::getFormattedBillingAddressFieldsFromParameters(
562 if (!empty($this->_params
['onbehalf_profile_id']) && !empty($this->_params
['onbehalf'])) {
563 $this->assign('onBehalfName', $this->_params
['organization_name']);
564 $locTypeId = array_keys($this->_params
['onbehalf_location']['email']);
565 $this->assign('onBehalfEmail', $this->_params
['onbehalf_location']['email'][$locTypeId[0]]['email']);
570 if ($this->_amount
> 0.0) {
573 elseif (!empty($this->_params
['selectMembership'])) {
574 $memFee = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params
['selectMembership'], 'minimum_fee');
580 // The concept of contributeMode is deprecated.
581 // The payment processor object can provide info about the fields it shows.
582 if ($isMonetary && is_a($this->_paymentProcessor
['object'], 'CRM_Core_Payment')) {
583 /** @var $paymentProcessorObject \CRM_Core_Payment */
584 $paymentProcessorObject = $this->_paymentProcessor
['object'];
586 $paymentFields = $paymentProcessorObject->getPaymentFormFields();
587 foreach ($paymentFields as $index => $paymentField) {
588 if (!isset($this->_params
[$paymentField])) {
589 unset($paymentFields[$index]);
592 if ($paymentField === 'credit_card_exp_date') {
593 $date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('credit_card_exp_date', $this->_params
));
594 $date = CRM_Utils_Date
::mysqlToIso($date);
595 $this->assign('credit_card_exp_date', $date);
597 elseif ($paymentField === 'credit_card_number') {
598 $this->assign('credit_card_number',
599 CRM_Utils_System
::mungeCreditCard(CRM_Utils_Array
::value('credit_card_number', $this->_params
))
603 $this->assign($paymentField, $this->_params
[$paymentField]);
606 $paymentFieldsetLabel = ts('%1 Information', array($paymentProcessorObject->getPaymentTypeLabel()));
607 if (empty($paymentFields)) {
608 $paymentFieldsetLabel = '';
610 $this->assign('paymentFieldsetLabel', $paymentFieldsetLabel);
611 $this->assign('paymentFields', $paymentFields);
615 $this->assign('email',
616 $this->controller
->exportValue('Main', "email-{$this->_bltID}")
619 // also assign the receipt_text
620 if (isset($this->_values
['receipt_text'])) {
621 $this->assign('receipt_text', $this->_values
['receipt_text']);
626 * Add the custom fields.
629 * @param string $name
630 * @param bool $viewOnly
631 * @param null $profileContactType
632 * @param array $fieldTypes
634 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
636 $contactID = $this->getContactID();
638 // we don't allow conflicting fields to be
639 // configured via profile - CRM 2100
640 $fieldsToIgnore = array(
646 'non_deductible_amount' => 1,
649 'contribution_status_id' => 1,
650 // @todo replace payment_instrument with payment instrument id.
651 // both are available now but the id field is the most consistent.
652 'payment_instrument' => 1,
653 'payment_instrument_id' => 1,
654 'contribution_check_number' => 1,
655 'financial_type' => 1,
658 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
, NULL, NULL, FALSE,
659 NULL, FALSE, NULL, CRM_Core_Permission
::CREATE
, NULL
663 // determine if email exists in profile so we know if we need to manually insert CRM-2888, CRM-15067
664 foreach ($fields as $key => $field) {
665 if (substr($key, 0, 6) == 'email-' &&
666 !in_array($profileContactType, array('honor', 'onbehalf'))
668 $this->_emailExists
= TRUE;
669 $this->set('emailExists', TRUE);
673 if (array_intersect_key($fields, $fieldsToIgnore)) {
674 $fields = array_diff_key($fields, $fieldsToIgnore);
675 CRM_Core_Session
::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
678 //remove common fields only if profile is not configured for onbehalf/honor
679 if (!in_array($profileContactType, array('honor', 'onbehalf'))) {
680 $fields = array_diff_key($fields, $this->_fields
);
683 CRM_Core_BAO_Address
::checkContactSharedAddressFields($fields, $contactID);
685 // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page
686 $viewOnlyFileValues = empty($profileContactType) ?
array() : array($profileContactType => array());
687 foreach ($fields as $key => $field) {
689 isset($field['data_type']) &&
690 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
692 //retrieve file value from submitted values on basis of $profileContactType
693 $fileValue = CRM_Utils_Array
::value($key, $this->_params
);
694 if (!empty($profileContactType) && !empty($this->_params
[$profileContactType])) {
695 $fileValue = CRM_Utils_Array
::value($key, $this->_params
[$profileContactType]);
699 $path = CRM_Utils_Array
::value('name', $fileValue);
700 $fileType = CRM_Utils_Array
::value('type', $fileValue);
701 $fileValue = CRM_Utils_File
::getFileURL($path, $fileType);
704 // format custom file value fetched from submitted value
705 if ($profileContactType) {
706 $viewOnlyFileValues[$profileContactType][$key] = $fileValue;
709 $viewOnlyFileValues[$key] = $fileValue;
712 // On viewOnly use-case (as in online contribution Confirm page) we no longer need to set
713 // required property because being required file is already uploaded while registration
714 $field['is_required'] = FALSE;
716 if ($profileContactType) {
717 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
718 if ($profileContactType == 'honor') {
719 $honoreeNamefields = array(
727 if (in_array($field['name'], $honoreeNamefields)) {
728 unset($fields[$field['name']]);
732 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
733 CRM_Core_BAO_UFGroup
::buildProfile(
736 CRM_Profile_Form
::MODE_CREATE
,
741 $this->_fields
[$profileContactType][$key] = $field;
744 unset($fields[$key]);
748 CRM_Core_BAO_UFGroup
::buildProfile(
751 CRM_Profile_Form
::MODE_CREATE
,
755 $this->_fields
[$key] = $field;
757 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
758 if ($field['add_captcha'] && !$this->_userID
) {
763 $this->assign($name, $fields);
765 if ($profileContactType && count($viewOnlyFileValues[$profileContactType])) {
766 $this->assign('viewOnlyPrefixFileValues', $viewOnlyFileValues);
768 elseif (count($viewOnlyFileValues)) {
769 $this->assign('viewOnlyFileValues', $viewOnlyFileValues);
772 if ($addCaptcha && !$viewOnly) {
773 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
774 $captcha->add($this);
775 $this->assign('isCaptcha', TRUE);
782 * Add onbehalf/honoree profile fields and native module fields.
785 * @param CRM_Core_Form $form
787 public function buildComponentForm($id, $form) {
792 $contactID = $this->getContactID();
794 foreach (array('soft_credit', 'on_behalf') as $module) {
795 if ($module == 'soft_credit') {
796 if (empty($form->_values
['honoree_profile_id'])) {
800 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values
['honoree_profile_id'], 'is_active')) {
801 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.'));
804 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($form->_values
['honoree_profile_id']);
805 $requiredProfileFields = array(
806 'Individual' => array('first_name', 'last_name'),
807 'Organization' => array('organization_name', 'email'),
808 'Household' => array('household_name', 'email'),
810 $validProfile = CRM_Core_BAO_UFGroup
::checkValidProfile($form->_values
['honoree_profile_id'], $requiredProfileFields[$profileContactType]);
811 if (!$validProfile) {
812 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.'));
815 foreach (array('honor_block_title', 'honor_block_text') as $name) {
816 $form->assign($name, $form->_values
[$name]);
819 $softCreditTypes = CRM_Core_OptionGroup
::values("soft_credit_type", FALSE);
821 // radio button for Honor Type
822 foreach ($form->_values
['soft_credit_types'] as $value) {
823 $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value);
825 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE);
827 $honoreeProfileFields = CRM_Core_BAO_UFGroup
::getFields(
828 $this->_values
['honoree_profile_id'], FALSE,
832 NULL, CRM_Core_Permission
::CREATE
834 $form->assign('honoreeProfileFields', $honoreeProfileFields);
836 // add the form elements
837 foreach ($honoreeProfileFields as $name => $field) {
838 // If soft credit type is not chosen then make omit requiredness from honoree profile fields
839 if (count($form->_submitValues
) &&
840 empty($form->_submitValues
['soft_credit_type_id']) &&
841 !empty($field['is_required'])
843 $field['is_required'] = FALSE;
845 CRM_Core_BAO_UFGroup
::buildProfile($form, $field, CRM_Profile_Form
::MODE_CREATE
, NULL, FALSE, FALSE, NULL, 'honor');
849 if (empty($form->_values
['onbehalf_profile_id'])) {
853 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $form->_values
['onbehalf_profile_id'], 'is_active')) {
854 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.'));
857 $member = CRM_Member_BAO_Membership
::getMembershipBlock($form->_id
);
858 if (empty($member['is_active'])) {
859 $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
860 $onBehalfProfile = CRM_Core_BAO_UFGroup
::profileGroups($form->_values
['onbehalf_profile_id']);
868 if (in_array($contactType, $onBehalfProfile) &&
869 (in_array('Membership', $onBehalfProfile) ||
870 in_array('Contribution', $onBehalfProfile)
873 CRM_Core_Error
::fatal($msg);
879 // retrieve all permissioned organizations of contact $contactID
880 $organizations = CRM_Contact_BAO_Relationship
::getPermissionedContacts($contactID, NULL, NULL, 'Organization');
882 if (count($organizations)) {
883 // Related org url - pass checksum if needed
885 'ufId' => $form->_values
['onbehalf_profile_id'],
888 if (!empty($_GET['cs'])) {
890 'ufId' => $form->_values
['onbehalf_profile_id'],
891 'uid' => $this->_contactID
,
896 $locDataURL = CRM_Utils_System
::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE);
897 $form->assign('locDataURL', $locDataURL);
899 if (count($organizations) > 0) {
900 $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array
::collect('name', $organizations));
903 0 => ts('Select an existing organization'),
904 1 => ts('Enter a new organization'),
906 $form->addRadio('org_option', ts('options'), $orgOptions);
907 $form->setDefaults(array('org_option' => 0));
911 $form->assign('fieldSetTitle', ts(CRM_Core_BAO_UFGroup
::getTitle($form->_values
['onbehalf_profile_id'])));
913 if (CRM_Utils_Array
::value('is_for_organization', $form->_values
)) {
914 if ($form->_values
['is_for_organization'] == 2) {
915 $form->assign('onBehalfRequired', TRUE);
918 $form->addElement('checkbox', 'is_for_organization',
919 $form->_values
['for_organization'],
925 $profileFields = CRM_Core_BAO_UFGroup
::getFields(
926 $form->_values
['onbehalf_profile_id'],
927 FALSE, CRM_Core_Action
::VIEW
, NULL,
928 NULL, FALSE, NULL, FALSE, NULL,
929 CRM_Core_Permission
::CREATE
, NULL
932 $form->assign('onBehalfOfFields', $profileFields);
933 if (!empty($form->_submitValues
['onbehalf'])) {
934 if (!empty($form->_submitValues
['onbehalfof_id'])) {
935 $form->assign('submittedOnBehalf', $form->_submitValues
['onbehalfof_id']);
937 $form->assign('submittedOnBehalfInfo', json_encode(str_replace('"', '\"', $form->_submitValues
['onbehalf']), JSON_HEX_APOS
));
940 $fieldTypes = array('Contact', 'Organization');
941 if (!empty($form->_membershipBlock
)) {
942 $fieldTypes = array_merge($fieldTypes, array('Membership'));
944 $contactSubType = CRM_Contact_BAO_ContactType
::subTypes('Organization');
945 $fieldTypes = array_merge($fieldTypes, $contactSubType);
947 foreach ($profileFields as $name => $field) {
948 if (in_array($field['field_type'], $fieldTypes)) {
949 list($prefixName, $index) = CRM_Utils_System
::explode('-', $name, 2);
950 if (in_array($prefixName, array('organization_name', 'email')) && empty($field['is_required'])) {
951 $field['is_required'] = 1;
953 if (count($form->_submitValues
) &&
954 empty($form->_submitValues
['is_for_organization']) &&
955 $form->_values
['is_for_organization'] == 1 &&
956 !empty($field['is_required'])
958 $field['is_required'] = FALSE;
960 CRM_Core_BAO_UFGroup
::buildProfile($form, $field, NULL, NULL, FALSE, 'onbehalf', NULL, 'onbehalf');
969 * Check template file exists.
971 * @param string $suffix
973 * @return null|string
975 public function checkTemplateFileExists($suffix = NULL) {
977 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
978 $template = CRM_Core_Form
::getTemplate();
979 if ($template->template_exists($templateFile)) {
980 return $templateFile;
987 * Use the form name to create the tpl file name.
991 public function getTemplateFileName() {
992 $fileName = $this->checkTemplateFileExists();
993 return $fileName ?
$fileName : parent
::getTemplateFileName();
997 * Add the extra.tpl in.
999 * Default extra tpl file basically just replaces .tpl with .extra.tpl
1000 * i.e. we do not override - why isn't this done at the CRM_Core_Form level?
1004 public function overrideExtraTemplateFileName() {
1005 $fileName = $this->checkTemplateFileExists('extra.');
1006 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();
1010 * Authenticate pledge user during online payment.
1012 public function authenticatePledgeUser() {
1013 //get the userChecksum and contact id
1014 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
1015 $contactID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
1017 //get pledge status and contact id
1018 $pledgeValues = array();
1019 $pledgeParams = array('id' => $this->_values
['pledge_id']);
1020 $returnProperties = array('contact_id', 'status_id');
1021 CRM_Core_DAO
::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
1024 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
1025 $validStatus = array(
1026 array_search('Pending', $allStatus),
1027 array_search('In Progress', $allStatus),
1028 array_search('Overdue', $allStatus),
1032 if ($this->_userID
&&
1033 $this->_userID
== $pledgeValues['contact_id']
1035 //check for authenticated user.
1038 elseif ($userChecksum && $pledgeValues['contact_id']) {
1039 //check for anonymous user.
1040 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($pledgeValues['contact_id'], $userChecksum);
1042 //make sure cid is same as pledge contact id
1043 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
1049 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."));
1052 //check for valid pledge status.
1053 if (!in_array($pledgeValues['status_id'], $validStatus)) {
1054 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))));
1059 * Cancel recurring contributions.
1061 * In case user cancel recurring contribution,
1062 * When we get the control back from payment gate way
1063 * lets delete the recurring and related contribution.
1065 public function cancelRecurring() {
1066 $isCancel = CRM_Utils_Request
::retrieve('cancel', 'Boolean');
1068 $isRecur = CRM_Utils_Request
::retrieve('isRecur', 'Boolean');
1069 $recurId = CRM_Utils_Request
::retrieve('recurId', 'Positive');
1070 //clean db for recurring contribution.
1071 if ($isRecur && $recurId) {
1072 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($recurId);
1074 $contribId = CRM_Utils_Request
::retrieve('contribId', 'Positive');
1076 CRM_Contribute_BAO_Contribution
::deleteContribution($contribId);
1082 * Build Membership Block in Contribution Pages.
1085 * Contact checked for having a current membership for a particular membership.
1086 * @param bool $isContributionMainPage
1087 * Is this the main page? If so add form input fields.
1088 * (or better yet don't have this functionality in a function shared with forms that don't share it).
1089 * @param int|array $selectedMembershipTypeID
1090 * Selected membership id.
1091 * @param bool $thankPage
1093 * @param null $isTest
1096 * Is this a separate membership payment
1098 protected function buildMembershipBlock(
1100 $isContributionMainPage = FALSE,
1101 $selectedMembershipTypeID = NULL,
1106 $separateMembershipPayment = FALSE;
1107 if ($this->_membershipBlock
) {
1108 $this->_currentMemberships
= array();
1110 $membershipTypeIds = $membershipTypes = $radio = array();
1111 $membershipPriceset = (!empty($this->_priceSetId
) && $this->_useForMember
) ?
TRUE : FALSE;
1113 $allowAutoRenewMembership = $autoRenewOption = FALSE;
1114 $autoRenewMembershipTypeOptions = array();
1116 $separateMembershipPayment = CRM_Utils_Array
::value('is_separate_payment', $this->_membershipBlock
);
1118 if ($membershipPriceset) {
1119 foreach ($this->_priceSet
['fields'] as $pField) {
1120 if (empty($pField['options'])) {
1123 foreach ($pField['options'] as $opId => $opValues) {
1124 if (empty($opValues['membership_type_id'])) {
1127 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
1131 elseif (!empty($this->_membershipBlock
['membership_types'])) {
1132 $membershipTypeIds = explode(',', $this->_membershipBlock
['membership_types']);
1135 if (!empty($membershipTypeIds)) {
1136 //set status message if wrong membershipType is included in membershipBlock
1137 if (isset($this->_mid
) && !$membershipPriceset) {
1138 $membershipTypeID = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
1140 'membership_type_id'
1142 if (!in_array($membershipTypeID, $membershipTypeIds)) {
1143 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');
1147 $membershipTypeValues = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, $membershipTypeIds);
1148 $this->_membershipTypeValues
= $membershipTypeValues;
1150 foreach ($membershipTypeIds as $value) {
1151 $memType = $membershipTypeValues[$value];
1152 if ($selectedMembershipTypeID != NULL) {
1153 if ($memType['id'] == $selectedMembershipTypeID) {
1154 $this->assign('minimum_fee',
1155 CRM_Utils_Array
::value('minimum_fee', $memType)
1157 $this->assign('membership_name', $memType['name']);
1158 if (!$thankPage && $cid) {
1159 $membership = new CRM_Member_DAO_Membership();
1160 $membership->contact_id
= $cid;
1161 $membership->membership_type_id
= $memType['id'];
1162 if ($membership->find(TRUE)) {
1163 $this->assign('renewal_mode', TRUE);
1164 $memType['current_membership'] = $membership->end_date
;
1165 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
1168 $membershipTypes[] = $memType;
1171 elseif ($memType['is_active']) {
1172 $javascriptMethod = NULL;
1173 $allowAutoRenewOpt = (int) $memType['auto_renew'];
1174 if (is_array($this->_paymentProcessors
)) {
1175 foreach ($this->_paymentProcessors
as $id => $val) {
1176 if ($id && !$val['is_recur']) {
1177 $allowAutoRenewOpt = 0;
1183 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
1184 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array
::value($value, CRM_Utils_Array
::value('auto_renew', $this->_membershipBlock
));;
1186 if ($allowAutoRenewOpt) {
1187 $allowAutoRenewMembership = TRUE;
1190 //add membership type.
1191 $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL,
1192 $memType['id'], $javascriptMethod
1195 $membership = new CRM_Member_DAO_Membership();
1196 $membership->contact_id
= $cid;
1197 $membership->membership_type_id
= $memType['id'];
1199 //show current membership, skip pending and cancelled membership records,
1200 //because we take first membership record id for renewal
1201 $membership->whereAdd('status_id != 5 AND status_id !=6');
1203 if (!is_null($isTest)) {
1204 $membership->is_test
= $isTest;
1208 $membership->orderBy('end_date DESC');
1210 if ($membership->find(TRUE)) {
1211 if (!$membership->end_date
) {
1212 unset($radio[$memType['id']]);
1213 $this->assign('islifetime', TRUE);
1216 $this->assign('renewal_mode', TRUE);
1217 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
1218 $memType['current_membership'] = $membership->end_date
;
1220 $endDate = $memType['current_membership'];
1221 $this->_defaultMemTypeId
= $memType['id'];
1223 if ($memType['current_membership'] < $endDate) {
1224 $endDate = $memType['current_membership'];
1225 $this->_defaultMemTypeId
= $memType['id'];
1229 $membershipTypes[] = $memType;
1234 $this->assign('membershipBlock', $this->_membershipBlock
);
1235 $this->assign('showRadio', $isContributionMainPage);
1236 $this->assign('membershipTypes', $membershipTypes);
1237 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
1238 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
1239 //give preference to user submitted auto_renew value.
1240 $takeUserSubmittedAutoRenew = (!empty($_POST) ||
$this->isSubmitted()) ?
TRUE : FALSE;
1241 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
1243 // Assign autorenew option (0:hide,1:optional,2:required) so we can use it in confirmation etc.
1244 $autoRenewOption = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($this->_priceSetId
);
1245 //$selectedMembershipTypeID is retrieved as an array for membership priceset if multiple
1246 //options for different organisation is selected on the contribution page.
1247 if (is_numeric($selectedMembershipTypeID) && isset($membershipTypeValues[$selectedMembershipTypeID]['auto_renew'])) {
1248 $this->assign('autoRenewOption', $membershipTypeValues[$selectedMembershipTypeID]['auto_renew']);
1251 $this->assign('autoRenewOption', $autoRenewOption);
1254 if ($isContributionMainPage) {
1255 if (!$membershipPriceset) {
1256 if (!$this->_membershipBlock
['is_required']) {
1257 $this->assign('showRadioNoThanks', TRUE);
1258 $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
1259 $this->addGroup($radio, 'selectMembership', NULL);
1261 elseif ($this->_membershipBlock
['is_required'] && count($radio) == 1) {
1262 $temp = array_keys($radio);
1263 $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
1264 $this->assign('singleMembership', TRUE);
1265 $this->assign('showRadio', FALSE);
1268 $this->addGroup($radio, 'selectMembership', NULL);
1271 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
1274 if ((!$this->_values
['is_pay_later'] ||
is_array($this->_paymentProcessors
)) && ($allowAutoRenewMembership ||
$autoRenewOption)) {
1275 if ($autoRenewOption == 2) {
1276 $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.'));
1279 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
1286 return $separateMembershipPayment;
1290 * Determine if recurring parameters need to be added to the form parameters.
1293 * - frequency_interval
1296 * For membership this is based on the membership type.
1298 * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing.
1300 * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout.
1302 protected function setRecurringMembershipParams() {
1303 $selectedMembershipTypeID = CRM_Utils_Array
::value('selectMembership', $this->_params
);
1304 if ($selectedMembershipTypeID) {
1305 // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore
1306 // 'selectMembership' and calculate from the price_x fields so we have one method that always works
1307 // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix
1308 // this with an else (calculate for price set).
1309 $membershipTypes = CRM_Price_BAO_PriceSet
::getMembershipTypesFromPriceSet($this->_priceSetId
);
1310 if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required'])
1311 ||
(in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) &&
1312 !empty($this->_params
['is_recur']))
1314 $this->_params
['auto_renew'] = TRUE;
1317 if ((!empty($this->_params
['selectMembership']) ||
!empty($this->_params
['priceSetId']))
1318 && !empty($this->_paymentProcessor
['is_recur']) &&
1319 CRM_Utils_Array
::value('auto_renew', $this->_params
)
1320 && empty($this->_params
['is_recur']) && empty($this->_params
['frequency_interval'])
1323 $this->_params
['is_recur'] = $this->_values
['is_recur'] = 1;
1324 // check if price set is not quick config
1325 if (!empty($this->_params
['priceSetId']) && !CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params
['priceSetId'], 'is_quick_config')) {
1326 list($this->_params
['frequency_interval'], $this->_params
['frequency_unit']) = CRM_Price_BAO_PriceSet
::getRecurDetails($this->_params
['priceSetId']);
1329 // FIXME: set interval and unit based on selected membership type
1330 $this->_params
['frequency_interval'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
1331 $this->_params
['selectMembership'], 'duration_interval'
1333 $this->_params
['frequency_unit'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
1334 $this->_params
['selectMembership'], 'duration_unit'