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