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 +--------------------------------------------------------------------+
11 <script type="text/javascript">
14 * This function use to copy fields
16 * @param fname string field name
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]');
25 // get the first element and it's value
26 var firstElement = elementId.eq(0);
27 var firstElementValue = firstElement.val();
29 //check if it is date element
30 var isDateElement = elementId.attr('format');
32 //get the element type
33 var elementType = elementId.attr('type');
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();
42 else if ( elementType == 'checkbox' ) {
44 // get the entity id of first element
45 var firstEntityId = $('.crm-copy-fields > tbody > tr');
47 if ( firstEntityId.length == 0 ) {
48 firstEntityId = firstElement.closest('div.crm-grid-row');
51 firstEntityId = firstEntityId.attr('entity_id');
53 var firstCheckElement = $('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]');
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);
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();
70 if ( firstCheckElement.prop('checked') ) {
71 $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked',true).change();
74 $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked', false).change();
78 else if (elementId.is('textarea')) {
79 var text = CRM.wysiwyg.getVal(firstElement);
80 elementId.each(function() {
81 CRM.wysiwyg.setVal(this, text);
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();
94 elementId.val(firstElementValue).change();
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 );
106 * Special function to handle setting values for date fields
108 * @param fname string field name
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]');
115 displayElement.val( displayElement.eq(0).val() );
116 timeElement.val( timeElement.eq(0).val() );
119 //bind the click event for action icon
120 $('.action-icon').click(function( ) {
121 copyFieldValues($(this).attr('fname'));