2014-02-28 15:25:50 -05:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
2014-11-08 23:36:38 -05:00
|
|
|
db = require('../database'),
|
2015-04-23 11:27:34 -04:00
|
|
|
utils = require('../../public/src/utils'),
|
|
|
|
|
plugins = require('../plugins');
|
2013-05-24 11:18:41 -04:00
|
|
|
|
2014-04-01 17:12:03 -04:00
|
|
|
module.exports = function(Categories) {
|
|
|
|
|
|
|
|
|
|
Categories.update = function(modified, callback) {
|
2013-06-24 17:18:58 -04:00
|
|
|
|
2014-02-28 15:25:50 -05:00
|
|
|
function updateCategory(cid, next) {
|
2015-01-18 14:31:37 -05:00
|
|
|
Categories.exists(cid, function(err, exists) {
|
|
|
|
|
if (err || !exists) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 08:31:21 +03:00
|
|
|
var modifiedFields = modified[cid];
|
|
|
|
|
|
2015-07-16 15:56:26 -04:00
|
|
|
if (modifiedFields.hasOwnProperty('name')) {
|
2015-07-05 08:31:21 +03:00
|
|
|
modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-16 15:56:26 -04:00
|
|
|
plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 20:10:44 +03:00
|
|
|
var category = categoryData.category;
|
2015-04-23 11:27:34 -04:00
|
|
|
var fields = Object.keys(category);
|
|
|
|
|
async.each(fields, function(key, next) {
|
|
|
|
|
updateCategoryField(cid, key, category[key], next);
|
2015-06-19 15:39:33 -04:00
|
|
|
}, function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
|
|
|
|
next();
|
|
|
|
|
});
|
2015-04-23 11:27:34 -04:00
|
|
|
});
|
2015-01-18 14:31:37 -05:00
|
|
|
});
|
2014-02-28 15:25:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cids = Object.keys(modified);
|
|
|
|
|
|
|
|
|
|
async.each(cids, updateCategory, function(err) {
|
|
|
|
|
callback(err, cids);
|
2013-06-24 17:18:58 -04:00
|
|
|
});
|
2013-05-24 11:18:41 -04:00
|
|
|
};
|
|
|
|
|
|
2014-11-15 22:41:45 -05:00
|
|
|
function updateCategoryField(cid, key, value, callback) {
|
2015-08-18 14:17:16 -04:00
|
|
|
if (key === 'parentCid') {
|
|
|
|
|
return updateParent(cid, value, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 22:41:45 -05:00
|
|
|
db.setObjectField('category:' + cid, key, value, function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 08:31:21 +03:00
|
|
|
if (key === 'order') {
|
2015-08-18 14:17:16 -04:00
|
|
|
updateOrder(cid, value, callback);
|
2014-11-15 22:41:45 -05:00
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 14:17:16 -04:00
|
|
|
function updateParent(cid, newParent, callback) {
|
2015-09-15 12:58:19 -04:00
|
|
|
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
|
|
|
|
|
return callback(new Error('[[error:cant-set-self-as-parent]]'));
|
|
|
|
|
}
|
2015-08-18 14:17:16 -04:00
|
|
|
Categories.getCategoryField(cid, 'parentCid', function(err, oldParent) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
function (next) {
|
|
|
|
|
oldParent = parseInt(oldParent, 10) || 0;
|
|
|
|
|
db.sortedSetRemove('cid:' + oldParent + ':children', cid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
newParent = parseInt(newParent, 10) || 0;
|
|
|
|
|
db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
|
|
|
|
|
}
|
|
|
|
|
], function(err, results) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateOrder(cid, order, callback) {
|
|
|
|
|
Categories.getCategoryField(cid, 'parentCid', function(err, parentCid) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.sortedSetAdd('categories:cid', order, cid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
parentCid = parseInt(parentCid, 10) || 0;
|
|
|
|
|
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
};
|