Merge pull request #3024 from relldoesphp/CRM-14503
[civicrm-core.git] / js / model / crm.uf.js
index 2fd7209906c94063a47e986b872e03b98de0a128..d94bc677c7c30c95dd0916418962640d344ff9ac 100644 (file)
@@ -1,5 +1,4 @@
-(function($) {
-  var CRM = (window.CRM) ? (window.CRM) : (window.CRM = {});
+(function($, _) {
   if (!CRM.UF) CRM.UF = {};
 
   var YESNO = [
@@ -88,6 +87,8 @@
         return 'membership_1';
       case 'Participant':
         return 'participant_1';
+      case 'Case':
+        return 'case_1';
       default:
         throw "Cannot guess entity name for field_type=" + field_type;
     }
       });
       return allMatched;
     },
-    getUFGroupModel: function() {
+    calculateContactEntityType: function() {
       var ufGroupModel = this;
 
       // set proper entity model based on selected profile
       var contactTypes = ['Individual', 'Household', 'Organization'];
-      var profileType = ufGroupModel.get('group_type');
+      var profileType = ufGroupModel.get('group_type') || '';
+      if (profileType[0]) {
+        profileType = profileType[0];
+      }
       profileType = profileType.split(',');
+
       var ufEntityModel;
       _.each(profileType, function (ptype) {
         if ($.inArray(ptype, contactTypes) > -1) {
-          ufEntityModel = [
-            {'entity_name': 'contact_1', 'entity_type': ptype + 'Model'}
-          ];
+          ufEntityModel = ptype + 'Model';
           return true;
         }
       });
 
       return ufEntityModel;
     },
+    setUFGroupModel: function(entityType, allEntityModels) {
+      var ufGroupModel = this;
+
+      var newUfEntityModels = [];
+      _.each(allEntityModels, function (values) {
+        if (values.entity_name == 'contact_1') {
+          values.entity_type = entityType;
+        }
+        newUfEntityModels.push(new CRM.UF.UFEntityModel(values));
+      });
+
+      ufGroupModel.getRel('ufEntityCollection').reset(newUfEntityModels);
+    },
     resetEntities: function() {
       var ufGroupModel = this;
       var deleteFieldList = [];
   CRM.UF.UFGroupCollection = CRM.Backbone.Collection.extend({
     model: CRM.UF.UFGroupModel
   });
-})(cj);
+})(CRM.$, CRM._);