2 +--------------------------------------------------------------------+
3 | Copyright CiviCRM LLC. All rights reserved. |
5 | This work is published under the GNU AGPLv3 license with some |
6 | permitted exceptions and without any warranty. For full license |
7 | and copyright information, see https://civicrm.org/licensing |
8 +--------------------------------------------------------------------+
10 {* this template is used for adding/editing/viewing relationships *}
12 {if $action eq 2 or $action eq 1} {* add and update actions *}
13 <div class="crm-block crm-form-block crm-relationship-form-block">
14 <table class="form-layout-compressed">
15 <tr class="crm-relationship-form-block-relationship_type_id">
16 <td class="label">{$form.relationship_type_id.label}</td>
17 <td>{$form.relationship_type_id.html}</td>
19 <tr class="crm-relationship-form-block-related_contact_id">
20 <td class="label">{$form.related_contact_id.label}</td>
21 <td>{$form.related_contact_id.html}</td>
23 <tr class="crm-relationship-form-block-is_current_employer" style="display:none;">
24 <td class="label">{$form.is_current_employer.label}</td>
25 <td>{$form.is_current_employer.html}</td>
27 <tr class="crm-relationship-form-block-start_date">
28 <td class="label">{$form.start_date.label}</td>
29 <td>{$form.start_date.html} {$form.end_date.label} {$form.end_date.html}<br /><span class="description">{ts}If this relationship has start and/or end dates, specify them here.{/ts}</span></td>
31 <tr class="crm-relationship-form-block-description">
32 <td class="label">{$form.description.label}</td>
33 <td>{$form.description.html}</td>
35 <tr class="crm-relationship-form-block-note">
36 <td class="label">{$form.note.label}</td>
37 <td>{$form.note.html}</td>
39 <tr class="crm-relationship-form-block-is_permission_a_b">
40 {capture assign="contact_b"}{if $action eq 1}{ts}selected contact(s){/ts}{else}{$display_name_b}{/if}{/capture}
41 <td class="label"><label>{ts}Permissions{/ts}</label></td>
43 {ts 1=$display_name_a 2=$contact_b}Permission for <strong>%1</strong> to access information about <strong>%2</strong>{/ts}<br />
44 {$form.is_permission_a_b.html}
47 <tr class="crm-relationship-form-block-is_permission_b_a">
48 <td class="label"> </td>
50 {ts 1=$contact_b|ucfirst 2=$display_name_a}Permission for <strong>%1</strong> to access information about <strong>%2</strong>{/ts}<br />
51 {$form.is_permission_b_a.html}
54 <tr class="crm-relationship-form-block-is_active">
55 <td class="label">{$form.is_active.label}</td>
56 <td>{$form.is_active.html}</td>
59 <div id="customData"></div>
60 <div class="spacer"></div>
63 {if ($action EQ 1) OR ($action EQ 2) }
64 {*include custom data js file *}
65 {include file="CRM/common/customData.tpl"}
66 <script type="text/javascript">
70 $form = $("form.{/literal}{$form.formClass}{literal}"),
71 $relationshipTypeSelect = $('[name=relationship_type_id]', $form),
72 relationshipData = {},
73 contactTypes = {/literal}{$contactTypes|@json_encode}{literal};
76 // Refresh options if relationship types were edited
77 $('body').on('crmOptionsEdited', 'a.crm-option-edit-link', refreshRelationshipData);
78 // Initial load and trigger change on select
79 refreshRelationshipData().done(function() {
80 $relationshipTypeSelect.change();
82 $relationshipTypeSelect.change(function() {
83 var $select = $(this);
85 // ensure we have relationship data before changing anything
86 getRelationshipData().then(function() {
87 updateSelect($select);
93 * Fetch contact types and reset relationship data
95 function refreshRelationshipData() {
97 relationshipData = {};
99 return getRelationshipData();
103 * Fetches the relationship data using latest relationship types
105 function getRelationshipData() {
106 var defer = $.Deferred();
108 if (!$.isEmptyObject(relationshipData)) {
109 defer.resolve(relationshipData);
112 CRM.api3("RelationshipType", "get", {"options": {"limit":0}})
113 .done(function (data) {
114 $.each(data.values, function (key, relType) {
115 // Loop over the suffixes for a relationship type
116 $.each(["a", "b"], function (index, suffix) {
117 var subtype = relType["contact_subtype_" + suffix];
118 var type = subtype || relType["contact_type_" + suffix];
119 var label = getContactTypeLabel(type) || "Contact";
120 label = label.toLowerCase();
121 relType["placeholder_" + suffix] = "- select " + label + " -";
124 relationshipData[relType["id"]] = relType;
127 defer.resolve(relationshipData);
130 return defer.promise();
134 * Gets a contact type label based on a provided name
135 * @param {String} name - the name of the contact type
137 function getContactTypeLabel(name) {
140 $.each(contactTypes, function(index, contactType) {
141 if (contactType.name === name) {
142 label = contactType.label;
150 function updateSelect($select) {
153 $contactField = $('#related_contact_id[type=text]', $form);
154 if (!val && $contactField.length) {
156 .prop('disabled', true)
157 .attr('placeholder', {/literal}'{ts escape='js'}- first select relationship type -{/ts}'{literal})
162 pieces = val.split('_'),
164 source = pieces[1], // a or b
165 target = pieces[2], // b or a
166 contact_type = relationshipData[rType]['contact_type_' + target],
167 contact_sub_type = relationshipData[rType]['contact_sub_type_' + target];
168 // ContactField only exists for ADD action, not update
169 if ($contactField.length) {
170 var api = {params: {}};
172 api.params.contact_type = contact_type;
174 if (contact_sub_type) {
175 api.params.contact_sub_type = contact_sub_type;
179 .prop('disabled', false)
180 .data('api-params', api)
181 .data('user-filter', {})
182 .attr('placeholder', relationshipData[rType]['placeholder_' + target])
186 // Show/hide employer field
187 $('.crm-relationship-form-block-is_current_employer', $form).toggle(rType === {/literal}'{$employmentRelationship}'{literal});
189 CRM.buildCustomData('Relationship', rType);
199 {ts}Are you sure you want to delete this Relationship?{/ts}
202 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>