From 6cc679d2c294822d485fc8b701e9444a79b9f3b2 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Fri, 4 Sep 2015 20:27:03 +0530 Subject: [PATCH] Fix onbehalf processing via snippet --- CRM/Contact/Form/ProfileContact.php | 9 +-- CRM/Contribute/Form/Contribution/Confirm.php | 19 +++---- CRM/Contribute/Form/Contribution/Main.php | 17 +----- CRM/Contribute/Form/Contribution/ThankYou.php | 4 ++ CRM/Contribute/Form/ContributionBase.php | 32 ++--------- templates/CRM/Contact/Form/ProfileContact.tpl | 57 ++++++++++++------- templates/CRM/UF/Form/Block.tpl | 11 +--- 7 files changed, 56 insertions(+), 93 deletions(-) diff --git a/CRM/Contact/Form/ProfileContact.php b/CRM/Contact/Form/ProfileContact.php index a96626e5a7..6bcebffff8 100644 --- a/CRM/Contact/Form/ProfileContact.php +++ b/CRM/Contact/Form/ProfileContact.php @@ -81,8 +81,7 @@ class CRM_Contact_Form_ProfileContact extends CRM_Core_Form { $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); $this->assign('locDataURL', $locDataURL); } - if (count($employer) > 1) { - $this->add('text', 'organization_id', ts('Select an existing related Organization OR enter a new one')); + if (count($employer) > 0) { $this->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $employer)); $orgOptions = array( @@ -92,12 +91,6 @@ class CRM_Contact_Form_ProfileContact extends CRM_Core_Form { $this->addRadio('org_option', ts('options'), $orgOptions); $this->setDefaults(array('org_option' => 0)); } - elseif (count($employer) == 1) { - foreach ($form->_employers as $id => $value) { - $form->assign('orgId', $id); - $form->assign('organizationName', $value['name']); - } - } } $profileFields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, CRM_Core_Action::VIEW, NULL, diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 3d031f0080..68c4dcc6f2 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -188,6 +188,10 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $this->_lineItem = $this->get('lineItem'); $this->_paymentProcessor = $this->get('paymentProcessor'); $this->_params = $this->controller->exportValues('Main'); + + if (!empty($this->_params['onbehalf_values'])) { + $this->_params['onbehalf'] = (array) json_decode($this->_params['onbehalf_values']); + } $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); $this->_params['amount'] = $this->get('amount'); if (isset($this->_params['amount'])) { @@ -230,14 +234,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // if onbehalf-of-organization if (!empty($this->_params['onbehalf_profile_id'])) { // CRM-15182 - if (empty($this->_params['org_option']) && empty($this->_params['organization_id'])) { - if (!empty($this->_params['onbehalfof_id'])) { - $this->_params['organization_id'] = $this->_params['onbehalfof_id']; - } - else { - $this->_params['organization_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_params['onbehalf']['organization_name'], 'id', 'display_name'); - } - } + $this->_params['organization_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_params['onbehalf']['organization_name'], 'id', 'display_name'); $this->_params['organization_name'] = $this->_params['onbehalf']['organization_name']; $addressBlocks = array( @@ -1154,7 +1151,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $isCurrentEmployer = FALSE; $dupeIDs = array(); $orgID = NULL; - if (!empty($behalfOrganization['organization_id']) && empty($behalfOrganization['org_option'])) { + if (!empty($behalfOrganization['organization_id'])) { $orgID = $behalfOrganization['organization_id']; unset($behalfOrganization['organization_id']); $isCurrentEmployer = TRUE; @@ -1971,7 +1968,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // organization params in a separate variable, to make sure // normal behavior is continued. And use that variable to // process on-behalf-of functionality. - if (!empty($this->_params['hidden_onbehalf_profile'])) { + if (!empty($this->_params['onbehalf_profile_id'])) { $behalfOrganization = array(); $orgFields = array('organization_name', 'organization_id', 'org_option'); foreach ($orgFields as $fld) { @@ -2099,7 +2096,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // If onbehalf-of-organization contribution / signup, add organization // and it's location. - if (isset($params['hidden_onbehalf_profile']) && isset($behalfOrganization['organization_name'])) { + if (isset($params['onbehalf_profile_id']) && isset($behalfOrganization['organization_name'])) { $ufFields = array(); foreach ($this->_fields['onbehalf'] as $name => $value) { $ufFields[$name] = 1; diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index ce7a0d5ef9..b6930aa80d 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -41,10 +41,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu */ public $_defaultMemTypeId; - public $_relatedOrganizationFound; - - public $_onBehalfRequired = FALSE; - public $_onbehalf = FALSE; public $_paymentProcessors; public $_membershipTypeValues; @@ -297,9 +293,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } // Build payment processor form - if (empty($_GET['onbehalf'])) { - CRM_Core_Payment_ProcessorForm::buildQuickForm($this); - } + CRM_Core_Payment_ProcessorForm::buildQuickForm($this); $config = CRM_Core_Config::singleton(); @@ -309,11 +303,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID)); } - // Return if we are in an ajax callback - if ($this->_onbehalf && $this->_snippet) { - return; - } - $this->applyFilter('__ALL__', 'trim'); $this->add('text', "email-{$this->_bltID}", ts('Email Address'), @@ -1007,10 +996,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $params['campaign_id'] = $params['contribution_campaign_id']; } - if (!empty($params['onbehalfof_id'])) { - $params['organization_id'] = $params['onbehalfof_id']; - } - $params['currencyID'] = $config->defaultCurrency; if (!empty($params['priceSetId'])) { diff --git a/CRM/Contribute/Form/Contribution/ThankYou.php b/CRM/Contribute/Form/Contribution/ThankYou.php index ff71a7bdaa..0532f482bb 100644 --- a/CRM/Contribute/Form/Contribution/ThankYou.php +++ b/CRM/Contribute/Form/Contribution/ThankYou.php @@ -240,6 +240,10 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont } $this->_submitValues = array_merge($this->_submitValues, $defaults); + + if (!empty($this->_params['onbehalf_values'])) { + $defaults['onbehalf'] = (array) json_decode($this->_params['onbehalf_values']); + } $this->setDefaults($defaults); $values['entity_id'] = $this->_id; diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 6f6e2bd0bd..1d48059ae2 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -787,33 +787,11 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } } - if ($contactID) { - $form->_employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($contactID); - - if (!empty($form->_membershipContactID) && $contactID != $form->_membershipContactID) { - // renewal case - membership being renewed may or may not be for organization - if (!empty($form->_employers) && array_key_exists($form->_membershipContactID, $form->_employers)) { - // if _membershipContactID belongs to employers list, we can say: - $form->_relatedOrganizationFound = TRUE; - } - } - elseif (!empty($form->_employers)) { - // not a renewal case and _employers list is not empty - $form->_relatedOrganizationFound = TRUE; - } - - if ($params['is_for_organization'] != 2) { - $form->assign('relatedOrganizationFound', $form->_relatedOrganizationFound); - } - else { - $form->assign('onBehalfRequired', $form->_onBehalfRequired); - } - - if (count($form->_employers) == 1) { - foreach ($form->_employers as $id => $value) { - $form->_organizationName = $value['name']; - } - } + $form->addElement('hidden', 'onbehalf_profile_id', $onBehalfProfileId); + // TODO: submitted values of on-behalf snippet aren't carried to successive form, + // so this is hackish fix for now to carry the submitted on-behalf profile values in json format + if (!empty($form->_submitValues['onbehalf'])) { + $form->addElement('hidden', 'onbehalf_values', json_encode($form->_submitValues['onbehalf'])); } if (CRM_Utils_Array::value('is_for_organization', $params)) { diff --git a/templates/CRM/Contact/Form/ProfileContact.tpl b/templates/CRM/Contact/Form/ProfileContact.tpl index 9c983a29b3..723e422231 100644 --- a/templates/CRM/Contact/Form/ProfileContact.tpl +++ b/templates/CRM/Contact/Form/ProfileContact.tpl @@ -67,6 +67,35 @@ var orgOption = $("input:radio[name=org_option]:checked").val( ); selectCreateOrg(orgOption, false); + if ($('#is_for_organization').length) { + var type = $('#is_for_organization').is(':checked'); + $('#is_for_organization').on('change', function() { + type = $(this).is(':checked') ? 1 : 0; + buildOnBehalfProfile(type); + }); + if (type) { + buildOnBehalfProfile(type); + } + } + else if (!snippet) { + var type = {/literal}"{$onBehalfRequired}"{literal}; + buildOnBehalfProfile(type); + } + + $("input:radio[name='org_option']").click( function( ) { + var orgOption = $(this).val(); + selectCreateOrg(orgOption, true); + }); + + var onbehalfof_id = $('#onbehalfof_id'); + onbehalfof_id.change(function() { + setLocationDetails($(this).val()); + }).change(); + + if (onbehalfof_id.length) { + setLocationDetails(onbehalfof_id.val()); + } + function resetValues() { // Don't trip chain-select when clearing values $('.crm-chain-select-control', "#select_org div").select2('val', ''); @@ -86,26 +115,6 @@ if (typeof dataUrl != 'undefined') {CRM.loadPage(dataUrl, {target: '#on-behalf-block'})}; } - if ($('#is_for_organization').length) { - $('#is_for_organization').on('change', function() { - var type = $(this).is(':checked') ? 1 : 0; - buildOnBehalfProfile(type); - }); - } - else if (!snippet) { - var type = {/literal}"{$onBehalfRequired}"{literal}; - buildOnBehalfProfile(type); - } - - $("input:radio[name='org_option']").click( function( ) { - var orgOption = $(this).val(); - selectCreateOrg(orgOption, true); - }); - - $('#onbehalfof_id').change(function() { - setLocationDetails($(this).val()); - }).change(); - function selectCreateOrg( orgOption, reset ) { if (orgOption == 0) { $("#onbehalfof_id").show().change(); @@ -114,6 +123,7 @@ else if ( orgOption == 1 ) { $("input#onbehalf_organization_name").show(); $("#onbehalfof_id").hide(); + reset = true; } if ( reset ) { @@ -122,6 +132,8 @@ } function setLocationDetails(contactID , reset) { + resetValues(); + var submittedCID = {/literal}"{$submittedOnBehalf}"{literal}; var submittedOnBehalfInfo = {/literal}'{$submittedOnBehalfInfo}'{literal}; if (submittedOnBehalfInfo) { @@ -135,8 +147,9 @@ function setLocationDetails(contactID , reset) { } } - resetValues(); - var locationUrl = {/literal}"{$locDataURL}"{literal} + contactID + "&ufId=" + {/literal}"{$profileId}"{literal}; + var profileID = {/literal}"{$profileId}"{literal}; + + var locationUrl = {/literal}"{$locDataURL}"{literal} + contactID + "&ufId=" + profileID; $.ajax({ url : locationUrl, dataType : "json", diff --git a/templates/CRM/UF/Form/Block.tpl b/templates/CRM/UF/Form/Block.tpl index 8608391965..804b436807 100644 --- a/templates/CRM/UF/Form/Block.tpl +++ b/templates/CRM/UF/Form/Block.tpl @@ -133,15 +133,8 @@ {/if} {else} {if $prefix} - {if $n eq 'organization_name'} - {if !empty($form.onbehalfof_id)} - {$form.onbehalfof_id.html} - {/if} - {if $organizationName} - - ( {ts}Enter a new organization{/ts} ) - - {/if} + {if $n eq 'organization_name' && !empty($form.onbehalfof_id)} + {$form.onbehalfof_id.html} {/if} {$form.$prefix.$n.html} {else} -- 2.25.1