forked from dragonling/phalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadController.php
executable file
·248 lines (198 loc) · 7.44 KB
/
ThreadController.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
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
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Eva\EvaComment\Controllers;
use Eva\EvaComment\Entities\Comments;
use Eva\EvaComment\Entities\Threads;
use Eva\EvaComment\Models\ThreadManager;
use Eva\EvaComment\Models\CommentManager;
use Eva\EvaEngine\Mvc\Controller\ControllerBase;
use Eva\EvaUser\Models\Login as LoginModel;
use Gregwar\Captcha\CaptchaBuilder;
class ThreadController extends ControllerBase
{
const VIEW_FLAT = 'flat';
const VIEW_TREE = 'tree';
public function initialize()
{
$this->view->setModuleViewsDir('EvaComment', '/views');
$this->view->setModulePartialsDir('EvaComment', '/views');
}
/**
* Get the comments of a thread. Creates a new thread if none exists.
*
* @param string $uniqueKey Id of the thread
*
*/
public function getThreadCommentsAction($uniqueKey)
{
$displayDepth = $this->request->getQuery('displayDepth');
$sorter = $this->request->getQuery('sorter','string',CommentManager::DEFAULT_SORT);
$currentPage = $this->request->getQuery('page', 'int', 1);
$threadManager = new ThreadManager();
$thread = $threadManager->findThreadByUniqueKey($uniqueKey);
// We're now sure it is no duplicate id, so create the thread
if (null === $thread) {
// Decode the permalink for cleaner storage (it is encoded on the client side)
$permalink = urldecode($this->request->getQuery('permalink'));
$thread = new Threads();
$thread->uniqueKey = $uniqueKey;
$thread->permalink = $permalink;
//todo validate
if ($thread->save() == false) {
foreach ($thread->getMessages() as $message) {
echo $message, "\n";
}
exit;
// throw new \Exception('Save failed');
}
}
$cacheKey = $uniqueKey.'###'.$thread->numComments.$thread->lastEditAt;
// $this->view->cache(
// array(
// "lifetime" => 86400,
// "key" => $cacheKey,
// )
// );
// $viewMode = $this->dispatcher->getParam('view');
$commentManager = new CommentManager();
$viewMode = 'tree';
switch ($viewMode) {
case self::VIEW_FLAT:
$comments = $commentManager->findCommentsByThread($thread, $sorter, $displayDepth);
// We need nodes for the api to return a consistent response, not an array of comments
// $comments = array_map(function($comment) {
// return array('comment' => $comment, 'children' => array());
// },
// $comments
// );
break;
case self::VIEW_TREE:
default:
$comments = $commentManager->findCommentsByThread($thread, $sorter, $displayDepth);
break;
}
$this->view->pick('thread/thread');
$query = array(
'page' => $currentPage,
);
$limit = 10;
$paginator = new \Eva\EvaEngine\Paginator(array(
"builder" => $comments,
"limit"=> $limit,
"page" => $currentPage
));
$paginator->setQuery($query);
$pager = $paginator->getPaginate();
$this->view->setVars(
array(
'pager' => $pager,
'comments' => $pager->items,
'displayDepth' => $displayDepth,
'sorter' => $sorter,
'thread' => $thread,
)
);
}
public function dialogAction()
{
}
public function counterAction()
{
$ids = $this->request->getQuery('ids');
if(!is_array($ids)){
$ids = array($ids);
}
$threadManager = new ThreadManager();
$data = array();
foreach($ids as $uniqueKey){
$thread = $threadManager->findThreadByUniqueKey($uniqueKey);
if ($thread) {
$data['threads'][] = array('uniqueKey'=>$uniqueKey,'numComments'=>$thread->numComments);
}
}
echo json_encode($data);
$this->view->disable();
}
/**
* Creates a new Comment for the Thread from the submitted data.
*
* @param string $uniqueKey The id of the thread
* @throws \Exception
*/
public function postThreadCommentsAction($uniqueKey)
{
$threadManager = new ThreadManager();
$thread = $threadManager->findThreadByUniqueKey($uniqueKey);
if (!$thread) {
throw new \Exception(sprintf('Thread with identifier of "%s" does not exist', $uniqueKey));
}
// if (!$thread->isCommentable()) {
// throw new \Exception(sprintf('Thread "%s" is not commentable', $uniqueKey));
// }
$parentId = $this->request->getPost('parentId');
$parent = $this->getValidCommentParent($thread, $parentId);
$content = $this->request->getPost('content');
$username = $this->request->getPost('username');
$commentManager = new CommentManager();
$comment = $commentManager->createComment($thread, $parent);
// if ($form->isValid()) {
$comment->content = $content;
// if(!empty($username)) $comment->username = $username;
$user = new LoginModel();
if ($user->isUserLoggedIn()) {
$userinfo = $user->getCurrentUser();
$comment->userId = $userinfo['id'];
$comment->username = $userinfo['username'];
}
$commentManager->filterContent($comment); //政治敏感词过滤
if ($commentManager->saveComment($comment) !== false) {
$errors = $comment->getMessages();
p($errors);
// return $this->getViewHandler()->handle($this->onCreateCommentSuccess($form, $id, $parent));
}
$this->view->pick('thread/comment');
$this->view->setVars(
array(
'comment' => $comment,
'thread' => $thread,
)
);
}
public function captchaAction()
{
$builder = new CaptchaBuilder;
$builder->build();
if($builder->testPhrase($userInput)) {
// instructions if user phrase is good
}
else {
// user phrase is wrong
}
header('Content-type: image/jpeg');
$builder->output();
}
/**
* Checks if a comment belongs to a thread. Returns the comment if it does.
*
* @param ThreadInterface $thread Thread object
* @param mixed $commentId Id of the comment.
*
* @return CommentInterface|null The comment.
*/
private function getValidCommentParent($thread, $commentId)
{
if (!empty($commentId)) {
$commentManager = new CommentManager();
$comment = $commentManager->findCommentById($commentId);
if (!$comment) {
//todo throw exception
exit(sprintf('Parent comment with identifier "%s" does not exist',$commentId));
// throw new NotFoundHttpException(sprintf('Parent comment with identifier "%s" does not exist', $commentId));
}
// if ($comment->getThread() !== $thread) {
// exit('Parent comment is not a comment of the given thread.');
//// throw new NotFoundHttpException('Parent comment is not a comment of the given thread.');
// }
return $comment;
}
}
}