forked from robrighter/node-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
237 lines (208 loc) · 7.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
(function(){
exports.CMS = function(settings){
require(__dirname + "/lib/setup").ext( __dirname + "/lib");
var Datastore = require('./datastore').Datastore;
var sys = require('sys');
var fs = require('fs');
var ds = new Datastore(settings);
var form = require('connect-form');
this.content = ds;
//grab the list of templates:
var frontEndTemplates = [];
fs.readdir(settings.FrontEndViews, function(err, files){
if(!err){
frontEndTemplates = files;
}
});
/////////////////////////////////////////////////////
// setup Routes
/////////////////////////////////////////////////////
this.setupExpressJSRoutes = function(server){
//main page loader route
server.get(new RegExp("^([a-zA-Z0-9\-\/]*)$"), function(req,res,next){
findContentOrPassToNext(req.params[0], next, function(result){
res.render(result.location.template, {
layout: true,
locals : {
title: result.content.title,
article: result.content.article
}
});
});
});
//REST Resources
server.post("/_content/UPDATE", function(req,res,next){
var isform = processForm(req, function(err, fields, files){
console.log('The arguements: ' + sys.inspect(arguments));
if(err){
req.flash('warning', 'Oops. The page encountered an error and was not saved.');
res.redirect('back');
}
var doc = processIncomingFormFields(fields, files);
ds.updateContent(doc.__id, doc, function(result){
if(result){
req.flash('confirm', 'Success! The page has been saved.');
res.redirect('back');
//res.send({status: true});
}
else{
req.flash('warning', 'Oops. The page encountered an error and was not saved.');
res.redirect('back');
}
});
})
if(!isform){
//no form was submitted so lets pass it on for a 404
next();
}
});
server.post("/_content/ADD", function(req,res,next){
var isform = processForm(req, function(err, fields, files){
if(err){
req.flash('warning', 'Oops. The page encountered an error and was not saved.');
res.redirect('back');
}
var doc = processIncomingFormFields(fields, files);
//make our content item and populate it with the form field values
var content = ds.makeContent(doc.__contenttype);
content.getProperties().forEach(function(item){
if(doc.hasOwnProperty(item.name)){
content[item.name] = doc[item.name];
}
});
ds.addContentToSitemap(content, doc.__parentid, doc.__template, function(result){
if(result){
ds.getPathForItem(result, function(path){
console.log('REDIRECTING TO: ' + '/_admin/'+path);
req.flash('confirm', 'Success! The page has been saved.');
res.redirect('/_admin/' + path);
});
//res.send({status: true, newslug: result.slug});
}
else{
req.flash('warning', 'Oops. The page encountered an error and was not saved.');
res.redirect('back');
}
});
})
if(!isform){
//no form was submitted so lets pass it on for a 404
next();
}
});
function processForm(req, callback){
if(req.form){
req.form.complete(callback);
return true;
}
else{
return false;
}
}
function processIncomingFormFields(fields, files){
var doc = fields;
if(!doc.hasOwnProperty('__hidden_from_navigation')){
doc['__hidden_from_navigation'] = 'false';
}
if(doc['__hidden_from_navigation'] == 'true'){
doc['__hidden_from_navigation'] = true;
}
else{
doc['__hidden_from_navigation'] = false;
}
for(key in files){
doc[key] = { filename: stripFileNameFromUploadPath(files[key].path), alt: '' };
}
console.log("FIELDS:");
console.log(sys.inspect(doc));
return doc;
}
//main admin page loader route
server.get(new RegExp("^\/_admin\/([a-zA-Z0-9\-\/]*)$"), function(req,res,next){
findContentOrPassToNext(req.params[0], next, function(result){
prepareModelResultForAdmin(result, function(locals){
locals.adminassets = settings.AdminAssetsWebPath;
locals.contenttypes = ds.contentTypes;
locals.sys = sys;
locals.uploadedfiles = settings.UploadedFilesWebPath;
locals.confirm = (req.flash('confirm') || '');
locals.warning = (req.flash('warning') || '');
res.render(settings.AdminAssetsFilePath + '/views/edit.ejs', {
layout: false,
locals : locals,
});
});
});
});
//admin create new item route
server.get(new RegExp("^\/_admin\/([a-zA-Z0-9\-\/]*)/?_new/([a-zA-Z0-9\-\/]*)$"), function(req,res,next){
var contenttype = req.params[1];
if(ds.contentTypes.indexOf(contenttype) < 0 ){
//move on, this is a 404. That content type is not valid
console.log('tried to go to a create new content page with an invalid content type');
next();
}
findContentOrPassToNext(req.params[0], next, function(result){
var locals = {};
locals.adminassets = settings.AdminAssetsWebPath;
locals.sys = sys;
locals.templates = frontEndTemplates;
locals.uploadedfiles = settings.UploadedFilesWebPath;
locals.confirm = (req.params['confirm'] || '');
locals.warning = (req.params['warning'] || '');
locals.location = {}
locals.location.template = result.location.template;
locals.location.parentid = ds.convertIdToString(result.location._id);
locals.location.contenttype = contenttype;
locals.content = ds.makeContent(contenttype);
locals.content.title = 'New ' + contenttype;
res.render(settings.AdminAssetsFilePath + '/views/edit.ejs', {
layout: false,
locals : locals,
});
});
});
//user login route
//admin update existing item route
}
function prepareModelResultForAdmin(result, callback){
var toreturn = {};
toreturn.content = result.content;
toreturn.location = result.location;
toreturn.id = ds.convertIdToString(result.location._id);
toreturn.templates = frontEndTemplates;
//get children from location
result.location.getChildren(function(children){
toreturn.children = children;
//run the preRenderingTasks if the model creator made them
if(result.content.hasOwnProperty('preRenderingTasks')){
result.content.preRenderingTasks(function(taskresults){
toreturn.prerendertasks = taskresults;
//setup form inputs
callback(toreturn);
});
}
else{
callback(toreturn);
}
});
}
function prepareModelResultForFrontEnd(result,callback){
//grab navigation lists
//preRenderingTasks
}
function findContentOrPassToNext(path, next, callback){
ds.getContentItemForPath(path, function(result){
if(result){
callback(result)
}
else{
next();//didnt find anything pass it off to next
}
});
}
function stripFileNameFromUploadPath(filepath){
return filepath.replace(settings.UploadedFiles, '').replace(/\//g, '');
}
}
})();