2013-10-05 19:24:08 +02:00
|
|
|
<?php
|
2013-10-05 21:24:20 +02:00
|
|
|
class CommentController
|
2013-10-05 19:24:08 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @route /comments
|
2013-10-17 22:57:32 +02:00
|
|
|
* @route /comments/{page}
|
|
|
|
* @validate page [0-9]+
|
2013-10-05 19:24:08 +02:00
|
|
|
*/
|
2013-10-17 22:57:32 +02:00
|
|
|
public function listAction($page)
|
2013-10-05 19:24:08 +02:00
|
|
|
{
|
2013-10-17 22:57:32 +02:00
|
|
|
$this->context->stylesheets []= 'post-small.css';
|
|
|
|
$this->context->stylesheets []= 'comment-list.css';
|
|
|
|
$this->context->stylesheets []= 'comment-small.css';
|
2013-10-22 23:41:33 +02:00
|
|
|
$this->context->stylesheets []= 'paginator.css';
|
2013-10-22 00:18:41 +02:00
|
|
|
if ($this->context->user->hasEnabledEndlessScrolling())
|
2013-10-17 22:57:32 +02:00
|
|
|
$this->context->scripts []= 'paginator-endless.js';
|
|
|
|
|
|
|
|
$page = intval($page);
|
|
|
|
$commentsPerPage = intval($this->config->comments->commentsPerPage);
|
2013-10-28 11:19:15 +01:00
|
|
|
$this->context->subTitle = 'comments';
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ListComments);
|
2013-10-17 22:57:32 +02:00
|
|
|
|
2013-11-30 13:59:29 +01:00
|
|
|
$page = max(1, $page);
|
2013-12-18 15:10:53 +01:00
|
|
|
$comments = CommentSearchService::getEntities(null, $commentsPerPage, $page);
|
|
|
|
$commentCount = CommentSearchService::getEntityCount(null, $commentsPerPage, $page);
|
2013-10-17 22:57:32 +02:00
|
|
|
$pageCount = ceil($commentCount / $commentsPerPage);
|
2013-12-18 15:10:53 +01:00
|
|
|
CommentModel::preloadCommenters($comments);
|
|
|
|
CommentModel::preloadPosts($comments);
|
|
|
|
$posts = array_map(function($comment) { return $comment->getPost(); }, $comments);
|
|
|
|
PostModel::preloadTags($posts);
|
2013-10-17 22:57:32 +02:00
|
|
|
|
|
|
|
$this->context->postGroups = true;
|
|
|
|
$this->context->transport->paginator = new StdClass;
|
|
|
|
$this->context->transport->paginator->page = $page;
|
|
|
|
$this->context->transport->paginator->pageCount = $pageCount;
|
|
|
|
$this->context->transport->paginator->entityCount = $commentCount;
|
|
|
|
$this->context->transport->paginator->entities = $comments;
|
|
|
|
$this->context->transport->paginator->params = func_get_args();
|
|
|
|
$this->context->transport->comments = $comments;
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:37:41 +02:00
|
|
|
|
|
|
|
|
2013-10-17 22:57:32 +02:00
|
|
|
/**
|
|
|
|
* @route /post/{postId}/add-comment
|
|
|
|
* @valdiate postId [0-9]+
|
|
|
|
*/
|
|
|
|
public function addAction($postId)
|
|
|
|
{
|
2013-10-18 00:09:50 +02:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::AddComment);
|
2013-10-17 22:57:32 +02:00
|
|
|
if ($this->config->registration->needEmailForCommenting)
|
|
|
|
PrivilegesHelper::confirmEmail($this->context->user);
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
$post = PostModel::findById($postId);
|
2013-10-17 22:57:32 +02:00
|
|
|
|
2013-11-17 14:16:52 +01:00
|
|
|
if (InputHelper::get('submit'))
|
2013-10-17 22:57:32 +02:00
|
|
|
{
|
2013-11-17 14:16:52 +01:00
|
|
|
$text = InputHelper::get('text');
|
2013-12-18 15:10:53 +01:00
|
|
|
$text = CommentModel::validateText($text);
|
2013-11-17 14:16:52 +01:00
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
$comment = CommentModel::spawn();
|
|
|
|
$comment->setPost($post);
|
2013-10-27 20:39:32 +01:00
|
|
|
if ($this->context->loggedIn)
|
2013-12-18 15:10:53 +01:00
|
|
|
$comment->setCommenter($this->context->user);
|
|
|
|
else
|
|
|
|
$comment->setCommenter(null);
|
|
|
|
$comment->commentDate = time();
|
2013-10-17 22:57:32 +02:00
|
|
|
$comment->text = $text;
|
|
|
|
if (InputHelper::get('sender') != 'preview')
|
2013-11-17 14:24:04 +01:00
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
CommentModel::save($comment);
|
2013-11-22 23:32:56 +01:00
|
|
|
LogHelper::log('{user} commented on {post}', ['post' => TextHelper::reprPost($post->id)]);
|
2013-11-17 14:24:04 +01:00
|
|
|
}
|
2013-10-17 22:57:32 +02:00
|
|
|
$this->context->transport->textPreview = $comment->getText();
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-17 22:57:32 +02:00
|
|
|
}
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|
2013-10-17 23:37:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @route /comment/{id}/delete
|
|
|
|
* @validate id [0-9]+
|
|
|
|
*/
|
|
|
|
public function deleteAction($id)
|
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
$comment = CommentModel::findById($id);
|
2013-11-30 00:53:09 +01:00
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::DeleteComment, PrivilegesHelper::getIdentitySubPrivilege($comment->getCommenter()));
|
|
|
|
CommentModel::remove($comment);
|
2013-11-22 21:20:56 +01:00
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
LogHelper::log('{user} removed comment from {post}', ['post' => TextHelper::reprPost($comment->getPost())]);
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::success();
|
2013-10-17 23:37:41 +02:00
|
|
|
}
|
2013-10-05 19:24:08 +02:00
|
|
|
}
|