diff --git a/lib/models/templates.js b/lib/models/templates.js index a6723eef..9bebb5b2 100644 --- a/lib/models/templates.js +++ b/lib/models/templates.js @@ -61,15 +61,17 @@ module.exports.create = (template, callback) => { let values = [name]; Object.keys(template).forEach(key => { - let value = template[key].trim(); + let value = template[key]; key = tools.toDbKey(key); + if (!allowedKeys.includes(key)) { + return; + } + value = value.trim(); if (key === 'description') { value = tools.purifyHTML(value); } - if (allowedKeys.indexOf(key) >= 0) { - keys.push(key); - values.push(value); - } + keys.push(key); + values.push(value); }); db.getConnection((err, connection) => { @@ -137,6 +139,16 @@ module.exports.update = (id, updates, callback) => { }); }; +module.exports.duplicate = (id, callback) => { + return this.get(id, (err, template) => { + if (!template) { + return callback(new Error(_('Template does not exist'))); + } + template.name = template.name + ' Copy'; + return this.create(template, callback); + }); +} + module.exports.delete = (id, callback) => { id = Number(id) || 0; diff --git a/routes/templates.js b/routes/templates.js index 3272df52..d91bdb43 100644 --- a/routes/templates.js +++ b/routes/templates.js @@ -136,6 +136,19 @@ router.post('/edit', passport.parseForm, passport.csrfProtection, (req, res) => }); }); +router.post('/duplicate', passport.parseForm, passport.csrfProtection, (req, res) => { + templates.duplicate(req.body.id, (err, duplicated) => { + if (err) { + req.flash('danger', err && err.message || err); + } else if (duplicated) { + req.flash('success', _('Template duplicated')); + } else { + req.flash('info', _('Could not duplicate specified template')); + } + return res.redirect('/templates/edit/' + duplicated); + }); +}); + router.post('/delete', passport.parseForm, passport.csrfProtection, (req, res) => { templates.delete(req.body.id, (err, deleted) => { if (err) { diff --git a/views/templates/edit.hbs b/views/templates/edit.hbs index 704b3a82..59e94d38 100644 --- a/views/templates/edit.hbs +++ b/views/templates/edit.hbs @@ -13,6 +13,11 @@ +
+