Merge pull request #23686 from demeritcowboy/primary-ts
[civicrm-core.git] / templates / CRM / Contact / Form / Relationship.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | Copyright CiviCRM LLC. All rights reserved. |
4 | |
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 +--------------------------------------------------------------------+
9 *}
10 {* this template is used for adding/editing/viewing relationships *}
11
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>
18 </tr>
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>
22 </tr>
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>
26 </tr>
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>
30 </tr>
31 <tr class="crm-relationship-form-block-description">
32 <td class="label">{$form.description.label}</td>
33 <td>{$form.description.html}</td>
34 </tr>
35 <tr class="crm-relationship-form-block-note">
36 <td class="label">{$form.note.label}</td>
37 <td>{$form.note.html}</td>
38 </tr>
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>
42 <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}
45 </td>
46 </tr>
47 <tr class="crm-relationship-form-block-is_permission_b_a">
48 <td class="label"> </td>
49 <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}
52 </td>
53 </tr>
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>
57 </tr>
58 </table>
59 <div id="customData"></div>
60 <div class="spacer"></div>
61 </div>
62 {/if}
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">
67 {literal}
68 CRM.$(function($) {
69 var
70 $form = $("form.{/literal}{$form.formClass}{literal}"),
71 $relationshipTypeSelect = $('[name=relationship_type_id]', $form),
72 relationshipData = {},
73 contactTypes = {/literal}{$contactTypes|@json_encode}{literal};
74
75 (function init () {
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();
81 });
82 $relationshipTypeSelect.change(function() {
83 var $select = $(this);
84
85 // ensure we have relationship data before changing anything
86 getRelationshipData().then(function() {
87 updateSelect($select);
88 })
89 });
90 })();
91
92 /**
93 * Fetch contact types and reset relationship data
94 */
95 function refreshRelationshipData() {
96 // reset
97 relationshipData = {};
98
99 return getRelationshipData();
100 }
101
102 /**
103 * Fetches the relationship data using latest relationship types
104 */
105 function getRelationshipData() {
106 var defer = $.Deferred();
107
108 if (!$.isEmptyObject(relationshipData)) {
109 defer.resolve(relationshipData);
110 }
111
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 + " -";
122 });
123
124 relationshipData[relType["id"]] = relType;
125 });
126
127 defer.resolve(relationshipData);
128 });
129
130 return defer.promise();
131 }
132
133 /**
134 * Gets a contact type label based on a provided name
135 * @param {String} name - the name of the contact type
136 */
137 function getContactTypeLabel(name) {
138 var label = "";
139
140 $.each(contactTypes, function(index, contactType) {
141 if (contactType.name === name) {
142 label = contactType.label;
143 return false;
144 }
145 });
146
147 return label;
148 }
149
150 function updateSelect($select) {
151 var
152 val = $select.val(),
153 $contactField = $('#related_contact_id[type=text]', $form);
154 if (!val && $contactField.length) {
155 $contactField
156 .prop('disabled', true)
157 .attr('placeholder', {/literal}'{ts escape='js'}- first select relationship type -{/ts}'{literal})
158 .change();
159 }
160 else if (val) {
161 var
162 pieces = val.split('_'),
163 rType = pieces[0],
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: {}};
171 if (contact_type) {
172 api.params.contact_type = contact_type;
173 }
174 if (contact_sub_type) {
175 api.params.contact_sub_type = contact_sub_type;
176 }
177 $contactField
178 .val('')
179 .prop('disabled', false)
180 .data('api-params', api)
181 .data('user-filter', {})
182 .attr('placeholder', relationshipData[rType]['placeholder_' + target])
183 .change();
184 }
185
186 // Show/hide employer field
187 $('.crm-relationship-form-block-is_current_employer', $form).toggle(rType === {/literal}'{$employmentRelationship}'{literal});
188
189 CRM.buildCustomData('Relationship', rType);
190 }
191 }
192 });
193 {/literal}
194 </script>
195 {/if}
196
197 {if $action eq 8}
198 <div class="status">
199 {ts}Are you sure you want to delete this Relationship?{/ts}
200 </div>
201 {/if}
202 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>