2019-11-23 19:30:34 +01:00
|
|
|
import Book from '../../data/Book';
|
2019-11-24 01:09:37 +01:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const moment = require('moment');
|
2019-11-23 19:30:34 +01:00
|
|
|
const book = new Book();
|
2019-11-25 22:58:34 +01:00
|
|
|
const uuidv4 = require('uuid/v4');
|
2019-11-26 05:03:50 +01:00
|
|
|
const settings = require('../../../site/settings.json');
|
2018-11-11 21:22:01 +01:00
|
|
|
//--------------------------
|
|
|
|
// POSTS
|
|
|
|
//--------------------------
|
2020-05-21 06:44:19 +02:00
|
|
|
router.get('/list/:filter?/:page?', function (req, res) {
|
2019-02-27 17:17:51 +01:00
|
|
|
var pageNum = req.params.page;
|
2019-03-01 23:48:05 +01:00
|
|
|
var filter = req.params.filter;
|
2019-02-27 17:17:51 +01:00
|
|
|
if (pageNum == '' || pageNum == null) pageNum = 1;
|
2020-05-21 06:44:19 +02:00
|
|
|
if (filter == '' || filter == null) filter = 'all';
|
2019-02-27 17:17:51 +01:00
|
|
|
if (req.session.user) {
|
2019-11-26 05:03:50 +01:00
|
|
|
book.getPage()
|
2020-03-01 06:01:34 +01:00
|
|
|
.then(pages => {
|
2019-11-26 05:03:50 +01:00
|
|
|
pages.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id));
|
|
|
|
let all = [];
|
|
|
|
let deleted = [];
|
|
|
|
let published = [];
|
|
|
|
let menu = [];
|
|
|
|
let featured = [];
|
|
|
|
for (let index = 0; index < pages.length; index++) {
|
|
|
|
let item = pages[index].metadata;
|
2020-03-01 22:13:18 +01:00
|
|
|
if (typeof item.deleted === 'undefined' || item.deleted === false) {
|
2019-11-26 05:03:50 +01:00
|
|
|
all.push(pages[index].metadata);
|
|
|
|
if (item.published == true) published.push(pages[index].metadata);
|
|
|
|
if (item.menu == true) menu.push(pages[index].metadata);
|
|
|
|
if (item.featured == true) featured.push(pages[index].metadata);
|
|
|
|
} else {
|
|
|
|
deleted.push(pages[index].metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var filtered;
|
|
|
|
switch (filter) {
|
|
|
|
case 'published':
|
|
|
|
filtered = published;
|
|
|
|
break;
|
|
|
|
case 'deleted':
|
|
|
|
filtered = deleted;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
filtered = all;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var count = Math.round(filtered.length / 6);
|
|
|
|
var pageItems = [];
|
|
|
|
var itemLimit = 6;
|
|
|
|
var rangeStart = pageNum * itemLimit - itemLimit;
|
|
|
|
for (var i = 0; i < itemLimit; i++) {
|
|
|
|
try {
|
|
|
|
if (filtered[i + rangeStart].id != null) {
|
|
|
|
pageItems.push(filtered[i + rangeStart]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
//console.log("NO POST", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.render('book-index', {
|
|
|
|
title: 'Dashbord | Book',
|
|
|
|
welcome: 'Your pages',
|
|
|
|
items: pageItems,
|
|
|
|
page_info: {
|
|
|
|
all: all.length,
|
|
|
|
deleted: deleted.length,
|
|
|
|
published: published.length,
|
|
|
|
pages: pages.length,
|
|
|
|
featured: featured.length
|
|
|
|
},
|
|
|
|
page_index: pageNum,
|
|
|
|
page_count: count,
|
|
|
|
postFilter: filter,
|
|
|
|
status: true
|
|
|
|
});
|
|
|
|
})
|
2019-02-27 17:17:51 +01:00
|
|
|
.then(() => {
|
|
|
|
//console.log(value);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//next(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
2019-11-26 05:03:50 +01:00
|
|
|
|
2018-11-11 21:22:01 +01:00
|
|
|
//--------------------------
|
|
|
|
// BLOG POST ADD DISPLAY
|
|
|
|
//--------------------------
|
2020-05-21 06:44:19 +02:00
|
|
|
router.get('/add/new', function (req, res) {
|
2019-02-27 17:17:51 +01:00
|
|
|
if (req.session.user) {
|
2019-11-26 05:03:50 +01:00
|
|
|
res.render('page-edit', {
|
|
|
|
id: settings.library_stats.current_index,
|
|
|
|
uuid: uuidv4(),
|
|
|
|
title: 'Add New Page',
|
|
|
|
user_status: true,
|
|
|
|
welcome: 'Add New Page',
|
|
|
|
date: moment(Date.now()).format('YYYY MMM DD'),
|
|
|
|
page: [],
|
|
|
|
rawDate: moment(Date.now()).format(),
|
|
|
|
status: ['false', 'false', 'false'],
|
|
|
|
edit: false
|
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
} else {
|
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
|
|
|
//--------------------------
|
|
|
|
// BLOG POST EDIT DISPLAY
|
|
|
|
//--------------------------
|
2020-05-21 06:44:19 +02:00
|
|
|
router.get('/edit/:id', function (req, res) {
|
2019-11-23 19:30:34 +01:00
|
|
|
var id = req.params.id;
|
2019-02-27 17:17:51 +01:00
|
|
|
if (req.session.user) {
|
2019-11-23 19:30:34 +01:00
|
|
|
book.getPage(id)
|
|
|
|
.then(page => {
|
|
|
|
res.render('page-edit', {
|
|
|
|
id: page.metadata.id,
|
2019-11-25 01:11:12 +01:00
|
|
|
uuid: page.metadata.uuid,
|
2019-11-23 19:30:34 +01:00
|
|
|
title: 'Edit Page',
|
|
|
|
welcome: 'Edit Page',
|
|
|
|
page: page.metadata,
|
2019-11-24 01:09:37 +01:00
|
|
|
date: moment(page.metadata.created).format('YYYY MMM DD'),
|
2020-05-21 22:23:21 +02:00
|
|
|
layout: page.metadata.layout,
|
2019-11-25 01:11:12 +01:00
|
|
|
rawDate: page.metadata.created,
|
2020-05-21 06:44:19 +02:00
|
|
|
content: page.content,
|
2019-11-23 19:30:34 +01:00
|
|
|
feature: page.metadata.feature,
|
|
|
|
status: [
|
2019-11-25 22:58:34 +01:00
|
|
|
String(page.metadata.menu),
|
2019-11-23 19:30:34 +01:00
|
|
|
String(page.metadata.featured),
|
|
|
|
String(page.metadata.published)
|
|
|
|
],
|
|
|
|
edit: true
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({ type: err });
|
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
} else {
|
2019-02-27 17:17:51 +01:00
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
module.exports = router;
|