fbd78867b26b9e3e3889fed9ec39d47c0a5b6843
[civicrm-core.git] / templates / CRM / Contribute / Form / Contribution / OnBehalfOf.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
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 *}
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 *}
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}
38 <div id='orgOptions' class="section crm-section">
39 <div class="content">
40 {$form.org_option.html}
41 </div>
42 </div>
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>
64 ( <a id='createNewOrg' href="#" onclick="createNew( ); return false;">{ts}Enter a new organization{/ts}</a> )
65 </span>
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>
73 {else}
74 {if $onBehalfOfFields.$fieldName.options_per_line}
75 <div class="label option-label">{$form.onbehalf.$fieldName.label}</div>
76 <div class="content">
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"}
83 {foreach name=outer key=key item=item from=$form.onbehalf.$fieldName}
84 {if $index < 10}
85 {assign var="index" value=`$index+1`}
86 {else}
87 <td class="labels font-light">{$form.onbehalf.$fieldName.$key.html}</td>
88 {if $count == $onBehalfOfFields.$fieldName.options_per_line}
89 </tr>
90 <tr>
91 {assign var="count" value="1"}
92 {else}
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>
104 {else}
105 <div class="label">{$form.onbehalf.$fieldName.label}</div>
106 <div class="content">
107 {if $fieldName eq 'organization_name' and !empty($form.onbehalfof_id)}
108 {$form.onbehalfof_id.html}
109 {/if}
110 {$form.onbehalf.$fieldName.html}
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 </fieldset>
133 {/if}
134 {if empty($snippet)}
135 {literal}
136 <script type="text/javascript">
137
138 showOnBehalf({/literal}"{$onBehalfRequired}"{literal});
139
140 cj( "#mode" ).hide( );
141 cj( "#mode" ).attr( 'checked', 'checked' );
142 if ( cj( "#mode" ).attr( 'checked' ) && !{/literal}"{$reset}"{literal} ) {
143 $text = ' {/literal}{ts escape="js"}Use existing organization{/ts}{literal} ';
144 cj( "#createNewOrg" ).text( $text );
145 cj( "#mode" ).removeAttr( 'checked' );
146 }
147
148 function showOnBehalf(onBehalfRequired) {
149 if ( cj( "#is_for_organization" ).attr( 'checked' ) || onBehalfRequired ) {
150 var urlPath = {/literal}"{crmURL p=$urlPath h=0 q="snippet=4&onbehalf=1&id=$contributionPageID&qfKey=$qfKey"}";
151 {if $mode eq 'test'}
152 urlPath += '&action=preview';
153 {/if}
154 {if $reset}
155 urlPath += '&reset={$reset}';
156 {/if}{literal}
157 cj("#onBehalfOfOrg").show();
158 if (cj("fieldset", '#onBehalfOfOrg').length < 1) {
159 cj('#onBehalfOfOrg').load(urlPath);
160 }
161 }
162 else {
163 cj("#onBehalfOfOrg").hide();
164 }
165 }
166
167 function resetValues() {
168 cj('input[type=text], select, textarea', "#select_org div").not('#onbehalfof_id').val('');
169 cj('input[type=radio], input[type=checkbox]', "#select_org tr td").prop('checked', false);
170 }
171
172 function createNew( ) {
173 if (cj("#mode").attr('checked')) {
174 var textMessage = ' {/literal}{ts escape="js"}Use existing organization{/ts}{literal} ';
175 cj("#onbehalf_organization_name").removeAttr('readonly');
176 cj("#mode").removeAttr('checked');
177 resetValues();
178 }
179 else {
180 var textMessage = ' {/literal}{ts escape="js"}Enter a new organization{/ts}{literal} ';
181 cj("#mode").attr('checked', 'checked');
182 setOrgName( );
183 }
184 cj("#createNewOrg").text(textMessage);
185 }
186
187 function setOrgName( ) {
188 var orgName = "{/literal}{$organizationName}{literal}";
189 var orgId = "{/literal}{$orgId}{literal}";
190 cj("#onbehalf_organization_name").val(orgName);
191 cj("#onbehalf_organization_name").attr('readonly', true);
192 setLocationDetails(orgId);
193 }
194
195
196 function setLocationDetails(contactID , reset) {
197 var submittedCID = {/literal}"{$submittedOnBehalf}"{literal};
198 var submittedOnBehalfInfo = {/literal}'{$submittedOnBehalfInfo}'{literal};
199 submittedOnBehalfInfo = cj.parseJSON(submittedOnBehalfInfo);
200 if (submittedCID == contactID) {
201 cj.each(submittedOnBehalfInfo, function(key, value) {
202 cj('#onbehalf_' + key ).val(value);
203 });
204 return;
205 }
206
207 resetValues();
208 var locationUrl = {/literal}"{$locDataURL}"{literal} + contactID + "&ufId=" + {/literal}"{$profileId}"{literal};
209 cj.ajax({
210 url : locationUrl,
211 dataType : "json",
212 timeout : 5000, //Time in milliseconds
213 success : function(data, status) {
214 for (var ele in data) {
215 if (data[ele].type == 'Radio') {
216 if (data[ele].value) {
217 cj("input[name='"+ ele +"']").filter("[value=" + data[ele].value + "]").attr('checked', 'checked');
218 }
219 }
220 else if (data[ele].type == 'CheckBox') {
221 if (data[ele].value) {
222 cj("input[name='"+ ele +"']").attr('checked','checked');
223 }
224 }
225 else if (data[ele].type == 'Multi-Select') {
226 for (var selectedOption in data[ele].value) {
227 cj('#' + ele + " option[value='" + selectedOption + "']").attr('selected', 'selected');
228 }
229 }
230 else if (data[ele].type == 'Autocomplete-Select') {
231 cj('#' + ele ).val( data[ele].value );
232 cj('#' + ele + '_id').val(data[ele].id);
233 }
234 else if (data[ele].type == 'AdvMulti-Select') {
235 var customFld = ele.replace('onbehalf_', '');
236 // remove empty value if any
237 cj('#onbehalf\\['+ customFld +'\\]-f option[value=""]').remove();
238 cj('#onbehalf\\['+ customFld +'\\]-t option[value=""]').remove();
239
240 for (var selectedOption in data[ele].value) {
241 // remove selected values from left and selected values to right
242 cj('#onbehalf\\['+ customFld +'\\]-f option[value="' + selectedOption + '"]').remove()
243 .appendTo('#onbehalf\\['+ customFld +'\\]-t');
244 }
245 }
246 else {
247 cj('#' + ele ).val(data[ele].value);
248 }
249 }
250 },
251 error : function(XMLHttpRequest, textStatus, errorThrown) {
252 console.error("HTTP error status: ", textStatus);
253 }
254 });
255 }
256
257 cj("input:radio[name='org_option']").click( function( ) {
258 var orgOption = cj(this).val();
259 selectCreateOrg(orgOption, true);
260 });
261
262 cj('#onbehalfof_id').change(function() {
263 setLocationDetails(cj(this).val());
264 }).change();
265
266 function selectCreateOrg( orgOption, reset ) {
267 if (orgOption == 0) {
268 cj("#onbehalfof_id").show().change();
269 cj("input#onbehalf_organization_name").hide()
270 }
271 else if ( orgOption == 1 ) {
272 cj("input#onbehalf_organization_name").show();
273 cj("#onbehalfof_id").hide();
274 }
275
276 if ( reset ) {
277 resetValues();
278 }
279 }
280
281 {/literal}
282 {if ($relatedOrganizationFound or $onBehalfRequired) and $reset and $organizationName}
283 setOrgName( );
284 {else}
285 cj("#orgOptions").show( );
286 var orgOption = cj("input:radio[name=org_option]:checked").val( );
287 selectCreateOrg(orgOption, false);
288 {/if}
289
290 {* If mid present in the url, take the required action (poping up related existing contact ..etc) *}
291 {if $membershipContactID}
292 {literal}
293 cj( function( ) {
294 cj('#organization_id').val("{/literal}{$membershipContactName}{literal}");
295 cj('#organization_name').val("{/literal}{$membershipContactName}{literal}");
296 cj('#onbehalfof_id').val("{/literal}{$membershipContactID}{literal}");
297 setLocationDetails( "{/literal}{$membershipContactID}{literal}" );
298 });
299 {/literal}
300 {/if}
301
302 </script>
303 {/if}