1/** 2 * Sends an email to the specified config.to Person.id 3 * 4 * Include: 5 * 6 * `<script src="/SystemMessages/js/frontend/SendEmail.js"></script>` 7 * 8 * Usage: 9 * 10 * `<a class="send-message" href="Javascript:jQuery.fn.SystemMessages_SendEmail({literal}{to: 1}{/literal});">Send Message</a>` 11 * 12 * Usage with captcha 13 * 14 * `<a class="send-message" href="Javascript:jQuery.fn.SystemMessages_SendEmail({literal}{to: 1,useCaptcha: true}}{/literal});">Send Message</a>` 15 * 16 * @param {Object} [config] 17 * 18 * - useCaptcha bool 19 * 20 * @constructor 21 */ 22jQuery.fn.SystemMessages_SendEmail = function(config) { 23 24 config = $.extend({ 25 useCaptcha: false, 26 fromPersonId: null, 27 showFromFields: false, 28 fromFieldsRequired: false, 29 bodySuffix: false, 30 addSenderDataToBody: false, 31 showMessageOnSuccess: false, 32 subjectDefaultValue: null 33 },config || {}); 34 35 var html = [ 36 '<form>', 37 '<p class="errors-holder" style="display: none; color: red;"></p>' 38 ]; 39 40 if(config.showFromFields){ 41 html = html.concat([ 42 '<label for="send-email-from-name">Your Name</label><br>', 43 '<input type="text" name="from-name" id="send-email-from-name" style="width: 97%; padding: 2px;"><br><br>', 44 '<label for="send-email-from-email">Your Email</label><br>', 45 '<input type="text" name="from-email" id="send-email-from-email" style="width: 97%; padding: 2px;"><br><br>' 46 ]); 47 } 48 49 html = html.concat([ 50 '<label for="send-email-subject">Subject</label><br>', 51 '<input type="text" name="subject" value="' + (config.subjectDefaultValue ? config.subjectDefaultValue : '') + '" id="send-email-subject" style="width: 97%; padding: 2px;"><br><br>', 52 '<label for="send-email-body">Message</label><br>', 53 '<textarea name="body" rows="6" id="send-email-body" style="width: 97%; padding: 2px;"></textarea>', 54 '<div class="info-message"></div>', 55 '</form>' 56 ]); 57 58 html = html.join(''); 59 60 var dialogDiv = $(document.createElement('div')).html(html); 61 62 if(SS.vars.LoggedIn && config.showFromFields){ 63 dialogDiv.find('input[name="from-name"]').val(SS.vars.Person.fullname).prop('readonly', true); 64 dialogDiv.find('input[name="from-email"]').val(SS.vars.Person.email).prop('readonly', true); 65 } 66 67 var me = this; 68 69 this.loadCaptcha = function(dialogDiv) { 70 if(!dialogDiv.find('.system-messages-captcha').length){ 71 dialogDiv.find('form').append('<div class="system-messages-captcha"></div>'); 72 dialogDiv.find('.system-messages-captcha').html('Captcha is loading...'); 73 }else{ 74 dialogDiv.find('.system-messages-captcha').html('Captcha is reloading...'); 75 } 76 77 if (typeof grecaptcha === 'undefined') { 78 $.getScript("https://www.google.com/recaptcha/api.js?render=explicit", function (data, textStatus, jqxhr) { 79 grecaptcha.ready(function() { 80 me.renderCaptcha(dialogDiv.find('.system-messages-captcha')); 81 }); 82 }); 83 } else { 84 me.renderCaptcha(dialogDiv.find('.system-messages-captcha')); 85 } 86 }; 87 88 this.renderCaptcha = function(renderContainer) { 89 if(SS.vars.Settings && SS.vars.Settings.GoogleRecaptcha && SS.vars.Settings.GoogleRecaptcha.site_key) { 90 if(renderContainer.length) { 91 renderContainer.html(''); 92 93 me.captchaId = grecaptcha.render(renderContainer[0], { 94 'sitekey': SS.vars.Settings.GoogleRecaptcha.site_key 95 }) 96 } 97 } else { 98 renderContainer.html('<div class="error">Google reCAPTCHA key not found. Please report this to an administrator.</div>'); 99 } 100 }; 101 102 // Append Captcha 103 if(config.useCaptcha && config.useCaptcha == true) { 104 me.loadCaptcha(dialogDiv); 105 } 106 107 dialogDiv.dialog({ 108 title: 'Send Message', 109 modal: true, 110 minWidth: 400, 111 minHeight: 200, 112 dialogClass: "ui-dialog-send-message", 113 buttons: { 114 "Send Email": function(event) { 115 var dialog = $(this), 116 errorEl = dialog.find('p.errors-holder').hide(); 117 118 var data = { 119 subject: $(this).find('#send-email-subject').val(), 120 body: $(this).find('#send-email-body').val() 121 }; 122 if(config.bodySuffix){ 123 data.bodySuffix = config.bodySuffix; 124 } 125 if(config.addSenderDataToBody){ 126 data.addSenderDataToBody = config.addSenderDataToBody ? 1 : 0; 127 } 128 if(config.fromPersonId){ 129 data.fromPersonId = config.fromPersonId; 130 } 131 if(config.showFromFields && !config.fromPersonId){ 132 data.fromName = $(this).find('#send-email-from-name').val(); 133 data.fromEmail = $(this).find('#send-email-from-email').val(); 134 } 135 136 if(config.useCaptcha && config.useCaptcha === true) { 137 data['g-recaptcha-response'] = grecaptcha.getResponse(me.captchaId); 138 } 139 140 $.ajax({ 141 url: SS.route('SystemMessages', 'SystemMessages', 'sendEmail', null, null), 142 type: 'post', 143 data: jQuery.extend(data, config || {}), 144 success: function() { 145 if(config.showMessageOnSuccess){ 146 // Show success message 147 dialog.find('.info-message').addClass('visible').html('Message has been sent successfully.'); 148 // Hide message and close dialog after 10 sec 149 setTimeout(function(){ 150 dialog.find('.info-message').removeClass('visible').html(''); 151 dialog.dialog('close'); 152 }, 10000); 153 } else { 154 dialog.dialog('close'); 155 } 156 }, 157 error: function(jqXHR, textStatus) { 158 errorEl.show().html(jqXHR.responseText || textStatus); 159 }, 160 complete: function() { 161 if (me.captchaId !== null) { 162 grecaptcha.reset(me.captchaId) 163 } 164 } 165 }); 166 } 167 } 168 }); 169 170 $(window).resize(function() 171 { 172 dialogDiv.dialog("option", "position", "center"); 173 }); 174};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.