szurubooru/src/Controllers/CommentController.php

82 lines
2.6 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-05 21:22:28 +02:00
$this->context->subTitle = 'comments';
if ($this->config->browsing->endlessScrolling)
$this->context->scripts []= 'paginator-endless.js';
$page = intval($page);
$commentsPerPage = intval($this->config->comments->commentsPerPage);
PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListComments);
$buildDbQuery = function($dbQuery)
{
$dbQuery->from('comment');
$dbQuery->orderBy('comment_date')->desc();
};
$countDbQuery = R::$f->begin();
$countDbQuery->select('COUNT(1)')->as('count');
$buildDbQuery($countDbQuery);
$commentCount = intval($countDbQuery->get('row')['count']);
$pageCount = ceil($commentCount / $commentsPerPage);
$page = max(1, min($pageCount, $page));
$searchDbQuery = R::$f->begin();
$searchDbQuery->select('comment.*');
$buildDbQuery($searchDbQuery);
$searchDbQuery->limit('?')->put($commentsPerPage);
$searchDbQuery->offset('?')->put(($page - 1) * $commentsPerPage);
$comments = $searchDbQuery->get();
$comments = R::convertToBeans('comment', $comments);
R::preload($comments, ['commenter' => '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;
}
/**
* @route /post/{postId}/add-comment
* @valdiate postId [0-9]+
*/
public function addAction($postId)
{
PrivilegesHelper::confirmWithException($this->context->user, 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;
$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();
$this->context->transport->success = true;
}
2013-10-05 19:24:08 +02:00
}
}