Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-03-03-17-36-50
[civicrm-core.git] / templates / CRM / Mailing / Form / InsertTokens.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*}
26
27{* Include a modified version of jquery-fieldselection
28 * FIXME: we use this plugin for so little it's hard to justify having it at all *}
29<script type="text/javascript" src="{$config->resourceBase}packages/jquery/plugins/jquery-fieldselection.js"></script>
30
31<script type="text/javascript">
ac0a3db5 32cj('form#{$form.formName}').data('tokens', {$tokens|@json_encode});
6a488035
TO
33var text_message = null;
34var html_message = null;
35var isPDF = false;
36var isMailing = false;
37
38
39{if $form.formName eq 'MessageTemplates'}
40 {literal}
41 text_message = "msg_text";
42 html_message = "msg_html";
43 {/literal}
44 {elseif $form.formName eq 'Address'}
45 {literal}
46 text_message = "mailing_format";
47 isMailing = false;
48 {/literal}
49 {else}
50 {literal}
51 text_message = "text_message";
52 html_message = (cj("#edit-html-message-value").length > 0) ? "edit-html-message-value" : "html_message";
53 isMailing = true;
54 {/literal}
55{/if}
56
57{if $form.formName eq 'PDF'}
58 {literal}
59 isPDF = true;
60 {/literal}
61{/if}
62
63{if $templateSelected}
64 {literal}
65 if ( document.getElementsByName("saveTemplate")[0].checked ) {
66 document.getElementById('template').selectedIndex = {/literal}{$templateSelected}{literal};
67 }
68{/literal}
69{/if}
70{literal}
71
72var editor = {/literal}"{$editor}"{literal};
73function showSaveUpdateChkBox() {
74 if (document.getElementById('template') == null) {
75 if (document.getElementsByName("saveTemplate")[0].checked){
76 document.getElementById("saveDetails").style.display = "block";
77 document.getElementById("editMessageDetails").style.display = "block";
78 }
79 else {
80 document.getElementById("saveDetails").style.display = "none";
81 document.getElementById("editMessageDetails").style.display = "none";
82 }
83 return;
84 }
85
86 if (document.getElementsByName("saveTemplate")[0].checked &&
87 document.getElementsByName("updateTemplate")[0].checked == false) {
88 document.getElementById("updateDetails").style.display = "none";
89 }
90 else if ( document.getElementsByName("saveTemplate")[0].checked &&
91 document.getElementsByName("updateTemplate")[0].checked ){
92 document.getElementById("editMessageDetails").style.display = "block";
93 document.getElementById("saveDetails").style.display = "block";
94 }
95 else if ( document.getElementsByName("saveTemplate")[0].checked == false &&
96 document.getElementsByName("updateTemplate")[0].checked ) {
97 document.getElementById("saveDetails").style.display = "none";
98 document.getElementById("editMessageDetails").style.display = "block";
99 }
100 else {
101 document.getElementById("saveDetails").style.display = "none";
102 document.getElementById("editMessageDetails").style.display = "none";
103 }
104}
105
106function selectValue( val ) {
107 document.getElementsByName("saveTemplate")[0].checked = false;
108 document.getElementsByName("updateTemplate")[0].checked = false;
109 showSaveUpdateChkBox();
110 if ( !val ) {
4c71ccb4 111 document.getElementById("subject").value ="";
6a488035
TO
112 if ( !isPDF ) {
113 document.getElementById(text_message).value ="";
6a488035
TO
114 }
115 if ( editor == "ckeditor" ) {
116 oEditor = CKEDITOR.instances[html_message];
117 oEditor.setData('');
118 }
119 else if ( editor == "tinymce" ) {
120 tinyMCE.getInstanceById(html_message).setContent( html_body );
121 }
122 else if ( editor == "joomlaeditor" ) {
123 document.getElementById(html_message).value = '' ;
124 tinyMCE.execCommand('mceSetContent',false, '');
125 }
126 else if ( editor =="drupalwysiwyg" ) {
127 if (Drupal.wysiwyg.instances[html_message].setContent) {
128 Drupal.wysiwyg.instances[html_message].setContent(html_body);
129 }
130 // @TODO: Remove this when http://drupal.org/node/614146 drops
131 else if (Drupal.wysiwyg.instances[html_message].insert) {
132 alert("Please note your editor doesn't completely support this function. You may need to clear the contents of the editor prior to choosing a new template.");
133 Drupal.wysiwyg.instances[html_message].insert(html_body);
134 }
135 else {
136 alert("Sorry, your editor doesn't support this function yet.");
137 }
138 }
139 else {
140 document.getElementById(html_message).value = '' ;
141 }
142 if ( isPDF ) {
143 showBindFormatChkBox();
144 }
145 return;
146 }
147
148 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/template' h=0 }"{literal};
149
150 cj.post( dataUrl, {tid: val}, function( data ) {
4c71ccb4 151 cj("#subject").val( data.subject );
6a488035 152 if ( !isPDF ) {
6a488035
TO
153 if ( data.msg_text ) {
154 cj("#"+text_message).val( data.msg_text );
155 cj("div.text").show();
156 cj(".head").find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
157 cj("#helptext").show();
158 }
159 else {
160 cj("#"+text_message).val("");
161 }
162 }
163 var html_body = "";
164 if ( data.msg_html ) {
165 html_body = data.msg_html;
166 }
167
168 if (editor == "ckeditor") {
169 oEditor = CKEDITOR.instances[html_message];
170 oEditor.setData( html_body );
171 }
172 else if (editor == "tinymce") {
173 tinyMCE.execInstanceCommand('html_message',"mceInsertContent",false, html_body );
174 }
175 else if (editor == "joomlaeditor") {
176 cj("#"+ html_message).val( html_body );
177 tinyMCE.execCommand('mceSetContent',false, html_body);
178 }
179 else if ( editor =="drupalwysiwyg") {
180 if (Drupal.wysiwyg.instances[html_message].setContent) {
181 Drupal.wysiwyg.instances[html_message].setContent(html_body);
182 }
183 // @TODO: Remove this when http://drupal.org/node/614146 drops
184 else if (Drupal.wysiwyg.instances[html_message].insert) {
185 alert("Please note your editor doesn't completely support this function. You may need to clear the contents of the editor prior to choosing a new template.");
186 Drupal.wysiwyg.instances[html_message].insert(html_body);
187 }
188 else {
189 alert("Sorry, your editor doesn't support this function yet.");
190 }
191 }
192 else {
193 cj("#"+ html_message).val( html_body );
194 }
195 if (isPDF) {
196 var bind = data.pdf_format_id ? true : false ;
197 selectFormat( data.pdf_format_id, bind );
198 if (!bind) {
199 document.getElementById("bindFormat").style.display = "none";
200 }
201 }
202 }, 'json');
203}
204
205if ( isMailing ) {
206 document.getElementById("editMessageDetails").style.display = "block";
207
208 function verify(select) {
209 if (document.getElementsByName("saveTemplate")[0].checked == false) {
210 document.getElementById("saveDetails").style.display = "none";
211 }
212 document.getElementById("editMessageDetails").style.display = "block";
213
214 var templateExists = true;
215 if (document.getElementById('template') == null) {
216 templateExists = false;
217 }
218
219 if (templateExists && document.getElementById('template').value) {
220 document.getElementById("updateDetails").style.display = '';
221 }
222 else {
223 document.getElementById("updateDetails").style.display = 'none';
224 }
225
226 document.getElementById("saveTemplateName").disabled = false;
227 }
228
229 function showSaveDetails(chkbox) {
230 if (chkbox.checked) {
231 document.getElementById("saveDetails").style.display = "block";
232 document.getElementById("saveTemplateName").disabled = false;
233 }
234 else {
235 document.getElementById("saveDetails").style.display = "none";
236 document.getElementById("saveTemplateName").disabled = true;
237 }
238 }
239
240 showSaveUpdateChkBox();
241
242 {/literal}
243 {if $editor eq "ckeditor"}
244 {literal}
245 cj( function() {
246 oEditor = CKEDITOR.instances['html_message'];
247 oEditor.BaseHref = '' ;
248 oEditor.UserFilesPath = '' ;
249 oEditor.on( 'focus', verify );
250 });
251 {/literal}
252 {elseif $editor eq "tinymce"}
253 {literal}
254 cj( function( ) {
255 if ( isMailing ) {
256 cj('div.html').hover(
257 function( ) {
258 if ( tinyMCE.get(html_message) ) {
259 tinyMCE.get(html_message).onKeyUp.add(function() {
260 verify( );
261 });
262 }
263 },
264 function( ) {
265 if ( tinyMCE.get(html_message) ) {
266 if ( tinyMCE.get(html_message).getContent() ) {
267 verify( );
268 }
269 }
270 }
271 );
272 }
273 });
274 {/literal}
275 {elseif $editor eq "drupalwysiwyg"}
276 {literal}
277 cj( function( ) {
278 if ( isMailing ) {
279 cj('div.html').hover(
280 verify,
281 verify
282 );
283 }
284 });
285 {/literal}
286 {/if}
287 {literal}
288}
289
ac0a3db5
CW
290cj(function($) {
291 function insertToken() {
292 var
293 token = $(this).val(),
294 field = $(this).data('field');
295 if (field === 'html_message') {
296 tokenReplHtml(token);
297 } else {
298 $('#' + field).replaceSelection(token);
299 }
300 $(this).select2('val', '');
301 if (isMailing) {
302 verify();
303 }
6a488035 304 }
6a488035 305
ac0a3db5
CW
306 function tokenReplHtml(token) {
307 var editor = {/literal}"{$editor}"{literal};
308 if ( editor == "tinymce" ) {
309 tinyMCE.execInstanceCommand('html_message',"mceInsertContent",false, token );
310 }
311 else if ( editor == "joomlaeditor" ) {
312 tinyMCE.execCommand('mceInsertContent',false, token);
313 var msg = document.getElementById(html_message).value;
314 var cursorlen = document.getElementById(html_message).selectionStart;
315 var textlen = msg.length;
316 document.getElementById(html_message).value = msg.substring(0, cursorlen) + token + msg.substring(cursorlen, textlen);
317 var cursorPos = (cursorlen + token.length);
318 document.getElementById(html_message).selectionStart = cursorPos;
319 document.getElementById(html_message).selectionEnd = cursorPos;
320 document.getElementById(html_message).focus();
321 }
322 else if ( editor == "ckeditor" ) {
323 oEditor = CKEDITOR.instances[html_message];
324 oEditor.insertHtml(token.toString() );
325 }
326 else if ( editor == "drupalwysiwyg" ) {
327 if (Drupal.wysiwyg.instances[html_message].insert) {
328 Drupal.wysiwyg.instances[html_message].insert(token.toString() );
329 }
330 else {
331 alert("Sorry, your editor doesn't support this function yet.");
332 }
6a488035
TO
333 }
334 else {
ac0a3db5 335 cj( "#"+ html_message ).replaceSelection( token );
6a488035
TO
336 }
337 }
6a488035 338
ac0a3db5
CW
339 // Initialize token selector widgets
340 var form = $('#{/literal}{$form.formName}{literal}');
341 $('input.crm-token-selector', form)
342 .addClass('crm-action-menu')
343 .change(insertToken)
344 .select2({
345 data: form.data('tokens'),
346 placeholder: '{/literal}{ts escape='js'}Insert Token{/ts}{literal}'
347 });
6a488035 348
6a488035
TO
349 cj('.accordion .head').addClass( "ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ");
350 cj('.resizable-textarea textarea').css( 'width', '99%' );
351 cj('.grippie').css( 'margin-right', '3px');
352 cj('.accordion .head').hover( function() { cj(this).addClass( "ui-state-hover");
353 }, function() { cj(this).removeClass( "ui-state-hover");
354 }).bind('click', function() {
355 var checkClass = cj(this).find('span').attr( 'class' );
356 var len = checkClass.length;
357 if ( checkClass.substring( len - 1, len ) == 's' ) {
358 cj(this).find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-e');
359 cj("span#help"+cj(this).find('span').attr('id')).hide();
360 }
361 else {
362 cj(this).find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
363 cj("span#help"+cj(this).find('span').attr('id')).show();
364 }
365 cj(this).next().toggle(); return false;
366 }).next().hide();
367 cj('span#html').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
368 cj("div.html").show();
369
370 if ( !isMailing ) {
371 cj("div.text").show();
372 }
6a488035 373
ac0a3db5
CW
374 function setSignature() {
375 var emailID = $("#fromEmailAddress").val( );
376 if ( !isNaN( emailID ) ) {
377 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/signature' h=0 }"{literal};
378 $.post( dataUrl, {emailID: emailID}, function( data ) {
379 var editor = {/literal}"{$editor}"{literal};
380
381 if (data.signature_text) {
382 // get existing text & html and append signatue
383 var textMessage = $("#"+ text_message).val( ) + '\n\n--\n' + data.signature_text;
384
385 // append signature
386 $("#"+ text_message).val( textMessage );
6a488035 387 }
ac0a3db5
CW
388
389 if ( data.signature_html ) {
390 var htmlMessage = $("#"+ html_message).val( ) + '<br/><br/>--<br/>' + data.signature_html;
391
392 // set wysiwg editor
393 if ( editor == "ckeditor" ) {
394 oEditor = CKEDITOR.instances[html_message];
395 var htmlMessage = oEditor.getData( ) + '<br/><br/>--' + data.signature_html;
396 oEditor.setData( htmlMessage );
397 }
398 else if ( editor == "tinymce" ) {
399 tinyMCE.execInstanceCommand('html_message',"mceInsertContent",false, htmlMessage);
400 }
401 else if ( editor == "drupalwysiwyg" ) {
402 if (Drupal.wysiwyg.instances[html_message].setContent) {
403 Drupal.wysiwyg.instances[html_message].setContent(htmlMessage);
404 }
405 // @TODO: Remove this when http://drupal.org/node/614146 drops
406 else if (Drupal.wysiwyg.instances[html_message].insert) {
407 alert("Please note your editor doesn't completely support this function. You may need to clear the contents of the editor prior to choosing a new template.");
408 Drupal.wysiwyg.instances[html_message].insert(htmlMessage);
409 }
410 else {
411 alert("Sorry, your editor doesn't support this function yet.");
412 }
413 }
414 else {
415 $("#"+ html_message).val(htmlMessage);
6a488035
TO
416 }
417 }
ac0a3db5 418 }, 'json');
6a488035 419 }
ac0a3db5 420 }
6a488035
TO
421 if (!cj().find('div.crm-error').text()) {
422 cj(window).load(function () {
423 setSignature();
424 });
425 }
6a488035
TO
426 cj("#fromEmailAddress").change( function( ) {
427 setSignature( );
428 });
429});
430
6a488035
TO
431</script>
432{/literal}