Merge pull request #3024 from relldoesphp/CRM-14503
[civicrm-core.git] / js / model / crm.uf.js
index 912e91d7a7a1302ee71d0d86796b1a00cea897a2..d94bc677c7c30c95dd0916418962640d344ff9ac 100644 (file)
@@ -1,5 +1,4 @@
-(function($) {
-  var CRM = (window.CRM) ? (window.CRM) : (window.CRM = {});
+(function($, _) {
   if (!CRM.UF) CRM.UF = {};
 
   var YESNO = [
@@ -78,6 +77,7 @@
       case 'Contact':
       case 'Individual':
       case 'Organization':
+      case 'Household':
         return 'contact_1';
       case 'Activity':
         return 'activity_1';
@@ -87,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;
     }
     },
     isInSelectorAllowed: function() {
       var visibility = _.first(_.where(VISIBILITY, {val: this.get('visibility')}));
-      return visibility.isInSelectorAllowed;
+      if (visibility) {
+        return visibility.isInSelectorAllowed;
+      }
+      else {
+        return false;
+      }
     },
     getFieldSchema: function() {
       return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
    */
   CRM.UF.UFGroupModel = CRM.Backbone.Model.extend({
     defaults: {
-      title: ts('Unnamed Form'),
+      title: ts('Unnamed Profile'),
       is_active: 1
     },
     schema: {
       });
       return allMatched;
     },
+    calculateContactEntityType: function() {
+      var ufGroupModel = this;
+
+      // set proper entity model based on selected profile
+      var contactTypes = ['Individual', 'Household', 'Organization'];
+      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 = 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 = [];
       ufGroupModel.getRel('ufFieldCollection').each(function(ufFieldModel){
         if (!ufFieldModel.getFieldSchema()) {
-          CRM.alert(ts('The data model no longer includes field "%1"! All references to the field have been removed.', {
-            1: ufFieldModel.get('entity_name') + "." + ufFieldModel.get('field_name')
+          CRM.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', {
+            1: ufFieldModel.get('label')
           }), '', 'alert', {expires: false});
-          ufFieldModel.destroyLocal();
+          deleteFieldList.push(ufFieldModel);
         }
       });
+
+      _.each(deleteFieldList, function(ufFieldModel) {
+        ufFieldModel.destroyLocal();
+      });
+
       this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
+
+      // reset to redraw the cancel after entity type is updated.
+      ufGroupModel.getRel('ufFieldCollection').reset(ufGroupModel.getRel('ufFieldCollection').toJSON());
     },
     /**
      *
   CRM.UF.UFGroupCollection = CRM.Backbone.Collection.extend({
     model: CRM.UF.UFGroupModel
   });
-})(cj);
+})(CRM.$, CRM._);