Ghost/core/admin/controllers/index.js

274 lines
11 KiB
JavaScript
Raw Normal View History

/*global require, module */
2013-05-11 20:44:25 +04:00
(function () {
"use strict";
var Ghost = require('../../ghost'),
dataExport = require('../../shared/data/export'),
dataImport = require('../../shared/data/import'),
2013-05-11 20:44:25 +04:00
_ = require('underscore'),
fs = require('fs'),
path = require('path'),
when = require('when'),
nodefn = require('when/node/function'),
api = require('../../shared/api'),
2013-05-11 20:44:25 +04:00
ghost = new Ghost(),
2013-06-01 18:47:41 +04:00
dataProvider = ghost.dataProvider,
2013-05-11 20:44:25 +04:00
adminNavbar,
adminControllers;
// TODO: combine path/navClass to single "slug(?)" variable with no prefix
adminNavbar = {
dashboard: {
name: 'Dashboard',
navClass: 'dashboard',
key: 'admin.navbar.dashboard',
2013-05-29 04:10:39 +04:00
// defaultString: 'dashboard',
path: '/'
2013-05-11 20:44:25 +04:00
},
2013-05-29 04:10:39 +04:00
content: {
2013-05-11 20:44:25 +04:00
name: 'Content',
navClass: 'content',
2013-05-29 04:10:39 +04:00
key: 'admin.navbar.content',
// defaultString: 'content',
path: '/content/'
2013-05-11 20:44:25 +04:00
},
add: {
name: 'New Post',
navClass: 'editor',
key: 'admin.navbar.editor',
2013-05-29 04:10:39 +04:00
// defaultString: 'editor',
path: '/editor/'
2013-05-11 20:44:25 +04:00
},
settings: {
name: 'Settings',
navClass: 'settings',
key: 'admin.navbar.settings',
2013-05-29 04:10:39 +04:00
// defaultString: 'settings',
path: '/settings/'
2013-05-11 20:44:25 +04:00
}
};
ghost.doFilter('messWithAdmin', adminNavbar, function () {
2013-05-29 04:10:39 +04:00
console.log('the dofilter hook called in /core/admin/controllers/index.js');
});
2013-05-11 20:44:25 +04:00
// TODO - make this a util or helper
function setSelected(list, name) {
_.each(list, function (item, key) {
item.selected = key === name;
});
return list;
}
adminControllers = {
'login': function (req, res) {
res.render('login', {
bodyClass: 'ghost-login',
hideNavbar: true,
adminNav: setSelected(adminNavbar, 'login')
});
},
'auth': function (req, res) {
2013-05-26 21:44:01 +04:00
api.users.check({email: req.body.email, pw: req.body.password}).then(function (user) {
console.log('user found: ', user);
req.session.user = "ghostadmin";
res.redirect(req.query.redirect || '/ghost/');
}, function (error) {
2013-05-26 21:44:01 +04:00
// Do something here to signal the reason for an error
req.flash('error', error.message);
2013-05-26 21:44:01 +04:00
res.redirect('/ghost/login/');
});
},
'signup': function (req, res) {
res.render('signup', {
bodyClass: 'ghost-login',
hideNavbar: true,
adminNav: setSelected(adminNavbar, 'login')
});
},
'doRegister': function (req, res) {
var email = req.body.email_address,
password = req.body.password;
if (email !== '' && password.length > 5) {
2013-05-26 21:44:01 +04:00
api.users.add({
email_address: email,
password: password
2013-05-26 21:44:01 +04:00
}).then(function (user) {
console.log('user added', user);
res.redirect('/ghost/login/');
}, function (error) {
req.flash('error', error.message);
res.redirect('/ghost/signup/');
});
} else {
req.flash('error', "The password is too short. Have at least 6 characters in there");
res.redirect('back');
}
},
'logout': function (req, res) {
delete req.session.user;
req.flash('success', "You were successfully logged out");
res.redirect('/ghost/login/');
},
2013-05-11 20:44:25 +04:00
'index': function (req, res) {
res.render('dashboard', {
bodyClass: 'dashboard',
adminNav: setSelected(adminNavbar, 'dashboard')
});
},
'editor': function (req, res) {
if (req.params.id !== undefined) {
api.posts.read({id: parseInt(req.params.id, 10)})
.then(function (post) {
res.render('editor', {
bodyClass: 'editor',
2013-05-29 04:10:39 +04:00
adminNav: setSelected(adminNavbar, 'content'),
title: post.get('title'),
content: post.get('content')
});
2013-05-11 20:44:25 +04:00
});
} else {
res.render('editor', {
bodyClass: 'editor',
adminNav: setSelected(adminNavbar, 'add')
});
}
},
2013-05-29 04:10:39 +04:00
'content': function (req, res) {
api.posts.browse({status: req.params.status || 'all'})
.then(function (page) {
2013-05-29 04:10:39 +04:00
res.render('content', {
bodyClass: 'manage',
2013-05-29 04:10:39 +04:00
adminNav: setSelected(adminNavbar, 'content'),
posts: page.posts
});
2013-05-11 20:44:25 +04:00
});
},
'settings': function (req, res) {
2013-05-23 17:57:37 +04:00
api.settings.browse()
.then(function (settings) {
res.render('settings', {
bodyClass: 'settings',
adminNav: setSelected(adminNavbar, 'settings'),
settings: settings
});
});
2013-05-11 20:44:25 +04:00
},
'debug': { /* ugly temporary stuff for managing the app before it's properly finished */
index: function (req, res) {
res.render('debug', {
bodyClass: 'settings',
adminNav: setSelected(adminNavbar, 'settings')
2013-05-11 20:44:25 +04:00
});
},
'export': function (req, res) {
// Get current version from settings
api.settings.read({ key: "currentVersion" })
.then(function (setting) {
// Export the current versions data
return dataExport(setting.value);
}, function () {
// If no setting, assume 001
return dataExport("001");
})
.then(function (exportedData) {
// Save the exported data to the file system for download
var fileName = path.resolve(__dirname + '/../../shared/data/export/exported-' + (new Date().getTime()) + '.json');
return nodefn.call(fs.writeFile, fileName, JSON.stringify(exportedData)).then(function () {
return when(fileName);
});
})
.then(function (exportedFilePath) {
// Send the exported data file
res.download(exportedFilePath, 'GhostData.json');
})
.otherwise(function (error) {
// Notify of an error if it occurs
req.flash("error", error.message || error);
res.redirect("/ghost/debug");
});
2013-05-11 20:44:25 +04:00
},
'import': function (req, res) {
if (!req.files.importfile) {
// Notify of an error if it occurs
req.flash("error", "Must select a file to import");
return res.redirect("/ghost/debug");
}
// Get the current version for importing
api.settings.read({ key: "currentVersion" })
.then(function (setting) {
return when(setting.value);
}, function () {
return when("001");
})
.then(function (currentVersion) {
// Read the file contents
return nodefn.call(fs.readFile, req.files.importfile.path)
.then(function (fileContents) {
var importData;
// Parse the json data
try {
importData = JSON.parse(fileContents);
} catch (e) {
return when.reject(new Error("Failed to parse the import file"));
}
if (!importData.meta || !importData.meta.version) {
return when.reject(new Error("Import data does not specify version"));
}
// Import for the current version
return dataImport(currentVersion, importData);
});
})
.then(function () {
req.flash("success", "Data imported");
})
.otherwise(function (error) {
// Notify of an error if it occurs
req.flash("error", error.message || error);
})
.then(function () {
res.redirect("/ghost/debug");
});
},
'reset': function (req, res) {
// Grab the current version so we can get the migration
api.settings.read({ key: "currentVersion" })
.then(function (setting) {
var migration = require("../../shared/data/migration/" + setting.value);
// Run the downward migration
return migration.down();
}, function () {
// If no version in the DB, assume 001
var migration = require("../../shared/data/migration/001");
// Run the downward migration
return migration.down();
})
.then(function () {
// Re-initalize the providers (should run the migration up again)
return dataProvider.init();
})
.then(function () {
req.flash("success", "Database reset");
})
.otherwise(function (error) {
req.flash("error", error.message || error);
})
.then(function () {
res.redirect('/ghost/debug');
});
2013-05-11 20:44:25 +04:00
}
}
};
module.exports = adminControllers;
}());