2014-03-03 16:35:21 -05:00
"use strict" ;
var async = require ( 'async' ) ,
2014-03-13 23:43:36 -04:00
user = require ( '../user' ) ,
categories = require ( '../categories' ) ,
topics = require ( '../topics' ) ,
meta = require ( '../meta' ) ,
db = require ( '../database' ) ,
events = require ( '../events' ) ,
languages = require ( '../languages' ) ,
plugins = require ( '../plugins' ) ,
widgets = require ( '../widgets' ) ,
groups = require ( '../groups' ) ,
pkg = require ( '../../package.json' ) ,
2014-03-13 14:08:58 -04:00
validator = require ( 'validator' ) ;
2014-03-12 17:34:59 -04:00
2014-03-03 16:35:21 -05:00
var adminController = {
categories : { } ,
2014-08-17 19:26:24 -04:00
tags : { } ,
2014-03-03 16:35:21 -05:00
topics : { } ,
groups : { } ,
2014-09-25 13:12:51 -04:00
appearance : { } ,
2014-09-28 20:22:04 -04:00
extend : {
widgets : { }
} ,
2014-03-03 16:35:21 -05:00
events : { } ,
database : { } ,
plugins : { } ,
languages : { } ,
settings : { } ,
2014-03-12 17:34:59 -04:00
logger : { } ,
2014-03-17 10:37:11 -04:00
sounds : { } ,
2014-03-12 17:59:29 -04:00
users : require ( './admin/users' ) ,
uploads : require ( './admin/uploads' )
2014-03-03 16:35:21 -05:00
} ;
adminController . home = function ( req , res , next ) {
2014-07-14 14:28:19 -04:00
async . parallel ( {
stats : function ( next ) {
getStats ( next ) ;
} ,
notices : function ( next ) {
var notices = [
{ done : ! meta . restartRequired , doneText : 'Restart not required' , notDoneText : 'Restart required' } ,
{ done : plugins . hasListeners ( 'action:email.send' ) , doneText : 'Emailer Installed' , notDoneText : 'Emailer not installed' } ,
{ done : plugins . hasListeners ( 'filter:search.query' ) , doneText : 'Search Plugin Installed' , notDoneText : 'Search Plugin not installed' }
] ;
plugins . fireHook ( 'filter:admin.notices' , notices , next ) ;
}
} , function ( err , results ) {
2014-07-01 16:30:06 -04:00
if ( err ) {
return next ( err ) ;
}
2014-09-28 21:20:34 -04:00
res . render ( 'admin/general/dashboard' , {
2014-07-01 16:30:06 -04:00
version : pkg . version ,
2014-07-14 14:28:19 -04:00
notices : results . notices ,
2014-09-27 16:06:01 -04:00
stats : results . stats
2014-07-01 16:30:06 -04:00
} ) ;
2014-03-09 20:05:14 -04:00
} ) ;
2014-03-03 16:35:21 -05:00
} ;
2014-07-01 16:30:06 -04:00
function getStats ( callback ) {
async . parallel ( [
function ( next ) {
getStatsForSet ( 'ip:recent' , next ) ;
} ,
function ( next ) {
getStatsForSet ( 'users:joindate' , next ) ;
} ,
function ( next ) {
getStatsForSet ( 'posts:pid' , next ) ;
} ,
function ( next ) {
getStatsForSet ( 'topics:tid' , next ) ;
}
] , function ( err , results ) {
if ( err ) {
return callback ( err ) ;
}
results [ 0 ] . name = 'Unique Visitors' ;
results [ 1 ] . name = 'Users' ;
results [ 2 ] . name = 'Posts' ;
results [ 3 ] . name = 'Topics' ;
callback ( null , results ) ;
} ) ;
}
function getStatsForSet ( set , callback ) {
var terms = {
day : 86400000 ,
week : 604800000 ,
month : 2592000000
} ;
var now = Date . now ( ) ;
async . parallel ( {
day : function ( next ) {
db . sortedSetCount ( set , now - terms . day , now , next ) ;
} ,
week : function ( next ) {
db . sortedSetCount ( set , now - terms . week , now , next ) ;
} ,
month : function ( next ) {
db . sortedSetCount ( set , now - terms . month , now , next ) ;
} ,
alltime : function ( next ) {
db . sortedSetCount ( set , 0 , now , next ) ;
}
} , callback ) ;
}
2014-03-03 16:35:21 -05:00
adminController . categories . active = function ( req , res , next ) {
2014-03-13 14:08:58 -04:00
filterAndRenderCategories ( req , res , next , true ) ;
2014-03-03 16:35:21 -05:00
} ;
adminController . categories . disabled = function ( req , res , next ) {
2014-03-13 14:08:58 -04:00
filterAndRenderCategories ( req , res , next , false ) ;
} ;
function filterAndRenderCategories ( req , res , next , active ) {
2014-08-22 19:10:26 -04:00
var uid = req . user ? parseInt ( req . user . uid , 10 ) : 0 ;
categories . getAllCategories ( uid , function ( err , categoryData ) {
if ( err ) {
return next ( err ) ;
}
2014-05-27 12:44:28 -04:00
categoryData = categoryData . filter ( function ( category ) {
2014-09-23 13:34:49 -04:00
if ( ! category ) {
return false ;
}
2014-03-13 14:08:58 -04:00
return active ? ! category . disabled : category . disabled ;
} ) ;
2014-09-28 21:09:40 -04:00
res . render ( 'admin/manage/categories' , {
2014-09-17 16:07:26 -04:00
categories : categoryData ,
csrf : req . csrfToken ( )
} ) ;
2014-03-03 16:35:21 -05:00
} ) ;
2014-03-13 14:08:58 -04:00
}
2014-03-03 16:35:21 -05:00
2014-08-17 19:26:24 -04:00
adminController . tags . get = function ( req , res , next ) {
2014-08-27 18:03:53 -04:00
topics . getTags ( 0 , 99 , function ( err , tags ) {
2014-08-17 19:26:24 -04:00
if ( err ) {
return next ( err ) ;
}
2014-09-28 21:20:34 -04:00
res . render ( 'admin/manage/tags' , { tags : tags } ) ;
2014-08-17 19:26:24 -04:00
} ) ;
} ;
2014-03-03 16:35:21 -05:00
adminController . database . get = function ( req , res , next ) {
db . info ( function ( err , data ) {
2014-09-28 21:09:40 -04:00
res . render ( 'admin/advanced/database' , data ) ;
2014-03-03 16:35:21 -05:00
} ) ;
} ;
adminController . events . get = function ( req , res , next ) {
events . getLog ( function ( err , data ) {
if ( err || ! data ) {
return next ( err ) ;
}
2014-03-13 14:08:58 -04:00
2014-03-03 16:35:21 -05:00
data = data . toString ( ) . split ( '\n' ) . reverse ( ) . join ( '\n' ) ;
2014-09-28 21:09:40 -04:00
res . render ( 'admin/advanced/events' , {
2014-03-09 20:05:14 -04:00
eventdata : data
} ) ;
2014-03-03 16:35:21 -05:00
} ) ;
} ;
adminController . plugins . get = function ( req , res , next ) {
2014-04-22 21:02:58 -04:00
plugins . getAll ( function ( err , plugins ) {
2014-03-03 16:35:21 -05:00
if ( err || ! Array . isArray ( plugins ) ) {
plugins = [ ] ;
}
2014-09-28 21:09:40 -04:00
res . render ( 'admin/extend/plugins' , {
2014-03-09 20:05:14 -04:00
plugins : plugins
} ) ;
2014-04-22 21:02:58 -04:00
} )
2014-03-03 16:35:21 -05:00
} ;
adminController . languages . get = function ( req , res , next ) {
languages . list ( function ( err , languages ) {
2014-09-28 21:09:40 -04:00
res . render ( 'admin/general/languages' , {
2014-03-09 20:05:14 -04:00
languages : languages
} ) ;
2014-03-03 16:35:21 -05:00
} ) ;
} ;
adminController . settings . get = function ( req , res , next ) {
2014-09-24 14:47:58 -04:00
var term = req . params . term ? req . params . term : 'general' ;
res . render ( 'admin/settings/' + term , {
2014-09-14 10:21:32 -04:00
'csrf' : req . csrfToken ( )
} ) ;
2014-03-03 16:35:21 -05:00
} ;
adminController . logger . get = function ( req , res , next ) {
2014-09-28 21:09:40 -04:00
res . render ( 'admin/development/logger' , { } ) ;
2014-03-03 16:35:21 -05:00
} ;
2014-09-25 13:12:51 -04:00
adminController . appearance . get = function ( req , res , next ) {
var term = req . params . term ? req . params . term : 'themes' ;
2014-09-28 20:22:04 -04:00
res . render ( 'admin/appearance/' + term , { } ) ;
2014-09-25 13:12:51 -04:00
} ;
2014-09-28 20:22:04 -04:00
adminController . extend . widgets = function ( req , res , next ) {
2014-03-03 16:35:21 -05:00
async . parallel ( {
areas : function ( next ) {
2014-03-31 12:27:24 -04:00
var defaultAreas = [
{ name : 'Global Sidebar' , template : 'global' , location : 'sidebar' } ,
2014-05-28 16:30:16 -04:00
{ name : 'Global Header' , template : 'global' , location : 'header' } ,
2014-03-31 12:27:24 -04:00
{ name : 'Global Footer' , template : 'global' , location : 'footer' } ,
] ;
plugins . fireHook ( 'filter:widgets.getAreas' , defaultAreas , next ) ;
2014-03-03 16:35:21 -05:00
} ,
widgets : function ( next ) {
plugins . fireHook ( 'filter:widgets.getWidgets' , [ ] , next ) ;
}
} , function ( err , widgetData ) {
2014-06-16 14:08:42 -04:00
widgetData . areas . push ( { name : 'Draft Zone' , template : 'global' , location : 'drafts' } ) ;
2014-03-31 16:19:57 -04:00
2014-03-03 16:35:21 -05:00
async . each ( widgetData . areas , function ( area , next ) {
widgets . getArea ( area . template , area . location , function ( err , areaData ) {
area . data = areaData ;
next ( err ) ;
} ) ;
} , function ( err ) {
for ( var w in widgetData . widgets ) {
if ( widgetData . widgets . hasOwnProperty ( w ) ) {
2014-04-16 21:02:25 -04:00
// if this gets anymore complicated, it needs to be a template
widgetData . widgets [ w ] . content += "<br /><label>Title:</label><input type=\"text\" class=\"form-control\" name=\"title\" placeholder=\"Title (only shown on some containers)\" /><br /><label>Container:</label><textarea rows=\"4\" class=\"form-control container-html\" name=\"container\" placeholder=\"Drag and drop a container or enter HTML here.\"></textarea><div class=\"checkbox\"><label><input name=\"registered-only\" type=\"checkbox\"> Hide from anonymous users?</label></div>" ;
2014-03-03 16:35:21 -05:00
}
}
2014-06-11 16:42:07 -04:00
var templates = [ ] ,
list = { } , index = 0 ;
widgetData . areas . forEach ( function ( area ) {
if ( typeof list [ area . template ] === 'undefined' ) {
list [ area . template ] = index ;
templates . push ( {
template : area . template ,
areas : [ ]
} ) ;
index ++ ;
}
templates [ list [ area . template ] ] . areas . push ( {
name : area . name ,
location : area . location
} ) ;
} ) ;
2014-09-28 20:22:04 -04:00
res . render ( 'admin/extend/widgets' , {
2014-06-11 16:42:07 -04:00
templates : templates ,
2014-03-03 16:35:21 -05:00
areas : widgetData . areas ,
2014-09-25 12:41:16 -04:00
widgets : widgetData . widgets
2014-03-09 20:05:14 -04:00
} ) ;
2014-03-03 16:35:21 -05:00
} ) ;
} ) ;
2014-09-28 20:22:04 -04:00
} ;
2014-03-03 16:35:21 -05:00
adminController . groups . get = function ( req , res , next ) {
2014-03-19 10:27:02 -04:00
groups . list ( {
expand : true ,
showSystemGroups : true ,
truncateUserList : true
} , function ( err , groups ) {
2014-06-12 18:53:58 -04:00
groups = groups . filter ( function ( group ) {
return group . name !== 'registered-users' && group . name !== 'guests' ;
} ) ;
2014-09-28 21:09:40 -04:00
res . render ( 'admin/manage/groups' , {
2014-03-09 20:05:14 -04:00
groups : groups ,
yourid : req . user . uid
} ) ;
2014-03-03 16:35:21 -05:00
} ) ;
} ;
2014-03-17 10:37:11 -04:00
adminController . sounds . get = function ( req , res , next ) {
2014-04-12 18:33:52 -04:00
meta . sounds . getFiles ( function ( err , sounds ) {
2014-03-17 10:37:11 -04:00
sounds = Object . keys ( sounds ) . map ( function ( name ) {
return {
name : name
} ;
} ) ;
2014-09-28 21:09:40 -04:00
res . render ( 'admin/general/sounds' , {
2014-03-17 10:37:11 -04:00
sounds : sounds
} ) ;
} ) ;
} ;
2014-04-10 20:31:57 +01:00
module . exports = adminController ;