CRM-16305 - Fix autofill of onBehalf profile fields
[civicrm-core.git] / templates / CRM / Contribute / Form / Contribution / OnBehalfOf.tpl
CommitLineData
6a488035
TO
1{*
2 +--------------------------------------------------------------------+
39de6fd5 3 | CiviCRM version 4.6 |
6a488035 4 +--------------------------------------------------------------------+
e7112fa7 5 | Copyright CiviCRM LLC (c) 2004-2015 |
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*}
061a96be 26{**
7f7fa13a 27 * This file provides the HTML for the on-behalf-of form.
061a96be
CW
28 * Also used for related contact edit form.
29 * FIXME: This is way more complex than it needs to be
061a96be
CW
30 * FIXME: Why are we not just using the dynamic form tpl to display this profile?
31 *}
6a488035
TO
32
33{if $buildOnBehalfForm or $onBehalfRequired}
34<fieldset id="for_organization" class="for_organization-group">
35<legend>{$fieldSetTitle}</legend>
36 {if ( $relatedOrganizationFound or $onBehalfRequired ) and !$organizationName}
f62e40cf
DG
37 <div id='orgOptions' class="section crm-section">
38 <div class="content">
39 {$form.org_option.html}
40 </div>
6a488035 41 </div>
6a488035
TO
42 {/if}
43
44<div id="select_org" class="crm-section">
45 {foreach from=$onBehalfOfFields item=onBehaldField key=fieldName}
46 {if $onBehalfOfFields.$fieldName.skipDisplay}
47 {continue}
48 {/if}
49 {if $onBehalfOfFields.$fieldName.field_type eq "Formatting"}
50 {$onBehalfOfFields.$fieldName.help_pre}
51 {continue}
52 {/if}
53 <div class="crm-section {$onBehalfOfFields.$fieldName.name}-section">
54 {if $onBehalfOfFields.$fieldName.help_pre}
55 &nbsp;&nbsp;<span class='description'>{$onBehalfOfFields.$fieldName.help_pre}</span>
56 {/if}
57
58 {if ( $fieldName eq 'organization_name' ) and $organizationName}
59 <div id='org_name' class="label">{$form.onbehalf.$fieldName.label}</div>
60 <div class="content">
61 {$form.onbehalf.$fieldName.html|crmAddClass:big}
62 <span>
f62e40cf
DG
63 ( <a id='createNewOrg' href="#" onclick="createNew( ); return false;">{ts}Enter a new organization{/ts}</a> )
64 </span>
6a488035
TO
65 <div id="id-onbehalf-orgname-enter-help" class="description">
66 {ts}Organization details have been prefilled for you. If this is not the organization you want to use, click "Enter a new organization" above.{/ts}
67 </div>
68 {if $onBehalfOfFields.$fieldName.help_post}
69 <span class='description'>{$onBehalfOfFields.$fieldName.help_post}</span>
70 {/if}
71 </div>
f62e40cf
DG
72 {else}
73 {if $onBehalfOfFields.$fieldName.options_per_line}
6a488035 74 <div class="label option-label">{$form.onbehalf.$fieldName.label}</div>
f62e40cf 75 <div class="content">
6a488035
TO
76 {assign var="count" value="1"}
77 {strip}
78 <table class="form-layout-compressed">
79 <tr>
80 {* sort by fails for option per line. Added a variable to iterate through the element array*}
81 {assign var="index" value="1"}
f62e40cf 82 {foreach name=outer key=key item=item from=$form.onbehalf.$fieldName}
6a488035
TO
83 {if $index < 10}
84 {assign var="index" value=`$index+1`}
f62e40cf 85 {else}
6a488035
TO
86 <td class="labels font-light">{$form.onbehalf.$fieldName.$key.html}</td>
87 {if $count == $onBehalfOfFields.$fieldName.options_per_line}
f62e40cf
DG
88 </tr>
89 <tr>
6a488035 90 {assign var="count" value="1"}
f62e40cf 91 {else}
6a488035
TO
92 {assign var="count" value=`$count+1`}
93 {/if}
94 {/if}
95 {/foreach}
96 </tr>
97 </table>
98 {/strip}
99 {if $onBehalfOfFields.$fieldName.help_post}
100 <span class='description'>{$onBehalfOfFields.$fieldName.help_post}</span>
101 {/if}
102 </div>
f62e40cf 103 {else}
6a488035
TO
104 <div class="label">{$form.onbehalf.$fieldName.label}</div>
105 <div class="content">
ca5bbc71
CW
106 {if $fieldName eq 'organization_name' and !empty($form.onbehalfof_id)}
107 {$form.onbehalfof_id.html}
6a488035 108 {/if}
6a488035 109 {$form.onbehalf.$fieldName.html}
6a488035
TO
110 {if !empty($onBehalfOfFields.$fieldName.html_type) && $onBehalfOfFields.$fieldName.html_type eq 'Autocomplete-Select'}
111 {assign var=elementName value=onbehalf[$fieldName]}
6a488035 112 {/if}
6a488035
TO
113 {if $onBehalfOfFields.$fieldName.name|substr:0:5 eq 'phone'}
114 {assign var="phone_ext_field" value=$onBehalfOfFields.$fieldName.name|replace:'phone':'phone_ext'}
115 {if $form.onbehalf.$phone_ext_field.html}
116 &nbsp;{$form.onbehalf.$phone_ext_field.html}
117 {/if}
7f7fa13a
EM
118 {/if}
119 {if $onBehalfOfFields.$fieldName.data_type eq 'Date'}
31e83de7 120 {assign var=elementName value=onbehalf[$fieldName]}
7f7fa13a 121 {include file="CRM/common/jcalendar.tpl" elementName=$elementName elementId=onbehalf_$fieldName}
6a488035
TO
122 {/if}
123 {if $onBehalfOfFields.$fieldName.help_post}
124 <br /><span class='description'>{$onBehalfOfFields.$fieldName.help_post}</span>
125 {/if}
126 </div>
127 {/if}
128 {/if}
129 <div class="clear"></div>
130 </div>
131 {/foreach}
132</div>
133<div>{$form.mode.html}</div>
ab234a8a 134</fieldset>
6a488035 135{/if}
ab234a8a 136{if empty($snippet)}
6a488035
TO
137{literal}
138<script type="text/javascript">
6a488035 139
061a96be 140 showOnBehalf({/literal}"{$onBehalfRequired}"{literal});
6a488035
TO
141
142 cj( "#mode" ).hide( );
8539f25d
CW
143 cj( "#mode" ).prop('checked', true );
144 if ( cj( "#mode" ).prop('checked' ) && !{/literal}"{$reset}"{literal} ) {
6a488035
TO
145 $text = ' {/literal}{ts escape="js"}Use existing organization{/ts}{literal} ';
146 cj( "#createNewOrg" ).text( $text );
8539f25d 147 cj( "#mode" ).prop('checked', false );
6a488035
TO
148 }
149
150function showOnBehalf(onBehalfRequired) {
9b10578b 151 if ( cj( "#is_for_organization" ).prop( 'checked' ) || onBehalfRequired ) {
ab234a8a 152 var urlPath = {/literal}"{crmURL p=$urlPath h=0 q="snippet=4&onbehalf=1&id=$contributionPageID&qfKey=$qfKey"}";
061a96be
CW
153 {if $mode eq 'test'}
154 urlPath += '&action=preview';
155 {/if}
156 {if $reset}
157 urlPath += '&reset={$reset}';
158 {/if}{literal}
159 cj("#onBehalfOfOrg").show();
ab234a8a
CW
160 if (cj("fieldset", '#onBehalfOfOrg').length < 1) {
161 cj('#onBehalfOfOrg').load(urlPath);
6a488035 162 }
6a488035
TO
163 }
164 else {
061a96be 165 cj("#onBehalfOfOrg").hide();
6a488035
TO
166 }
167}
168
c31d8ebb 169function resetValues() {
2fd2fc79
CW
170 // Don't trip chain-select when clearing values
171 cj('.crm-chain-select-control', "#select_org div").select2('val', '');
172 cj('input[type=text], select, textarea', "#select_org div").not('.crm-chain-select-control, #onbehalfof_id').val('').change();
173 cj('input[type=radio], input[type=checkbox]', "#select_org div").prop('checked', false).change();
6a488035
TO
174}
175
176function createNew( ) {
8539f25d 177 if (cj("#mode").prop('checked')) {
061a96be 178 var textMessage = ' {/literal}{ts escape="js"}Use existing organization{/ts}{literal} ';
8282178a 179 cj("#onbehalf_organization_name").prop('readonly', false);
8539f25d 180 cj("#mode").prop('checked', false);
c31d8ebb 181 resetValues();
6a488035
TO
182 }
183 else {
061a96be 184 var textMessage = ' {/literal}{ts escape="js"}Enter a new organization{/ts}{literal} ';
8539f25d 185 cj("#mode").prop('checked', true);
6a488035
TO
186 setOrgName( );
187 }
188 cj("#createNewOrg").text(textMessage);
189}
190
191function setOrgName( ) {
192 var orgName = "{/literal}{$organizationName}{literal}";
193 var orgId = "{/literal}{$orgId}{literal}";
194 cj("#onbehalf_organization_name").val(orgName);
195 cj("#onbehalf_organization_name").attr('readonly', true);
196 setLocationDetails(orgId);
197}
198
199
8d7965a3 200function setLocationDetails(contactID , reset) {
201 var submittedCID = {/literal}"{$submittedOnBehalf}"{literal};
202 var submittedOnBehalfInfo = {/literal}'{$submittedOnBehalfInfo}'{literal};
d940bda2 203 if (submittedOnBehalfInfo) {
204 submittedOnBehalfInfo = cj.parseJSON(submittedOnBehalfInfo);
0f449ae6 205
206 if (submittedCID == contactID) {
207 cj.each(submittedOnBehalfInfo, function(key, value) {
208 cj('#onbehalf_' + key ).val(value);
209 });
210 return;
211 }
8d7965a3 212 }
213
c31d8ebb 214 resetValues();
6a488035
TO
215 var locationUrl = {/literal}"{$locDataURL}"{literal} + contactID + "&ufId=" + {/literal}"{$profileId}"{literal};
216 cj.ajax({
217 url : locationUrl,
218 dataType : "json",
219 timeout : 5000, //Time in milliseconds
220 success : function(data, status) {
221 for (var ele in data) {
2fd2fc79 222 if (cj("#"+ ele).hasClass('crm-chain-select-target')) {
bd551bb3 223 cj("#"+ ele).data('newVal', data[ele].value).off('.autofill').on('crmOptionsUpdated.autofill', function() {
2fd2fc79
CW
224 cj(this).off('.autofill').val(cj(this).data('newVal')).change();
225 });
226 }
9c3a8754
CW
227 else if (cj('#' + ele).data('select2')) {
228 cj('#' + ele).select2('val', data[ele].value);
229 }
6a488035
TO
230 if (data[ele].type == 'Radio') {
231 if (data[ele].value) {
71c84c87 232 var fldName = ele.replace('onbehalf_', '');
233 cj("input[name='onbehalf["+ fldName +"]']").filter("[value='" + data[ele].value + "']").prop('checked', true);
6a488035
TO
234 }
235 }
236 else if (data[ele].type == 'CheckBox') {
71c84c87 237 for (var selectedOption in data[ele].value) {
238 var fldName = ele.replace('onbehalf_', '');
239 cj("input[name='onbehalf["+ fldName+"]["+ selectedOption +"]']").prop('checked','checked');
6a488035
TO
240 }
241 }
6a488035
TO
242 else if (data[ele].type == 'AdvMulti-Select') {
243 var customFld = ele.replace('onbehalf_', '');
244 // remove empty value if any
245 cj('#onbehalf\\['+ customFld +'\\]-f option[value=""]').remove();
246 cj('#onbehalf\\['+ customFld +'\\]-t option[value=""]').remove();
247
248 for (var selectedOption in data[ele].value) {
249 // remove selected values from left and selected values to right
250 cj('#onbehalf\\['+ customFld +'\\]-f option[value="' + selectedOption + '"]').remove()
251 .appendTo('#onbehalf\\['+ customFld +'\\]-t');
d940bda2 252 cj('#onbehalf_'+ customFld).val(selectedOption);
6a488035
TO
253 }
254 }
255 else {
71c84c87 256 // do not set defaults to file type fields
257 if (cj('#' + ele).attr('type') != 'file') {
258 cj('#' + ele ).val(data[ele].value).change();
259 }
6a488035
TO
260 }
261 }
262 },
263 error : function(XMLHttpRequest, textStatus, errorThrown) {
822863c1 264 CRM.console('error', "HTTP error status: ", textStatus);
6a488035
TO
265 }
266 });
267}
268
6a488035 269cj("input:radio[name='org_option']").click( function( ) {
ca5bbc71 270 var orgOption = cj(this).val();
6a488035
TO
271 selectCreateOrg(orgOption, true);
272});
273
ca5bbc71
CW
274cj('#onbehalfof_id').change(function() {
275 setLocationDetails(cj(this).val());
276}).change();
277
6a488035
TO
278function selectCreateOrg( orgOption, reset ) {
279 if (orgOption == 0) {
ca5bbc71 280 cj("#onbehalfof_id").show().change();
c343285b 281 cj("input#onbehalf_organization_name").hide();
6a488035
TO
282 }
283 else if ( orgOption == 1 ) {
ca5bbc71
CW
284 cj("input#onbehalf_organization_name").show();
285 cj("#onbehalfof_id").hide();
6a488035
TO
286 }
287
288 if ( reset ) {
c31d8ebb 289 resetValues();
6a488035
TO
290 }
291}
292
293{/literal}
294{if ($relatedOrganizationFound or $onBehalfRequired) and $reset and $organizationName}
295 setOrgName( );
296{else}
297 cj("#orgOptions").show( );
298 var orgOption = cj("input:radio[name=org_option]:checked").val( );
299 selectCreateOrg(orgOption, false);
300{/if}
301
302{* If mid present in the url, take the required action (poping up related existing contact ..etc) *}
303{if $membershipContactID}
304{literal}
3cc60a06 305 CRM.$(function($) {
ae8f569f
CW
306 $('#organization_id').val("{/literal}{$membershipContactName}{literal}");
307 $('#organization_name').val("{/literal}{$membershipContactName}{literal}");
308 $('#onbehalfof_id').val("{/literal}{$membershipContactID}{literal}");
6a488035
TO
309 setLocationDetails( "{/literal}{$membershipContactID}{literal}" );
310 });
311{/literal}
312{/if}
313
314</script>
ab234a8a 315{/if}