Created
August 7, 2013 10:54
-
-
Save AVVS/6173069 to your computer and use it in GitHub Desktop.
This file contains 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
mongoose = require 'mongoose' | |
s = require('validator').sanitize | |
Schema = mongoose.Schema | |
ObjectId = Schema.Types.ObjectId | |
async = require 'async' | |
a = {"Ё":"YO","Й":"I","Ц":"TS","У":"U","К":"K","Е":"E","Н":"N","Г":"G","Ш":"SH","Щ":"SCH","З":"Z","Х":"H","Ъ":"","ё":"yo","й":"i","ц":"ts","у":"u","к":"k","е":"e","н":"n","г":"g","ш":"sh","щ":"sch","з":"z","х":"h","ъ":"","Ф":"F","Ы":"I","В":"V","А":"a","П":"P","Р":"R","О":"O","Л":"L","Д":"D","Ж":"ZH","Э":"E","ф":"f","ы":"i","в":"v","а":"a","п":"p","р":"r","о":"o","л":"l","д":"d","ж":"zh","э":"e","Я":"Ya","Ч":"CH","С":"S","М":"M","И":"I","Т":"T","Ь":"","Б":"B","Ю":"YU","я":"ya","ч":"ch","с":"s","м":"m","и":"i","т":"t","ь":"","б":"b","ю":"yu"} | |
transliterate = (word)-> | |
return word.split('').map((char)=> | |
return a[char] || char | |
).join("") | |
CustomPages = new Schema | |
# if url exists - then its a root element | |
url: | |
type: String # - can be access via /pages-url and exported is false | |
required: true | |
unique: true | |
title: | |
type: String # - title of the page | |
default: null | |
exported: | |
type: Boolean | |
default: false | |
links: [{ | |
type: ObjectId, ref: 'Menu' | |
}] | |
assigned: | |
type: Number | |
default: 0 | |
min: 0 | |
# - common structure | |
templates: [{ | |
type: ObjectId, ref: 'Pages' | |
}] | |
CustomPages.statics.access = { | |
all: [], | |
user: [], | |
admin: ['create'] | |
} | |
CustomPages.statics.pageExists = (eventId, urlHash, cb)-> | |
@findOne({assigned: {$gt: 0}, url: urlHash}).populate('links').populate('templates').exec (err, customPage)=> | |
if !err and customPage | |
async.detect customPage.links, (menuObj, acb)=> | |
acb menuObj.event.equals(eventId) | |
,(result)=> | |
if result | |
cb true, customPage | |
else | |
cb false | |
else | |
cb false | |
CustomPages.statics.getHidden = (callback)-> | |
@find { assigned: 0 }, callback | |
CustomPages.statics.deletepgs = (req, res)-> | |
@remove {_id: req.body._id}, (err)=> | |
unless err | |
res.json {success: true} | |
else | |
res.json {error:yes,success: false, err} | |
CustomPages.statics.getPages = (callback)-> | |
@find {}, "url _id assigned title", (err, pages)=> | |
callback err, JSON.stringify(pages) | |
CustomPages.statics.create = (req,res)-> | |
post = req.body | |
app = req.app | |
title = s(s(post.title).xss()).trim() | |
if title.length > 5 | |
url = "pages-"+transliterate(title).replace(/[\s\-]/g, "_").toLowerCase().replace(/[\'\"!:<>;^&=*\.\/\\]/g,"") | |
page = new @ {title, url} | |
page.save (err)=> | |
unless err | |
res.json {success: true, page} | |
else | |
res.json {success: false, err} | |
else | |
res.json {success: false, err: "Минимальная длина названия - 6 символов"} | |
exports.schemas = { CustomPages } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment