From aeb76c8f60afa67e873a062bcdd7bc74836ebef8 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Wed, 15 Jul 2015 01:16:25 +0530 Subject: [PATCH] migrating onBehalf preprocess to profileContact --- CRM/Contact/Form/ProfileContact.php | 142 +++++++++++++++--- CRM/Contribute/BAO/ContributionPage.php | 4 +- CRM/Contribute/Form/Contribution/Main.php | 27 +--- CRM/Contribute/Form/ContributionBase.php | 11 -- .../Form/ContributionPage/Settings.php | 11 +- 5 files changed, 125 insertions(+), 70 deletions(-) diff --git a/CRM/Contact/Form/ProfileContact.php b/CRM/Contact/Form/ProfileContact.php index d60b3ff1fe..69d9e4c553 100644 --- a/CRM/Contact/Form/ProfileContact.php +++ b/CRM/Contact/Form/ProfileContact.php @@ -43,29 +43,129 @@ class CRM_Contact_Form_ProfileContact { $session = CRM_Core_Session::singleton(); $contactID = $session->get('userID'); - $ufJoinParams = array( - 'module' => 'soft_credit', - 'entity_table' => 'civicrm_contribution_page', - 'entity_id' => $form->_id, - ); - $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); - $form->_honoreeProfileId = $profileId[0]; + foreach (array('soft_credit', 'on_behalf') as $module) { + $ufJoinParams = array( + 'module' => $module, + 'entity_table' => 'civicrm_contribution_page', + 'entity_id' => $form->_id, + ); - if (!$form->_honoreeProfileId || - !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_honoreeProfileId, 'is_active') - ) { - 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.')); - } + $ufJoin = new CRM_Core_DAO_UFJoin(); + $ufJoin->copyValues($ufJoinParams); + $ufJoin->find(TRUE); + if (!$ufJoin->is_active) { + continue; + } - $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_honoreeProfileId); - $requiredProfileFields = array( - 'Individual' => array('first_name', 'last_name'), - 'Organization' => array('organization_name', 'email'), - 'Household' => array('household_name', 'email'), - ); - $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_honoreeProfileId, $requiredProfileFields[$profileContactType]); - if (!$validProfile) { - 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.')); + if ($module == 'soft_credit') { + $form->_honoreeProfileId = $ufJoin->uf_group_id; + + if (!$form->_honoreeProfileId || + !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_honoreeProfileId, 'is_active') + ) { + 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.')); + } + + $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_honoreeProfileId); + $requiredProfileFields = array( + 'Individual' => array('first_name', 'last_name'), + 'Organization' => array('organization_name', 'email'), + 'Household' => array('household_name', 'email'), + ); + $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_honoreeProfileId, $requiredProfileFields[$profileContactType]); + if (!$validProfile) { + 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.')); + } + } + else { + $form->_onbehalf = FALSE; + $params = CRM_Contribute_BAO_ContributionPage::formatMultilingualOnBehalfParams($ufJoin->module_data); + if (CRM_Utils_Array::value('is_for_organization', $params)) { + if ($params['is_for_organization'] == 2) { + $form->_onBehalfRequired = TRUE; + } + // Add organization profile if 1 of the following are true: + // If the org profile is required + if ($form->_onBehalfRequired || + // Or we are building the form for the first time + empty($_POST) || + // Or the user has submitted the form and checked the "On Behalf" checkbox + !empty($_POST['is_for_organization']) + ) { + $form->_onbehalf = TRUE; + $form->_profileId = $ufJoin->uf_group_id; + + if (!$form->_profileId || + !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $form->_profileId, 'is_active') + ) { + 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.')); + } + + $requiredProfileFields = array('organization_name', 'email'); + $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($form->_profileId, $requiredProfileFields); + if (!$validProfile) { + CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of an organization and the required fields of the selected onbehalf profile are disabled.')); + } + + $form->assign('profileId', $form->_profileId); + $form->assign('mode', $form->_mode); + + 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 ($form->_relatedOrganizationFound) { + // Related org url - pass checksum if needed + $args = array('cid' => ''); + if (!empty($_GET['cs'])) { + $args = array( + 'uid' => $form->_contactID, + 'cs' => $_GET['cs'], + 'cid' => '', + ); + } + $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); + $form->assign('locDataURL', $locDataURL); + + if (!empty($form->_submitValues['onbehalf'])) { + if (!empty($form->_submitValues['onbehalfof_id'])) { + $form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']); + } + $form->assign('submittedOnBehalfInfo', json_encode($form->_submitValues['onbehalf'])); + } + } + + 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']; + $orgId = $id; + } + $form->assign('orgId', $orgId); + $form->assign('organizationName', $form->_organizationName); + } + } + } + } + $this->assign('onBehalfRequired', $form->_onBehalfRequired); + } } } diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php index 05e3d89b86..46f7aa088c 100644 --- a/CRM/Contribute/BAO/ContributionPage.php +++ b/CRM/Contribute/BAO/ContributionPage.php @@ -859,7 +859,7 @@ LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm if ($setDefault) { $onBehalfJsonDecode = json_decode($params); - $onBehalfJsonDecode = (array) $onBehalfJsonDecode->is_for_organization; + $onBehalfJsonDecode = (array) $onBehalfJsonDecode->on_behalf; if (!$domain->locales && !empty($onBehalfJsonDecode['default'])) { //monolingual state $onBehalfJsonDecode += (array) $onBehalfJsonDecode['default']; @@ -904,7 +904,7 @@ LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm $ufJoinDAO->find(TRUE); $jsonData = json_decode($ufJoinDAO->module_data); if ($jsonData) { - $onBehalfJson['on_behalf'] = array_merge((array) $jsonData->soft_credit, $onBehalfJson['on_behalf']); + $onBehalfJson['on_behalf'] = array_merge((array) $jsonData->on_behalf, $onBehalfJson['on_behalf']); } $onBehalfJson = json_encode($onBehalfJson); } diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 0832bd9aa4..680938a938 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -78,32 +78,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $this->assign('mainDisplay', CRM_Utils_Request::retrieve('_qf_Main_display', 'Boolean', CRM_Core_DAO::$_nullObject)); - // Possible values for 'is_for_organization': - // * 0 - org profile disabled - // * 1 - org profile optional - // * 2 - org profile required - $this->_onbehalf = FALSE; - if (!empty($this->_values['is_for_organization'])) { - if ($this->_values['is_for_organization'] == 2) { - $this->_onBehalfRequired = TRUE; - } - // Add organization profile if 1 of the following are true: - // If the org profile is required - if ($this->_onBehalfRequired || - // Or we are building the form for the first time - empty($_POST) || - // Or the user has submitted the form and checked the "On Behalf" checkbox - !empty($_POST['is_for_organization']) - ) { - $this->_onbehalf = TRUE; - CRM_Contribute_Form_Contribution_OnBehalfOf::preProcess($this); - } - } - $this->assign('onBehalfRequired', $this->_onBehalfRequired); - - if ($this->_honor_block_is_active) { - CRM_Contact_Form_ProfileContact::preprocess($this); - } + CRM_Contact_Form_ProfileContact::preprocess($this); if ($this->_snippet) { $this->assign('isOnBehalfCallback', CRM_Utils_Array::value('onbehalf', $_GET, FALSE)); diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index cd0102dde4..7be232c3c0 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -210,17 +210,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { // this was used prior to the cleverer this_>getContactID - unsure now $this->_userID = CRM_Core_Session::singleton()->get('userID'); - //Check if honor block is enabled for current contribution - $ufJoinParams = array( - 'module' => 'soft_credit', - 'entity_table' => 'civicrm_contribution_page', - 'entity_id' => $this->_id, - ); - $ufJoin = new CRM_Core_DAO_UFJoin(); - $ufJoin->copyValues($ufJoinParams); - $ufJoin->find(TRUE); - $this->_honor_block_is_active = $ufJoin->is_active; - $this->_contactID = $this->_membershipContactID = $this->getContactID(); $this->_mid = NULL; if ($this->_contactID) { diff --git a/CRM/Contribute/Form/ContributionPage/Settings.php b/CRM/Contribute/Form/ContributionPage/Settings.php index 8007d4d0ed..eec20aa160 100644 --- a/CRM/Contribute/Form/ContributionPage/Settings.php +++ b/CRM/Contribute/Form/ContributionPage/Settings.php @@ -308,8 +308,6 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ public function postProcess() { // get the submitted form values. $params = $this->controller->exportValues($this->_name); - CRM_Core_Error::debug( '$params', $params ); - exit; // we do this in case the user has hit the forward/back button if ($this->_id) { @@ -339,11 +337,6 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ $params['honor_block_title'] = NULL; $params['honor_block_text'] = NULL; } - else { - $sctJSON = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($params); - } - - $dao = CRM_Contribute_BAO_ContributionPage::create($params); @@ -368,14 +361,12 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ $ufJoinParam['weight'] = 1; $ufJoinParam['is_active'] = 1; if ($index == 'honor_block_is_active') { - $ufJoinParam['module'] = 'soft_credit'; $ufJoinParam['uf_group_id'] = $params['honoree_profile']; $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($params); } else { - $ufJoinParam['module'] = 'on_behalf'; $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id']; - $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($params); + $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatMultilingualOnBehalfParams($params); } CRM_Core_BAO_UFJoin::create($ufJoinParam); } -- 2.25.1