salix/services/mailer/application/template.js

88 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-05-30 06:06:14 +00:00
var fs = require('fs');
var mustache = require('mustache');
2017-06-07 13:28:42 +00:00
var locale = require('./locale.js');
2017-05-31 12:55:41 +00:00
var path = require('path');
2017-05-30 06:06:14 +00:00
2017-06-07 13:28:42 +00:00
module.exports = {
2017-05-30 06:06:14 +00:00
/**
* Obtiene la plantilla.
2017-06-07 13:28:42 +00:00
* @param {String} template - Nombre de la plantilla
* @param {Object} countryCode - Código del idioma
2017-05-30 06:06:14 +00:00
* @param {Object} params - Datos a reemplazar.
2017-06-07 13:28:42 +00:00
* @param {Object} cb - Callback
2017-05-30 06:06:14 +00:00
*/
2017-06-07 13:28:42 +00:00
getTemplate: function(template, countryCode, params, cb) {
var templatePath = path.join(__dirname, 'template', `${template}`, `${template}.html`);
var classPath = path.join(__dirname, 'template', `${template}`, `${template}.js`);
2017-05-30 06:06:14 +00:00
2017-06-07 13:28:42 +00:00
fs.stat(templatePath, (error, stat) => {
if (error)
return cb(null, 'Template ' + template + ' not found');
2017-05-30 06:06:14 +00:00
2017-06-07 13:28:42 +00:00
let TemplateClass = require(classPath);
let instance = new TemplateClass();
2017-05-31 12:55:41 +00:00
let getDataCb = () => {
this.render(templatePath, instance, body => {
var titleSubject = body.match(new RegExp('<title>(.*?)</title>', 'i'))[1];
2017-06-07 13:28:42 +00:00
this.getAttachments(template, body, attachments => {
cb({body: body, subject: titleSubject, attachments: attachments});
2017-06-07 13:28:42 +00:00
});
});
};
locale.load(template, countryCode, (translations, error) => {
instance._ = translations;
instance.getData(params, () => getDataCb());
2017-05-31 12:55:41 +00:00
});
2017-05-30 06:06:14 +00:00
});
},
/**
* Renderiza las plantillas
* @param {String} path - Ruta de la plantilla
2017-06-07 13:28:42 +00:00
* @param {Object} data - Listado de parámetros a remplazar
* @param {Object} cb - Callback
2017-05-30 06:06:14 +00:00
*/
2017-06-07 13:28:42 +00:00
render: function(path, data, cb) {
2017-05-30 06:06:14 +00:00
fs.readFile(path, 'utf8', function(error, body) {
mustache.parse(body);
2017-06-07 13:28:42 +00:00
cb(mustache.render(body, data));
});
},
/**
* Obtiene todos los ficheros adjuntos de la plantilla
* @param {String} template - Nombre de la plantilla
* @param {String} body - html de la plantilla
* @param {Object} cb - Callback
*/
getAttachments: function(template, body, cb) {
var attachments = [];
var tplAttachments = body.match(new RegExp('src="cid:(.*?)"', 'ig'));
for (var i = 0; i < tplAttachments.length; i++) {
var name = tplAttachments[i].replace('src="cid:', '').replace('"', '');
var attachmentPath = path.join(__dirname, 'template/image', name);
attachments.push({filename: name, path: attachmentPath, cid: name});
}
var attachmentsPath = path.join(__dirname, 'template', `${template}`, 'attachment.json');
fs.stat(attachmentsPath, (error, stats) => {
if (error) return cb(null, 'Could not load attachments from template ' + template);
var attachObj = require(attachmentsPath);
for (var i = 0; i < attachObj.length; i++) {
var attachmentPath = path.join(__dirname, 'template', `${template}`, 'attachment', attachObj[i]);
attachments.push({filename: attachObj[i], path: attachmentPath, cid: attachObj[i]});
}
cb(attachments);
2017-05-30 06:06:14 +00:00
});
}
};