| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-01 15:02:17 +03:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2014-06-10 17:48:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-01 15:02:17 +03:00
										 |  |  | var db = require('../../database'); | 
					
						
							|  |  |  | var groups = require('../../groups'); | 
					
						
							|  |  |  | var categories = require('../../categories'); | 
					
						
							|  |  |  | var privileges = require('../../privileges'); | 
					
						
							|  |  |  | var plugins = require('../../plugins'); | 
					
						
							|  |  |  | var Categories = {}; | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Categories.create = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-04-01 15:02:17 +03:00
										 |  |  | 	if (!data) { | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	categories.create(data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | Categories.getAll = function(socket, data, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 		async.apply(db.getSortedSetRange, 'categories:cid', 0, -1), | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 		async.apply(categories.getCategoriesData), | 
					
						
							|  |  |  | 		function(categories, next) { | 
					
						
							|  |  |  | 			//Hook changes, there is no req, and res
 | 
					
						
							|  |  |  | 			plugins.fireHook('filter:admin.categories.get', {categories: categories}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function(result, next){ | 
					
						
							|  |  |  | 			next(null, categories.getTree(result.categories, 0)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	], function(err, categoriesTree) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, categoriesTree); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 18:19:51 -04:00
										 |  |  | Categories.getNames = function(socket, data, callback) { | 
					
						
							|  |  |  | 	categories.getAllCategoryFields(['cid', 'name'], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-10 17:48:48 -04:00
										 |  |  | Categories.purge = function(socket, cid, callback) { | 
					
						
							| 
									
										
										
										
											2016-02-25 18:01:59 +02:00
										 |  |  | 	categories.purge(cid, socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-06-10 17:48:48 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | Categories.update = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-04-01 15:02:17 +03:00
										 |  |  | 	if (!data) { | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	categories.update(data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.setPrivilege = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 11:52:34 -04:00
										 |  |  | 	if (Array.isArray(data.privilege)) { | 
					
						
							|  |  |  | 		async.each(data.privilege, function(privilege, next) { | 
					
						
							|  |  |  | 			groups[data.set ? 'join' : 'leave']('cid:' + data.cid + ':privileges:' + privilege, data.member, next); | 
					
						
							|  |  |  | 		}, callback); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		groups[data.set ? 'join' : 'leave']('cid:' + data.cid + ':privileges:' + data.privilege, data.member, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getPrivilegeSettings = function(socket, cid, callback) { | 
					
						
							| 
									
										
										
										
											2015-03-25 15:42:15 -04:00
										 |  |  | 	privileges.categories.list(cid, callback); | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 15:47:09 +02:00
										 |  |  | Categories.copyPrivilegesToChildren = function(socket, cid, callback) { | 
					
						
							| 
									
										
										
										
											2016-05-29 21:52:13 +03:00
										 |  |  | 	categories.getCategories([cid], socket.uid, function(err, categories) {		 | 
					
						
							| 
									
										
										
										
											2016-01-12 15:47:09 +02:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-29 21:52:13 +03:00
										 |  |  | 		var category = categories[0]; | 
					
						
							| 
									
										
										
										
											2016-01-12 15:47:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.eachSeries(category.children, function(child, next) { | 
					
						
							| 
									
										
										
										
											2016-05-29 21:52:13 +03:00
										 |  |  | 			copyPrivilegesToChildrenRecursive(cid, child, next); | 
					
						
							| 
									
										
										
										
											2016-01-12 15:47:09 +02:00
										 |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 21:52:13 +03:00
										 |  |  | function copyPrivilegesToChildrenRecursive(parentCid, category, callback) { | 
					
						
							|  |  |  | 	categories.copyPrivilegesFrom(parentCid, category.cid, function(err) { | 
					
						
							| 
									
										
										
										
											2016-01-12 15:47:09 +02:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		async.eachSeries(category.children, function(child, next) { | 
					
						
							| 
									
										
										
										
											2016-05-29 21:52:13 +03:00
										 |  |  | 			copyPrivilegesToChildrenRecursive(parentCid, child, next); | 
					
						
							| 
									
										
										
										
											2016-01-12 15:47:09 +02:00
										 |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-01 15:02:17 +03:00
										 |  |  | Categories.copySettingsFrom = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-05-29 22:27:37 +03:00
										 |  |  | 	categories.copySettingsFrom(data.fromCid, data.toCid, true, callback); | 
					
						
							| 
									
										
										
										
											2016-04-01 15:02:17 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 12:59:33 +03:00
										 |  |  | Categories.copyPrivilegesFrom = function(socket, data, callback) { | 
					
						
							|  |  |  | 	categories.copyPrivilegesFrom(data.fromCid, data.toCid, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | module.exports = Categories; |