Merge pull request #16584 from eileenmcnaughton/role
[civicrm-core.git] / templates / CRM / common / batchCopy.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 {literal}
11 <script type="text/javascript">
12 CRM.$(function($) {
13 /**
14 * This function use to copy fields
15 *
16 * @param fname string field name
17 * @return void
18 */
19 function copyFieldValues( fname ) {
20 // this is the most common pattern for elements, so first check if it exits
21 // this check field starting with "field[" and contains [fname] and is not
22 // hidden ( for checkbox hidden element is created )
23 var elementId = $('.crm-copy-fields [name^="field["][name*="[' + fname +']"][type!=hidden]');
24
25 // get the first element and it's value
26 var firstElement = elementId.eq(0);
27 var firstElementValue = firstElement.val();
28
29 //check if it is date element
30 var isDateElement = elementId.attr('format');
31
32 //get the element type
33 var elementType = elementId.attr('type');
34
35 // set the value for all the elements, elements needs to be handled are
36 // select, checkbox, radio, date fields, text, textarea, multi-select
37 // wysiwyg editor, advanced multi-select ( to do )
38 if ( elementType == 'radio' ) {
39 firstElementValue = elementId.filter(':checked').eq(0).val();
40 elementId.filter("[value='" + firstElementValue + "']").prop("checked",true).change();
41 }
42 else if ( elementType == 'checkbox' ) {
43 // handle checkbox
44 // get the entity id of first element
45 var firstEntityId = $('.crm-copy-fields > tbody > tr');
46
47 if ( firstEntityId.length == 0 ) {
48 firstEntityId = firstElement.closest('div.crm-grid-row');
49 }
50
51 firstEntityId = firstEntityId.attr('entity_id');
52
53 var firstCheckElement = $('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]');
54
55 if ( firstCheckElement.length > 1 ) {
56 // lets uncheck all the checkbox except first one
57 $('.crm-copy-fields [type=checkbox][name^="field["][name*="[' + fname +']"][type=checkbox]:not([name^="field['+ firstEntityId +']['+ fname +']["])').prop('checked', false);
58
59 //here for each checkbox for first row, check if it is checked and set remaining checkboxes
60 firstCheckElement.each(function() {
61 if ($(this).prop('checked') ) {
62 var elementName = $(this).attr('name');
63 var correctIndex = elementName.split('field['+ firstEntityId +']['+ fname +'][');
64 correctIndexValue = correctIndex[1].replace(']', '');
65 $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']['+ correctIndexValue+']"][type!=hidden]').prop('checked',true).change();
66 }
67 });
68 }
69 else {
70 if ( firstCheckElement.prop('checked') ) {
71 $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked',true).change();
72 }
73 else {
74 $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked', false).change();
75 }
76 }
77 }
78 else if (elementId.is('textarea')) {
79 var text = CRM.wysiwyg.getVal(firstElement);
80 elementId.each(function() {
81 CRM.wysiwyg.setVal(this, text);
82 });
83 }
84 else {
85 if (elementId.is('select') === true && firstElement.parent().find(':input').select().index() >= 1 && firstElement.parent().find('select').select().length > 1) {
86 // its a multiselect case
87 firstElement.parent().find(':input').select().each( function(count) {
88 var firstElementValue = $(this).val();
89 var elementId = $('.crm-copy-fields [name^="field["][name*="[' + fname +'][' + count + '"][type!=hidden]');
90 elementId.val(firstElementValue).not(":first").change();
91 });
92 }
93 else {
94 elementId.val(firstElementValue).change();
95 }
96 }
97
98 // since we use different display field for date we also need to set it.
99 // also check for date time field and set the value correctly
100 if ( isDateElement ) {
101 copyValuesDate( fname );
102 }
103 }
104
105 /**
106 * Special function to handle setting values for date fields
107 *
108 * @param fname string field name
109 * @return void
110 */
111 function copyValuesDate(fname) {
112 var displayElement = $('.crm-copy-fields [name^="field_"][name*="_' + fname +'_display"]:visible');
113 var timeElement = $('.crm-copy-fields [name^="field["][name*="[' + fname +'_time]"][type!=hidden]');
114
115 displayElement.val( displayElement.eq(0).val() );
116 timeElement.val( timeElement.eq(0).val() );
117 }
118
119 //bind the click event for action icon
120 $('.action-icon').click(function( ) {
121 copyFieldValues($(this).attr('fname'));
122 });
123 });
124
125
126 </script>
127 {/literal}