2016-03-28 01:18:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Holds the comment object.
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Comment object.
|
|
|
|
* @package Sakura
|
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
|
|
*/
|
|
|
|
class Comment
|
|
|
|
{
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* The comment identifier.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $id = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The category this comment belongs to.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $category = "";
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The timestamp this comment was posted at.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $time = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Id of the user that posted this comment.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $user = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Id of the comment this comment is replying to.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $reply = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The content of this comment.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $text = "";
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The upvotes this comment has received.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $upvotes = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The downvotes this comment has received.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public $downvotes = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache of reply objects.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
private $replyCache = [];
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache of the parsed text content.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
private $parsedCache = "";
|
2016-03-28 01:18:59 +00:00
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @var int $id
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function __construct($id = 0)
|
|
|
|
{
|
|
|
|
// Get comment data from the database
|
|
|
|
$data = DB::table('comments')
|
|
|
|
->where('comment_id', $id)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// Check if anything was returned and assign data
|
|
|
|
if ($data) {
|
|
|
|
$data = $data[0];
|
|
|
|
|
|
|
|
$this->id = $data->comment_id;
|
|
|
|
$this->category = $data->comment_category;
|
|
|
|
$this->time = $data->comment_timestamp;
|
|
|
|
$this->user = $data->comment_poster;
|
|
|
|
$this->reply = $data->comment_reply_to;
|
|
|
|
$this->text = $data->comment_text;
|
|
|
|
|
|
|
|
$this->getVotes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Saving changes made to this comment.
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
// Create submission data, insert and update take the same format
|
|
|
|
$data = [
|
|
|
|
'comment_category' => $this->category,
|
|
|
|
'comment_timestamp' => $this->time,
|
|
|
|
'comment_poster' => $this->user,
|
|
|
|
'comment_reply_to' => $this->reply,
|
|
|
|
'comment_text' => $this->text,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Update if id isn't 0
|
|
|
|
if ($this->id) {
|
|
|
|
DB::table('comments')
|
|
|
|
->where('comment_id', $this->id)
|
|
|
|
->update($data);
|
|
|
|
} else {
|
|
|
|
$this->id = DB::table('comments')
|
|
|
|
->insertGetId($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Delete this comment.
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
foreach ($this->replies() as $reply) {
|
|
|
|
$reply->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::table('comments')
|
|
|
|
->where('comment_id', $this->id)
|
|
|
|
->delete();
|
|
|
|
|
|
|
|
$this->id = 0;
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Gets and caches the upvotes.
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
private function getVotes()
|
|
|
|
{
|
|
|
|
$votes = DB::table('comment_votes')
|
|
|
|
->where('vote_comment', $this->id)
|
|
|
|
->get();
|
|
|
|
|
2016-03-30 21:30:15 +00:00
|
|
|
$this->upvotes = 0;
|
|
|
|
$this->downvotes = 0;
|
|
|
|
|
2016-03-28 01:18:59 +00:00
|
|
|
foreach ($votes as $vote) {
|
2016-03-30 21:30:15 +00:00
|
|
|
if (intval($vote->vote_state) !== 0) {
|
2016-03-28 14:47:43 +00:00
|
|
|
$this->upvotes += 1;
|
2016-03-28 01:18:59 +00:00
|
|
|
} else {
|
2016-03-28 14:47:43 +00:00
|
|
|
$this->downvotes += 1;
|
2016-03-28 01:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the parsed comment text
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function parsed()
|
|
|
|
{
|
|
|
|
if (!$this->parsedCache) {
|
2016-04-01 21:44:31 +00:00
|
|
|
$this->parsedCache = BBcode::parseEmoticons(clean_string($this->text));
|
2016-03-28 14:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parsedCache;
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Get the replies to this comment.
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function replies()
|
|
|
|
{
|
|
|
|
if (!$this->replyCache) {
|
|
|
|
$commentIds = DB::table('comments')
|
|
|
|
->where('comment_reply_to', $this->id)
|
2016-03-28 14:47:43 +00:00
|
|
|
->orderBy('comment_id', 'desc')
|
2016-03-28 01:18:59 +00:00
|
|
|
->get(['comment_id']);
|
2016-03-28 14:47:43 +00:00
|
|
|
$commentIds = array_column($commentIds, 'comment_id');
|
2016-03-28 01:18:59 +00:00
|
|
|
|
|
|
|
foreach ($commentIds as $comment) {
|
|
|
|
$this->replyCache[$comment] = new Comment($comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->replyCache;
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the user object of the poster.
|
|
|
|
* @return User
|
|
|
|
*/
|
2016-03-28 01:18:59 +00:00
|
|
|
public function userData()
|
|
|
|
{
|
|
|
|
return User::construct($this->user);
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Casts a vote on a comment.
|
|
|
|
* @param int $user
|
|
|
|
* @param bool $vote
|
|
|
|
*/
|
2016-03-30 21:30:15 +00:00
|
|
|
public function vote($user, $vote)
|
2016-03-28 01:18:59 +00:00
|
|
|
{
|
2016-03-30 21:30:15 +00:00
|
|
|
$vote = $vote ? '1' : '0';
|
|
|
|
|
|
|
|
// Attempt to get previous vote
|
|
|
|
$previous = DB::table('comment_votes')
|
|
|
|
->where('vote_user', $user)
|
|
|
|
->where('vote_comment', $this->id)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// Check if anything was returned
|
|
|
|
if ($previous) {
|
|
|
|
// Check if the vote that's being casted is the same
|
|
|
|
if ($previous[0]->vote_state == $vote) {
|
|
|
|
// Delete the vote
|
|
|
|
DB::table('comment_votes')
|
|
|
|
->where('vote_user', $user)
|
|
|
|
->where('vote_comment', $this->id)
|
|
|
|
->delete();
|
|
|
|
} else {
|
|
|
|
// Otherwise update the vote
|
|
|
|
DB::table('comment_votes')
|
|
|
|
->where('vote_user', $user)
|
|
|
|
->where('vote_comment', $this->id)
|
|
|
|
->update([
|
|
|
|
'vote_state' => $vote,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Create a vote
|
|
|
|
DB::table('comment_votes')
|
|
|
|
->insert([
|
|
|
|
'vote_user' => $user,
|
|
|
|
'vote_comment' => $this->id,
|
|
|
|
'vote_state' => $vote,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getVotes();
|
2016-03-28 01:18:59 +00:00
|
|
|
}
|
|
|
|
}
|