Merge pull request #16546 from eileenmcnaughton/part_end2
[civicrm-core.git] / templates / CRM / common / batchCopy.tpl
CommitLineData
6a488035
TO
1{*
2 +--------------------------------------------------------------------+
1188c7a8 3 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 4 | |
1188c7a8
TO
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 |
6a488035
TO
8 +--------------------------------------------------------------------+
9*}
10{literal}
11<script type="text/javascript">
3cc60a06 12 CRM.$(function($) {
ae8f569f
CW
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
ae8f569f
CW
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();
6a488035 41 }
ae8f569f
CW
42 else if ( elementType == 'checkbox' ) {
43 // handle checkbox
44 // get the entity id of first element
45 var firstEntityId = $('.crm-copy-fields > tbody > tr');
6a488035 46
ae8f569f
CW
47 if ( firstEntityId.length == 0 ) {
48 firstEntityId = firstElement.closest('div.crm-grid-row');
49 }
6a488035 50
ae8f569f 51 firstEntityId = firstEntityId.attr('entity_id');
6a488035 52
ae8f569f 53 var firstCheckElement = $('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]');
6a488035 54
ae8f569f
CW
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 });
6a488035
TO
68 }
69 else {
ae8f569f
CW
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 }
6a488035
TO
76 }
77 }
13d9bc82
CW
78 else if (elementId.is('textarea')) {
79 var text = CRM.wysiwyg.getVal(firstElement);
80 elementId.each(function() {
81 CRM.wysiwyg.setVal(this, text);
82 });
6a488035
TO
83 }
84 else {
15e1b48d 85 if (elementId.is('select') === true && firstElement.parent().find(':input').select().index() >= 1 && firstElement.parent().find('select').select().length > 1) {
f813f78e 86 // its a multiselect case
b5469340 87 firstElement.parent().find(':input').select().each( function(count) {
ae8f569f
CW
88 var firstElementValue = $(this).val();
89 var elementId = $('.crm-copy-fields [name^="field["][name*="[' + fname +'][' + count + '"][type!=hidden]');
b5469340
DS
90 elementId.val(firstElementValue).not(":first").change();
91 });
92 }
93 else {
94 elementId.val(firstElementValue).change();
95 }
6a488035
TO
96 }
97
ae8f569f
CW
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) {
d99491c5
CW
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]');
ae8f569f
CW
114
115 displayElement.val( displayElement.eq(0).val() );
116 timeElement.val( timeElement.eq(0).val() );
6a488035 117 }
ae8f569f
CW
118
119 //bind the click event for action icon
120 $('.action-icon').click(function( ) {
121 copyFieldValues($(this).attr('fname'));
122 });
123 });
124
6a488035
TO
125
126</script>
127{/literal}