2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2015-09-15 15:56:19 -04:00
|
|
|
|
2019-08-13 10:36:48 -04:00
|
|
|
const categories = require('../../categories');
|
|
|
|
|
const analytics = require('../../analytics');
|
|
|
|
|
const plugins = require('../../plugins');
|
|
|
|
|
const translator = require('../../translator');
|
2016-03-22 16:47:30 +02:00
|
|
|
|
2019-08-13 10:36:48 -04:00
|
|
|
const categoriesController = module.exports;
|
2015-09-15 15:56:19 -04:00
|
|
|
|
2019-08-13 10:36:48 -04:00
|
|
|
categoriesController.get = async function (req, res, next) {
|
|
|
|
|
const [categoryData, parent, allCategories] = await Promise.all([
|
|
|
|
|
categories.getCategories([req.params.category_id], req.uid),
|
|
|
|
|
categories.getParents([req.params.category_id]),
|
2020-07-30 14:55:28 -04:00
|
|
|
categories.buildForSelectAll(),
|
2019-08-13 10:36:48 -04:00
|
|
|
]);
|
2015-09-15 15:56:19 -04:00
|
|
|
|
2019-08-13 10:36:48 -04:00
|
|
|
const category = categoryData[0];
|
|
|
|
|
if (!category) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
2017-05-29 14:47:01 -04:00
|
|
|
|
2019-08-13 10:36:48 -04:00
|
|
|
category.parent = parent[0];
|
|
|
|
|
allCategories.forEach(function (category) {
|
|
|
|
|
if (category) {
|
2020-07-30 14:55:28 -04:00
|
|
|
category.selected = parseInt(category.cid, 10) === parseInt(req.params.category_id, 10);
|
2019-08-13 10:36:48 -04:00
|
|
|
}
|
|
|
|
|
});
|
2020-07-30 14:55:28 -04:00
|
|
|
const selectedCategory = allCategories.find(c => c.selected);
|
2017-05-29 14:47:01 -04:00
|
|
|
|
2020-11-20 16:06:26 -05:00
|
|
|
const data = await plugins.hooks.fire('filter:admin.category.get', {
|
2019-08-13 10:36:48 -04:00
|
|
|
req: req,
|
|
|
|
|
res: res,
|
|
|
|
|
category: category,
|
|
|
|
|
customClasses: [],
|
|
|
|
|
allCategories: allCategories,
|
|
|
|
|
});
|
|
|
|
|
data.category.name = translator.escape(String(data.category.name));
|
2019-08-14 08:18:41 -04:00
|
|
|
data.category.description = translator.escape(String(data.category.description));
|
2019-02-04 13:54:48 -05:00
|
|
|
|
2019-08-13 10:36:48 -04:00
|
|
|
res.render('admin/manage/category', {
|
|
|
|
|
category: data.category,
|
2020-07-30 14:55:28 -04:00
|
|
|
categories: data.allCategories,
|
|
|
|
|
selectedCategory: selectedCategory,
|
2019-08-13 10:36:48 -04:00
|
|
|
customClasses: data.customClasses,
|
|
|
|
|
});
|
2015-09-15 15:56:19 -04:00
|
|
|
};
|
|
|
|
|
|
2020-07-29 11:57:20 -04:00
|
|
|
categoriesController.getAll = async function (req, res) {
|
2016-03-25 13:24:40 -04:00
|
|
|
// Categories list will be rendered on client side with recursion, etc.
|
2020-07-29 11:57:20 -04:00
|
|
|
const cids = await categories.getAllCidsFromSet('categories:cid');
|
|
|
|
|
const fields = [
|
2020-07-29 12:19:07 -04:00
|
|
|
'cid', 'name', 'icon', 'parentCid', 'disabled', 'link',
|
2020-07-29 11:57:20 -04:00
|
|
|
'color', 'bgColor', 'backgroundImage', 'imageClass',
|
|
|
|
|
];
|
|
|
|
|
const categoriesData = await categories.getCategoriesFields(cids, fields);
|
2020-11-20 16:06:26 -05:00
|
|
|
const result = await plugins.hooks.fire('filter:admin.categories.get', { categories: categoriesData, fields: fields });
|
2020-07-29 11:57:20 -04:00
|
|
|
const tree = categories.getTree(result.categories, 0);
|
|
|
|
|
res.render('admin/manage/categories', {
|
|
|
|
|
categories: tree,
|
|
|
|
|
});
|
2015-09-15 15:56:19 -04:00
|
|
|
};
|
|
|
|
|
|
2019-08-13 10:36:48 -04:00
|
|
|
categoriesController.getAnalytics = async function (req, res) {
|
|
|
|
|
const [name, analyticsData] = await Promise.all([
|
|
|
|
|
categories.getCategoryField(req.params.category_id, 'name'),
|
|
|
|
|
analytics.getCategoryAnalytics(req.params.category_id),
|
|
|
|
|
]);
|
|
|
|
|
res.render('admin/manage/category-analytics', {
|
|
|
|
|
name: name,
|
|
|
|
|
analytics: analyticsData,
|
|
|
|
|
});
|
2016-03-25 13:24:40 -04:00
|
|
|
};
|