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-06-12 11:41:02 +00:00
|
|
|
var inlineCss = require('inline-css');
|
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
|
|
|
/**
|
2017-05-31 16:19:55 +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-08-30 12:50:46 +00:00
|
|
|
get: function(template, params, cb) {
|
2017-06-07 13:28:42 +00:00
|
|
|
var templatePath = path.join(__dirname, 'template', `${template}`, `${template}.html`);
|
|
|
|
var classPath = path.join(__dirname, 'template', `${template}`, `${template}.js`);
|
2017-06-12 11:41:02 +00:00
|
|
|
var stylePath = path.join(__dirname, 'template', `${template}`, `${template}.css`);
|
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
|
|
|
|
2017-06-12 11:41:02 +00:00
|
|
|
let getRenderedStyles = body => {
|
|
|
|
this.renderStyles(stylePath, body, body => {
|
2017-06-07 17:29:16 +00:00
|
|
|
var titleSubject = body.match(new RegExp('<title>(.*?)</title>', 'i'))[1];
|
|
|
|
this.getAttachments(template, body, attachments => {
|
2017-08-30 12:50:46 +00:00
|
|
|
cb({recipient: instance.email, subject: titleSubject, body: body, attachments: attachments});
|
2017-06-07 13:28:42 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-07 17:29:16 +00:00
|
|
|
};
|
|
|
|
|
2017-06-12 11:41:02 +00:00
|
|
|
let getDataCb = () => {
|
|
|
|
this.render(templatePath, instance, body => getRenderedStyles(body));
|
|
|
|
};
|
|
|
|
|
2017-08-30 12:50:46 +00:00
|
|
|
instance.getData(params, () => {
|
|
|
|
locale.load(template, instance.countryCode, (translations, error) => {
|
|
|
|
instance._ = translations;
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-06-12 11:41:02 +00:00
|
|
|
/**
|
|
|
|
* Renderiza los estilos de las plantillas.
|
|
|
|
* @param {String} path - Ruta de la hoja de estilos
|
|
|
|
* @param {String} body - Html renderizado
|
|
|
|
* @param {Object} cb - Callback
|
|
|
|
*/
|
|
|
|
renderStyles: function(path, html, cb) {
|
|
|
|
fs.stat(path, error => {
|
|
|
|
if (error) return cb(null, 'Template stylesheet not found');
|
|
|
|
fs.readFile(path, 'utf8', (error, css) => {
|
|
|
|
let style = '<style>' + css + '</style>';
|
|
|
|
let body = style + html;
|
|
|
|
let options = {url: ' '};
|
|
|
|
|
|
|
|
inlineCss(body, options)
|
|
|
|
.then(function(body) {
|
|
|
|
cb(body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-06-07 13:28:42 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|