szurubooru/src/Controllers/CommentController.php

87 lines
2.8 KiB
PHP
Raw Normal View History

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
* @route /comments/{page}
* @validate page [0-9]+
2013-10-05 19:24:08 +02:00
*/
public function listAction($page)
2013-10-05 19:24:08 +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())
$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-28 11:19:15 +01:00
$commentCount = Model_Comment::getEntityCount(null);
$pageCount = ceil($commentCount / $commentsPerPage);
$page = max(1, min($pageCount, $page));
2013-10-28 11:19:15 +01:00
$comments = Model_Comment::getEntities(null, $commentsPerPage, $page);
R::preload($comments, ['commenter' => 'user', 'post', 'post.uploader' => 'user']);
$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
/**
* @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);
if ($this->config->registration->needEmailForCommenting)
PrivilegesHelper::confirmEmail($this->context->user);
$post = Model_Post::locate($postId);
$text = InputHelper::get('text');
if (!empty($text))
{
$text = Model_Comment::validateText($text);
$comment = R::dispense('comment');
$comment->post = $post;
if ($this->context->loggedIn)
$comment->commenter = $this->context->user;
$comment->comment_date = time();
$comment->text = $text;
if (InputHelper::get('sender') != 'preview')
R::store($comment);
$this->context->transport->textPreview = $comment->getText();
2013-11-16 21:21:43 +01:00
LogHelper::logEvent('comment-add', '+{user} commented on @{post}', ['post' => $post->id]);
2013-11-16 18:40:26 +01:00
StatusHelper::success();
}
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)
{
$comment = Model_Comment::locate($id);
2013-10-19 20:13:46 +02:00
R::preload($comment, ['commenter' => 'user']);
2013-10-18 00:09:50 +02:00
PrivilegesHelper::confirmWithException(Privilege::DeleteComment, PrivilegesHelper::getIdentitySubPrivilege($comment->commenter));
2013-11-16 21:21:43 +01:00
LogHelper::logEvent('comment-del', '+{user} removed comment from @{post}', ['post' => $comment->post->id]);
2013-10-17 23:37:41 +02:00
R::trash($comment);
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
}