Skip to content

Commit

Permalink
CRM-21849: Switch loading relationship data from backend to frontend
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mickadoo committed Apr 16, 2018
1 parent a2f1945 commit 5ede39b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
3 changes: 0 additions & 3 deletions CRM/Contact/Form/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,6 @@ public function buildQuickForm() {
// 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);
Expand Down
95 changes: 92 additions & 3 deletions templates/CRM/Contact/Form/Relationship.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,97 @@
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();
getContactTypes().then(function() {
resetRelationshipData().then(function() {
defer.resolve();
});
});
return defer.promise();
}
/**
* Refreshes the relationship data using latest relationship types
*/
function resetRelationshipData() {
var subtype,
type,
label,
placeholder,
defer = $.Deferred();
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);
});
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
val = $(this).val(),
$contactField = $('#related_contact_id[type=text]', $form);
Expand Down Expand Up @@ -190,7 +279,7 @@
CRM.buildCustomData('Relationship', rType);
}
}).change();
});
});
{/literal}
</script>
Expand Down

0 comments on commit 5ede39b

Please sign in to comment.