| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | var nconf = require('nconf'), | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	validator = require('validator'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 15:11:38 -04:00
										 |  |  | 	translator = require('../../public/src/modules/translator'), | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	categories = require('../categories'), | 
					
						
							| 
									
										
										
										
											2015-03-06 13:40:35 -05:00
										 |  |  | 	plugins = require('../plugins'), | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	meta = require('../meta'); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | var helpers = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 20:28:36 -05:00
										 |  |  | helpers.notFound = function(req, res, error) { | 
					
						
							| 
									
										
										
										
											2015-03-06 13:40:35 -05:00
										 |  |  | 	if (plugins.hasListeners('action:meta.override404')) { | 
					
						
							|  |  |  | 		plugins.fireHook('action:meta.override404', { | 
					
						
							|  |  |  | 			req: req, | 
					
						
							|  |  |  | 			res: res, | 
					
						
							|  |  |  | 			error: error | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} else if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2014-12-01 20:28:36 -05:00
										 |  |  | 		res.status(404).json({path: req.path.replace(/^\/api/, ''), error: error}); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-12-01 20:28:36 -05:00
										 |  |  | 		res.status(404).render('404', {path: req.path, error: error}); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 21:07:23 -05:00
										 |  |  | helpers.notAllowed = function(req, res, error) { | 
					
						
							| 
									
										
										
										
											2015-04-01 00:23:57 -04:00
										 |  |  | 	if (req.uid) { | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2015-04-01 00:23:57 -04:00
										 |  |  | 			res.status(403).json({path: req.path.replace(/^\/api/, ''), loggedIn: !!req.uid, error: error}); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-04-01 00:23:57 -04:00
										 |  |  | 			res.status(403).render('403', {path: req.path, loggedIn: !!req.uid, error: error}); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:16:17 -05:00
										 |  |  | 			req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, ''); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 			res.status(401).json('not-authorized'); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:16:17 -05:00
										 |  |  | 			req.session.returnTo = nconf.get('relative_path') + req.url; | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 			res.redirect(nconf.get('relative_path') + '/login'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | helpers.redirect = function(res, url) { | 
					
						
							|  |  |  | 	if (res.locals.isAPI) { | 
					
						
							|  |  |  | 		res.status(302).json(url); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-03-09 21:00:07 -04:00
										 |  |  | 		res.redirect(nconf.get('relative_path') + url); | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | helpers.buildCategoryBreadcrumbs = function(cid, callback) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	var breadcrumbs = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.whilst(function() { | 
					
						
							|  |  |  | 		return parseInt(cid, 10); | 
					
						
							|  |  |  | 	}, function(next) { | 
					
						
							| 
									
										
										
										
											2015-05-27 19:08:59 -04:00
										 |  |  | 		categories.getCategoryFields(cid, ['name', 'slug', 'parentCid', 'disabled'], function(err, data) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-27 19:08:59 -04:00
										 |  |  | 			if (!parseInt(data.disabled, 10)) { | 
					
						
							|  |  |  | 				breadcrumbs.unshift({ | 
					
						
							|  |  |  | 					text: validator.escape(data.name), | 
					
						
							|  |  |  | 					url: nconf.get('relative_path') + '/category/' + data.slug | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			cid = data.parentCid; | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 		breadcrumbs.unshift({ | 
					
						
							|  |  |  | 			text: '[[global:home]]', | 
					
						
							|  |  |  | 			url: nconf.get('relative_path') + '/' | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, breadcrumbs); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | helpers.buildBreadcrumbs = function(crumbs) { | 
					
						
							|  |  |  | 	var breadcrumbs = [ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text: '[[global:home]]', | 
					
						
							|  |  |  | 			url: nconf.get('relative_path') + '/' | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	crumbs.forEach(function(crumb) { | 
					
						
							|  |  |  | 		if (crumb) { | 
					
						
							|  |  |  | 			if (crumb.url) { | 
					
						
							|  |  |  | 				crumb.url = nconf.get('relative_path') + crumb.url; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			breadcrumbs.push(crumb); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return breadcrumbs; | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = helpers; |