Last active
December 12, 2015 22:14
-
-
Save alonronin/27915c8d3f6ae115f3ff to your computer and use it in GitHub Desktop.
Ronin CMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var app = require('./app'); | |
var models = require('./models'); | |
var async = require('async'); | |
var _ = require('lodash'); | |
/* | |
middle-wares | |
*/ | |
var config = models.config.middleware(); | |
var crumbs = models.navigation.crumbs(); | |
var menu = models.navigation.menu(); | |
var languages = models.language.middlware(); | |
/* | |
Search for a post with current url: | |
res.locals.post | |
res.locals.page | |
if not than a navigation item: | |
res.locals.page | |
*/ | |
var getByUrl = function(req, res, next) { | |
var params = req.params[0]; | |
models.navigation | |
.findOne() | |
.where('url', params) | |
.where('show', true) | |
.populate('language') | |
.lean() | |
.exec(function (err, page) { | |
if(err) return next(err); | |
if(page){ | |
res.locals.page = page; | |
res.locals.language = req.session.language = page.language; | |
return next(); | |
} | |
models.posts.findOne() | |
.where('url', params) | |
.where('show', true) | |
.populate('navigation') | |
.lean() | |
.exec(function (err, post) { | |
if (err) return next(err); | |
if (post && post.navigation) { | |
res.locals.page = post.navigation; | |
res.locals.page.post = _.omit(post, 'navigation'); | |
} | |
return next(); | |
}); | |
}); | |
}; | |
var pageModels = function(req, res, next){ | |
var arr = [], | |
page = res.locals.page; | |
arr.push(models.posts.latest()); | |
if(page) { | |
switch(page.template){ | |
case 'homepage': | |
arr.push(models.homepage.fetch()); | |
break; | |
case 'posts': | |
arr.push(models.posts.byNavigationId()); | |
break; | |
} | |
} | |
async.each(arr, function(item, cb){ | |
item(req, res, cb); | |
}, function(err){ | |
next(err); | |
}); | |
}; | |
// Rewriter | |
//require('./rewrite').forEach(function(rule){ | |
// app.get(encodeURI(rule.route), function(req, res){ | |
// res.redirect(rule.status, rule.redirect); | |
// }) | |
//}); | |
// CMS rule | |
app.get('*', [languages, config, getByUrl, crumbs, menu, pageModels], function (req, res, next) { | |
if (!res.locals.page) | |
return next(); | |
res.render(res.locals.page.template || 'index'); | |
}); | |
// catch 404 and forward to error handler | |
app.use(function (req, res, next) { | |
res.locals.page || (res.locals.page = {}); | |
var config = (res.locals.config && res.locals.config._404) || {}; | |
_.assign(res.locals.page, { | |
title: config.title || 'Not Found', | |
content: config.content || 'The requested document was not found on this server.' | |
}); | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var Types = Schema.Types; | |
var _ = require('lodash'); | |
var schema = new Schema({ | |
language: { type: String, ref: 'language', default: 'he', hide: true } | |
}); | |
/* | |
Return site config and some other: | |
res.locals.config | |
res.locals.http_params | |
*/ | |
schema.statics.middleware = function() { | |
var config = this; | |
return function(req, res, next) { | |
var language = res.locals.language; | |
var q = config.findOne().where('language', language._id).lean().exec(); | |
q.then(function(config) { | |
res.locals.http_params = { | |
query: req.query, | |
headers: req.headers, | |
body: req.body, | |
url: req.url, | |
debug: req.app.get('env') == 'development' | |
}; | |
_.assign(config.site, req.session.config); | |
res.locals.config = config; | |
next(); | |
}).end(next); | |
}; | |
}; | |
schema.formage = { | |
list: ['language'], | |
list_populate: ['language'], | |
section: 'Configuration' | |
}; | |
module.exports = schema; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var Types = Schema.Types; | |
var _ = require('lodash'); | |
var schema = new Schema({ | |
_id: { type: String, unique: true }, | |
name: { type: String, unique: true }, | |
url: { type: String }, | |
order: { type: Number, editable: false, default: 0 }, | |
active: { type: Boolean, default: true } | |
}); | |
schema.methods.toString = function(){ | |
return this.name; | |
}; | |
schema.statics.middlware = function(){ | |
var language = this; | |
return function(req, res, next){ | |
language.find({ active: true }) | |
.sort({ order: 1 }) | |
.lean() | |
.exec(function(err, items) { | |
if(items) { | |
res.locals.language = req.session.language || items[0]; | |
res.locals.languages = { items: items }; | |
} | |
next(err); | |
}) | |
} | |
}; | |
schema.formage = { | |
list: ['name','url','active'], | |
order_by: ['order'], | |
sortable: 'order', | |
section: 'Configuration' | |
}; | |
module.exports = schema; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var Types = Schema.Types; | |
var async = require('async'); | |
var _ = require('lodash'); | |
var schema = new Schema({ | |
navigation: { type: Types.ObjectId, ref: 'navigation' }, | |
title: { type: String }, | |
picture: {type: Types.Filepicker, widget: 'FilepickerPictureWidget'}, | |
text: { type: Types.Html }, | |
url: { type: String, trim: true, lowercase: true }, | |
order: { type: Number, editable: false, default: 0 }, | |
show: { type: Boolean, default: true } | |
}); | |
schema.methods.toString = function(){ | |
return this.title; | |
}; | |
schema.pre('validate', function(next) { | |
var url = this.url; | |
if (!url) | |
url = '/' + this.title; | |
url = url.replace(/[\?\'\"\@\!\#\$\%\^\&\*\(\)\+\=\_\~\{\}\[\]\\\|\,\;\:]/g, "") | |
.replace(/ +/g, "-") | |
.replace(/\-+/g, '-') | |
.replace(/(?:^\-|\-$)/g, ''); | |
if (url.substr(0,1) !== '/') | |
url = '/' + url; | |
this.url = url.toLowerCase(); | |
next(); | |
}); | |
schema.path('url').validate(function(v, callback){ | |
var self = this; | |
async.each(['posts', 'navigation'], function(item, cb){ | |
var query = self.db.model(item).findOne().where('url', self.url); | |
if('posts' == item) query.ne('_id', self._id); | |
query.exec(function(err, url){ | |
cb(err || url); | |
}); | |
}, function(err){ | |
callback(!err); | |
}); | |
}, 'url already exists'); | |
schema.statics.byNavigationId = function(){ | |
var posts = this; | |
return function(req, res, cb){ | |
var page = res.locals.page; | |
var config = res.locals.config; | |
posts | |
.find() | |
.where('navigation', page._id) | |
.where('show', 1) | |
.sort({'order': 1}) | |
.lean() | |
.exec(function(err, results){ | |
if(results.length) res.locals.page.posts = { items: results }; | |
cb(err); | |
}) | |
} | |
}; | |
schema.formage = { | |
list: ['navigation', 'title', 'picture', 'url', 'show'], | |
list_populate: ['navigation'], | |
order_by: ['order'], | |
sortable: 'order' | |
}; | |
module.exports = schema; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment