migrating onBehalf preprocess to profileContact
authormonishdeb <monish.deb@webaccessglobal.com>
Tue, 14 Jul 2015 19:46:25 +0000 (01:16 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Thu, 10 Sep 2015 08:24:30 +0000 (13:54 +0530)
CRM/Contact/Form/ProfileContact.php
CRM/Contribute/BAO/ContributionPage.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php
CRM/Contribute/Form/ContributionPage/Settings.php

index d60b3ff1fe6d11a71a93eeb70b41892ae0088423..69d9e4c553e3cc644823ea0bf723bc15c14ab8a3 100644 (file)
@@ -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);
+      }
     }
   }
 
index 05e3d89b8644ef86f85f4606651bcb44a7df2751..46f7aa088c38e1d878cb290258afd808076f27ac 100644 (file)
@@ -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);
     }
index 0832bd9aa47fd006f5fe563461b8d33121ba78f9..680938a93860f4af9d69fef9b98c6041d8acae9f 100644 (file)
@@ -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));
index cd0102dde4d231276b6915349ac923c3cd7f274f..7be232c3c0853ac4370dcac3b5b96a3c4be0bf1d 100644 (file)
@@ -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) {
index 8007d4d0ed21e8511423862ff477b07b41e2192d..eec20aa160ac576c14b19c859e843500af1fead0 100644 (file)
@@ -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);
       }