From 13e4fbdd8df6c355ebb40e1ae63734ff6a5da10f Mon Sep 17 00:00:00 2001
From: monishdeb <monish.deb@webaccess.co.in>
Date: Sun, 19 Jan 2014 01:49:54 +0530
Subject: [PATCH] CRM-13981, handling profile selector for mixed profile types
 in Edit and Copy case

----------------------------------------
* CRM-13981: Migrate "In Honor of" to Soft Credits
  http://issues.civicrm.org/jira/browse/CRM-13981
---
 .../Form/ContributionPage/Settings.php        |  3 +
 CRM/UF/Page/ProfileEditor.php                 |  7 ++
 js/model/crm.uf.js                            |  3 +-
 js/view/crm.designer.js                       | 68 +++++++++++++------
 4 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/CRM/Contribute/Form/ContributionPage/Settings.php b/CRM/Contribute/Form/ContributionPage/Settings.php
index 33e2dbd3a4..876952da24 100644
--- a/CRM/Contribute/Form/ContributionPage/Settings.php
+++ b/CRM/Contribute/Form/ContributionPage/Settings.php
@@ -205,6 +205,9 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
 
     $entities = array(
       array('entity_name' => 'contact_1',
+        'entity_type' => 'ContactModel'
+      ),
+      array('entity_name' => 'individual_1',
         'entity_type' => 'IndividualModel'
       ),
       array('entity_name' => 'organization_1',
diff --git a/CRM/UF/Page/ProfileEditor.php b/CRM/UF/Page/ProfileEditor.php
index f7cb404a94..22184127f6 100644
--- a/CRM/UF/Page/ProfileEditor.php
+++ b/CRM/UF/Page/ProfileEditor.php
@@ -106,6 +106,13 @@ class CRM_UF_Page_ProfileEditor extends CRM_Core_Page {
         //dpm($availableFields);
       }
       switch ($entityType) {
+        case 'ContactModel':
+          $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
+            'Contact',
+            ts('Contact'),
+            $availableFields
+          );
+          break;
         case 'IndividualModel':
           $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
             'Individual',
diff --git a/js/model/crm.uf.js b/js/model/crm.uf.js
index d956c2b79f..1a01ac9560 100644
--- a/js/model/crm.uf.js
+++ b/js/model/crm.uf.js
@@ -76,8 +76,9 @@
   CRM.UF.guessEntityName = function(field_type) {
     switch (field_type) {
       case 'Contact':
-      case 'Individual':
         return 'contact_1';
+      case 'Individual':
+        return 'individual_1';
       case 'Organization':
         return 'organization_1';
       case 'Household':
diff --git a/js/view/crm.designer.js b/js/view/crm.designer.js
index 0098ca9b1e..43abe18831 100644
--- a/js/view/crm.designer.js
+++ b/js/view/crm.designer.js
@@ -323,29 +323,55 @@
       var paletteFieldsByEntitySection = this.model.getRel('paletteFieldCollection').getFieldsByEntitySection();
 
       paletteView.model.getRel('ufEntityCollection').each(function(ufEntityModel){
+	  var ufGroupTypes = [];
+
+	  if (typeof paletteView.model.attributes.group_type == 'undefined'){
+             //Case 'Copy' where we don't get the group_type directly
+             //TODO: extract the group_type from it and split to carry forward the normal functionality
+             //as in case of Edit
+	      CRM.api('UFGroup', 'getsingle' , {'name': paletteView.model.previousAttributes().name}, {
+		  success: function(data) {
+		      ufGroupTypes.push(data.group_type.split(','));
+		  }
+	      });
+	      //console.log(ufGroupTypes['']);
+	  }
+	  else {
+	      ufGroupTypes = paletteView.model.attributes.group_type.split(',');
+	  }
+
+	  var allowedEntityNames = [];
+	  for(key in ufGroupTypes) {
+	      allowedEntityNames[key] = CRM.UF.guessEntityName(ufGroupTypes[key]);
+	  }
         _.each(ufEntityModel.getSections(), function(section, sectionKey){
-          var entitySection = ufEntityModel.get('entity_name') + '-' + sectionKey;
+	    var entitySection = null;
+	    if ($.inArray(ufEntityModel.get('entity_name'), allowedEntityNames) != -1 || !ufGroupTypes) {
+		entitySection = ufEntityModel.get('entity_name') + '-' + sectionKey;
+	    }
           var items = [];
-          if (paletteFieldsByEntitySection[entitySection]) {
-            _.each(paletteFieldsByEntitySection[entitySection], function(paletteFieldModel, k) {
-              items.push({data: paletteFieldModel.getLabel(), attr: {'class': 'crm-designer-palette-field', 'data-plm-cid': paletteFieldModel.cid}});
-            });
-          }
-          if (section.is_addable) {
-            items.push({data: ts('+ Add New Field'), attr: {'class': 'crm-designer-palette-add'}});
-          }
-          if (items.length > 0) {
-            treeData.push({
-              data: section.title,
-              children: items,
-              state: _.contains(paletteView.openTreeNodes, sectionKey) ? 'open' : 'closed',
-              attr: {
-                'class': 'crm-designer-palette-section',
-                'data-section': sectionKey,
-                'data-entity': ufEntityModel.get('entity_name')
-              }
-            });
-          }
+          if (entitySection) {
+            if (paletteFieldsByEntitySection[entitySection]) {
+              _.each(paletteFieldsByEntitySection[entitySection], function(paletteFieldModel, k) {
+                items.push({data: paletteFieldModel.getLabel(), attr: {'class': 'crm-designer-palette-field', 'data-plm-cid': paletteFieldModel.cid}});
+              });
+            }
+            if (section.is_addable) {
+              items.push({data: ts('+ Add New Field'), attr: {'class': 'crm-designer-palette-add'}});
+            }
+            if (items.length > 0) {
+	      treeData.push({
+                data: section.title,
+                children: items,
+                state: _.contains(paletteView.openTreeNodes, sectionKey) ? 'open' : 'closed',
+                attr: {
+                  'class': 'crm-designer-palette-section',
+                  'data-section': sectionKey,
+                  'data-entity': ufEntityModel.get('entity_name')
+		}
+              });
+            }
+	  }
         })
       });
 
-- 
GitLab