2016-08-23 14:07:25 +02:00
|
|
|
// # Themes API
|
|
|
|
// RESTful API for Themes
|
2019-07-01 16:56:23 +02:00
|
|
|
const localUtils = require('./utils'),
|
2018-09-27 16:06:57 +02:00
|
|
|
common = require('../../lib/common'),
|
|
|
|
models = require('../../models'),
|
2019-07-01 16:56:23 +02:00
|
|
|
themeService = require('../../../frontend/services/themes');
|
2018-09-18 19:29:56 +05:30
|
|
|
|
|
|
|
let themes;
|
2016-08-23 14:07:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Themes API Methods
|
|
|
|
*
|
2017-12-14 14:13:40 +01:00
|
|
|
* **See:** [API Methods](constants.js.html#api%20methods)
|
2016-08-23 14:07:25 +02:00
|
|
|
*/
|
|
|
|
themes = {
|
2017-10-10 14:36:35 +02:00
|
|
|
/**
|
|
|
|
* Every role can browse all themes. The response contains a list of all available themes in your content folder.
|
|
|
|
* The active theme get's marked as `active:true` and contains an extra object `templates`, which
|
|
|
|
* contains the custom templates of the active theme. These custom templates are used to show a dropdown
|
|
|
|
* in the PSM to be able to choose a custom post template.
|
|
|
|
*/
|
2018-09-18 19:29:56 +05:30
|
|
|
browse(options) {
|
2017-12-13 22:14:19 +01:00
|
|
|
return localUtils
|
2017-12-11 22:47:46 +01:00
|
|
|
// Permissions
|
2017-03-20 18:02:44 +00:00
|
|
|
.handlePermissions('themes', 'browse')(options)
|
|
|
|
// Main action
|
2018-09-18 19:29:56 +05:30
|
|
|
.then(() => {
|
2017-03-20 18:02:44 +00:00
|
|
|
// Return JSON result
|
2019-07-09 16:35:18 +02:00
|
|
|
return themeService.getJSON();
|
2017-03-20 18:02:44 +00:00
|
|
|
});
|
2017-02-21 14:59:03 +00:00
|
|
|
},
|
|
|
|
|
2018-09-18 19:29:56 +05:30
|
|
|
activate(options) {
|
|
|
|
let themeName = options.name,
|
2017-03-08 10:46:03 +00:00
|
|
|
newSettings = [{
|
2017-04-24 19:41:00 +02:00
|
|
|
key: 'active_theme',
|
2017-03-08 10:46:03 +00:00
|
|
|
value: themeName
|
2019-07-01 16:56:23 +02:00
|
|
|
}];
|
2017-03-13 11:44:44 +00:00
|
|
|
|
2017-12-13 22:14:19 +01:00
|
|
|
return localUtils
|
2017-12-11 22:47:46 +01:00
|
|
|
// Permissions
|
2017-03-13 11:44:44 +00:00
|
|
|
.handlePermissions('themes', 'activate')(options)
|
2019-07-01 16:56:23 +02:00
|
|
|
// Validation & activation
|
2018-09-18 19:29:56 +05:30
|
|
|
.then(() => {
|
2019-07-01 16:56:23 +02:00
|
|
|
return themeService.activate(themeName);
|
2017-03-13 11:44:44 +00:00
|
|
|
})
|
2017-03-20 18:02:44 +00:00
|
|
|
// Update setting
|
2019-07-01 16:56:23 +02:00
|
|
|
.then((checkedTheme) => {
|
|
|
|
// @NOTE: We use the model, not the API here, as we don't want to trigger permissions
|
|
|
|
return models.Settings.edit(newSettings, options)
|
|
|
|
.then(() => checkedTheme);
|
2017-03-13 11:44:44 +00:00
|
|
|
})
|
2019-07-01 16:56:23 +02:00
|
|
|
.then((checkedTheme) => {
|
2017-03-20 18:02:44 +00:00
|
|
|
// Return JSON result
|
2019-07-09 16:35:18 +02:00
|
|
|
return themeService.getJSON(themeName, checkedTheme);
|
2017-03-13 11:44:44 +00:00
|
|
|
});
|
2017-03-08 10:46:03 +00:00
|
|
|
},
|
|
|
|
|
2018-09-18 19:29:56 +05:30
|
|
|
upload(options) {
|
2016-08-23 14:07:25 +02:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
// consistent filename uploads
|
|
|
|
options.originalname = options.originalname.toLowerCase();
|
|
|
|
|
2018-09-18 19:29:56 +05:30
|
|
|
let zip = {
|
2019-07-01 16:56:23 +02:00
|
|
|
path: options.path,
|
|
|
|
name: options.originalname
|
|
|
|
};
|
2016-08-23 14:07:25 +02:00
|
|
|
|
2017-12-13 22:14:19 +01:00
|
|
|
return localUtils
|
2018-08-12 16:31:08 +02:00
|
|
|
// Permissions
|
2017-03-20 18:02:44 +00:00
|
|
|
.handlePermissions('themes', 'add')(options)
|
|
|
|
// Validation
|
2018-09-18 19:29:56 +05:30
|
|
|
.then(() => {
|
2019-07-09 16:35:18 +02:00
|
|
|
return themeService.storage.setFromZip(zip);
|
2016-08-23 14:07:25 +02:00
|
|
|
})
|
2019-07-11 12:58:08 +05:30
|
|
|
.then(({theme}) => {
|
2019-02-05 23:38:40 +07:00
|
|
|
common.events.emit('theme.uploaded');
|
2019-07-01 16:56:23 +02:00
|
|
|
return theme;
|
2016-08-23 14:07:25 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-09-18 19:29:56 +05:30
|
|
|
download(options) {
|
2019-07-01 16:56:23 +02:00
|
|
|
let themeName = options.name;
|
2016-08-23 14:07:25 +02:00
|
|
|
|
2017-12-13 22:14:19 +01:00
|
|
|
return localUtils
|
2017-12-11 22:47:46 +01:00
|
|
|
// Permissions
|
2017-03-20 18:02:44 +00:00
|
|
|
.handlePermissions('themes', 'read')(options)
|
2018-09-18 19:29:56 +05:30
|
|
|
.then(() => {
|
2019-07-09 16:35:18 +02:00
|
|
|
return themeService.storage.getZip(themeName);
|
2016-08-23 14:07:25 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove theme zip
|
|
|
|
* remove theme folder
|
|
|
|
*/
|
2018-09-18 19:29:56 +05:30
|
|
|
destroy(options) {
|
2019-07-01 16:56:23 +02:00
|
|
|
let themeName = options.name;
|
2016-08-23 14:07:25 +02:00
|
|
|
|
2017-12-13 22:14:19 +01:00
|
|
|
return localUtils
|
2017-12-11 22:47:46 +01:00
|
|
|
// Permissions
|
2017-03-20 18:02:44 +00:00
|
|
|
.handlePermissions('themes', 'destroy')(options)
|
|
|
|
// Validation
|
2018-09-18 19:29:56 +05:30
|
|
|
.then(() => {
|
2019-07-09 16:35:18 +02:00
|
|
|
return themeService.storage.destroy(themeName);
|
2016-08-23 14:07:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = themes;
|