-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.php
More file actions
135 lines (120 loc) · 5.08 KB
/
Copy pathRoute.php
File metadata and controls
135 lines (120 loc) · 5.08 KB
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
<?php
namespace LinkCMS\Modules\Pages;
use \Flight;
use LinkCMS\Actor\Config;
use LinkCMS\Actor\Display;
use LinkCMS\Actor\Notify;
use LinkCMS\Actor\User;
use LinkCMS\Actor\Route as Router;
use LinkCMS\Controller\Content as ContentController;
use LinkCMS\Modules\Pages\Controller as PageController;
use LinkCMS\Model\User as UserModel;
use LinkCMS\Modules\Pages\Model\Page as Page;
class Route {
public static function add_assets_directory() {
Router::register_folder_map(__DIR__ . '/public/js', 'pages/assets/js');
}
// TODO: Run cron to publish scheduled posts
public static function do_routes() {
Flight::route('GET /@slug', function($slug) {
$page = PageController::load_by('slug', $slug);
if ($page) {
$page = new Page($page);
if ($page->status !== 'published') {
return true;
}
Display::load_page('content/' . $page->template . '.twig', ['page' => $page]);
} else {
return true;
}
});
Flight::route('GET /manage/pages', function() {
User::is_authorized(UserModel::USER_LEVEL_AUTHOR);
$pages = PageController::load_all();
Display::load_page('/pages/manage/index.twig', ['pages'=>$pages]);
});
Flight::route('GET /manage/pages/create', function() {
User::is_authorized(UserModel::USER_LEVEL_AUTHOR);
Display::load_page('pages/manage/edit.twig', ['page'=>false]);
});
Flight::route('POST /api/pages/save', function() {
if (User::is_authorized(UserModel::USER_LEVEL_AUTHOR)) {
// TODO: If updating, have to check that the current user is allowed to update that post
if (!empty($_POST)) {
$page = new Page($_POST);
if ($page->id) {
$results = ContentController::update($page);
if ($results) {
new Notify(['type' => 'update'], 'success');
} else {
new Notify('Database problem', 'error');
}
} else {
$results = ContentController::save($page);
if ($results) {
new Notify(['type'=>'insert', 'id'=> $results], 'success');
} else {
new Notify('Database problem', 'error');
}
}
}
}
});
Flight::route('GET /manage/pages/edit/@id', function($id) {
if (User::is_authorized(UserModel::USER_LEVEL_AUTHOR)) {
$page = new Page(ContentController::load_by('id', $id));
if ($page) {
$page = json_encode($page);
Display::load_page('pages/manage/edit.twig', ['page'=>$page, 'id'=>$id]);
} else {
Flight::error('No such page found');
}
}
});
Flight::route('GET /@slug/preview', function($slug) {
User::is_authorized(UserModel::USER_LEVEL_AUTHOR);
$page = PageController::load_by('slug', $slug);
if ($page) {
$page = new Page($page);
$page->publishedContent = $page->draftContent;
Display::load_page('content/' . $page->template . '.twig', ['page' => $page]);
} else {
return true;
}
});
Flight::route('GET /manage/pages/preview/@id', function($id) {
User::is_authorized(UserModel::USER_LEVEL_AUTHOR);
$page = PageController::load_by('id', $id);
if ($page) {
$page = new Page($page);
$page->publishedContent = $page->draftContent;
Display::load_page('content/' . $page->template . '.twig', ['page' => $page]);
} else {
return true;
}
});
Flight::route('GET /manage/pages/delete/@id', function($id) {
if (User::is_authorized(UserModel::USER_LEVEL_ADMIN)) {
$pageContent = ContentController::load_by('id', $id);
if ($pageContent) {
$page = new Page($pageContent);
Display::load_page('pages/manage/delete.twig', ['page'=>$page]);
} else {
Flight::error('No such page');
}
}
});
Flight::route('POST /manage/pages/delete/@id', function($id) {
if (User::is_authorized(UserModel::USER_LEVEL_ADMIN)) {
$pageContent = ContentController::load_by('id', $id);
if ($pageContent) {
ContentController::delete_by('id', $id);
$url = Config::get_config('siteUrl');
Flight::redirect($url . '/manage/pages');
} else {
Flight::error('No such page');
}
}
});
}
}