diff --git a/lib/models/fields.js b/lib/models/fields.js index 52b40310..ef86f342 100644 --- a/lib/models/fields.js +++ b/lib/models/fields.js @@ -24,6 +24,7 @@ module.exports.types = { 'date-eur': 'Date (DD/MM/YYYY)', 'birthday-us': 'Birthday (MM/DD)', 'birthday-eur': 'Birthday (DD/MM)', + json: 'JSON value for custom rendering', option: 'Option' }; @@ -34,6 +35,7 @@ module.exports.genericTypes = { website: 'string', longtext: 'textarea', gpg: 'textarea', + json: 'textarea', number: 'number', 'date-us': 'date', 'date-eur': 'date', @@ -106,7 +108,7 @@ module.exports.get = (id, callback) => { return callback(err); } let field = rows && rows[0] && tools.convertKeys(rows[0]) || false; - field.isGroup = module.exports.grouped.includes(field.type); + field.isGroup = module.exports.grouped.includes(field.type) || field.type === 'json'; return callback(null, field); }); }); @@ -329,6 +331,7 @@ function addCustomField(listId, name, defaultValue, type, group, groupTemplate, break; case 'gpg': case 'longtext': + case 'json': query = 'ALTER TABLE `subscription__' + listId + '` ADD COLUMN `' + column + '` TEXT DEFAULT NULL'; break; case 'number': @@ -412,6 +415,35 @@ module.exports.getRow = (fieldList, values, useDate, showAll, onlyExisting) => { row.push(item); break; } + case 'json': + { + let value; + let json = (valueList[field.column] || '').toString().trim(); + try { + let parsed = JSON.parse(json); + if (Array.isArray(parsed)) { + parsed = { + values: parsed + }; + } + value = json ? render(field.groupTemplate, parsed) : ''; + } catch (E) { + value = E.message; + } + + let item = { + type: field.type, + name: field.name, + column: field.column, + value: (valueList[field.column] || '').toString().trim(), + visible: !!field.visible, + mergeTag: field.key, + mergeValue: value || field.defaultValue, + ['type' + (field.type || '').toString().trim().replace(/(?:^|\-)([a-z])/g, (m, c) => c.toUpperCase())]: true + }; + row.push(item); + break; + } case 'number': { let item = { diff --git a/views/lists/fields/create.hbs b/views/lists/fields/create.hbs index 04852fff..04e58aa6 100644 --- a/views/lists/fields/create.hbs +++ b/views/lists/fields/create.hbs @@ -28,6 +28,7 @@ + @@ -67,10 +68,10 @@
- +
- For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from \{{values}} array, for example \{{#each values}} \{{this}} \{{/each}}. If template is not defined then multiple values are joined with commas. + For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from \{{values}} array, for example \{{#each values}} \{{this}} \{{/each}}. If template is not defined then multiple values are joined with commas. You can also use this template to render JSON values (if the JSON is an array then the array is exposed as values, otherwise you can access the JSON keys directly).
diff --git a/views/lists/fields/edit.hbs b/views/lists/fields/edit.hbs index da99e051..33509a99 100644 --- a/views/lists/fields/edit.hbs +++ b/views/lists/fields/edit.hbs @@ -35,6 +35,7 @@ + @@ -81,10 +82,10 @@ {{#if field.isGroup}}
- +
- For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from \{{values}} array, for example \{{#each values}} \{{this}} \{{/each}}. If template is not defined then multiple values are joined with commas. + For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from \{{values}} array, for example \{{#each values}} \{{this}} \{{/each}}. If template is not defined then multiple values are joined with commas. You can also use this template to render JSON values (if the JSON is an array then the array is exposed as values, otherwise you can access the JSON keys directly).
diff --git a/views/lists/subscription/add.hbs b/views/lists/subscription/add.hbs index 9d58af1a..59acd202 100644 --- a/views/lists/subscription/add.hbs +++ b/views/lists/subscription/add.hbs @@ -54,6 +54,10 @@ {{/if}} + {{#if typeJson}} + + {{/if}} + {{#if typeGpg}} Insert a GPG public key that will be used to encrypt messages sent this subscriber diff --git a/views/lists/subscription/edit.hbs b/views/lists/subscription/edit.hbs index e45ef648..9821089e 100644 --- a/views/lists/subscription/edit.hbs +++ b/views/lists/subscription/edit.hbs @@ -67,6 +67,10 @@ {{/if}} + {{#if typeJson}} + + {{/if}} + {{#if typeGpg}} Insert a GPG public key that will be used to encrypt messages sent this subscriber diff --git a/views/subscription/manage.hbs b/views/subscription/manage.hbs index 2d025369..8e278905 100644 --- a/views/subscription/manage.hbs +++ b/views/subscription/manage.hbs @@ -48,6 +48,10 @@ {{/if}} + {{#if typeJson}} + + {{/if}} + {{#if typeGpg}} {{#if ../hasPubkey}}
diff --git a/views/subscription/subscribe.hbs b/views/subscription/subscribe.hbs index 567daf2b..6cc7eeb2 100644 --- a/views/subscription/subscribe.hbs +++ b/views/subscription/subscribe.hbs @@ -45,6 +45,10 @@ {{/if}} + {{#if typeJson}} + + {{/if}} + {{#if typeGpg}} {{#if ../hasPubkey}}