Fix onbehalf processing via snippet
authormonishdeb <monish.deb@webaccessglobal.com>
Fri, 4 Sep 2015 14:57:03 +0000 (20:27 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Thu, 10 Sep 2015 08:24:30 +0000 (13:54 +0530)
CRM/Contact/Form/ProfileContact.php
CRM/Contribute/Form/Contribution/Confirm.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/Contribution/ThankYou.php
CRM/Contribute/Form/ContributionBase.php
templates/CRM/Contact/Form/ProfileContact.tpl
templates/CRM/UF/Form/Block.tpl

index a96626e5a73fb43cd62024029b03f42587206057..6bcebffff82fee9dce31bf940d76ac2f5f53f6ec 100644 (file)
@@ -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,
index 3d031f008072209db49c22c775ccc6bcbe4c766b..68c4dcc6f229914326f2d1208774f3f88fcda27e 100644 (file)
@@ -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;
index ce7a0d5ef950b6262b08fe2421d6046036606f1f..b6930aa80d0f93a2f9d1c19664ac0cc41cdc9999 100644 (file)
@@ -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'])) {
index ff71a7bdaa36351404885c7720a9e6b35b01e1bb..0532f482bbd8df665b2fcf63866d75144808249e 100644 (file)
@@ -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;
index 6f6e2bd0bd4e3a134fe07c8d5eaaf68bfff208fa..1d48059ae2ec5a5f1ca5c414d7b7f192ddcecb1f 100644 (file)
@@ -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)) {
index 9c983a29b318c6cd969bc167167fd052f3b77bd0..723e422231910d4d29a103e7e8ddc4c7c8f4dd78 100644 (file)
     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', '');
       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();
     else if ( orgOption == 1 ) {
       $("input#onbehalf_organization_name").show();
       $("#onbehalfof_id").hide();
+      reset = true;
     }
 
     if ( reset ) {
   }
 
 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",
index 86083919658c9cd0560c2e17ac0b9a226dad7b15..804b43680721d8a04abbde93984d8ad0b9d3817d 100644 (file)
                 {/if}
               {else}
                 {if $prefix}
-                  {if $n eq 'organization_name'}
-                    {if !empty($form.onbehalfof_id)}
-                      {$form.onbehalfof_id.html}
-                    {/if}
-                    {if $organizationName}
-                      <span>
-                        ( <a id='createNewOrg' href="#" onclick="createNew( ); return false;">{ts}Enter a new organization{/ts}</a> )
-                      </span>
-                    {/if}
+                  {if $n eq 'organization_name' && !empty($form.onbehalfof_id)}
+                    {$form.onbehalfof_id.html}
                   {/if}
                   {$form.$prefix.$n.html}
                {else}