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