From 633463108e4ecd39d28c0e2ee5dfbfac567af238 Mon Sep 17 00:00:00 2001 From: witzig Date: Sat, 4 Mar 2017 18:37:41 +0100 Subject: [PATCH] Merge tag reference abstraction/partial MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Perhaps helpers.js would be a more suitable place for getDefaultMergeTags() and getListMergeTags() … ? --- lib/tools.js | 72 +++++++++++++++++++++++ routes/campaigns.js | 81 +++++--------------------- routes/templates.js | 21 +++++-- views/campaigns/edit-rss.hbs | 46 +-------------- views/campaigns/edit-triggered.hbs | 38 +----------- views/campaigns/edit.hbs | 38 +----------- views/partials/merge-tag-reference.hbs | 43 ++++++++++++++ views/templates/edit.hbs | 48 +-------------- 8 files changed, 149 insertions(+), 238 deletions(-) create mode 100644 views/partials/merge-tag-reference.hbs diff --git a/lib/tools.js b/lib/tools.js index 41c8b782..b1aecb72 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -20,6 +20,8 @@ module.exports = { formatMessage, getMessageLinks, prepareHtml, + getDefaultMergeTags, + getListMergeTags, workers: new Set() }; @@ -222,3 +224,73 @@ function prepareHtml(html, callback) { return callback(null, juice(preparedHtml)); }); } + +function getDefaultMergeTags(callback) { + // Using a callback for the sake of future-proofness + callback(null, [ + { + key: 'LINK_UNSUBSCRIBE', + value: 'URL that points to the unsubscribe page' + }, { + key: 'LINK_PREFERENCES', + value: 'URL that points to the preferences page of the subscriber' + }, { + key: 'LINK_BROWSER', + value: 'URL to preview the message in a browser' + }, { + key: 'EMAIL', + value: 'Email address' + }, { + key: 'FIRST_NAME', + value: 'First name' + }, { + key: 'LAST_NAME', + value: 'Last name' + }, { + key: 'FULL_NAME', + value: 'Full name (first and last name combined)' + }, { + key: 'SUBSCRIPTION_ID', + value: 'Unique ID that identifies the recipient' + }, { + key: 'LIST_ID', + value: 'Unique ID that identifies the list used for this campaign' + }, { + key: 'CAMPAIGN_ID', + value: 'Unique ID that identifies current campaign' + } + ]); +} + +function getListMergeTags(listId, callback) { + let lists = require('./models/lists'); + let fields = require('./models/fields'); + + lists.get(listId, (err, list) => { + if (err) { + return callback(err); + } + if (!list) { + list = { + id: listId + }; + } + + fields.list(list.id, (err, fieldList) => { + if (err && !fieldList) { + fieldList = []; + } + + let mergeTags = []; + + fieldList.forEach(field => { + mergeTags.push({ + key: field.key, + value: field.name + }); + }); + + return callback(null, mergeTags); + }); + }); +} diff --git a/routes/campaigns.js b/routes/campaigns.js index e5d715a9..4a653cf6 100644 --- a/routes/campaigns.js +++ b/routes/campaigns.js @@ -184,76 +184,25 @@ router.get('/edit/:id', passport.csrfProtection, (req, res, next) => { view = 'campaigns/edit'; } - let getList = (listId, callback) => { - lists.get(listId, (err, list) => { - if (err) { - return callback(err); - } - if (!list) { - list = { - id: listId - }; - } - - fields.list(list.id, (err, fieldList) => { - if (err && !fieldList) { - fieldList = []; - } - - let mergeTags = [ - // keep indentation - { - key: 'LINK_UNSUBSCRIBE', - value: 'URL that points to the preferences page of the subscriber' - }, { - key: 'LINK_PREFERENCES', - value: 'URL that points to the unsubscribe page' - }, { - key: 'LINK_BROWSER', - value: 'URL to preview the message in a browser' - }, { - key: 'EMAIL', - value: 'Email address' - }, { - key: 'FIRST_NAME', - value: 'First name' - }, { - key: 'LAST_NAME', - value: 'Last name' - }, { - key: 'FULL_NAME', - value: 'Full name (first and last name combined)' - }, { - key: 'SUBSCRIPTION_ID', - value: 'Unique ID that identifies the recipient' - }, { - key: 'LIST_ID', - value: 'Unique ID that identifies the list used for this campaign' - }, { - key: 'CAMPAIGN_ID', - value: 'Unique ID that identifies current campaign' - } - ]; - - fieldList.forEach(field => { - mergeTags.push({ - key: field.key, - value: field.name - }); - }); - - return callback(null, list, mergeTags); - }); - }); - }; - - getList(campaign.list, (err, list, mergeTags) => { + tools.getDefaultMergeTags((err, defaultMergeTags) => { if (err) { req.flash('danger', err.message || err); return res.redirect('/'); } - campaign.mergeTags = mergeTags; - res.render(view, campaign); + + tools.getListMergeTags(campaign.list, (err, listMergeTags) => { + if (err) { + req.flash('danger', err.message || err); + return res.redirect('/'); + } + + campaign.mergeTags = defaultMergeTags.concat(listMergeTags); + campaign.type === 2 && campaign.mergeTags.push({ + key: 'RSS_ENTRY', + value: 'content from an RSS entry' + }); + res.render(view, campaign); + }); }); }); }); diff --git a/routes/templates.js b/routes/templates.js index 576d446a..778f4cb4 100644 --- a/routes/templates.js +++ b/routes/templates.js @@ -111,12 +111,21 @@ router.get('/edit/:id', passport.csrfProtection, (req, res, next) => { if (err) { return next(err); } - template.csrfToken = req.csrfToken(); - template.useEditor = true; - template.editorName = template.editorName || 'summernote'; - template.editorConfig = config[template.editorName]; - template.disableWysiwyg = configItems.disableWysiwyg; - res.render('templates/edit', template); + + tools.getDefaultMergeTags((err, defaultMergeTags) => { + if (err) { + req.flash('danger', err.message || err); + return res.redirect('/templates'); + } + + template.mergeTags = defaultMergeTags; + template.csrfToken = req.csrfToken(); + template.useEditor = true; + template.editorName = template.editorName || 'summernote'; + template.editorConfig = config[template.editorName]; + template.disableWysiwyg = configItems.disableWysiwyg; + res.render('templates/edit', template); + }); }); }); }); diff --git a/views/campaigns/edit-rss.hbs b/views/campaigns/edit-rss.hbs index 466fb021..c8efcc2b 100644 --- a/views/campaigns/edit-rss.hbs +++ b/views/campaigns/edit-rss.hbs @@ -71,51 +71,7 @@ -
-
- -
-

- Merge tags are tags that are replaced before sending out the message. The format of the merge tag is the following: [TAG_NAME] or [TAG_NAME/fallback] where fallback is an optional text value - used when TAG_NAME is empty. -

- - - - - - - - - - - - - - - {{#each mergeTags}} - - - - - {{/each}} - -
- Merge tag - - Description -
- [RSS_ENTRY] - - content from an RSS entry -
- [{{key}}] - - {{value}} -
-
-
-
+ {{> merge_tag_reference}}
diff --git a/views/campaigns/edit-triggered.hbs b/views/campaigns/edit-triggered.hbs index b4e575db..3cdeec96 100644 --- a/views/campaigns/edit-triggered.hbs +++ b/views/campaigns/edit-triggered.hbs @@ -129,43 +129,7 @@
{{else}} - -
-
- -
-

- Merge tags are tags that are replaced before sending out the message. The format of the merge tag is the following: [TAG_NAME] or [TAG_NAME/fallback] where fallback is an optional - text value used when TAG_NAME is empty. -

- - - - - - - - - - {{#each mergeTags}} - - - - - {{/each}} - -
- Merge tag - - Description -
- [{{key}}] - - {{value}} -
-
-
-
+ {{> merge_tag_reference}} {{> plaintext}} diff --git a/views/campaigns/edit.hbs b/views/campaigns/edit.hbs index b4df6a0d..3b747608 100644 --- a/views/campaigns/edit.hbs +++ b/views/campaigns/edit.hbs @@ -145,43 +145,7 @@ {{else}} - -
-
- -
-

- Merge tags are tags that are replaced before sending out the message. The format of the merge tag is the following: [TAG_NAME] or [TAG_NAME/fallback] where fallback is an optional - text value used when TAG_NAME is empty. -

- - - - - - - - - - {{#each mergeTags}} - - - - - {{/each}} - -
- Merge tag - - Description -
- [{{key}}] - - {{value}} -
-
-
-
+ {{> merge_tag_reference}} {{> plaintext}} diff --git a/views/partials/merge-tag-reference.hbs b/views/partials/merge-tag-reference.hbs new file mode 100644 index 00000000..392d12c4 --- /dev/null +++ b/views/partials/merge-tag-reference.hbs @@ -0,0 +1,43 @@ +{{#if mergeTags}} +
+
+ +
+

+ Merge tags are tags that are replaced before sending out the message. The format of the merge tag is the following: [TAG_NAME] or [TAG_NAME/fallback] where fallback is an optional text value + used when TAG_NAME is empty. +

+ + + + + + + + + + {{#each mergeTags}} + + + + + {{/each}} + +
+ Merge tag + + Description +
+ [{{key}}] + + {{value}} +
+ + {{#if mergeTagReferenceFooterText}} +

{{mergeTagReferenceFooterText}}

+ {{/if}} + +
+
+
+{{/if}} diff --git a/views/templates/edit.hbs b/views/templates/edit.hbs index 019ee274..ad4677a8 100644 --- a/views/templates/edit.hbs +++ b/views/templates/edit.hbs @@ -24,53 +24,7 @@ -
-
- -
-

- Merge tags are tags that are replaced before sending out the message. The format of the merge tag is the following: [TAG_NAME] or [TAG_NAME/fallback] where fallback is an optional text value used - when TAG_NAME is empty. -

- -
    -
  • - [EMAIL] – email address of the subscriber -
  • -
  • - [FIRST_NAME] – first name of the subscriber -
  • -
  • - [LAST_NAME] – last name of the subscriber -
  • -
  • - [FULL_NAME] – first and last names of the subscriber joined -
  • -
  • - [LINK_UNSUBSCRIBE] – URL that points to the preferences page of the subscriber -
  • -
  • - [LINK_PREFERENCES] – URL that points to the unsubscribe page -
  • -
  • - [LINK_BROWSER] – URL to preview the message in a browser -
  • -
  • - [SUBSCRIPTION_ID] – Unique ID that identifies the recipient -
  • -
  • - [LIST_ID] – Unique ID that identifies the list used for this campaign -
  • -
  • - [CAMPAIGN_ID] – Unique ID that identifies current campaign -
  • -
-

- In addition to that any custom field can have its own merge tag. -

-
-
-
+ {{> merge_tag_reference mergeTagReferenceFooterText='In addition to that any custom field can have its own merge tag.'}} {{> plaintext}}