Merge pull request #18768 from eileenmcnaughton/ids
[civicrm-core.git] / templates / CRM / Custom / Form / Field.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 <div class="crm-block crm-form-block crm-custom-field-form-block">
11 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
12 <table class="form-layout">
13 <tr class="crm-custom-field-form-block-label">
14 <td class="label">{$form.label.label}
15 {if $action == 2}
16 {include file='CRM/Core/I18n/Dialog.tpl' table='civicrm_custom_field' field='label' id=$id}
17 {/if}
18 </td>
19 <td class="html-adjust">{$form.label.html}</td>
20 </tr>
21 <tr class="crm-custom-field-form-block-data_type">
22 <td class="label">{$form.data_type.label}</td>
23 <td class="html-adjust">{$form.data_type.html}</td>
24 </tr>
25 <tr class="crm-custom-field-form-block-html_type">
26 <td class="label">{$form.html_type.label}</td>
27 <td class="html-adjust">{$form.html_type.html}</td>
28 </tr>
29 <tr class="crm-custom-field-form-block-serialize">
30 <td class="label">{$form.serialize.label}</td>
31 <td class="html-adjust">{$form.serialize.html}</td>
32 </tr>
33 {if $form.in_selector}
34 <tr class='crm-custom-field-form-block-in_selector'>
35 <td class='label'>{$form.in_selector.label}</td>
36 <td class='html-adjust'>{$form.in_selector.html} {help id="id-in_selector"}</td>
37 </tr>
38 {/if}
39 <tr class="crm-custom-field-form-block-text_length" id="textLength" {if !( $action eq 1 || $action eq 2 ) && ($form.data_type.value.0.0 != 0)}class="hiddenElement"{/if}>
40 <td class="label">{$form.text_length.label}</td>
41 <td class="html-adjust">{$form.text_length.html}</td>
42 </tr>
43
44 <tr id='showoption' {if $action eq 1 or $action eq 2 }class="hiddenElement"{/if}>
45 <td colspan="2">
46 <table class="form-layout-compressed">
47 {* Conditionally show table for setting up selection options - for field types = radio, checkbox or select *}
48 {include file="CRM/Custom/Form/Optionfields.tpl"}
49 </table>
50 </td>
51 </tr>
52 <tr id='contact_reference_group'>
53 <td class="label">{$form.group_id.label}</td>
54 <td class="html-adjust">
55 {$form.group_id.html}
56 &nbsp;&nbsp;<span><a class="crm-hover-button toggle-contact-ref-mode" href="#Advance">{ts}Advanced Filter{/ts}</a></span>
57 {capture assign=searchPreferences}{crmURL p="civicrm/admin/setting/search" q="reset=1"}{/capture}
58 <div class="messages status no-popup"><i class="crm-i fa-exclamation-triangle" aria-hidden="true"></i> {ts 1=$searchPreferences}If you are planning on using this field in front-end profile, event registration or contribution forms, you should 'Limit List to Group' or configure an 'Advanced Filter' (so that you do not unintentionally expose your entire set of contacts). Users must have either 'access contact reference fields' OR 'access CiviCRM' permission in order to use contact reference autocomplete fields. You can assign 'access contact reference fields' to the anonymous role if you want un-authenticated visitors to use this field. Use <a href='%1'>Search Preferences - Contact Reference Options</a> to control the fields included in the search results.{/ts}
59 </td>
60 </tr>
61 <tr id='field_advance_filter'>
62 <td class="label">{$form.filter.label}</td>
63 <td class="html-adjust">
64 {$form.filter.html}
65 &nbsp;&nbsp;<span><a class="crm-hover-button toggle-contact-ref-mode" href="#Group">{ts}Filter by Group{/ts}</a></span>
66 <br />
67 <span class="description">{ts}Filter contact search results for this field using Contact get API parameters. EXAMPLE: To list Students in group 3:{/ts} "action=get&group=3&contact_sub_type=Student" {docURL page="dev/api"}</span>
68 </td>
69 </tr>
70 <tr class="crm-custom-field-form-block-options_per_line" id="optionsPerLine" {if $action neq 2 && ($form.data_type.value.0.0 >= 4 && $form.data_type.value.1.0 neq 'CheckBox' || $form.data_type.value.1.0 neq 'Radio' )}class="hiddenElement"{/if}>
71 <td class="label">{$form.options_per_line.label}</td>
72 <td class="html-adjust">{$form.options_per_line.html|crmAddClass:two}</td>
73 </tr>
74 <tr class="crm-custom-field-form-block-start_date_years" id="startDateRange" {if $action neq 2 && ($form.data_type.value.0.0 != 5)}class="hiddenElement"{/if}>
75 <td class="label">{$form.start_date_years.label}</td>
76 <td class="html-adjust">{$form.start_date_years.html} {ts}years prior to current date.{/ts}</td>
77 </tr>
78 <tr class="crm-custom-field-form-block-end_date_years" id="endDateRange" {if $action neq 2 && ($form.data_type.value.0.0 != 5)}class="hiddenElement"{/if}>
79 <td class="label">{$form.end_date_years.label}</td>
80 <td class="html-adjust">{$form.end_date_years.html} {ts}years after the current date.{/ts}</td>
81 </tr>
82 <tr class="crm-custom-field-form-block-date_format" id="includedDatePart" {if $action neq 2 && ($form.data_type.value.0.0 != 5)}class="hiddenElement"{/if}>
83 <td class="label">{$form.date_format.label}</td>
84 <td class="html-adjust">{$form.date_format.html}&nbsp;&nbsp;&nbsp;{$form.time_format.label}&nbsp;&nbsp;{$form.time_format.html}</td>
85 </tr>
86 <tr class="crm-custom-field-form-block-note_rows" id="noteRows" {if $action neq 2 && ($form.data_type.value.0.0 != 4)}class="hiddenElement"{/if}>
87 <td class="label">{$form.note_rows.label}</td>
88 <td class="html-adjust">{$form.note_rows.html}</td>
89 </tr>
90 <tr class="crm-custom-field-form-block-note_columns" id="noteColumns" {if $action neq 2 && ($form.data_type.value.0.0 != 4)}class="hiddenElement"{/if}>
91 <td class="label">{$form.note_columns.label}</td>
92 <td class="html-adjust">{$form.note_columns.html}</td>
93 </tr>
94 <tr class="crm-custom-field-form-block-note_length" id="noteLength" {if $action neq 2 && ($form.data_type.value.0.0 != 4)}class="hiddenElement"{/if}>
95 <td class="label">{$form.note_length.label}</td>
96 <td class="html-adjust">{$form.note_length.html} <span class="description">{ts}Leave blank for unlimited. This limit is not implemented by all browsers and rich text editors.{/ts}</span></td>
97 </tr>
98 <tr class="crm-custom-field-form-block-weight" >
99 <td class="label">{$form.weight.label}</td>
100 <td>{$form.weight.html|crmAddClass:two}
101 {if $action neq 4}
102 <span class="description">{ts}Weight controls the order in which fields are displayed in a group. Enter a positive or negative integer - lower numbers are displayed ahead of higher numbers.{/ts}</span>
103 {/if}
104 </td>
105 </tr>
106 <tr class="crm-custom-field-form-block-default_value" id="hideDefault" {if $action eq 2 && ($form.data_type.value.0.0 < 4 && $form.data_type.value.1.0 NEQ 'Text')}class="hiddenElement"{/if}>
107 <td title="hideDefaultValTxt" class="label">{$form.default_value.label}</td>
108 <td title="hideDefaultValDef" class="html-adjust">{$form.default_value.html}</td>
109 </tr>
110 <tr class="crm-custom-field-form-block-description" id="hideDesc" {if $action neq 4 && $action eq 2 && ($form.data_type.value.0.0 < 4 && $form.data_type.value.1.0 NEQ 'Text')}class="hiddenElement"{/if}>
111 <td title="hideDescTxt" class="label">&nbsp;</td>
112 <td title="hideDescDef" class="html-adjust"><span class="description">{ts}If you want to provide a default value for this field, enter it here. For date fields, format is YYYY-MM-DD.{/ts}</span></td>
113 </tr>
114 <tr class="crm-custom-field-form-block-help_pre">
115 <td class="label">{$form.help_pre.label} {if $action == 2}{include file='CRM/Core/I18n/Dialog.tpl' table='civicrm_custom_field' field='help_pre' id=$id}{/if}</td>
116 <td class="html-adjust">{$form.help_pre.html|crmAddClass:huge}</td>
117 </tr>
118 <tr class="crm-custom-field-form-block-help_post">
119 <td class="label">{$form.help_post.label} {if $action == 2}{include file='CRM/Core/I18n/Dialog.tpl' table='civicrm_custom_field' field='help_post' id=$id}{/if}</td>
120 <td class="html-adjust">{$form.help_post.html|crmAddClass:huge}
121 {if $action neq 4}
122 <span class="description">{ts}Explanatory text displayed on back-end forms. Pre help is displayed inline on the form (above the field). Post help is displayed in a pop-up - users click the help balloon to view help text.{/ts}</span>
123 {/if}
124 </td>
125 </tr>
126 <tr class="crm-custom-field-form-block-is_required">
127 <td class="label">{$form.is_required.label}</td>
128 <td class="html-adjust">{$form.is_required.html}
129 {if $action neq 4}
130 <br /><span class="description">{ts}Do not make custom fields required unless you want to force all users to enter a value anytime they add or edit this type of record. You can always make the field required when used in a specific Profile form.{/ts}</span>
131 {/if}
132 </td>
133 </tr>
134 <tr id ="searchable" class="crm-custom-field-form-block-is_searchable">
135 <td class="label">{$form.is_searchable.label}</td>
136 <td class="html-adjust">{$form.is_searchable.html}
137 {if $action neq 4}
138 <br /><span class="description">{ts}Can you search on this field in the Advanced and component search forms? Also determines whether you can include this field as a display column and / or filter in related detail reports.{/ts}</span>
139 {/if}
140 </td>
141 </tr>
142 <tr id="searchByRange" class="crm-custom-field-form-block-is_search_range">
143 <td class="label">{$form.is_search_range.label}</td>
144 <td class="html-adjust">{$form.is_search_range.html}</td>
145 </tr>
146 <tr class="crm-custom-field-form-block-is_active">
147 <td class="label">{$form.is_active.label}</td>
148 <td class="html-adjust">{$form.is_active.html}</td>
149 </tr>
150 <tr class="crm-custom-field-form-block-is_view">
151 <td class="label">{$form.is_view.label}</td>
152 <td class="html-adjust">{$form.is_view.html}
153 <span class="description">{ts}Is this field set by PHP code (via a custom hook). This field will not be updated by CiviCRM.{/ts}</span>
154 </td>
155 </tr>
156 </table>
157 {if $action ne 4}
158 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
159 {else}
160 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
161 {/if}
162 </div>
163 {literal}
164 <script type="text/javascript">
165 CRM.$(function($) {
166 var _ = CRM._,
167 $form = $('form.{/literal}{$form.formClass}{literal}'),
168 dataToHTML = {/literal}{$dataToHTML|@json_encode}{literal},
169 originalHtmlType = '{/literal}{$originalHtmlType}{literal}',
170 existingMultiValueCount = {/literal}{if empty($existingMultiValueCount)}null{else}{$existingMultiValueCount}{/if}{literal},
171 originalSerialize = {/literal}{if empty($originalSerialize)}false{else}true{/if}{literal},
172 htmlTypes = CRM.utils.getOptions($('#html_type', $form));
173
174 function onChangeDataType() {
175 var dataType = $(this).val(),
176 allowedHtmlTypes = _.filter(htmlTypes, function(type) {
177 return _.includes(dataToHTML[dataType], type.key);
178 });
179 CRM.utils.setOptions($('#html_type', $form), allowedHtmlTypes);
180 if (!$('#html_type', $form).val()) {
181 $('#html_type', $form).val(dataToHTML[dataType][0]).change();
182 }
183 customOptionHtmlType(dataType);
184 makeDefaultValueField(dataType);
185 }
186
187 function onChangeHtmlType() {
188 var htmlType = $(this).val(),
189 dataType = $('#data_type', $form).val();
190
191 if (htmlType === 'CheckBox' || htmlType === 'Radio') {
192 $('#serialize', $form).prop('checked', htmlType === 'CheckBox');
193 }
194
195 showSearchRange(dataType);
196 customOptionHtmlType(dataType);
197 }
198
199 $('#data_type', $form).each(onChangeDataType).change(onChangeDataType);
200 $('#html_type', $form).each(onChangeHtmlType).change(onChangeHtmlType);
201
202 function showSearchRange(dataType) {
203 if (_.includes(['Date', 'Int', 'Float', 'Money'], dataType)) {
204 $("#searchByRange", $form).toggle($('#is_searchable', $form).is(':checked'));
205 } else {
206 $("#searchByRange", $form).hide();
207 }
208 }
209
210 function toggleContactRefFilter(e) {
211 var setSelected = $(this).attr('href');
212 if (!setSelected) {
213 setSelected = $('#filter_selected').val();
214 } else {
215 $('#filter_selected').val(setSelected.slice(1));
216 }
217 if (setSelected == '#Advance') {
218 $('#contact_reference_group').hide( );
219 $('#field_advance_filter').show( );
220 } else {
221 $('#field_advance_filter').hide( );
222 $('#contact_reference_group').show( );
223 }
224 e && e.preventDefault && e.preventDefault();
225 }
226 $('.toggle-contact-ref-mode', $form).click(toggleContactRefFilter);
227
228 function customOptionHtmlType(dataType) {
229 var htmlType = $("#html_type", $form).val(),
230 serialize = $("#serialize", $form).is(':checked');
231
232 if (!htmlType) {
233 return;
234 }
235
236 if (dataType === 'ContactReference') {
237 toggleContactRefFilter();
238 } else {
239 $('#field_advance_filter, #contact_reference_group', $form).hide();
240 }
241
242 if (_.includes(['String', 'Int', 'Float', 'Money'], dataType)) {
243 if (htmlType !== "Text") {
244 $("#showoption, #searchable", $form).show();
245 $("#hideDefault, #hideDesc, #searchByRange", $form).hide();
246 } else {
247 $("#showoption").hide();
248 $("#hideDefault, #hideDesc, #searchable", $form).show();
249 }
250 } else {
251 if (dataType === 'File') {
252 $("#default_value", $form).val('');
253 $("#hideDefault, #searchable, #hideDesc", $form).hide();
254 } else if (dataType === 'ContactReference') {
255 $("#hideDefault").hide();
256 } else {
257 $("#hideDefault, #searchable, #hideDesc", $form).show();
258 }
259 $("#showoption").hide();
260 }
261
262 if (_.includes(['String', 'Int', 'Float', 'Money'], dataType) && htmlType !== 'Text') {
263 if (serialize) {
264 $('div[id^=checkbox]', '#optionField').show();
265 $('div[id^=radio]', '#optionField').hide();
266 } else {
267 $('div[id^=radio]', '#optionField').show();
268 $('div[id^=checkbox]', '#optionField').hide();
269 }
270 }
271
272 $("#optionsPerLine", $form).toggle((htmlType === "CheckBox" || htmlType === "Radio") && dataType !== 'Boolean');
273
274 $("#startDateRange, #endDateRange, #includedDatePart", $form).toggle(dataType === 'Date');
275
276 $("#textLength", $form).toggle(dataType === 'String');
277
278 $("#noteColumns, #noteRows, #noteLength", $form).toggle(dataType === 'Memo');
279
280 $(".crm-custom-field-form-block-serialize", $form).toggle((htmlType === 'Select' || htmlType === 'Autocomplete-Select') && dataType !== 'ContactReference');
281 }
282
283 function makeDefaultValueField(dataType) {
284 var field = $('#default_value', $form);
285 field.crmDatepicker('destroy');
286 field.crmSelect2('destroy');
287 switch (dataType) {
288 case 'Date':
289 field.crmDatepicker({date: 'yy-mm-dd', time: false});
290 break;
291
292 case 'Boolean':
293 field.crmSelect2({data: [{id: '1', text: ts('Yes')}, {id: '0', text: ts('No')}], placeholder: ' '});
294 break;
295
296 case 'Country':
297 field.crmEntityRef({entity: 'Country'});
298 break;
299
300 case 'StateProvince':
301 field.crmEntityRef({entity: 'StateProvince', api: {description_field: ['country_id.name']}});
302 break;
303 }
304 }
305
306 $('#is_searchable, #serialize', $form).change(onChangeHtmlType);
307
308 $form.submit(function() {
309 var htmlType = $('#html_type', $form).val(),
310 serialize = $("#serialize", $form).is(':checked'),
311 htmlTypeLabel = (serialize && _.includes(['Select', 'Autocomplete-Select'], htmlType)) ? ts('Multi-Select') : _.find(htmlTypes, {key: htmlType}).value;
312 if (originalHtmlType && (originalHtmlType !== htmlType || originalSerialize !== serialize)) {
313 var origHtmlTypeLabel = (originalSerialize && originalHtmlType === 'Select') ? ts('Multi-Select') : _.find(htmlTypes, {key: originalHtmlType}).value;
314 if (originalSerialize && !serialize && existingMultiValueCount) {
315 return confirm(ts('WARNING: Changing this multivalued field to singular will result in the loss of data!')
316 + "\n" + ts('%1 existing records contain multiple values - the data in each of these fields will be truncated to a single value.', {1: existingMultiValueCount})
317 )
318 } else {
319 return confirm(ts('Change this field from %1 to %2? Existing data will be preserved.', {1: origHtmlTypeLabel, 2: htmlTypeLabel}));
320 }
321 }
322 });
323 });
324 </script>
325 {/literal}
326 {* Give link to view/edit option group *}
327 {if $action eq 2 && !empty($hasOptionGroup) }
328 <div class="action-link">
329 {crmButton p="civicrm/admin/custom/group/field/option" q="reset=1&action=browse&fid=`$id`&gid=`$gid`" icon="pencil"}{ts}View / Edit Multiple Choice Options{/ts}{/crmButton}
330 </div>
331 {/if}