2014-11-08 23:54:21 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
|
|
|
|
db = require('../database'),
|
2014-11-11 22:58:34 -05:00
|
|
|
privileges = require('../privileges'),
|
2014-11-08 23:54:21 -05:00
|
|
|
utils = require('../../public/src/utils');
|
|
|
|
|
|
|
|
|
|
module.exports = function(Categories) {
|
|
|
|
|
|
|
|
|
|
Categories.create = function(data, callback) {
|
|
|
|
|
db.incrObjectField('global', 'nextCid', function(err, cid) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 12:31:08 -05:00
|
|
|
var slug = cid + '/' + utils.slugify(data.name),
|
|
|
|
|
order = data.order || cid; // If no order provided, place it at the end
|
2014-11-08 23:54:21 -05:00
|
|
|
|
|
|
|
|
var category = {
|
|
|
|
|
cid: cid,
|
|
|
|
|
name: data.name,
|
|
|
|
|
description: data.description,
|
|
|
|
|
icon: data.icon,
|
|
|
|
|
bgColor: data.bgColor,
|
|
|
|
|
color: data.color,
|
|
|
|
|
slug: slug,
|
|
|
|
|
parentCid: 0,
|
|
|
|
|
topic_count: 0,
|
|
|
|
|
post_count: 0,
|
|
|
|
|
disabled: 0,
|
2015-02-27 12:31:08 -05:00
|
|
|
order: order,
|
2014-11-08 23:54:21 -05:00
|
|
|
link: '',
|
|
|
|
|
numRecentReplies: 1,
|
|
|
|
|
class: 'col-md-3 col-xs-6',
|
|
|
|
|
imageClass: 'auto'
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-11 22:58:34 -05:00
|
|
|
var defaultPrivileges = ['find', 'read', 'topics:create', 'topics:reply'];
|
|
|
|
|
|
2014-11-08 23:54:21 -05:00
|
|
|
async.series([
|
|
|
|
|
async.apply(db.setObject, 'category:' + cid, category),
|
2015-02-27 12:31:08 -05:00
|
|
|
async.apply(db.sortedSetAdd, 'categories:cid', order, cid),
|
2014-11-11 22:58:34 -05:00
|
|
|
async.apply(privileges.categories.give, defaultPrivileges, cid, 'administrators'),
|
2014-11-17 12:39:07 -05:00
|
|
|
async.apply(privileges.categories.give, defaultPrivileges, cid, 'registered-users'),
|
|
|
|
|
async.apply(privileges.categories.give, ['find', 'read'], cid, 'guests')
|
2014-11-08 23:54:21 -05:00
|
|
|
], function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, category);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|