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