2013-10-27 01:06:58 +02:00
|
|
|
<?php
|
|
|
|
class CommentController extends ApplicationController
|
|
|
|
{
|
|
|
|
protected function init()
|
|
|
|
{
|
|
|
|
$this->helper('Avatar', 'Post');
|
|
|
|
}
|
2016-09-14 00:32:30 +02:00
|
|
|
|
2013-10-27 01:06:58 +02:00
|
|
|
protected function filters()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'before' => [
|
|
|
|
'member_only' => ['only' => array('create', 'destroy', 'update')],
|
|
|
|
'janitor_only' => ['only' => array('moderate')]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit()
|
|
|
|
{
|
|
|
|
$this->comment = Comment::find($this->params()->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
$comment = Comment::find($this->params()->id);
|
|
|
|
if (current_user()->has_permission($comment)) {
|
2013-11-19 16:54:12 +01:00
|
|
|
$comment->updateAttributes(array_merge($this->params()->comment, ['updater_ip_addr' => $this->request()->remoteIp()]));
|
2013-10-27 01:06:58 +02:00
|
|
|
$this->respond_to_success("Comment updated", '#index');
|
|
|
|
} else {
|
|
|
|
$this->access_denied();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy()
|
|
|
|
{
|
|
|
|
$comment = Comment::find($this->params()->id);
|
|
|
|
if (current_user()->has_permission($comment)) {
|
|
|
|
$comment->destroy();
|
|
|
|
$this->respond_to_success("Comment deleted", array('post#show', 'id' => $comment->post_id));
|
|
|
|
} else {
|
|
|
|
$this->access_denied();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
if (current_user()->is_member_or_lower() && $this->params()->commit == "Post" && Comment::where("user_id = ? AND created_at > ?", current_user()->id, strtotime('-1 hour'))->count() >= CONFIG()->member_comment_limit) {
|
|
|
|
# TODO: move this to the model
|
|
|
|
$this->respond_to_error("Hourly limit exceeded", '#index', array('status' => 421));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_id = current_user()->id;
|
2016-09-14 00:32:30 +02:00
|
|
|
|
2013-10-27 01:06:58 +02:00
|
|
|
$comment = new Comment(array_merge($this->params()->comment, array('ip_addr' => $this->request()->remoteIp(), 'user_id' => $user_id)));
|
|
|
|
if ($this->params()->commit == "Post without bumping") {
|
|
|
|
$comment->do_not_bump_post = true;
|
|
|
|
}
|
2016-09-14 00:32:30 +02:00
|
|
|
|
2013-10-27 01:06:58 +02:00
|
|
|
if ($comment->save()) {
|
|
|
|
$this->respond_to_success("Comment created", '#index');
|
|
|
|
} else {
|
|
|
|
$this->respond_to_error($comment, '#index');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show()
|
|
|
|
{
|
|
|
|
$this->set_title('Comment');
|
|
|
|
$this->comment = Comment::find($this->params()->id);
|
|
|
|
$this->respond_to_list("comment");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->set_title('Comments');
|
2016-09-14 00:32:30 +02:00
|
|
|
|
2013-10-27 01:06:58 +02:00
|
|
|
if ($this->request()->format() == "json" || $this->request()->format() == "xml") {
|
|
|
|
$this->comments = Comment::generate_sql($this->params()->all())->order("id DESC")->paginate($this->page_number(), 25);
|
|
|
|
$this->respond_to_list("comments");
|
|
|
|
} else {
|
|
|
|
$this->posts = Post::where("last_commented_at IS NOT NULL")->order("last_commented_at DESC")->paginate($this->page_number(), 10);
|
|
|
|
|
|
|
|
$comments = new Rails\ActiveRecord\Collection();
|
|
|
|
$this->posts->each(function($post)use($comments){$comments->merge($post->recent_comments());});
|
|
|
|
|
|
|
|
$newest_comment = $comments->max(function($a, $b){return $a->created_at > $b->created_at ? $a : $b;});
|
|
|
|
if (!current_user()->is_anonymous() && $newest_comment && current_user()->last_comment_read_at < $newest_comment->created_at) {
|
|
|
|
current_user()->updateAttribute('last_comment_read_at', $newest_comment->created_at);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->posts->deleteIf(function($x){return !$x->can_be_seen_by(current_user(), array('show_deleted' => true));});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function search()
|
|
|
|
{
|
2014-01-22 02:16:33 +01:00
|
|
|
$query = Comment::order('id desc');
|
|
|
|
$search_query = explode(' ', $this->params()->query);
|
|
|
|
$search_terms = array();
|
2016-09-14 00:32:30 +02:00
|
|
|
$user_search = false;
|
|
|
|
|
2014-01-22 02:16:33 +01:00
|
|
|
foreach ($search_query as $s) {
|
|
|
|
if (!$s) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-14 00:32:30 +02:00
|
|
|
|
|
|
|
if (!$user_search && strpos($s, 'user:') === 0 && strlen($s) > 5) {
|
2014-01-22 02:16:33 +01:00
|
|
|
list($search_type, $param) = explode(':', $s);
|
2016-09-14 00:32:30 +02:00
|
|
|
|
2014-01-22 02:16:33 +01:00
|
|
|
if ($user = User::where(['name' => $param])->first()) {
|
|
|
|
$query->where('user_id = ?', $user->id);
|
2016-09-14 00:32:30 +02:00
|
|
|
$user_search = true;
|
2013-10-27 01:06:58 +02:00
|
|
|
}
|
2016-09-14 00:32:30 +02:00
|
|
|
|
2014-01-22 02:16:33 +01:00
|
|
|
continue;
|
2013-10-27 01:06:58 +02:00
|
|
|
}
|
2014-01-22 02:16:33 +01:00
|
|
|
|
|
|
|
$search_terms[] = $s;
|
|
|
|
}
|
2016-09-14 00:32:30 +02:00
|
|
|
|
2014-01-22 02:16:33 +01:00
|
|
|
if ($search_terms) {
|
2013-10-27 01:06:58 +02:00
|
|
|
$query->where('body LIKE ?', '%' . implode('%', $search_terms) . '%');
|
2016-09-14 00:32:30 +02:00
|
|
|
} elseif (!$user_search) {
|
2013-10-27 01:06:58 +02:00
|
|
|
$query->where('false');
|
2014-01-22 02:16:33 +01:00
|
|
|
}
|
2013-10-27 01:06:58 +02:00
|
|
|
|
|
|
|
$this->comments = $query->paginate($this->page_number(), 30);
|
|
|
|
|
|
|
|
$this->respond_to_list("comments");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function moderate()
|
|
|
|
{
|
|
|
|
$this->set_title('Moderate Comments');
|
|
|
|
if ($this->request()->isPost()) {
|
|
|
|
$ids = array_keys($this->params()->c);
|
|
|
|
$coms = Comment::where("id IN (?)", $ids)->take();
|
|
|
|
|
|
|
|
if ($this->params()->commit == "Delete") {
|
|
|
|
$coms->each('destroy');
|
|
|
|
} elseif ($this->params()->commit == "Approve") {
|
|
|
|
$coms->each('updateAttribute', array('is_spam', false));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->redirectTo('#moderate');
|
|
|
|
} else {
|
|
|
|
$this->comments = Comment::where("is_spam = TRUE")->order("id DESC")->take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markAsSpam()
|
|
|
|
{
|
|
|
|
$this->comment = Comment::find($this->params()->id);
|
|
|
|
$this->comment->updateAttributes(array('is_spam' => true));
|
|
|
|
$this->respond_to_success("Comment marked as spam", '#index');
|
|
|
|
}
|
|
|
|
}
|