2017-05-30 06:06:14 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var mustache = require('mustache');
|
|
|
|
var database = require('./database.js');
|
|
|
|
var logger = require('./logger.js');
|
2017-05-31 11:46:18 +00:00
|
|
|
var language = require('./language.js');
|
2017-05-30 06:06:14 +00:00
|
|
|
var Template = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtiene la plantilla
|
2017-05-31 11:46:18 +00:00
|
|
|
* @param {String} templateName - Nombre de la plantilla
|
2017-05-30 06:06:14 +00:00
|
|
|
* @param {Object} params - Datos a reemplazar.
|
|
|
|
* @param {Object} callback - Callback
|
|
|
|
*/
|
2017-05-31 11:46:18 +00:00
|
|
|
getTemplate: function(templateName, langCode, params, callback) {
|
|
|
|
database.pool.query('SELECT name, attachmentPath FROM vn.mailTemplates WHERE name = ?', [templateName], function(error, rs)
|
2017-05-30 06:06:14 +00:00
|
|
|
{
|
2017-05-31 11:46:18 +00:00
|
|
|
// Comprobamos que exista la plantilla
|
2017-05-30 06:06:14 +00:00
|
|
|
if (rs.length == 0) {
|
2017-05-31 11:46:18 +00:00
|
|
|
logger.print(__LOG_ERROR, 'La plantilla ' + templateName + ' no existe');
|
2017-05-30 06:06:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-31 11:46:18 +00:00
|
|
|
var langParams = language.load(templateName, langCode);
|
|
|
|
params = Object.assign({}, langParams, params);
|
2017-05-30 06:06:14 +00:00
|
|
|
var path = './Application/Template/' + rs[0].name + '.html';
|
|
|
|
|
|
|
|
if (!fs.existsSync(path)) {
|
|
|
|
logger.print(__LOG_ERROR, 'No se ha podido cargar la plantilla ' + name + '.html');
|
|
|
|
} else {
|
2017-05-31 11:46:18 +00:00
|
|
|
Template.render(path, params, function(tplBody) {
|
|
|
|
callback({subject: params.subject, body: tplBody});
|
2017-05-30 06:06:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renderiza las plantillas
|
|
|
|
* @param {String} path - Ruta de la plantilla
|
2017-05-31 11:46:18 +00:00
|
|
|
* @param {String} language - Idioma
|
2017-05-30 06:06:14 +00:00
|
|
|
* @param {Object} params - Listado de parámetros a remplazar
|
|
|
|
* @param {Object} callback - Callback
|
|
|
|
*/
|
|
|
|
render: function(path, params, callback) {
|
|
|
|
fs.readFile(path, 'utf8', function(error, body) {
|
|
|
|
mustache.parse(body);
|
|
|
|
callback(mustache.render(body, params));
|
|
|
|
});
|
2017-05-31 11:46:18 +00:00
|
|
|
},
|
2017-05-30 06:06:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Template;
|