From 41fd8e268836965426adc93fd7344306c729df33 Mon Sep 17 00:00:00 2001 From: kurund Date: Fri, 24 Jan 2014 00:44:01 -0800 Subject: [PATCH] add filter based on entity types for profile builder --- CRM/Campaign/Form/Survey/Questions.php | 2 +- .../Form/ContributionPage/Custom.php | 2 +- .../Form/ContributionPage/Settings.php | 15 ++++---- js/jquery/jquery.crmProfileSelector.js | 4 +-- js/model/crm.uf.js | 19 ++++++---- js/view/crm.designer.js | 36 +++++++++---------- templates/CRM/UF/Page/ProfileTemplates.tpl | 8 ++++- 7 files changed, 49 insertions(+), 37 deletions(-) diff --git a/CRM/Campaign/Form/Survey/Questions.php b/CRM/Campaign/Form/Survey/Questions.php index 5f83230d02..a9ec2f1325 100644 --- a/CRM/Campaign/Form/Survey/Questions.php +++ b/CRM/Campaign/Form/Survey/Questions.php @@ -94,7 +94,7 @@ class CRM_Campaign_Form_Survey_Questions extends CRM_Campaign_Form_Survey { 'ActivityType' => array($subTypeId), ); $entities = array( - array('entity_name' => 'individual_1', 'entity_type' => 'IndividualModel'), + array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'), array('entity_name' => 'activity_1', 'entity_type' => 'ActivityModel', 'entity_sub_type' => $subTypeId), ); $this->addProfileSelector('contact_profile_id', ts('Contact Info'), $allowCoreTypes, $allowSubTypes, $entities); diff --git a/CRM/Contribute/Form/ContributionPage/Custom.php b/CRM/Contribute/Form/ContributionPage/Custom.php index ee6fbaa891..2b402fe7e6 100644 --- a/CRM/Contribute/Form/ContributionPage/Custom.php +++ b/CRM/Contribute/Form/ContributionPage/Custom.php @@ -48,7 +48,7 @@ class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_Co // Register 'individual_1' model $entities = array(); - $entities[] = array('entity_name' => 'individual_1', 'entity_type' => 'IndividualModel'); + $entities[] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'); $allowCoreTypes = array_merge(array('Contact', 'Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual')); $allowSubTypes = array(); diff --git a/CRM/Contribute/Form/ContributionPage/Settings.php b/CRM/Contribute/Form/ContributionPage/Settings.php index d30646557f..5addc70229 100644 --- a/CRM/Contribute/Form/ContributionPage/Settings.php +++ b/CRM/Contribute/Form/ContributionPage/Settings.php @@ -222,20 +222,19 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ ); $entities = array( - array('entity_name' => 'individual_1', - 'entity_type' => 'IndividualModel' - ), - array('entity_name' => 'organization_1', - 'entity_type' => 'OrganizationModel' - ), - array('entity_name' => 'household_1', - 'entity_type' => 'HouseholdModel' + array( + 'entity_name' => 'contact_1', + 'entity_type' => 'IndividualModel', ), ); + $allowCoreTypes = array_merge(array('Contact', 'Individual', 'Organization', 'Household'), CRM_Contact_BAO_ContactType::subTypes('Individual')); $allowSubTypes = array(); $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities); + CRM_UF_Page_ProfileEditor::registerProfileScripts(); + CRM_UF_Page_ProfileEditor::registerSchemas(array('OrganizationModel','HouseholdModel')); + if (!empty($this->_submitValues['honor_block_is_active'])) { $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required'); diff --git a/js/jquery/jquery.crmProfileSelector.js b/js/jquery/jquery.crmProfileSelector.js index 62a0ec7f1c..3c09e7f460 100644 --- a/js/jquery/jquery.crmProfileSelector.js +++ b/js/jquery/jquery.crmProfileSelector.js @@ -14,11 +14,11 @@ * ... * cj('.crm-profile-selector').crmProfileSelector({ * groupTypeFilter: "Contact,Individual,Activity;;ActivityType:7", - * entities: "individual_1:IndividualModel,activity_1:ActivityModel" + * entities: "contact_1:IndividualModel,activity_1:ActivityModel" * }); * * Note: The system does not currently support dynamic entities -- it only supports - * a couple of entities named "individual_1" and "activity_1". See also + * a couple of entities named "contact_1" and "activity_1". See also * CRM.UF.guessEntityName(). */ $.fn.crmProfileSelector = function(options) { diff --git a/js/model/crm.uf.js b/js/model/crm.uf.js index d88578f627..79d43a7a6d 100644 --- a/js/model/crm.uf.js +++ b/js/model/crm.uf.js @@ -77,11 +77,9 @@ switch (field_type) { case 'Contact': case 'Individual': - return 'individual_1'; case 'Organization': - return 'organization_1'; case 'Household': - return 'household_1'; + return 'contact_1'; case 'Activity': return 'activity_1'; case 'Contribution': @@ -686,15 +684,24 @@ }, 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()); }, /** * diff --git a/js/view/crm.designer.js b/js/view/crm.designer.js index fff8dacbd9..dccef6abda 100644 --- a/js/view/crm.designer.js +++ b/js/view/crm.designer.js @@ -326,20 +326,8 @@ paletteView.model.getRel('ufEntityCollection').each(function(ufEntityModel){ _.each(ufEntityModel.getSections(), function(section, sectionKey){ - // build filter select - if (sectionKey == 'default') { - paletteView.$('.crm-contact-types').append(''); - } - - if (!paletteView.selectedContactType) { - paletteView.selectedContactType = paletteView.$('.crm-contact-types option:first').val(); - } // set selected option as default, since we are rebuilding palette - paletteView.$('.crm-contact-types').val(paletteView.selectedContactType).prop('selected','selected'); - - if (paletteView.selectedContactType != ufEntityModel.get('entity_name')) { - return true; - } + paletteView.$('.crm-contact-types').val(ufEntityModel.attributes.entity_type).prop('selected','selected'); var entitySection = ufEntityModel.get('entity_name') + '-' + sectionKey; var items = []; @@ -415,8 +403,16 @@ $('.crm-designer-palette-tree').jstree("search", $(event.target).val()); }, doSetPaletteEntity: function(event) { - this.selectedContactType = $('.crm-contact-types :selected').val(); - this.render(); + // loop through entity collection and remove non-valid entity section's + var newUfEntityModels = []; + this.model.getRel('ufEntityCollection').each(function(oldUfEntityModel){ + var values = oldUfEntityModel.toJSON(); + if (values.entity_name == 'contact_1') { + values.entity_type = $('.crm-contact-types :selected').val(); + } + newUfEntityModels.push(new CRM.UF.UFEntityModel(values)); + }); + this.model.getRel('ufEntityCollection').reset(newUfEntityModels); }, doAddToCanvas: function(event) { var paletteFieldModel = this.model.getRel('paletteFieldCollection').get($(event.currentTarget).attr('data-plm-cid')); @@ -474,7 +470,9 @@ var paletteFieldCollection = this.model.getRel('paletteFieldCollection'); var paletteFieldModel = paletteFieldCollection.getFieldByName(ufFieldModel.get('entity_name'), ufFieldModel.get('field_name')); var isAddable = ufFieldCollection.isAddable(ufFieldModel); - this.$('[data-plm-cid='+paletteFieldModel.cid+']').toggleClass('disabled', !isAddable); + if (paletteFieldModel) { + this.$('[data-plm-cid='+paletteFieldModel.cid+']').toggleClass('disabled', !isAddable); + } }, toggleAll: function(event) { if ($('.crm-designer-palette-search input').val() == '') { @@ -502,13 +500,15 @@ this.model.getRel('ufFieldCollection') .on('add', this.updatePlaceholder, this) .on('remove', this.updatePlaceholder, this) - .on('add', this.addUFFieldView, this); + .on('add', this.addUFFieldView, this) + .on('reset', this.render, this); }, onClose: function() { this.model.getRel('ufFieldCollection') .off('add', this.updatePlaceholder, this) .off('remove', this.updatePlaceholder, this) - .off('add', this.addUFFieldView, this); + .off('add', this.addUFFieldView, this) + .off('reset', this.render, this); }, render: function() { var ufFieldCanvasView = this; diff --git a/templates/CRM/UF/Page/ProfileTemplates.tpl b/templates/CRM/UF/Page/ProfileTemplates.tpl index 0456c61482..f3ecc0f216 100644 --- a/templates/CRM/UF/Page/ProfileTemplates.tpl +++ b/templates/CRM/UF/Page/ProfileTemplates.tpl @@ -26,7 +26,13 @@