|
|
|
@ -10,44 +10,51 @@ module.exports = function(Categories) {
|
|
|
|
|
|
|
|
|
|
Categories.update = function(modified, callback) {
|
|
|
|
|
|
|
|
|
|
function updateCategory(cid, next) {
|
|
|
|
|
Categories.exists(cid, function(err, exists) {
|
|
|
|
|
if (err || !exists) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
var cids = Object.keys(modified);
|
|
|
|
|
|
|
|
|
|
async.each(cids, function(cid, next) {
|
|
|
|
|
updateCategory(cid, modified[cid], next);
|
|
|
|
|
}, function(err) {
|
|
|
|
|
callback(err, cids);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var modifiedFields = modified[cid];
|
|
|
|
|
function updateCategory(cid, modifiedFields, callback) {
|
|
|
|
|
Categories.exists(cid, function(err, exists) {
|
|
|
|
|
if (err || !exists) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (modifiedFields.hasOwnProperty('name')) {
|
|
|
|
|
modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (modifiedFields.hasOwnProperty('name')) {
|
|
|
|
|
modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name);
|
|
|
|
|
plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
|
|
|
|
|
var category = categoryData.category;
|
|
|
|
|
var fields = Object.keys(category);
|
|
|
|
|
// move parent to front, so its updated first
|
|
|
|
|
var parentCidIndex = fields.indexOf('parentCid');
|
|
|
|
|
if (parentCidIndex !== -1 && fields.length > 1) {
|
|
|
|
|
fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.eachSeries(fields, function(key, next) {
|
|
|
|
|
updateCategoryField(cid, key, category[key], next);
|
|
|
|
|
}, function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var category = categoryData.category;
|
|
|
|
|
var fields = Object.keys(category);
|
|
|
|
|
async.each(fields, function(key, next) {
|
|
|
|
|
updateCategoryField(cid, key, category[key], next);
|
|
|
|
|
}, function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cids = Object.keys(modified);
|
|
|
|
|
|
|
|
|
|
async.each(cids, updateCategory, function(err) {
|
|
|
|
|
callback(err, cids);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateCategoryField(cid, key, value, callback) {
|
|
|
|
|
if (key === 'parentCid') {
|
|
|
|
|