3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 use Civi\Payment\System
;
37 * This class generates form components for processing a contribution.
39 class CRM_Contribute_Form_ContributionBase
extends CRM_Core_Form
{
42 * The id of the contribution page that we are processing.
49 * The mode that we are in
57 * The contact id related to a membership
61 public $_membershipContactID;
64 * The values for the contribution db object
71 * The paymentProcessor attributes for this page
75 public $_paymentProcessor;
77 public $_paymentObject = NULL;
80 * The membership block for this page
84 public $_membershipBlock = NULL;
87 * Does this form support a separate membership payment
90 protected $_separateMembershipPayment;
93 * The params submitted by the form and computed by the app
97 public $_params = array();
100 * The fields involved in this contribution page
104 public $_fields = array();
107 * The billing location id for this contribution page.
114 * Cache the amount to make things easier
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
151 * The Membership ID for membership renewal
155 public $_membershipId;
158 * Price Set ID, if the new price set method is used
165 * Array of fields for the price set
174 * Is honor block is enabled for this contribution?
178 public $_honor_block_is_active = FALSE;
181 * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
184 public $_contributeMode;
187 * Contribution page supports memberships
190 public $_useForMember;
192 public $_isBillingAddressRequiredForPayLater;
195 * Set variables up before form is built.
197 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
200 public function preProcess() {
202 // current contribution page id
203 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
205 // seems like the session is corrupted and/or we lost the id trail
206 // lets just bump this to a regular session error and redirect user to main page
207 $this->controller
->invalidKeyRedirect();
210 // this was used prior to the cleverer this_>getContactID - unsure now
211 $this->_userID
= CRM_Core_Session
::singleton()->get('userID');
213 //Check if honor block is enabled for current contribution
214 $ufJoinParams = array(
215 'module' => 'soft_credit',
216 'entity_table' => 'civicrm_contribution_page',
217 'entity_id' => $this->_id
,
219 $ufJoin = new CRM_Core_DAO_UFJoin();
220 $ufJoin->copyValues($ufJoinParams);
222 $this->_honor_block_is_active
= $ufJoin->is_active
;
224 $this->_contactID
= $this->_membershipContactID
= $this->getContactID();
226 if ($this->_contactID
) {
227 $this->_mid
= CRM_Utils_Request
::retrieve('mid', 'Positive', $this);
229 $membership = new CRM_Member_DAO_Membership();
230 $membership->id
= $this->_mid
;
232 if ($membership->find(TRUE)) {
233 $this->_defaultMemTypeId
= $membership->membership_type_id
;
234 if ($membership->contact_id
!= $this->_contactID
) {
235 $validMembership = FALSE;
236 $employers = CRM_Contact_BAO_Relationship
::getPermissionedEmployer($this->_userID
);
237 if (!empty($employers) && array_key_exists($membership->contact_id
, $employers)) {
238 $this->_membershipContactID
= $membership->contact_id
;
239 $this->assign('membershipContactID', $this->_membershipContactID
);
240 $this->assign('membershipContactName', $employers[$this->_membershipContactID
]['name']);
241 $validMembership = TRUE;
244 $membershipType = new CRM_Member_BAO_MembershipType();
245 $membershipType->id
= $membership->membership_type_id
;
246 if ($membershipType->find(TRUE)) {
247 // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values.
248 // Convert to comma separated list.
249 $inheritedRelTypes = implode(CRM_Utils_Array
::explodePadded($membershipType->relationship_type_id
), ',');
250 $permContacts = CRM_Contact_BAO_Relationship
::getPermissionedContacts($this->_userID
, $membershipType->relationship_type_id
);
251 if (array_key_exists($membership->contact_id
, $permContacts)) {
252 $this->_membershipContactID
= $membership->contact_id
;
253 $validMembership = TRUE;
257 if (!$validMembership) {
258 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');
263 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');
269 // we do not want to display recently viewed items, so turn off
270 $this->assign('displayRecent', FALSE);
271 // Contribution page values are cleared from session, so can't use normal Printer Friendly view.
272 // Use Browser Print instead.
273 $this->assign('browserPrint', TRUE);
276 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
277 $this->assign('action', $this->_action
);
280 $this->_mode
= ($this->_action
== 1024) ?
'test' : 'live';
282 $this->_values
= $this->get('values');
283 $this->_fields
= $this->get('fields');
284 $this->_bltID
= $this->get('bltID');
285 $this->_paymentProcessor
= $this->get('paymentProcessor');
286 $this->_priceSetId
= $this->get('priceSetId');
287 $this->_priceSet
= $this->get('priceSet');
289 if (!$this->_values
) {
290 // get all the values from the dao object
291 $this->_values
= array();
292 $this->_fields
= array();
294 CRM_Contribute_BAO_ContributionPage
::setValues($this->_id
, $this->_values
);
296 if (empty($this->_values
['is_active'])) {
297 throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id
);
300 $this->assignBillingType();
302 // check for is_monetary status
303 $isMonetary = CRM_Utils_Array
::value('is_monetary', $this->_values
);
304 $isPayLater = CRM_Utils_Array
::value('is_pay_later', $this->_values
);
307 (!$isPayLater ||
!empty($this->_values
['payment_processor']))
309 $this->_paymentProcessorIDs
= explode(
310 CRM_Core_DAO
::VALUE_SEPARATOR
,
311 CRM_Utils_Array
::value('payment_processor', $this->_values
)
314 $this->assignPaymentProcessor();
319 CRM_Price_BAO_PriceSet
::initSet($this, $this->_id
, 'civicrm_contribution_page');
321 // this avoids getting E_NOTICE errors in php
322 $setNullFields = array(
323 'amount_block_is_active',
324 'is_allow_other_amount',
327 foreach ($setNullFields as $f) {
328 if (!isset($this->_values
[$f])) {
329 $this->_values
[$f] = NULL;
333 //check if Membership Block is enabled, if Membership Fields are included in profile
334 //get membership section for this contribution page
335 $this->_membershipBlock
= CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
336 $this->set('membershipBlock', $this->_membershipBlock
);
338 if ($this->_values
['custom_pre_id']) {
339 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($this->_values
['custom_pre_id']);
342 if ($this->_values
['custom_post_id']) {
343 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($this->_values
['custom_post_id']);
346 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
347 (isset($preProfileType) && $preProfileType == 'Membership')
349 !$this->_membershipBlock
['is_active']
351 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.'));
354 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock
::getPledgeBlock($this->_id
);
357 $this->_values
['pledge_block_id'] = CRM_Utils_Array
::value('id', $pledgeBlock);
358 $this->_values
['max_reminders'] = CRM_Utils_Array
::value('max_reminders', $pledgeBlock);
359 $this->_values
['initial_reminder_day'] = CRM_Utils_Array
::value('initial_reminder_day', $pledgeBlock);
360 $this->_values
['additional_reminder_day'] = CRM_Utils_Array
::value('additional_reminder_day', $pledgeBlock);
362 //set pledge id in values
363 $pledgeId = CRM_Utils_Request
::retrieve('pledgeId', 'Positive', $this);
365 //authenticate pledge user for pledge payment.
367 $this->_values
['pledge_id'] = $pledgeId;
369 //lets override w/ pledge campaign.
370 $this->_values
['campaign_id'] = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge',
374 self
::authenticatePledgeUser();
377 $this->set('values', $this->_values
);
378 $this->set('fields', $this->_fields
);
382 $pcpId = CRM_Utils_Request
::retrieve('pcpId', 'Positive', $this);
384 $pcp = CRM_PCP_BAO_PCP
::handlePcp($pcpId, 'contribute', $this->_values
);
385 $this->_pcpId
= $pcp['pcpId'];
386 $this->_pcpBlock
= $pcp['pcpBlock'];
387 $this->_pcpInfo
= $pcp['pcpInfo'];
390 // Link (button) for users to create their own Personal Campaign page
391 if ($linkText = CRM_PCP_BAO_PCP
::getPcpBlockStatus($this->_id
, 'contribute')) {
392 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
393 "action=add&reset=1&pageId={$this->_id}&component=contribute",
396 $this->assign('linkTextUrl', $linkTextUrl);
397 $this->assign('linkText', $linkText);
400 //set pledge block if block id is set
401 if (!empty($this->_values
['pledge_block_id'])) {
402 $this->assign('pledgeBlock', TRUE);
405 // check if one of the (amount , membership) blocks is active or not.
406 $this->_membershipBlock
= $this->get('membershipBlock');
408 if (!$this->_values
['amount_block_is_active'] &&
409 !$this->_membershipBlock
['is_active'] &&
412 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.'));
415 if ($this->_values
['amount_block_is_active']) {
416 $this->set('amount_block_is_active', $this->_values
['amount_block_is_active']);
419 $this->_contributeMode
= $this->get('contributeMode');
420 $this->assign('contributeMode', $this->_contributeMode
);
422 //assigning is_monetary and is_email_receipt to template
423 $this->assign('is_monetary', $this->_values
['is_monetary']);
424 $this->assign('is_email_receipt', $this->_values
['is_email_receipt']);
425 $this->assign('bltID', $this->_bltID
);
427 //assign cancelSubscription URL to templates
428 $this->assign('cancelSubscriptionUrl',
429 CRM_Utils_Array
::value('cancelSubscriptionUrl', $this->_values
)
432 // assigning title to template in case someone wants to use it, also setting CMS page title
434 $this->assign('title', $this->_pcpInfo
['title']);
435 CRM_Utils_System
::setTitle($this->_pcpInfo
['title']);
438 $this->assign('title', $this->_values
['title']);
439 CRM_Utils_System
::setTitle($this->_values
['title']);
441 $this->_defaults
= array();
443 $this->_amount
= $this->get('amount');
446 $config = CRM_Core_Config
::singleton();
447 $config->defaultCurrency
= CRM_Utils_Array
::value('currency',
449 $config->defaultCurrency
452 //lets allow user to override campaign.
453 $campID = CRM_Utils_Request
::retrieve('campID', 'Positive', $this);
454 if ($campID && CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
455 $this->_values
['campaign_id'] = $campID;
458 //do check for cancel recurring and clean db, CRM-7696
459 if (CRM_Utils_Request
::retrieve('cancel', 'Boolean', CRM_Core_DAO
::$_nullObject)) {
460 self
::cancelRecurring();
463 // check if billing block is required for pay later
464 if (CRM_Utils_Array
::value('is_pay_later', $this->_values
)) {
465 $this->_isBillingAddressRequiredForPayLater
= CRM_Utils_Array
::value('is_billing_required', $this->_values
);
466 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater
);
471 * Set the default values.
473 public function setDefaultValues() {
474 return $this->_defaults
;
478 * Assign the minimal set of variables to the template.
480 public function assignToTemplate() {
481 $name = CRM_Utils_Array
::value('billing_first_name', $this->_params
);
482 if (!empty($this->_params
['billing_middle_name'])) {
483 $name .= " {$this->_params['billing_middle_name']}";
485 $name .= ' ' . CRM_Utils_Array
::value('billing_last_name', $this->_params
);
487 $this->assign('billingName', $name);
488 $this->set('name', $name);
490 $this->assign('paymentProcessor', $this->_paymentProcessor
);
499 $config = CRM_Core_Config
::singleton();
500 if (isset($this->_values
['is_recur']) && !empty($this->_paymentProcessor
['is_recur'])) {
501 $this->assign('is_recur_enabled', 1);
502 $vars = array_merge($vars, array(
504 'frequency_interval',
510 if (in_array('CiviPledge', $config->enableComponents
) &&
511 CRM_Utils_Array
::value('is_pledge', $this->_params
) == 1
513 $this->assign('pledge_enabled', 1);
515 $vars = array_merge($vars, array(
517 'pledge_frequency_interval',
518 'pledge_frequency_unit',
519 'pledge_installments',
523 if (isset($this->_params
['amount_other']) ||
isset($this->_params
['selectMembership'])) {
524 $this->_params
['amount_level'] = '';
527 foreach ($vars as $v) {
528 if (isset($this->_params
[$v])) {
529 if ($v == "amount" && $this->_params
[$v] === 0) {
530 $this->_params
[$v] = CRM_Utils_Money
::format($this->_params
[$v], NULL, NULL, TRUE);
532 $this->assign($v, $this->_params
[$v]);
536 // assign the address formatted up for display
537 $addressParts = array(
538 "street_address-{$this->_bltID}",
539 "city-{$this->_bltID}",
540 "postal_code-{$this->_bltID}",
541 "state_province-{$this->_bltID}",
542 "country-{$this->_bltID}",
545 $addressFields = array();
546 foreach ($addressParts as $part) {
547 list($n, $id) = explode('-', $part);
548 $addressFields[$n] = CRM_Utils_Array
::value('billing_' . $part, $this->_params
);
551 $this->assign('address', CRM_Utils_Address
::format($addressFields));
553 if (!empty($this->_params
['hidden_onbehalf_profile'])) {
554 $this->assign('onBehalfName', $this->_params
['organization_name']);
555 $locTypeId = array_keys($this->_params
['onbehalf_location']['email']);
556 $this->assign('onBehalfEmail', $this->_params
['onbehalf_location']['email'][$locTypeId[0]]['email']);
560 $assignCCInfo = FALSE;
561 if ($this->_amount
> 0.0) {
562 $assignCCInfo = TRUE;
564 elseif (!empty($this->_params
['selectMembership'])) {
565 $memFee = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params
['selectMembership'], 'minimum_fee');
567 $assignCCInfo = TRUE;
571 if ($this->_contributeMode
== 'direct' && $assignCCInfo) {
572 if ($this->_paymentProcessor
&&
573 $this->_paymentProcessor
['payment_type'] & CRM_Core_Payment
::PAYMENT_TYPE_DIRECT_DEBIT
575 $this->assign('account_holder', $this->_params
['account_holder']);
576 $this->assign('bank_identification_number', $this->_params
['bank_identification_number']);
577 $this->assign('bank_name', $this->_params
['bank_name']);
578 $this->assign('bank_account_number', $this->_params
['bank_account_number']);
581 $date = CRM_Utils_Date
::format(CRM_Utils_array
::value('credit_card_exp_date', $this->_params
));
582 $date = CRM_Utils_Date
::mysqlToIso($date);
583 $this->assign('credit_card_exp_date', $date);
584 $this->assign('credit_card_number',
585 CRM_Utils_System
::mungeCreditCard(CRM_Utils_array
::value('credit_card_number', $this->_params
))
590 $this->assign('email',
591 $this->controller
->exportValue('Main', "email-{$this->_bltID}")
594 // also assign the receipt_text
595 if (isset($this->_values
['receipt_text'])) {
596 $this->assign('receipt_text', $this->_values
['receipt_text']);
601 * Add the custom fields.
604 * @param string $name
605 * @param bool $viewOnly
606 * @param null $profileContactType
607 * @param array $fieldTypes
609 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
611 $contactID = $this->getContactID();
613 // we don't allow conflicting fields to be
614 // configured via profile - CRM 2100
615 $fieldsToIgnore = array(
621 'non_deductible_amount' => 1,
624 'contribution_status_id' => 1,
625 'payment_instrument' => 1,
627 'financial_type' => 1,
631 if ($contactID && CRM_Core_BAO_UFGroup
::filterUFGroups($id, $contactID)) {
632 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
, NULL, NULL, FALSE,
633 NULL, FALSE, NULL, CRM_Core_Permission
::CREATE
, NULL
637 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
, NULL, NULL, FALSE,
638 NULL, FALSE, NULL, CRM_Core_Permission
::CREATE
, NULL
643 // unset any email-* fields since we already collect it, CRM-2888
644 foreach (array_keys($fields) as $fieldName) {
645 if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') {
646 unset($fields[$fieldName]);
650 if (array_intersect_key($fields, $fieldsToIgnore)) {
651 $fields = array_diff_key($fields, $fieldsToIgnore);
652 CRM_Core_Session
::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
655 $fields = array_diff_assoc($fields, $this->_fields
);
657 CRM_Core_BAO_Address
::checkContactSharedAddressFields($fields, $contactID);
659 foreach ($fields as $key => $field) {
661 isset($field['data_type']) &&
662 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
664 // ignore file upload fields
668 if ($profileContactType) {
669 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
670 $honoreeNamefields = array(
678 if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) {
679 unset($fields[$field['name']]);
682 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
683 CRM_Core_BAO_UFGroup
::buildProfile(
686 CRM_Profile_Form
::MODE_CREATE
,
691 $this->_fields
[$profileContactType][$key] = $field;
694 unset($fields[$key]);
698 CRM_Core_BAO_UFGroup
::buildProfile(
701 CRM_Profile_Form
::MODE_CREATE
,
705 $this->_fields
[$key] = $field;
707 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
708 if ($field['add_captcha'] && !$this->_userID
) {
713 $this->assign($name, $fields);
715 if ($addCaptcha && !$viewOnly) {
716 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
717 $captcha->add($this);
718 $this->assign('isCaptcha', TRUE);
725 * Check template file exists.
727 * @param string $suffix
729 * @return null|string
731 public function checkTemplateFileExists($suffix = NULL) {
733 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
734 $template = CRM_Core_Form
::getTemplate();
735 if ($template->template_exists($templateFile)) {
736 return $templateFile;
743 * Use the form name to create the tpl file name.
747 public function getTemplateFileName() {
748 $fileName = $this->checkTemplateFileExists();
749 return $fileName ?
$fileName : parent
::getTemplateFileName();
753 * Add the extra.tpl in.
755 * Default extra tpl file basically just replaces .tpl with .extra.tpl
756 * i.e. we do not override - why isn't this done at the CRM_Core_Form level?
760 public function overrideExtraTemplateFileName() {
761 $fileName = $this->checkTemplateFileExists('extra.');
762 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();
766 * Authenticate pledge user during online payment.
768 public function authenticatePledgeUser() {
769 //get the userChecksum and contact id
770 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
771 $contactID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
773 //get pledge status and contact id
774 $pledgeValues = array();
775 $pledgeParams = array('id' => $this->_values
['pledge_id']);
776 $returnProperties = array('contact_id', 'status_id');
777 CRM_Core_DAO
::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
780 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
781 $validStatus = array(
782 array_search('Pending', $allStatus),
783 array_search('In Progress', $allStatus),
784 array_search('Overdue', $allStatus),
788 if ($this->_userID
&&
789 $this->_userID
== $pledgeValues['contact_id']
791 //check for authenticated user.
794 elseif ($userChecksum && $pledgeValues['contact_id']) {
795 //check for anonymous user.
796 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($pledgeValues['contact_id'], $userChecksum);
798 //make sure cid is same as pledge contact id
799 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
805 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."));
808 //check for valid pledge status.
809 if (!in_array($pledgeValues['status_id'], $validStatus)) {
810 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))));
815 * Cancel recurring contributions.
817 * In case user cancel recurring contribution,
818 * When we get the control back from payment gate way
819 * lets delete the recurring and related contribution.
821 public function cancelRecurring() {
822 $isCancel = CRM_Utils_Request
::retrieve('cancel', 'Boolean', CRM_Core_DAO
::$_nullObject);
824 $isRecur = CRM_Utils_Request
::retrieve('isRecur', 'Boolean', CRM_Core_DAO
::$_nullObject);
825 $recurId = CRM_Utils_Request
::retrieve('recurId', 'Positive', CRM_Core_DAO
::$_nullObject);
826 //clean db for recurring contribution.
827 if ($isRecur && $recurId) {
828 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($recurId);
830 $contribId = CRM_Utils_Request
::retrieve('contribId', 'Positive', CRM_Core_DAO
::$_nullObject);
832 CRM_Contribute_BAO_Contribution
::deleteContribution($contribId);
838 * Build Membership Block in Contribution Pages.
841 * Contact checked for having a current membership for a particular membership.
842 * @param bool $isContributionMainPage
843 * Is this the main page? If so add form input fields.
844 * (or better yet don't have this functionality in a function shared with forms that don't share it).
845 * @param int $selectedMembershipTypeID
846 * Selected membership id.
847 * @param bool $thankPage
849 * @param null $isTest
852 * Is this a separate membership payment
854 protected function buildMembershipBlock(
856 $isContributionMainPage = FALSE,
857 $selectedMembershipTypeID = NULL,
862 $separateMembershipPayment = FALSE;
863 if ($this->_membershipBlock
) {
864 $this->_currentMemberships
= array();
866 $membershipTypeIds = $membershipTypes = $radio = array();
867 $membershipPriceset = (!empty($this->_priceSetId
) && $this->_useForMember
) ?
TRUE : FALSE;
869 $allowAutoRenewMembership = $autoRenewOption = FALSE;
870 $autoRenewMembershipTypeOptions = array();
872 $separateMembershipPayment = CRM_Utils_Array
::value('is_separate_payment', $this->_membershipBlock
);
874 if ($membershipPriceset) {
875 foreach ($this->_priceSet
['fields'] as $pField) {
876 if (empty($pField['options'])) {
879 foreach ($pField['options'] as $opId => $opValues) {
880 if (empty($opValues['membership_type_id'])) {
883 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
887 elseif (!empty($this->_membershipBlock
['membership_types'])) {
888 $membershipTypeIds = explode(',', $this->_membershipBlock
['membership_types']);
891 if (!empty($membershipTypeIds)) {
892 //set status message if wrong membershipType is included in membershipBlock
893 if (isset($this->_mid
) && !$membershipPriceset) {
894 $membershipTypeID = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
898 if (!in_array($membershipTypeID, $membershipTypeIds)) {
899 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');
903 $membershipTypeValues = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, $membershipTypeIds);
904 $this->_membershipTypeValues
= $membershipTypeValues;
906 foreach ($membershipTypeIds as $value) {
907 $memType = $membershipTypeValues[$value];
908 if ($selectedMembershipTypeID != NULL) {
909 if ($memType['id'] == $selectedMembershipTypeID) {
910 $this->assign('minimum_fee',
911 CRM_Utils_Array
::value('minimum_fee', $memType)
913 $this->assign('membership_name', $memType['name']);
914 if (!$thankPage && $cid) {
915 $membership = new CRM_Member_DAO_Membership();
916 $membership->contact_id
= $cid;
917 $membership->membership_type_id
= $memType['id'];
918 if ($membership->find(TRUE)) {
919 $this->assign('renewal_mode', TRUE);
920 $memType['current_membership'] = $membership->end_date
;
921 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
924 $membershipTypes[] = $memType;
927 elseif ($memType['is_active']) {
928 $javascriptMethod = NULL;
929 $allowAutoRenewOpt = (int) $memType['auto_renew'];
930 if (is_array($this->_paymentProcessors
)) {
931 foreach ($this->_paymentProcessors
as $id => $val) {
932 if (!$val['is_recur']) {
933 $allowAutoRenewOpt = 0;
939 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
940 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array
::value($value, CRM_Utils_Array
::value('auto_renew', $this->_membershipBlock
));;
942 if ($allowAutoRenewOpt) {
943 $allowAutoRenewMembership = TRUE;
946 //add membership type.
947 $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL,
948 $memType['id'], $javascriptMethod
951 $membership = new CRM_Member_DAO_Membership();
952 $membership->contact_id
= $cid;
953 $membership->membership_type_id
= $memType['id'];
955 //show current membership, skip pending and cancelled membership records,
956 //because we take first membership record id for renewal
957 $membership->whereAdd('status_id != 5 AND status_id !=6');
959 if (!is_null($isTest)) {
960 $membership->is_test
= $isTest;
964 $membership->orderBy('end_date DESC');
966 if ($membership->find(TRUE)) {
967 if (!$membership->end_date
) {
968 unset($radio[$memType['id']]);
969 $this->assign('islifetime', TRUE);
972 $this->assign('renewal_mode', TRUE);
973 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
974 $memType['current_membership'] = $membership->end_date
;
976 $endDate = $memType['current_membership'];
977 $this->_defaultMemTypeId
= $memType['id'];
979 if ($memType['current_membership'] < $endDate) {
980 $endDate = $memType['current_membership'];
981 $this->_defaultMemTypeId
= $memType['id'];
985 $membershipTypes[] = $memType;
990 $this->assign('membershipBlock', $this->_membershipBlock
);
991 $this->assign('showRadio', $isContributionMainPage);
992 $this->assign('membershipTypes', $membershipTypes);
993 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
994 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
995 //give preference to user submitted auto_renew value.
996 $takeUserSubmittedAutoRenew = (!empty($_POST) ||
$this->isSubmitted()) ?
TRUE : FALSE;
997 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
999 if ($isContributionMainPage) {
1000 if (!$membershipPriceset) {
1001 if (!$this->_membershipBlock
['is_required']) {
1002 $this->assign('showRadioNoThanks', TRUE);
1003 $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
1004 $this->addGroup($radio, 'selectMembership', NULL);
1006 elseif ($this->_membershipBlock
['is_required'] && count($radio) == 1) {
1007 $temp = array_keys($radio);
1008 $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
1009 $this->assign('singleMembership', TRUE);
1010 $this->assign('showRadio', FALSE);
1013 $this->addGroup($radio, 'selectMembership', NULL);
1016 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
1019 $autoRenewOption = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($this->_priceSetId
);
1020 $this->assign('autoRenewOption', $autoRenewOption);
1023 if (!$this->_values
['is_pay_later'] && is_array($this->_paymentProcessors
) && ($allowAutoRenewMembership ||
$autoRenewOption)) {
1024 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
1030 return $separateMembershipPayment;
1034 * Determine if recurring parameters need to be added to the form parameters.
1036 * - frequency_interval
1039 * For membership this is based on the membership type.
1041 * This needs to be done before processing the pre-approval redirect where relevant on the main page or before any payment processing.
1043 * Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout.
1045 protected function setRecurringMembershipParams() {
1046 if (!empty($this->_params
['priceSetId']) && !empty($this->_params
['selectMembership'])) {
1047 // @todo the price_x fields will ALWAYS allow us to determine the membership - so we should ignore
1048 // 'selectMembership' and calculate from the price_x fields so we have one method that always works
1049 // this is lazy & only catches when selectMembership is set, but the worst of all worlds would be to fix
1050 // this with an else (calculate for price set).
1051 $membershipTypes = CRM_Price_BAO_PriceSet
::getMembershipTypesFromPriceSet($this->_params
['priceSetId']);
1052 if (in_array($this->_params
['selectMembership'], $membershipTypes['autorenew'])) {
1053 $this->_params
['auto_renew'] = TRUE;
1056 if ((!empty($this->_params
['selectMembership']) ||
!empty($this->_params
['priceSetId'])) && !empty($this->_paymentProcessor
['is_recur']) &&
1057 CRM_Utils_Array
::value('auto_renew', $this->_params
) && empty($this->_params
['is_recur']) && empty($this->_params
['frequency_interval'])
1060 $this->_params
['is_recur'] = $this->_values
['is_recur'] = 1;
1061 // check if price set is not quick config
1062 if (!empty($this->_params
['priceSetId']) && !CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params
['priceSetId'], 'is_quick_config')) {
1063 list($this->_params
['frequency_interval'], $this->_params
['frequency_unit']) = CRM_Price_BAO_PriceSet
::getRecurDetails($this->_params
['priceSetId']);
1066 // FIXME: set interval and unit based on selected membership type
1067 $this->_params
['frequency_interval'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
1068 $this->_params
['selectMembership'], 'duration_interval'
1070 $this->_params
['frequency_unit'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
1071 $this->_params
['selectMembership'], 'duration_unit'