all files / src/categories/ update.js

16.92% Statements 11/65
2.78% Branches 1/36
12.5% Functions 3/24
16.92% Lines 11/65
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                                                                                                                                                                                                                                           
 
'use strict';
 
var async = require('async'),
	db = require('../database'),
	utils = require('../../public/src/utils'),
	translator = require('../../public/src/modules/translator'),
	plugins = require('../plugins');
 
module.exports = function(Categories) {
 
	Categories.update = function(modified, callback) {
 
		var cids = Object.keys(modified);
 
		async.each(cids, function(cid, next) {
			updateCategory(cid, modified[cid], next);
		}, function(err) {
			callback(err, cids);
		});
	};
 
	function updateCategory(cid, modifiedFields, callback) {
		Categories.exists(cid, function(err, exists) {
			if (err || !exists) {
				return callback(err);
			}
 
 
			if (modifiedFields.hasOwnProperty('name')) {
				translator.translate(modifiedFields.name, function(translated) {
					modifiedFields.slug = cid + '/' + utils.slugify(translated);
				});
			}
 
			plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) {
				if (err) {
					return callback(err);
				}
 
				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 callback(err);
					}
					plugins.fireHook('action:category.update', {cid: cid, modified: category});
					callback();
				});
			});
		});
	}
 
	function updateCategoryField(cid, key, value, callback) {
		if (key === 'parentCid') {
			return updateParent(cid, value, callback);
		}
 
		db.setObjectField('category:' + cid, key, value, function(err) {
			if (err) {
				return callback(err);
			}
 
			if (key === 'order') {
				updateOrder(cid, value, callback);
			} else if (key === 'description') {
				Categories.parseDescription(cid, value, callback);
			} else {
				callback();
			}
		});
	}
 
	function updateParent(cid, newParent, callback) {
		if (parseInt(cid, 10) === parseInt(newParent, 10)) {
			return callback(new Error('[[error:cant-set-self-as-parent]]'));
		}
		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) {
				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);
		});
	}
 
	Categories.parseDescription = function(cid, description, callback) {
		plugins.fireHook('filter:parse.raw', description, function(err, parsedDescription) {
			Iif (err) {
				return callback(err);
			}
			Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, callback);
		});
	};
 
};