var email_template = (function ($, window, jQuery) { var _vars_ = {}; return { "set_vars": function (options) { $.extend(_vars_, options || {}); }, "get_vars": function () { return _vars_; }, "_create" : { "modal" : function(email_type_id, email_type){ app.modal.focusout_solution("on"); app.modal.activate_tinymce("on"); var _form = $("form#emailTemplateForm"); _form.data("email_type_id", email_type_id); $.ajax({ url : app.get_vars().baseurl+"email/email_template/get_default_template", type : "POST", data : { email_type_id : email_type_id }, success : function(result){ bootbox.dialog({ "title": app.get_vars().email.title.create_template + "   "+email_type, "className" : "my-modal-with-large", "message": _form, "show": false, "buttons" : [ { "label": app.get_vars()._app.btn._close, "class": "btn btn-default", "callback": function () { } }, { "label": app.get_vars().email.btn.create_new_template, "class": "btn btn-success", "callback": function () { email_template._validate._init('add'); return false; } } ] }).on('shown.bs.modal', function() { $("#email_tpl_name").focus(); $(this).off('shown.bs.modal'); }).on('hide.bs.modal', function(e) { try { tinymce.remove(); $("#email_tpl_detail").html(''); } catch (e) {} app._form._reset(this); app._form._resetFormValidation("#emailTemplateForm"); $("#email_tpl_detail").html(""); app.modal.focusout_solution("off"); app.modal.activate_tinymce("off"); $(this).data('bs.modal', null); $(this).off('hidden.bs.modal'); }) .modal('show') alert("Reakky") .init(function(){ //load wysiwyg html editor try { $("#email_tpl_detail").html(result.mdata.email_type_template); email_template._editor._preview('#preview_tpl_detail'); email_template._editor._load("#email_tpl_detail"); $("#email_tpl_status option[value='2']").prop('selected',true); $("#email_tpl_subject").val(result.mdata.email_type_subject); $("#email_tpl_creation_status").show(function(){ $("input[name='email_tpl_creation_status'][value='CUSTOMIZED']").prop("checked", true); }); /* save default email subject on temp var, this will be displayed on the cofirmation dialog and will be a replacement for empty subject */ email_template.set_vars({email_type_subject : result.mdata.email_type_subject}); //tinymce.EditorManager.execCommand('mceAddEditor',true, 'email_tpl_detail'); } catch (e) {} }); } }); }, "save" : function(form){ var wait_loader = null, email_tpl_status = $("#email_tpl_status option:selected").val(); $.ajax({ "url" : app.get_vars().baseurl+"create_email_template", "type" : "POST", "dataType": "json", "data" : email_template._prepare._data_to_server("add"), "beforeSend" : function(){ if(email_tpl_status == 2) { wait_loader = bootbox.dialog({ message: '

'+app.get_vars().email.pub_email_tpl.publishing_email_template+'

', closeButton: false }); } }, "success" : function(result){ //hide loder modal if(email_tpl_status == 2){ wait_loader.modal("hide"); } if(app.isalive(result)) { app._notify(result.mtype, result.message); if (result.mtype == "success") { //reload grid //bootbox.hideAll(); // (event_reminder.get_vars()._event_reminder).ajax.reload( null, false ); // location.reload(); window.setTimeout(function(){window.location.href = app.get_vars().baseurl+"email_template";},500); } } } }); } }, "_update" : { "modal" : function(row){ app.modal.focusout_solution("on"); app.modal.activate_tinymce("on"); var rowData = (email_template.get_vars()._email_template).row(row).data(); var _form = $("#emailTemplateForm"); _form.data("email_tpl", {"email_tpl_id" : rowData.action.email_tpl_id, "email_type_id" : rowData.action.email_type_id}); if((rowData.email_tpl_creation_status).toUpperCase() == "DEFAULT"){ $("div#email_tpl_creation_status").hide(); } else { $("div#email_tpl_creation_status").show(); } bootbox.dialog({ "title": app.get_vars().email.title.edit_template + "   "+rowData.email_tpl_name , "className" : "my-modal-with-large", "message": _form, "show": false, /* We will show it manually later */ "buttons" : [ { "label": app.get_vars()._app.btn._close, "class": "btn btn-default", "callback": function () { } }, { "label": app.get_vars().email.btn.save_changes, "class": "btn btn-success", "callback": function () { email_template._validate._init('edit'); return false; } } ] }).on('shown.bs.modal', function() { $("#email_tpl_name").focus(); $(this).off('shown.bs.modal'); }).on('hide.bs.modal', function(e) { try { //tinymce.EditorManager.execCommand('mceRemoveEditor',false, 'email_tpl_detail'); tinymce.remove(); } catch (e) {} app._form._resetFormValidation("#emailTemplateForm"); app._form._reset(this); $("#email_tpl_detail").html(""); app.modal.focusout_solution("off"); app.modal.activate_tinymce("off"); $(this).data('bs.modal', null); $(this).off('hidden.bs.modal'); }) .modal('show') .init(function(){ //set tinymce content email_template._editor._preview('#preview_tpl_detail'); email_template._prepare._set_data(rowData); //submit form on keypress or enter app._form._form_submit_on_keypress("emailTemplateForm", function(){ email_template._validate._init('edit'); }); }); }, "save" : function(form){ var wait_loader = null, email_tpl_status = $("input#status option:selected").val(), _form = $("#emailTemplateForm").data("email_tpl"); $.ajax({ "url" : app.get_vars().baseurl+"update_email_template/"+_form.email_tpl_id, type : "POST", dataType : "json", data : email_template._prepare._data_to_server("edit"), beforeSend : function(){ if(email_tpl_status == 2) { wait_loader = bootbox.dialog({ message: '

'+app.get_vars().email.pub_email_tpl.publishing_email_template+'

', closeButton: false }); } }, success : function(result){ //hide loder modal if(email_tpl_status == 2){ wait_loader.modal("hide"); } if(app.isalive(result)) { app._notify(result.mtype, result.message); if (result.mtype == "success") { //reload grid //bootbox.hideAll(); //(event_reminder.get_vars()._event_reminder).ajax.reload( null, false ); window.setTimeout(function(){window.location.href = app.get_vars().baseurl+"email_template";},500); // location.reload(); } } } }); } }, "_delete" : { "confirm" : function(email_tpl_id){ app.modal.confirm_box({ "message" : app.get_vars().email.cmsg.delete_email_template+"

"+app.get_vars()._app.cma_msg.note+"", "_continue" : function() { email_template._delete.save(email_tpl_id); }, "id" : "delete_confirm_box", "className" : "delete_confirm_box" }); }, "save" : function(email_tpl_id){ $.ajax({ "url" : app.get_vars().baseurl+"check_template_dependencies/"+email_tpl_id, "type" : "POST", "dataType" : "json", "success" : function(result){ if(app.isalive(result)) { app._notify(result.mtype, result.message); if (result.mtype == "success") { //reload grid bootbox.hideAll(); (email_template.get_vars()._email_template).ajax.reload(null, false); //window.setTimeout(function(){window.location.href = app.get_vars().baseurl+"event_reminder";},1000); } } } }); } }, "_view" : { "modal": function (row) { var data = (email_template.get_vars()._email_template).row(row).data(); $.ajax({ "url" : app.get_vars().baseurl+"get_email_template_content/"+data.action.email_tpl_id, "type" : "POST", "dataType" : "json", "success": function (result) { if (app.isalive(result)) { if (result.mtype == "success") { //set content in a dialog try { bootbox.dialog({ "title": data.email_tpl_name, "className": 'my-modal-with-medium', "message": '', "buttons": [ { "label": app.get_vars()._app.btn._close, "class": "btn btn-default w-mb", "callback": function () { } } ] }).on('shown.bs.modal', function () { var previewcontent = email_template._preview._changevariables(result.mdetail[0].email_tpl_detail); $("iframe#preview_event_content").contents().find('html').html(previewcontent); $(this).off('shown.bs.modal'); }); } catch (e) { } } } } }); } }, "_editor" :{ "_load" : function(selector, initcallback){ var dynamicVars = app.get_vars().email.dynamicVars, items=""; $.map(dynamicVars, function(val, index){ items +=index+" ";}); tinymce.init({ selector: selector, height: 300, theme: 'modern', language: 'fr_FR', invalid_elements : "script", file_browser_callback_types: 'file image media', convert_urls: true, relative_urls: false, remove_script_host: false, plugins: [ 'autolink lists link charmap anchor preview code' ], toolbar: 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | PreviewButton | uploadtpl code', menu : { file : {title : "Fichier" , items : 'newdocument'}, edit : {title : 'Editer' , items : 'undo redo | cut copy paste pastetext | selectall'}, insert : {title : 'Insérer', items : 'link media | template hr'}, view : {title : 'Vue' , items : 'visualaid'}, format : {title : 'Format', items : 'bold italic underline strikethrough superscript subscript | formats | removeformat'}, tools : {title : 'Outils' , items : 'spellchecker code'}, variables: {title : 'Variables dynamiques', items : items} }, setup: function(editor) { var originalcontent; var previewcontent; email_template.menuItems._add(editor); // email_template._editor._addTplForm(); editor.addButton('PreviewButton', { text: "Aperçu de l'email", icon: 'preview', context: 'previewbutton', onclick: function() { var editor = tinymce.get('email_tpl_detail'); // use your own editor id here - equals the id of your textarea var preview = tinymce.get('preview_tpl_detail'); originalcontent = editor.getContent(); previewcontent = email_template._preview._changevariables(originalcontent); preview.setContent(previewcontent); preview.execCommand('mcePreview'); return; } }); }, init_instance_callback : (typeof initcallback != "undefined")?initcallback:function(){} }); }, "_preview" : function(selector){ tinymce.init({ selector: selector, height: 300, theme: 'modern', language: 'fr_FR', convert_urls: true, relative_urls: false, remove_script_host: false, //element_format : 'html', //entity_encoding : 'raw', file_browser_callback_types: 'file image media', plugins: [ 'autolink lists link charmap anchor preview' ] }); } }, "_grid" : { "load" : function(){ var table = $('#email_template_list').DataTable({ "dom" : '<"top"l><"toolbar">rt<"bottom"ip><"clear">', //"dom": '<"toolbar"><"top"lf>rt', "autoWidth": true, "responsive": true, "processing": true, //Feature control the processing indicator. "serverSide": true, //Feature control DataTables' server-side processing mode. "order": [], //Initial no order. "oLanguage": fr_onload_lang.oLanguage, // Load data for the table's content from an Ajax source "ajax": { "url": app.get_vars().baseurl+"get_email_template_list", "type": "POST" , "data" : function(d){ //var da_ta = $("#custom_filter_search_form").serializeArray(); //if(da_ta.length > 0) { // var x=0; // $.map(da_ta, function(val, index){ // if(val.name != "custom_filter_search" && x==0){ // d.keyname = val.name; // x++; // } // d[val.name] = val.value; // }); //} } }, "columns": [ { "data": "email_tpl_order" }, { "data": "email_tpl_name"}, { "data": "email_type"}, { "data": "email_tpl_creation_status", render : function(data, type, full, meta){ if(full.email_tpl_creation_status == "DEFAULT"){ return "OUI"; } return "NON"; }}, { "data": "email_tpl_status" }, { "data": "email_tpl_author" }, { "data": "action" , render : function(data, type, full, meta){ var action_btns = ""; if (authorize(PAGE_CODE['email'], PRIVS[PAGE_CODE['email']]['template_edit'])) { action_btns += app.grid.create_action_btn(app.get_vars()._app.action._edit, "pencil", "success", "email_template._update.modal("+meta.row+")"); } if (authorize(PAGE_CODE['email'], PRIVS[PAGE_CODE['email']]['template_view'])) { action_btns += app.grid.create_action_btn(app.get_vars()._app.action._openpreview, "eye","info", "email_template._view.modal("+meta.row+")"); } if ( authorize(PAGE_CODE['email'], PRIVS[PAGE_CODE['email']]['template_delete']) && (full.email_tpl_creation_status).toUpperCase() != "DEFAULT" ) { action_btns +=app.grid.create_action_btn(app.get_vars()._app.action._delete, "trash", "danger", "email_template._delete.confirm("+full.action.email_tpl_id +")"); } return '
'+action_btns+'
'; }}, { "data": "email_tpl_date_created" }, { "data": "email_tpl_subject" } ], "fnDrawCallback": function() { app._tooltip._hide(); $('#email_template_list tbody tr td').each( function(x, y) { var _this = $(this); if(_this.hasClass('truncate-content-text')){ _this.css('max-width', '180px'); this.setAttribute( 'title', _this.text()); this.setAttribute( 'data-toggle',"tooltip"); } }); app._tooltip._pop(); }, //Set column definition initialisation properties. "columnDefs": [ { "targets": [0], "orderable": false}, { "targets": [6], "orderable": false}, { "targets": [3], "orderable": true}, { "targets": [5], "orderable": true, className : "truncate-content-text custom-popover-tooltip"}, { "targets": [4], "orderable": true}, { "targets": [1], width: "20%", className : "truncate-content-text custom-popover-tooltip"}, { "targets": [8], "orderable": false, "visible": true } ], "initComplete": function(settings, json) { //run tooltip app._tooltip._hide(); app._tooltip._pop(); $("div#email_template_list_length").css({"width" : "20%", "float":"left"}); $("select[name='email_template_list_length']").css({"width" : "70px"}); if (authorize(PAGE_CODE['email'], PRIVS[PAGE_CODE['email']]['template_add'])) { $("div.toolbar").html(''); } email_template.util.load_email_type(); } }); email_template.set_vars({_email_template : table}); } }, "_preview" : { "_changevariables" : function(originalcontent){ var mapVar = {},eVars=""; var dynamicVars = app.get_vars().email.dynamicVars, dynamicVarsCount = dynamicVars.length; $.map(dynamicVars, function(items, index){ mapVar[items.code] = items.val; var tmp = (items.code).replace("[[", "\\[\\["), sep = ((dynamicVarsCount-1) != index)?"|":""; eVars +=tmp+sep; }); previewcontent = originalcontent.replace(new RegExp(eVars,"g"), function(matched){ return mapVar[matched]; } ); return previewcontent; } }, "menuItems" : { "_add" : function(editor){ var menuItem = app.get_vars().email.dynamicVars; $.map(menuItem, function(item, index){ editor.addMenuItem(index, { text: item.text, context: 'newmenu', onclick: function () { editor.insertContent(item.code); } }); }); } }, "_prepare" : { "_get_surheader" : function(template){ var tinymceTmpContent = $(template), extractSurheader = tinymceTmpContent.find("a[href*='new_tab_url']").parent().parent(), extractContent = tinymceTmpContent.find("table.body:first"), email_tpl_surheader = {name: "email_tpl_surheader", value: ""}; $.each(extractSurheader.find("div"), function(index,div){ var getText = $(div).children() //select all the children .remove() //remove all the children .end() //again go back to selected element .text().trim(); if(getText != "") { email_tpl_surheader.name = "email_tpl_surheader"; email_tpl_surheader.value = "
" + getText +" | " + (extractContent.text().replace(/\n/g," ").trim()).replace(/\s+/g, " ").substr(0, 700)+"
\n" + "...
"; return false; } }); return email_tpl_surheader; }, "_data_to_server" : function(action){ var _form = $("form#emailTemplateForm"), form_data = _form.serializeArray(), tinymceTmpContent = tinyMCE.get("email_tpl_detail").getContent(), checker = 0; //extract the surheader form_data.push(email_template._prepare._get_surheader(tinymceTmpContent)); //check email template subject (($("#email_tpl_subject").val().trim().length > 0) ? app.get_vars().email.cmsg.create_new_template : (app.get_vars().email.cmsg.empty_subject + '\"'+email_template.get_vars().email_type_subject+'\"') ) $.map(form_data, function(x, t){ if(x.name == "email_tpl_detail") { form_data[t].value = tinymceTmpContent; checker = 1; } else if(x.name == "email_tpl_subject") { form_data[t].value = (((form_data[t].value).trim().length > 0) ? form_data[t].value : email_template.get_vars().email_type_subject); } }); if(checker == 0){ form_data.push({"name" : "email_tpl_detail", "value" : tinymceTmpContent}); } if(action == "edit"){ var form_data_ = _form.data("email_tpl"); form_data.push({"name" : "email_type_id", "value" : form_data_.email_type_id}); } else { form_data.push({"name" : "email_type_id", "value" :_form.data("email_type_id")}); } return form_data; }, "_set_data" : function(data){ $.ajax({ "url" : app.get_vars().baseurl+"get_email_template_content/"+data.action.email_tpl_id, "type" : "POST", "dataType" : "json", "success" : function(result){ if(app.isalive(result)) { if (result.mtype == "success") { try { email_template._editor._load("#email_tpl_detail", function(){ tinyMCE.get('email_tpl_detail').setContent(result.mdetail[0].email_tpl_detail); }); //set values $("input[name='email_tpl_creation_status'][value='" + data.email_tpl_creation_status + "']").prop("checked", true).change(); $("#email_tpl_name").val(data.email_tpl_name); $("#email_tpl_subject").val(data.email_tpl_subject); /* save default email subject on temp var, this will be displayed on the cofirmation dialog and will be a replacement for empty subject */ email_template.set_vars({email_type_subject : data.action.email_type_subject}); switch(data.action.email_tpl_id){ case '3': $("#email_tpl_subject").attr('placeholder', "Confirmation de votre réservation pour `Nom de l'evenement`"); break; case '5': $("#email_tpl_subject").attr('placeholder', "Annulation de votre réservation pour `Nom de l'evenement`"); break; case '6': $("#email_tpl_subject").attr('placeholder', "Réservation sur la liste d'attente"); break; default: $("#email_tpl_subject").attr('placeholder', "Nom de l'evenement"); break; } $("#email_tpl_status").val(data.action.email_tpl_status); } catch (e) {} } else { app._notify(result.mtype, result.message); } } } }); } }, "_validate" : { "_init": function (action) { var options = { boot_box: { "action" : action, "message": (action == "add") ? (($("#email_tpl_subject").val().trim().length > 0) ? app.get_vars().email.cmsg.create_new_template : (app.get_vars().email.cmsg.empty_subject + '\"'+email_template.get_vars().email_type_subject+'\"') ) : (($("#email_tpl_subject").val().trim().length > 0) ? app.get_vars().email.cmsg.edit_template : (app.get_vars().email.cmsg.empty_subject + '\"'+email_template.get_vars().email_type_subject+'\"') ) , "_continue": function () { var _form_ = $("form#emailTemplateForm"); (action == "add") ? email_template._create.save(_form_) : email_template._update.save(_form_) } }, "validation": { "custom" : { "rules": { "check_email_tpl_desc": { "_fnc": function (value, element, options) { var mail_content = tinyMCE.get('email_tpl_detail').getContent(); if (mail_content.trim() == "") { tinymce.execCommand('mceFocus', false, 'email_tpl_detail'); return false; } return true; }, msg: app.get_vars().email.val.description_req } }, "informal_val": { "_rules": function (action) { app._form._resetFormValidation("#emailTemplateForm"); var error = [], message = ""; var _form = $("#emailTemplateForm"); if(action == "add") { //check email type var email_type_id = _form.data("email_type_id"); if (typeof email_type_id == "undefined" || parseInt(email_type_id) <= 0) { message += "* " + app.get_vars().email.val.template_type_req+"
"; error.push(1); } } else { //check email type var email_tpl = _form.data("email_tpl"); if (typeof email_tpl == "undefined" || parseInt(email_tpl) <= 0) { error.push(1); message += "* " + app.get_vars().email.val.email_tpl_req+"
"; } } if (message != "") { $("#help-block-msg").html(message); } return error; }, "_callback": function (error) { if (error.length >= 1) { //scroll to top for (i = 0; i < error.length; i++) { return false; } } } } }, "form_id": "form#emailTemplateForm", "ignore": "", "rules": { "email_tpl_status": { "required": true }, "email_tpl_subject": { "required": false }, "email_tpl_detail": { "check_email_tpl_desc": true }, "email_type_id": { "required": true }, "email_tpl_name": { "minlength" : 3 }, "email_tpl_creation_status": { "required": true } } } }; app._form._validate(options); }, check_email_tpl_status : function(){ var email_tpl_status = $("#email_tpl_status option:selected").val(); if(parseInt(email_tpl_status) == 2 && email_tpl_status !="") { return true; } else { return false; } } }, "_default_tmplt" : { "modal" : function(email_type_id, email_type){ app.modal.focusout_solution("on"); app.modal.activate_tinymce("on"); var _form = $("form#emailTemplateForm"); _form.data("email_type_id", email_type_id); $.ajax({ url : app.get_vars().baseurl+"email/email_template/get_default_tmplt", type : "POST", data : { email_type_id : email_type_id }, success : function(result){ bootbox.dialog({ "title": app.get_vars().email.title.create_template + "   "+email_type, "className" : "my-modal-with-large", "message": _form, "show": false, "buttons" : [ { "label": app.get_vars()._app.btn._close, "class": "btn btn-default", "callback": function () { } }, { "label": app.get_vars().email.btn.create_new_template, "class": "btn btn-success", "callback": function () { email_template._validate._init('add'); return false; } } ] }).on('shown.bs.modal', function() { $("#email_tpl_name").focus(); $(this).off('shown.bs.modal'); }).on('hide.bs.modal', function(e) { try { tinymce.remove(); $("#email_tpl_detail").html(''); } catch (e) {} app._form._reset(this); app._form._resetFormValidation("#emailTemplateForm"); $("#email_tpl_detail").html(""); app.modal.focusout_solution("off"); app.modal.activate_tinymce("off"); $(this).data('bs.modal', null); $(this).off('hidden.bs.modal'); }) .modal('show') .init(function(){ //load wysiwyg html editor try { $("#email_tpl_detail").html(result.mdata.email_tpl_detail); email_template._editor._preview('#preview_tpl_detail'); email_template._editor._load("#email_tpl_detail"); $("#email_tpl_status option[value='2']").prop('selected',true); $("#email_tpl_subject").val(result.mdata.email_tpl_subject); $("#email_tpl_creation_status").show(function(){ $("input[name='email_tpl_creation_status'][value='CUSTOMIZED']").prop("checked", true); }); /* save default email subject on temp var, this will be displayed on the cofirmation dialog and will be a replacement for empty subject */ email_template.set_vars({email_type_subject : result.mdata.email_type_subject}); //tinymce.EditorManager.execCommand('mceAddEditor',true, 'email_tpl_detail'); } catch (e) {} }); } }); }, "save" : function(form){ var wait_loader = null, email_tpl_status = $("#email_tpl_status option:selected").val(); $.ajax({ "url" : app.get_vars().baseurl+"create_email_template", "type" : "POST", "dataType": "json", "data" : email_template._prepare._data_to_server("add"), "beforeSend" : function(){ if(email_tpl_status == 2) { wait_loader = bootbox.dialog({ message: '

'+app.get_vars().email.pub_email_tpl.publishing_email_template+'

', closeButton: false }); } }, "success" : function(result){ //hide loder modal if(email_tpl_status == 2){ wait_loader.modal("hide"); } if(app.isalive(result)) { app._notify(result.mtype, result.message); if (result.mtype == "success") { //reload grid //bootbox.hideAll(); // (event_reminder.get_vars()._event_reminder).ajax.reload( null, false ); // location.reload(); window.setTimeout(function(){window.location.href = app.get_vars().baseurl+"email_template";},500); } } } }); } }, util : { loader : function(el){ el.html('

Loading...

'); }, load_email_type : function(){ $.get(app.get_vars().baseurl+"load_email_type/tpl_list/", function(data) { var datalen = data.length, options = ""; if(datalen > 0) { $.map(data, function(val, i){ options += "
  • " + ""+val.email_type+"
  • "; if(i == 0 || (i+1) != datalen){ options += '
  • ';} }); $("#emailtemplatetype").html(options); } else { $("#emailtemplatetype").html('
  • '); } }); } } };//end of return // Pass in jQuery. })(jQuery, this); $(function() { //load datatable email_template._grid.load(); $("input.email_tpl_creation_status").on("change", function(){ var val = $(this).val(); if(val == "DEFAULT"){ $("#email_tpl_status").find("option[value='1']").prop("disabled", true); } else{ $("#email_tpl_status").find("option[value='1']").prop("disabled", false); } $("#email_tpl_status").find("option[value='2']").prop("selected", true); }); });