version fixes
[civicrm-core.git] / templates / CRM / Mailing / Form / InsertTokens.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.7 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2015 |
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 <script type="text/javascript">
27 cj('form.{$form.formClass}').data('tokens', {$tokens|@json_encode});
28 var text_message = null;
29 var html_message = null;
30 var prefix = '';
31 var isPDF = false;
32 var isMailing = false;
33
34 {if $form.formName eq 'MessageTemplates'}
35 {literal}
36 text_message = "msg_text";
37 html_message = "msg_html";
38 {/literal}
39 {elseif $form.formName eq 'Address'}
40 {literal}
41 text_message = "mailing_format";
42 isMailing = false;
43 {/literal}
44 {elseif $form.formClass eq 'CRM_SMS_Form_Upload' || $form.formClass eq 'CRM_Contact_Form_Task_SMS'}
45 {literal}
46 prefix = "SMS";
47 text_message = "sms_text_message";
48 isMailing = true;
49 {/literal}
50 {else}
51 {literal}
52 text_message = "text_message";
53 html_message = (cj("#edit-html-message-value").length > 0) ? "edit-html-message-value" : "html_message";
54 isMailing = true;
55 {/literal}
56 {/if}
57
58 {if $form.formName eq 'PDF'}
59 {literal}
60 isPDF = true;
61 {/literal}
62 {/if}
63
64 {if $templateSelected}
65 {literal}
66 if ( document.getElementsByName(prefix + "saveTemplate")[0].checked ) {
67 document.getElementById(prefix + "template").selectedIndex = {/literal}{$templateSelected}{literal};
68 }
69 {/literal}
70 {/if}
71 {literal}
72
73 function showSaveUpdateChkBox(prefix) {
74 prefix = prefix || '';
75 if (document.getElementById(prefix + "template") == null) {
76 if (document.getElementsByName(prefix + "saveTemplate")[0].checked){
77 document.getElementById(prefix + "saveDetails").style.display = "block";
78 document.getElementById(prefix + "editMessageDetails").style.display = "block";
79 }
80 else {
81 document.getElementById(prefix + "saveDetails").style.display = "none";
82 document.getElementById(prefix + "updateDetails").style.display = "none";
83 }
84 return;
85 }
86
87 if (document.getElementsByName(prefix + "saveTemplate")[0].checked &&
88 document.getElementsByName(prefix + "updateTemplate")[0].checked == false) {
89 document.getElementById(prefix + "updateDetails").style.display = "none";
90 }
91 else if ( document.getElementsByName(prefix + "saveTemplate")[0].checked &&
92 document.getElementsByName(prefix + "updateTemplate")[0].checked ){
93 document.getElementById(prefix + "editMessageDetails").style.display = "block";
94 document.getElementById(pefix + "saveDetails").style.display = "block";
95 }
96 else if ( document.getElementsByName(prefix + "saveTemplate")[0].checked == false &&
97 document.getElementsByName(prefix + "updateTemplate")[0].checked ) {
98 document.getElementById(prefix + "saveDetails").style.display = "none";
99 document.getElementById(prefix + "editMessageDetails").style.display = "block";
100 }
101 else {
102 document.getElementById(prefix + "saveDetails").style.display = "none";
103 document.getElementById(prefix + "updateDetails").style.display = "none";
104 }
105 }
106
107 function selectValue( val, prefix) {
108 document.getElementsByName(prefix + "saveTemplate")[0].checked = false;
109 document.getElementsByName(prefix + "updateTemplate")[0].checked = false;
110 showSaveUpdateChkBox(prefix);
111 if ( !val ) {
112 if (document.getElementById("subject").length) {
113 document.getElementById("subject").value ="";
114 }
115 if ( !isPDF ) {
116 if (prefix == 'SMS') {
117 document.getElementById("sms_text_message").value ="";
118 return;
119 }
120 else {
121 document.getElementById("text_message").value ="";
122 }
123 }
124 CRM.wysiwyg.setVal('#' + html_message, '');
125 if ( isPDF ) {
126 showBindFormatChkBox();
127 }
128 return;
129 }
130
131 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/template' h=0 }"{literal};
132
133 cj.post( dataUrl, {tid: val}, function( data ) {
134 if ( !isPDF ) {
135 if (prefix == "SMS") {
136 text_message = "sms_text_message";
137 }
138 if ( data.msg_text ) {
139 cj("#"+text_message).val( data.msg_text );
140 cj("div.text").show();
141 cj(".head").find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
142 cj("#helptext").show();
143 }
144 else {
145 cj("#"+text_message).val("");
146 }
147 }
148
149 if (prefix == "SMS") {
150 return;
151 }
152 else {
153 cj("#subject").val( data.subject );
154 }
155
156 CRM.wysiwyg.setVal('#' + html_message, data.msg_html || '');
157
158 if (isPDF) {
159 var bind = data.pdf_format_id ? true : false ;
160 selectFormat( data.pdf_format_id, bind );
161 if (!bind) {
162 document.getElementById("bindFormat").style.display = "none";
163 }
164 }
165 }, 'json');
166 }
167
168 if ( isMailing ) {
169 document.getElementById(prefix + "editMessageDetails").style.display = "block";
170
171 function verify(select, prefix) {
172 prefix = prefix || '';
173 if (document.getElementsByName(prefix + "saveTemplate")[0].checked == false) {
174 document.getElementById(prefix + "saveDetails").style.display = "none";
175 }
176 document.getElementById(prefix + "editMessageDetails").style.display = "block";
177
178 var templateExists = true;
179 if (document.getElementById(prefix + "template") == null) {
180 templateExists = false;
181 }
182
183 if (templateExists && document.getElementById(prefix + "template").value) {
184 document.getElementById(prefix + "updateDetails").style.display = '';
185 }
186 else {
187 document.getElementById(prefix + "updateDetails").style.display = 'none';
188 }
189
190 document.getElementById(prefix + "saveTemplateName").disabled = false;
191 }
192
193 function showSaveDetails(chkbox, prefix) {
194 prefix = prefix || '';
195 if (chkbox.checked) {
196 document.getElementById(prefix + "saveDetails").style.display = "block";
197 document.getElementById(prefix + "saveTemplateName").disabled = false;
198 }
199 else {
200 document.getElementById(prefix + "saveDetails").style.display = "none";
201 document.getElementById(prefix + "saveTemplateName").disabled = true;
202 }
203 }
204
205 if (cj("#sms_text_message").length) {
206 showSaveUpdateChkBox('SMS');
207 }
208 if (cj("#text_message").length) {
209 showSaveUpdateChkBox();
210 }
211
212 cj('#' + html_message).on('focus change', verify);
213 }
214
215 CRM.$(function($) {
216 function insertToken() {
217 var
218 token = $(this).val(),
219 field = $(this).data('field');
220 if (field !== 'html_message') {
221 field = textMsgID($(this));
222 }
223 CRM.wysiwyg.insert('#' + field, token);
224 $(this).select2('val', '');
225 if (isMailing) {
226 verify();
227 }
228 }
229
230 function textMsgID(obj) {
231 if (obj.parents().is("#sms")) {
232 field = 'sms #' + obj.data('field');
233 }
234 else if(obj.parents().is("#email")) {
235 field = 'email #' + obj.data('field');
236 }
237 else {
238 field = obj.data('field');
239 }
240
241 return field;
242 }
243
244 // Initialize token selector widgets
245 var form = $('form.{/literal}{$form.formClass}{literal}');
246 $('input.crm-token-selector', form)
247 .addClass('crm-action-menu action-icon-token')
248 .change(insertToken)
249 .crmSelect2({
250 data: form.data('tokens'),
251 placeholder: '{/literal}{ts escape='js'}Tokens{/ts}{literal}'
252 });
253
254 $('.accordion .head').addClass( "ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ")
255 .hover( function() { $(this).addClass( "ui-state-hover");
256 }, function() { $(this).removeClass( "ui-state-hover");
257 }).bind('click', function() {
258 var checkClass = $(this).find('span').attr( 'class' );
259 var len = checkClass.length;
260 if ( checkClass.substring( len - 1, len ) == 's' ) {
261 $(this).find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-e');
262 $("span#help"+$(this).find('span').attr('id')).hide();
263 }
264 else {
265 $(this).find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
266 $("span#help"+$(this).find('span').attr('id')).show();
267 }
268 $(this).next().toggle(); return false;
269 }).next().hide();
270 $('span#html').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
271 $("div.html").show();
272
273 if ( !isMailing ) {
274 $("div.text").show();
275 }
276
277 function setSignature() {
278 var emailID = $("#fromEmailAddress").val( );
279 if ( !isNaN( emailID ) ) {
280 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/signature' h=0 }"{literal};
281 $.post( dataUrl, {emailID: emailID}, function( data ) {
282
283 if (data.signature_text) {
284 var textMessage = $("#"+ text_message).val( ) + '\n\n--\n' + data.signature_text;
285 $("#"+ text_message).val( textMessage );
286 }
287
288 if (data.signature_html) {
289 var htmlMessage = CRM.wysiwyg.getVal("#" + html_message) + '<br/><br/>--<br/>' + data.signature_html;
290 CRM.wysiwyg.setVal("#" + html_message, htmlMessage);
291 }
292 }, 'json');
293 }
294 }
295 $("#fromEmailAddress", form).change(setSignature);
296 });
297
298 </script>
299 {/literal}