-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontroller.php
184 lines (148 loc) · 4.87 KB
/
controller.php
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
<?php
/**
* @package Wiki
* @author Alan Hardman <[email protected]>
* @version 1.1.1
*/
namespace Plugin\Wiki;
class Controller extends \Controller {
public function index($f3) {
$this->_requireLogin(\Model\User::RANK_GUEST);
$page = new Model\Page;
$page->indent = 0;
$pages = $this->build_tree($page->find(array("deleted_date IS NULL"), array("order" => "name ASC")));
$f3->set("title", $f3->get('dict.wiki.wiki'));
$f3->set("pages", $pages);
$this->_render("wiki/view/index.html");
}
public function single($f3, $params) {
$this->_requireLogin(\Model\User::RANK_GUEST);
$page = new Model\Page;
$page->load(array("slug = ?", $params["page"]));
// Handle 404s
if(!$page->id) {
$f3->status(404);
$this->_render("wiki/view/404.html");
return;
}
// Load latest update
$update = new Model\Page\Update;
$db = $f3->get("db.instance");
$maxUpdateId = $db->exec("SELECT MAX(id) id FROM wiki_page_update WHERE wiki_page_id = :id", array(":id" => $page->id));
$update->load(intval($maxUpdateId[0]['id']));
// Load update user
$user = new \Model\User;
$user->load($update->user_id);
$f3->set("update_user", $user);
// Load update count
$updateCount = $db->exec("SELECT COUNT(*) num FROM wiki_page_update WHERE wiki_page_id = :id", array(":id" => $page->id));
$f3->set("update_count", intval($updateCount[0]['num']));
// Load pages list
$pages = $this->build_tree($page->find(array("deleted_date IS NULL"), array("order" => "name ASC")));
// Set default parser options to disable issue-related replacements
if($f3->get("wiki.parse") === null) {
$f3->set("wiki.parse", array());
}
$f3->set("wiki.parse.ids", false);
$f3->set("wiki.parse.hashtags", false);
$f3->set("pages", $pages);
$f3->set("title", $page->name);
$f3->set("page", $page);
$f3->set("update", $update);
$this->_render("wiki/view/single.html");
}
public function edit($f3, $params) {
$this->_requireLogin(\Model\User::RANK_USER);
if(!isset($params["page"])) {
$params["page"] = null;
$f3->set("PARAMS.page", "");
}
$page = new Model\Page;
$page->indent = 0;
if($params["page"]) {
$page->load(array("slug = ?", $params["page"]));
}
if($page->id) {
$pages_selection = $page->find(array("deleted_date IS NULL AND id != ?", $page->id), array("order" => "name ASC"));
} else {
$pages_selection = $page->find(array("deleted_date IS NULL"), array("order" => "name ASC"));
}
$f3->set("pages", $this->build_tree($pages_selection, true));
if($f3->get("POST")) {
if(!strlen($f3->get("POST.name"))) {
$f3->set("error", "Page title is required.");
}
if(!strlen($f3->get("POST.slug"))) {
$f3->set("error", "Page slug is required.");
}
if(!$f3->get("error")) {
// Set created date on new pages
if(!$page->id) {
$page->created_date = gmdate("Y-m-d H:i:s");
}
$page->slug = trim($f3->get("POST.slug"), " -");
// Save page update
$update = new Model\Page\Update;
$update->user_id = $f3->get("user.id");
$update->old_name = $page->name;
$page->name = $f3->get("POST.name");
$update->new_name = $page->name;
$update->old_content = $page->content;
$page->content = $f3->get("POST.content");
$update->new_content = $page->content;
$page->parent_id = $f3->get("POST.parent_id");
$update->created_date = gmdate("Y-m-d H:i:s");
$page->save();
$update->wiki_page_id = $page->id;
$update->save();
$f3->reroute("/wiki/" . $page->slug);
return;
}
}
$f3->set("title", "Edit Page");
$f3->set("page", $page);
$this->_render("wiki/view/edit.html");
}
public function delete($f3, $params) {
$this->_requireLogin(\Model\User::RANK_MANAGER);
$page = new Model\Page;
$page->load(array("slug = ?", $params["page"]));
$page->delete();
$f3->reroute("/wiki");
}
function build_tree($pages, $add_spaces = false) {
$data = array();
$index = array();
foreach($pages as $row){
$id = $row["id"];
$parent_id = $row["parent_id"] === NULL ? "" : $row["parent_id"];
$data[$id] = $row;
$index[$parent_id][] = $id;
}
$newarray = array();
$level = 0;
foreach ($pages as $row) {
if($row["parent_id"] == "" || $row["parent_id"] == 0) {
if($add_spaces){
$row["name"] = str_repeat(" ", $level) . $row["name"];
}
$row["indent"] = $level;
$newarray[] = $row;
$this->display_child_nodes($row['id'], $level + 1, $data, $index, $newarray, $add_spaces);
}
}
return $newarray;
}
function display_child_nodes($parent_id, $level, &$data, &$index, &$newarray, $add_spaces = false) {
if (isset($index[$parent_id])) {
foreach ($index[$parent_id] as $id) {
if($add_spaces){
$data[$id]["name"] = str_repeat(" ", $level) . $data[$id]["name"];
}
$data[$id]["indent"] = $level;
$newarray[] = $data[$id];
$this->display_child_nodes($id, $level + 1, $data, $index, $newarray, $add_spaces);
}
}
}
}