From 601cc898b1cc0bec9743a74b319e890db0bed98d Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 16 Apr 2018 13:06:05 +0100 Subject: [PATCH] CRM-21849: Switch loading relationship data from backend to frontend The formatting of relationship data is a FE related task and can be done in Javascript. This also allows reloading the data if some options change after editing relationship types. --- CRM/Contact/Form/Relationship.php | 3 - templates/CRM/Contact/Form/Relationship.tpl | 111 +++++++++++++++++++- 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index 16932158ad..f4d9a14781 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -299,9 +299,6 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { // Select list $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId); - // Metadata needed on clientside - $this->assign('relationshipData', self::getRelationshipTypeMetadata($relationshipList)); - foreach ($this->_allRelationshipNames as $id => $vals) { if ($vals['name_a_b'] === 'Employee of') { $this->assign('employmentRelationship', $id); diff --git a/templates/CRM/Contact/Form/Relationship.tpl b/templates/CRM/Contact/Form/Relationship.tpl index 77a3244014..1c69d1032e 100644 --- a/templates/CRM/Contact/Form/Relationship.tpl +++ b/templates/CRM/Contact/Form/Relationship.tpl @@ -144,10 +144,113 @@ CRM.$(function($) { var $form = $("form.{/literal}{$form.formClass}{literal}"), - relationshipData = {/literal}{$relationshipData|@json_encode}{literal}; - $('[name=relationship_type_id]', $form).change(function() { + $relationshipTypeSelect = $('[name=relationship_type_id]', $form), + relationshipData = {}, + contactTypes = {}; + + $('body').on('crmOptionsEdited', 'a.crm-option-edit-link', refreshRelationshipData); + + // Initial load and trigger change on select + refreshRelationshipData().done(function() { + $relationshipTypeSelect.change(); + }); + + /** + * Fetch contact types and reset relationship data + */ + function refreshRelationshipData() { + var defer = $.Deferred(); + + // reset + relationshipData = {}; + + getContactTypes().then(function() { + getRelationshipData().then(function() { + defer.resolve(); + }); + }); + + return defer.promise(); + } + + /** + * Fetches the relationship data using latest relationship types + */ + function getRelationshipData() { + var subtype, + type, + label, + placeholder, + defer = $.Deferred(); + + if ($.isEmptyObject(relationshipData)) { + CRM.api3("RelationshipType", "get").done(function (data) { + $.each(data.values, function (key, relType) { + $.each(["a", "b"], function (index, suffix) { + subtype = relType["contact_subtype_" + suffix]; + type = subtype || relType["contact_type_" + suffix]; + label = getContactTypeLabel(type) || "Contact"; + placeholder = "- select " + label.toLowerCase() + " -"; + relType["placeholder_" + suffix] = placeholder; + }); + relationshipData[relType["id"]] = relType; + }); + + defer.resolve(relationshipData); + }); + } else { + defer.resolve(relationshipData); + } + + return defer.promise(); + } + + /** + * Gets a contact type label based on a provided name + * @param {String} name - the name of the contact type + */ + function getContactTypeLabel(name) { + var label = ""; + + $.each(contactTypes, function(index, contactType) { + if (contactType.name === name) { + label = contactType.label; + return false; + } + }); + + return label; + } + + /** + * Fetches contact types + */ + function getContactTypes() { + var defer = $.Deferred(); + if ($.isEmptyObject(contactTypes)) { + CRM.api3("ContactType", "get").done(function (data) { + contactTypes = data.values; + defer.resolve(contactTypes); + }); + } else { + defer.resolve(contactTypes); + } + + return defer.promise(); + } + + $relationshipTypeSelect.change(function() { + var $select = $(this); + + // ensure we have relationship data before changing anything + getRelationshipData().then(function() { + updateSelect($select); + }) + }); + + function updateSelect($select) { var - val = $(this).val(), + val = $select.val(), $contactField = $('#related_contact_id[type=text]', $form); if (!val && $contactField.length) { $contactField @@ -190,7 +293,7 @@ CRM.buildCustomData('Relationship', rType); } - }).change(); + } }); {/literal} -- 2.25.1