Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-14-13-42-39
[civicrm-core.git] / templates / CRM / common / batchCopy.tpl
CommitLineData
6a488035
TO
1{*
2 +--------------------------------------------------------------------+
819d0d41 3 | CiviCRM version 4.5 |
6a488035 4 +--------------------------------------------------------------------+
819d0d41 5 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25*}
26{literal}
27<script type="text/javascript">
3cc60a06 28 CRM.$(function($) {
6a488035
TO
29 //bind the click event for action icon
30 cj('.action-icon').click( function( ) {
31 copyFieldValues( cj(this).attr('fname') );
32 });
33 });
34
35 /**
36 * This function use to copy fieldsi
37 *
38 * @param fname string field name
39 * @return void
40 */
41 function copyFieldValues( fname ) {
42 // this is the most common pattern for elements, so first check if it exits
43 // this check field starting with "field[" and contains [fname] and is not
44 // hidden ( for checkbox hidden element is created )
45 var elementId = cj('.crm-copy-fields [name^="field["][name*="[' + fname +']"][type!=hidden]');
46
47 // get the first element and it's value
48 var firstElement = elementId.eq(0);
49 var firstElementValue = firstElement.val();
50
51 //console.log( elementId );
52 //console.log( firstElement );
53 //console.log( firstElementValue );
54
55 //check if it is date element
56 var isDateElement = elementId.attr('format');
57
58 // check if it is wysiwyg element
59 var editor = elementId.attr('editor');
60
61 //get the element type
62 var elementType = elementId.attr('type');
63
64 // set the value for all the elements, elements needs to be handled are
65 // select, checkbox, radio, date fields, text, textarea, multi-select
66 // wysiwyg editor, advanced multi-select ( to do )
67 if ( elementType == 'radio' ) {
68 firstElementValue = elementId.filter(':checked').eq(0).val();
69 elementId.filter("[value=" + firstElementValue + "]").prop("checked",true).change();
70 }
71 else if ( elementType == 'checkbox' ) {
72 // handle checkbox
73 // get the entity id of first element
74 var firstEntityId = cj('.crm-copy-fields > tbody > tr');
75
76 if ( firstEntityId.length == 0 ) {
77 firstEntityId = firstElement.closest('div.crm-grid-row');
78 }
79
80 firstEntityId = firstEntityId.attr('entity_id');
81
82 var firstCheckElement = cj('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]');
83
84 if ( firstCheckElement.length > 1 ) {
85 // lets uncheck all the checkbox except first one
8539f25d 86 cj('.crm-copy-fields [type=checkbox][name^="field["][name*="[' + fname +']"][type=checkbox]:not([name^="field['+ firstEntityId +']['+ fname +']["])').prop('checked', false);
6a488035
TO
87
88 //here for each checkbox for first row, check if it is checked and set remaining checkboxes
89 firstCheckElement.each(function() {
90 if (cj(this).prop('checked') ) {
91 var elementName = cj(this).attr('name');
92 var correctIndex = elementName.split('field['+ firstEntityId +']['+ fname +'][');
93 correctIndexValue = correctIndex[1].replace(']', '');
8539f25d 94 cj('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']['+ correctIndexValue+']"][type!=hidden]').prop('checked',true).change();
6a488035
TO
95 }
96 });
97 }
98 else {
99 if ( firstCheckElement.prop('checked') ) {
8539f25d 100 cj('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked',true).change();
6a488035
TO
101 }
102 else {
8539f25d 103 cj('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked', false).change();
6a488035
TO
104 }
105 }
106 }
107 else if ( editor ) {
108 var firstElementId = firstElement.attr('id');
109 switch ( editor ) {
110 case 'ckeditor':
111 //get the content of first element
112 oEditor = CKEDITOR.instances[firstElementId];
113 var htmlContent = oEditor.getData( );
114
115 // copy first element content to all the elements
116 elementId.each( function() {
117 var elemtId = cj(this).attr('id');
118 oEditor = CKEDITOR.instances[elemtId];
119 oEditor.setData( htmlContent );
120 });
121 break;
122 case 'tinymce':
123 //get the content of first element
124 var htmlContent = tinyMCE.get( firstElementId ).getContent();
125
126 // copy first element content to all the elements
127 elementId.each( function() {
128 var elemtId = cj(this).attr('id');
129 tinyMCE.get( elemtId ).setContent( htmlContent );
130 });
131 break;
132 case 'joomlaeditor':
133 // TO DO
134 case 'drupalwysiwyg':
135 // TO DO
136 default:
137 elementId.val( firstElementValue ).change();
138
139 }
140 }
141 else {
bd8af239 142 if (elementId.is('select') === true && firstElement.parent().find(':input').select().index() >= 1 && firstElement.parent().find('select').select().index < 1) {
f813f78e 143 // its a multiselect case
b5469340
DS
144 firstElement.parent().find(':input').select().each( function(count) {
145 var firstElementValue = cj(this).val();
146 var elementId = cj('.crm-copy-fields [name^="field["][name*="[' + fname +'][' + count + '"][type!=hidden]');
147 elementId.val(firstElementValue).not(":first").change();
148 });
149 }
150 else {
151 elementId.val(firstElementValue).change();
152 }
6a488035
TO
153 }
154
155 // since we use different display field for date we also need to set it.
156 // also check for date time field and set the value correctly
157 if ( isDateElement ) {
158 copyValuesDate( fname );
159 }
160 }
161
162 /**
163 * Special function to handle setting values for date fields
164 *
165 * @param fname string field name
166 * @return void
167 */
168 function copyValuesDate(fname) {
169 var fnameDisplay = fname + '_display';
170 var fnameTime = fname + '_time';
171
172 var displayElement = cj('.crm-copy-fields [name^="field_"][name$="_' + fnameDisplay +'"][type!=hidden]');
173 var timeElement = cj('.crm-copy-fields [name^="field["][name*="[' + fnameTime +']"][type!=hidden]');
174
175 displayElement.val( displayElement.eq(0).val() );
176 timeElement.val( timeElement.eq(0).val() );
177 }
178
179</script>
180{/literal}