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;
92 * The default values for the form
99 * The params submitted by the form and computed by the app
106 * The fields involved in this contribution page
110 public $_fields = array();
113 * The billing location id for this contribution page.
120 * Cache the amount to make things easier
148 * The contact id of the person for whom membership is being added or renewed based on the cid in the url,
149 * checksum, or session
157 * The Membership ID for membership renewal
161 public $_membershipId;
164 * Price Set ID, if the new price set method is used
171 * Array of fields for the price set
180 * Is honor block is enabled for this contribution?
184 public $_honor_block_is_active = FALSE;
187 * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
190 public $_contributeMode;
193 * Contribution page supports memberships
196 public $_useForMember;
198 public $_isBillingAddressRequiredForPayLater;
201 * Set variables up before form is built.
203 * @throws \CRM_Contribute_Exception_InactiveContributionPageException
206 public function preProcess() {
208 // current contribution page id
209 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
211 // seems like the session is corrupted and/or we lost the id trail
212 // lets just bump this to a regular session error and redirect user to main page
213 $this->controller
->invalidKeyRedirect();
216 // this was used prior to the cleverer this_>getContactID - unsure now
217 $this->_userID
= CRM_Core_Session
::singleton()->get('userID');
219 //Check if honor block is enabled for current contribution
220 $ufJoinParams = array(
221 'module' => 'soft_credit',
222 'entity_table' => 'civicrm_contribution_page',
223 'entity_id' => $this->_id
,
225 $ufJoin = new CRM_Core_DAO_UFJoin();
226 $ufJoin->copyValues($ufJoinParams);
228 $this->_honor_block_is_active
= $ufJoin->is_active
;
230 $this->_contactID
= $this->_membershipContactID
= $this->getContactID();
232 if ($this->_contactID
) {
233 $this->_mid
= CRM_Utils_Request
::retrieve('mid', 'Positive', $this);
235 $membership = new CRM_Member_DAO_Membership();
236 $membership->id
= $this->_mid
;
238 if ($membership->find(TRUE)) {
239 $this->_defaultMemTypeId
= $membership->membership_type_id
;
240 if ($membership->contact_id
!= $this->_contactID
) {
241 $validMembership = FALSE;
242 $employers = CRM_Contact_BAO_Relationship
::getPermissionedEmployer($this->_userID
);
243 if (!empty($employers) && array_key_exists($membership->contact_id
, $employers)) {
244 $this->_membershipContactID
= $membership->contact_id
;
245 $this->assign('membershipContactID', $this->_membershipContactID
);
246 $this->assign('membershipContactName', $employers[$this->_membershipContactID
]['name']);
247 $validMembership = TRUE;
250 $membershipType = new CRM_Member_BAO_MembershipType();
251 $membershipType->id
= $membership->membership_type_id
;
252 if ($membershipType->find(TRUE)) {
253 // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values.
254 // Convert to comma separated list.
255 $inheritedRelTypes = implode(CRM_Utils_Array
::explodePadded($membershipType->relationship_type_id
), ',');
256 $permContacts = CRM_Contact_BAO_Relationship
::getPermissionedContacts($this->_userID
, $membershipType->relationship_type_id
);
257 if (array_key_exists($membership->contact_id
, $permContacts)) {
258 $this->_membershipContactID
= $membership->contact_id
;
259 $validMembership = TRUE;
263 if (!$validMembership) {
264 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 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 // we do not want to display recently viewed items, so turn off
276 $this->assign('displayRecent', FALSE);
277 // Contribution page values are cleared from session, so can't use normal Printer Friendly view.
278 // Use Browser Print instead.
279 $this->assign('browserPrint', TRUE);
282 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
283 $this->assign('action', $this->_action
);
286 $this->_mode
= ($this->_action
== 1024) ?
'test' : 'live';
288 $this->_values
= $this->get('values');
289 $this->_fields
= $this->get('fields');
290 $this->_bltID
= $this->get('bltID');
291 $this->_paymentProcessor
= $this->get('paymentProcessor');
292 $this->_priceSetId
= $this->get('priceSetId');
293 $this->_priceSet
= $this->get('priceSet');
295 if (!$this->_values
) {
296 // get all the values from the dao object
297 $this->_values
= array();
298 $this->_fields
= array();
300 CRM_Contribute_BAO_ContributionPage
::setValues($this->_id
, $this->_values
);
302 if (empty($this->_values
['is_active'])) {
303 throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id
);
306 $this->assignBillingType();
308 // check for is_monetary status
309 $isMonetary = CRM_Utils_Array
::value('is_monetary', $this->_values
);
310 $isPayLater = CRM_Utils_Array
::value('is_pay_later', $this->_values
);
313 (!$isPayLater ||
!empty($this->_values
['payment_processor']))
315 $this->_paymentProcessorIDs
= explode(
316 CRM_Core_DAO
::VALUE_SEPARATOR
,
317 CRM_Utils_Array
::value('payment_processor', $this->_values
)
320 $this->assignPaymentProcessor();
325 CRM_Price_BAO_PriceSet
::initSet($this, $this->_id
, 'civicrm_contribution_page');
327 // this avoids getting E_NOTICE errors in php
328 $setNullFields = array(
329 'amount_block_is_active',
330 'is_allow_other_amount',
333 foreach ($setNullFields as $f) {
334 if (!isset($this->_values
[$f])) {
335 $this->_values
[$f] = NULL;
339 //check if Membership Block is enabled, if Membership Fields are included in profile
340 //get membership section for this contribution page
341 $this->_membershipBlock
= CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
342 $this->set('membershipBlock', $this->_membershipBlock
);
344 if ($this->_values
['custom_pre_id']) {
345 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($this->_values
['custom_pre_id']);
348 if ($this->_values
['custom_post_id']) {
349 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($this->_values
['custom_post_id']);
352 if (((isset($postProfileType) && $postProfileType == 'Membership') ||
353 (isset($preProfileType) && $preProfileType == 'Membership')
355 !$this->_membershipBlock
['is_active']
357 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.'));
360 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock
::getPledgeBlock($this->_id
);
363 $this->_values
['pledge_block_id'] = CRM_Utils_Array
::value('id', $pledgeBlock);
364 $this->_values
['max_reminders'] = CRM_Utils_Array
::value('max_reminders', $pledgeBlock);
365 $this->_values
['initial_reminder_day'] = CRM_Utils_Array
::value('initial_reminder_day', $pledgeBlock);
366 $this->_values
['additional_reminder_day'] = CRM_Utils_Array
::value('additional_reminder_day', $pledgeBlock);
368 //set pledge id in values
369 $pledgeId = CRM_Utils_Request
::retrieve('pledgeId', 'Positive', $this);
371 //authenticate pledge user for pledge payment.
373 $this->_values
['pledge_id'] = $pledgeId;
375 //lets override w/ pledge campaign.
376 $this->_values
['campaign_id'] = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge',
380 self
::authenticatePledgeUser();
383 $this->set('values', $this->_values
);
384 $this->set('fields', $this->_fields
);
388 $pcpId = CRM_Utils_Request
::retrieve('pcpId', 'Positive', $this);
390 $pcp = CRM_PCP_BAO_PCP
::handlePcp($pcpId, 'contribute', $this->_values
);
391 $this->_pcpId
= $pcp['pcpId'];
392 $this->_pcpBlock
= $pcp['pcpBlock'];
393 $this->_pcpInfo
= $pcp['pcpInfo'];
396 // Link (button) for users to create their own Personal Campaign page
397 if ($linkText = CRM_PCP_BAO_PCP
::getPcpBlockStatus($this->_id
, 'contribute')) {
398 $linkTextUrl = CRM_Utils_System
::url('civicrm/contribute/campaign',
399 "action=add&reset=1&pageId={$this->_id}&component=contribute",
402 $this->assign('linkTextUrl', $linkTextUrl);
403 $this->assign('linkText', $linkText);
406 //set pledge block if block id is set
407 if (!empty($this->_values
['pledge_block_id'])) {
408 $this->assign('pledgeBlock', TRUE);
411 // check if one of the (amount , membership) blocks is active or not.
412 $this->_membershipBlock
= $this->get('membershipBlock');
414 if (!$this->_values
['amount_block_is_active'] &&
415 !$this->_membershipBlock
['is_active'] &&
418 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.'));
421 if ($this->_values
['amount_block_is_active']) {
422 $this->set('amount_block_is_active', $this->_values
['amount_block_is_active']);
425 $this->_contributeMode
= $this->get('contributeMode');
426 $this->assign('contributeMode', $this->_contributeMode
);
428 //assigning is_monetary and is_email_receipt to template
429 $this->assign('is_monetary', $this->_values
['is_monetary']);
430 $this->assign('is_email_receipt', $this->_values
['is_email_receipt']);
431 $this->assign('bltID', $this->_bltID
);
433 //assign cancelSubscription URL to templates
434 $this->assign('cancelSubscriptionUrl',
435 CRM_Utils_Array
::value('cancelSubscriptionUrl', $this->_values
)
438 // assigning title to template in case someone wants to use it, also setting CMS page title
440 $this->assign('title', $this->_pcpInfo
['title']);
441 CRM_Utils_System
::setTitle($this->_pcpInfo
['title']);
444 $this->assign('title', $this->_values
['title']);
445 CRM_Utils_System
::setTitle($this->_values
['title']);
447 $this->_defaults
= array();
449 $this->_amount
= $this->get('amount');
452 $config = CRM_Core_Config
::singleton();
453 $config->defaultCurrency
= CRM_Utils_Array
::value('currency',
455 $config->defaultCurrency
458 //lets allow user to override campaign.
459 $campID = CRM_Utils_Request
::retrieve('campID', 'Positive', $this);
460 if ($campID && CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
461 $this->_values
['campaign_id'] = $campID;
464 //do check for cancel recurring and clean db, CRM-7696
465 if (CRM_Utils_Request
::retrieve('cancel', 'Boolean', CRM_Core_DAO
::$_nullObject)) {
466 self
::cancelRecurring();
469 // check if billing block is required for pay later
470 if (CRM_Utils_Array
::value('is_pay_later', $this->_values
)) {
471 $this->_isBillingAddressRequiredForPayLater
= CRM_Utils_Array
::value('is_billing_required', $this->_values
);
472 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater
);
477 * Set the default values.
479 public function setDefaultValues() {
480 return $this->_defaults
;
484 * Assign the minimal set of variables to the template.
486 public function assignToTemplate() {
487 $name = CRM_Utils_Array
::value('billing_first_name', $this->_params
);
488 if (!empty($this->_params
['billing_middle_name'])) {
489 $name .= " {$this->_params['billing_middle_name']}";
491 $name .= ' ' . CRM_Utils_Array
::value('billing_last_name', $this->_params
);
493 $this->assign('billingName', $name);
494 $this->set('name', $name);
496 $this->assign('paymentProcessor', $this->_paymentProcessor
);
505 $config = CRM_Core_Config
::singleton();
506 if (isset($this->_values
['is_recur']) && !empty($this->_paymentProcessor
['is_recur'])) {
507 $this->assign('is_recur_enabled', 1);
508 $vars = array_merge($vars, array(
510 'frequency_interval',
516 if (in_array('CiviPledge', $config->enableComponents
) &&
517 CRM_Utils_Array
::value('is_pledge', $this->_params
) == 1
519 $this->assign('pledge_enabled', 1);
521 $vars = array_merge($vars, array(
523 'pledge_frequency_interval',
524 'pledge_frequency_unit',
525 'pledge_installments',
529 if (isset($this->_params
['amount_other']) ||
isset($this->_params
['selectMembership'])) {
530 $this->_params
['amount_level'] = '';
533 foreach ($vars as $v) {
534 if (isset($this->_params
[$v])) {
535 if ($v == "amount" && $this->_params
[$v] === 0) {
536 $this->_params
[$v] = CRM_Utils_Money
::format($this->_params
[$v], NULL, NULL, TRUE);
538 $this->assign($v, $this->_params
[$v]);
542 // assign the address formatted up for display
543 $addressParts = array(
544 "street_address-{$this->_bltID}",
545 "city-{$this->_bltID}",
546 "postal_code-{$this->_bltID}",
547 "state_province-{$this->_bltID}",
548 "country-{$this->_bltID}",
551 $addressFields = array();
552 foreach ($addressParts as $part) {
553 list($n, $id) = explode('-', $part);
554 $addressFields[$n] = CRM_Utils_Array
::value('billing_' . $part, $this->_params
);
557 $this->assign('address', CRM_Utils_Address
::format($addressFields));
559 if (!empty($this->_params
['hidden_onbehalf_profile'])) {
560 $this->assign('onBehalfName', $this->_params
['organization_name']);
561 $locTypeId = array_keys($this->_params
['onbehalf_location']['email']);
562 $this->assign('onBehalfEmail', $this->_params
['onbehalf_location']['email'][$locTypeId[0]]['email']);
566 $assignCCInfo = FALSE;
567 if ($this->_amount
> 0.0) {
568 $assignCCInfo = TRUE;
570 elseif (!empty($this->_params
['selectMembership'])) {
571 $memFee = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params
['selectMembership'], 'minimum_fee');
573 $assignCCInfo = TRUE;
577 if ($this->_contributeMode
== 'direct' && $assignCCInfo) {
578 if ($this->_paymentProcessor
&&
579 $this->_paymentProcessor
['payment_type'] & CRM_Core_Payment
::PAYMENT_TYPE_DIRECT_DEBIT
581 $this->assign('account_holder', $this->_params
['account_holder']);
582 $this->assign('bank_identification_number', $this->_params
['bank_identification_number']);
583 $this->assign('bank_name', $this->_params
['bank_name']);
584 $this->assign('bank_account_number', $this->_params
['bank_account_number']);
587 $date = CRM_Utils_Date
::format(CRM_Utils_array
::value('credit_card_exp_date', $this->_params
));
588 $date = CRM_Utils_Date
::mysqlToIso($date);
589 $this->assign('credit_card_exp_date', $date);
590 $this->assign('credit_card_number',
591 CRM_Utils_System
::mungeCreditCard(CRM_Utils_array
::value('credit_card_number', $this->_params
))
596 $this->assign('email',
597 $this->controller
->exportValue('Main', "email-{$this->_bltID}")
600 // also assign the receipt_text
601 if (isset($this->_values
['receipt_text'])) {
602 $this->assign('receipt_text', $this->_values
['receipt_text']);
607 * Add the custom fields.
610 * @param string $name
611 * @param bool $viewOnly
612 * @param null $profileContactType
613 * @param array $fieldTypes
615 public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
617 $contactID = $this->getContactID();
619 // we don't allow conflicting fields to be
620 // configured via profile - CRM 2100
621 $fieldsToIgnore = array(
627 'non_deductible_amount' => 1,
630 'contribution_status_id' => 1,
631 'payment_instrument' => 1,
633 'financial_type' => 1,
637 if ($contactID && CRM_Core_BAO_UFGroup
::filterUFGroups($id, $contactID)) {
638 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
, NULL, NULL, FALSE,
639 NULL, FALSE, NULL, CRM_Core_Permission
::CREATE
, NULL
643 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
, NULL, NULL, FALSE,
644 NULL, FALSE, NULL, CRM_Core_Permission
::CREATE
, NULL
649 // unset any email-* fields since we already collect it, CRM-2888
650 foreach (array_keys($fields) as $fieldName) {
651 if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') {
652 unset($fields[$fieldName]);
656 if (array_intersect_key($fields, $fieldsToIgnore)) {
657 $fields = array_diff_key($fields, $fieldsToIgnore);
658 CRM_Core_Session
::setStatus(ts('Some of the profile fields cannot be configured for this page.'), ts('Warning'), 'alert');
661 $fields = array_diff_assoc($fields, $this->_fields
);
663 CRM_Core_BAO_Address
::checkContactSharedAddressFields($fields, $contactID);
665 foreach ($fields as $key => $field) {
667 isset($field['data_type']) &&
668 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
670 // ignore file upload fields
674 if ($profileContactType) {
675 //Since we are showing honoree name separately so we are removing it from honoree profile just for display
676 $honoreeNamefields = array(
684 if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) {
685 unset($fields[$field['name']]);
688 if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
689 CRM_Core_BAO_UFGroup
::buildProfile(
692 CRM_Profile_Form
::MODE_CREATE
,
697 $this->_fields
[$profileContactType][$key] = $field;
700 unset($fields[$key]);
704 CRM_Core_BAO_UFGroup
::buildProfile(
707 CRM_Profile_Form
::MODE_CREATE
,
711 $this->_fields
[$key] = $field;
713 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
714 if ($field['add_captcha'] && !$this->_userID
) {
719 $this->assign($name, $fields);
721 if ($addCaptcha && !$viewOnly) {
722 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
723 $captcha->add($this);
724 $this->assign('isCaptcha', TRUE);
731 * Check template file exists.
733 * @param string $suffix
735 * @return null|string
737 public function checkTemplateFileExists($suffix = NULL) {
739 $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
740 $template = CRM_Core_Form
::getTemplate();
741 if ($template->template_exists($templateFile)) {
742 return $templateFile;
749 * Use the form name to create the tpl file name.
753 public function getTemplateFileName() {
754 $fileName = $this->checkTemplateFileExists();
755 return $fileName ?
$fileName : parent
::getTemplateFileName();
759 * Add the extra.tpl in.
761 * Default extra tpl file basically just replaces .tpl with .extra.tpl
762 * i.e. we do not override - why isn't this done at the CRM_Core_Form level?
766 public function overrideExtraTemplateFileName() {
767 $fileName = $this->checkTemplateFileExists('extra.');
768 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();
772 * Authenticate pledge user during online payment.
774 public function authenticatePledgeUser() {
775 //get the userChecksum and contact id
776 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
777 $contactID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
779 //get pledge status and contact id
780 $pledgeValues = array();
781 $pledgeParams = array('id' => $this->_values
['pledge_id']);
782 $returnProperties = array('contact_id', 'status_id');
783 CRM_Core_DAO
::commonRetrieve('CRM_Pledge_DAO_Pledge', $pledgeParams, $pledgeValues, $returnProperties);
786 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
787 $validStatus = array(
788 array_search('Pending', $allStatus),
789 array_search('In Progress', $allStatus),
790 array_search('Overdue', $allStatus),
794 if ($this->_userID
&&
795 $this->_userID
== $pledgeValues['contact_id']
797 //check for authenticated user.
800 elseif ($userChecksum && $pledgeValues['contact_id']) {
801 //check for anonymous user.
802 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($pledgeValues['contact_id'], $userChecksum);
804 //make sure cid is same as pledge contact id
805 if ($validUser && ($pledgeValues['contact_id'] != $contactID)) {
811 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."));
814 //check for valid pledge status.
815 if (!in_array($pledgeValues['status_id'], $validStatus)) {
816 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))));
821 * Cancel recurring contributions.
823 * In case user cancel recurring contribution,
824 * When we get the control back from payment gate way
825 * lets delete the recurring and related contribution.
827 public function cancelRecurring() {
828 $isCancel = CRM_Utils_Request
::retrieve('cancel', 'Boolean', CRM_Core_DAO
::$_nullObject);
830 $isRecur = CRM_Utils_Request
::retrieve('isRecur', 'Boolean', CRM_Core_DAO
::$_nullObject);
831 $recurId = CRM_Utils_Request
::retrieve('recurId', 'Positive', CRM_Core_DAO
::$_nullObject);
832 //clean db for recurring contribution.
833 if ($isRecur && $recurId) {
834 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($recurId);
836 $contribId = CRM_Utils_Request
::retrieve('contribId', 'Positive', CRM_Core_DAO
::$_nullObject);
838 CRM_Contribute_BAO_Contribution
::deleteContribution($contribId);
844 * Build Membership Block in Contribution Pages.
847 * Contact checked for having a current membership for a particular membership.
848 * @param bool $isContributionMainPage
849 * Is this the main page? If so add form input fields.
850 * (or better yet don't have this functionality in a function shared with forms that don't share it).
851 * @param int $selectedMembershipTypeID
852 * Selected membership id.
853 * @param bool $thankPage
855 * @param null $isTest
858 * Is this a separate membership payment
860 protected function buildMembershipBlock(
862 $isContributionMainPage = FALSE,
863 $selectedMembershipTypeID = NULL,
868 $separateMembershipPayment = FALSE;
869 if ($this->_membershipBlock
) {
870 $this->_currentMemberships
= array();
872 $membershipTypeIds = $membershipTypes = $radio = array();
873 $membershipPriceset = (!empty($this->_priceSetId
) && $this->_useForMember
) ?
TRUE : FALSE;
875 $allowAutoRenewMembership = $autoRenewOption = FALSE;
876 $autoRenewMembershipTypeOptions = array();
878 $separateMembershipPayment = CRM_Utils_Array
::value('is_separate_payment', $this->_membershipBlock
);
880 if ($membershipPriceset) {
881 foreach ($this->_priceSet
['fields'] as $pField) {
882 if (empty($pField['options'])) {
885 foreach ($pField['options'] as $opId => $opValues) {
886 if (empty($opValues['membership_type_id'])) {
889 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
893 elseif (!empty($this->_membershipBlock
['membership_types'])) {
894 $membershipTypeIds = explode(',', $this->_membershipBlock
['membership_types']);
897 if (!empty($membershipTypeIds)) {
898 //set status message if wrong membershipType is included in membershipBlock
899 if (isset($this->_mid
) && !$membershipPriceset) {
900 $membershipTypeID = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
904 if (!in_array($membershipTypeID, $membershipTypeIds)) {
905 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');
909 $membershipTypeValues = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, $membershipTypeIds);
910 $this->_membershipTypeValues
= $membershipTypeValues;
912 foreach ($membershipTypeIds as $value) {
913 $memType = $membershipTypeValues[$value];
914 if ($selectedMembershipTypeID != NULL) {
915 if ($memType['id'] == $selectedMembershipTypeID) {
916 $this->assign('minimum_fee',
917 CRM_Utils_Array
::value('minimum_fee', $memType)
919 $this->assign('membership_name', $memType['name']);
920 if (!$thankPage && $cid) {
921 $membership = new CRM_Member_DAO_Membership();
922 $membership->contact_id
= $cid;
923 $membership->membership_type_id
= $memType['id'];
924 if ($membership->find(TRUE)) {
925 $this->assign('renewal_mode', TRUE);
926 $memType['current_membership'] = $membership->end_date
;
927 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
930 $membershipTypes[] = $memType;
933 elseif ($memType['is_active']) {
934 $javascriptMethod = NULL;
935 $allowAutoRenewOpt = 1;
936 if (is_array($this->_paymentProcessors
)) {
937 foreach ($this->_paymentProcessors
as $id => $val) {
938 if (!$val['is_recur']) {
939 $allowAutoRenewOpt = 0;
945 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
946 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array
::value($value, CRM_Utils_Array
::value('auto_renew', $this->_membershipBlock
));;
948 if ($allowAutoRenewOpt) {
949 $allowAutoRenewMembership = TRUE;
952 //add membership type.
953 $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL,
954 $memType['id'], $javascriptMethod
957 $membership = new CRM_Member_DAO_Membership();
958 $membership->contact_id
= $cid;
959 $membership->membership_type_id
= $memType['id'];
961 //show current membership, skip pending and cancelled membership records,
962 //because we take first membership record id for renewal
963 $membership->whereAdd('status_id != 5 AND status_id !=6');
965 if (!is_null($isTest)) {
966 $membership->is_test
= $isTest;
970 $membership->orderBy('end_date DESC');
972 if ($membership->find(TRUE)) {
973 if (!$membership->end_date
) {
974 unset($radio[$memType['id']]);
975 $this->assign('islifetime', TRUE);
978 $this->assign('renewal_mode', TRUE);
979 $this->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
980 $memType['current_membership'] = $membership->end_date
;
982 $endDate = $memType['current_membership'];
983 $this->_defaultMemTypeId
= $memType['id'];
985 if ($memType['current_membership'] < $endDate) {
986 $endDate = $memType['current_membership'];
987 $this->_defaultMemTypeId
= $memType['id'];
991 $membershipTypes[] = $memType;
996 $this->assign('membershipBlock', $this->_membershipBlock
);
997 $this->assign('showRadio', $isContributionMainPage);
998 $this->assign('membershipTypes', $membershipTypes);
999 $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
1000 $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
1001 //give preference to user submitted auto_renew value.
1002 $takeUserSubmittedAutoRenew = (!empty($_POST) ||
$this->isSubmitted()) ?
TRUE : FALSE;
1003 $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
1005 if ($isContributionMainPage) {
1006 if (!$membershipPriceset) {
1007 if (!$this->_membershipBlock
['is_required']) {
1008 $this->assign('showRadioNoThanks', TRUE);
1009 $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
1010 $this->addGroup($radio, 'selectMembership', NULL);
1012 elseif ($this->_membershipBlock
['is_required'] && count($radio) == 1) {
1013 $temp = array_keys($radio);
1014 $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
1015 $this->assign('singleMembership', TRUE);
1016 $this->assign('showRadio', FALSE);
1019 $this->addGroup($radio, 'selectMembership', NULL);
1022 $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
1025 $autoRenewOption = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($this->_priceSetId
);
1026 $this->assign('autoRenewOption', $autoRenewOption);
1029 if (!$this->_values
['is_pay_later'] && is_array($this->_paymentProcessors
) && ($allowAutoRenewMembership ||
$autoRenewOption)) {
1030 $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
1036 return $separateMembershipPayment;