2015-09-03 19:44:14 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the holds the comment handler class.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-09-03 19:44:14 +00:00
|
|
|
namespace Sakura;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Handles and serves comments on pages.
|
2016-02-18 23:28:44 +00:00
|
|
|
* Needs a reimplementation.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-18 19:06:30 +00:00
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class Comments
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The array containing the comments.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $comments = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The comment category.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $category;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount of comments.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $count = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param mixed $category The category that comments should be fetched from.
|
|
|
|
*/
|
2015-09-18 21:56:54 +00:00
|
|
|
public function __construct($category)
|
|
|
|
{
|
|
|
|
// Set category
|
|
|
|
$this->category = $category;
|
|
|
|
|
|
|
|
// Get the comments and assign them to $comments
|
2016-03-13 20:35:51 +00:00
|
|
|
$comments = DB::table('comments')
|
|
|
|
->where('comment_category', $this->category)
|
|
|
|
->where('comment_reply_to', 0)
|
|
|
|
->orderBy('comment_id', 'desc')
|
|
|
|
->get();
|
2015-09-18 21:56:54 +00:00
|
|
|
|
|
|
|
// Feed them into the sorter
|
|
|
|
$this->comments = $this->sortComments($comments);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Sort the comments.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param array $comments Array containing comments.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return array Array containing the sorted comments.
|
|
|
|
*/
|
2015-09-18 21:56:54 +00:00
|
|
|
public function sortComments($comments)
|
2015-09-15 20:37:29 +00:00
|
|
|
{
|
2015-09-18 21:56:54 +00:00
|
|
|
// Create storage array
|
|
|
|
$layer = [];
|
|
|
|
|
|
|
|
// Sort comments
|
|
|
|
foreach ($comments as $comment) {
|
2016-03-13 20:35:51 +00:00
|
|
|
// Temporary hackjob to get rid of the old database layer, will reimplement later
|
|
|
|
$comment = get_object_vars($comment);
|
|
|
|
|
2015-09-18 21:56:54 +00:00
|
|
|
// Attach the poster
|
2016-01-22 20:07:44 +00:00
|
|
|
$comment['comment_poster'] = User::construct($comment['comment_poster']);
|
2016-02-13 13:36:21 +00:00
|
|
|
$comment['comment_text'] = BBcode::parseEmoticons(Utils::cleanString($comment['comment_text']));
|
2015-09-18 21:56:54 +00:00
|
|
|
|
2015-10-10 21:17:50 +00:00
|
|
|
// Get likes and dislikes
|
|
|
|
$votes = $this->getVotes($comment['comment_id']);
|
|
|
|
$comment['comment_likes'] = 0;
|
|
|
|
$comment['comment_dislikes'] = 0;
|
|
|
|
|
|
|
|
// Store amount in their respective variables
|
|
|
|
foreach ($votes as $vote) {
|
2016-03-13 20:35:51 +00:00
|
|
|
$vote = get_object_vars($vote);
|
2015-10-10 21:17:50 +00:00
|
|
|
if ($vote['vote_state']) {
|
|
|
|
$comment['comment_likes'] += 1;
|
|
|
|
} else {
|
|
|
|
$comment['comment_dislikes'] += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:56:54 +00:00
|
|
|
// Add post to posts array
|
|
|
|
$layer[$comment['comment_id']] = $comment;
|
|
|
|
|
|
|
|
// Up the comment count
|
|
|
|
$this->count += 1;
|
|
|
|
|
|
|
|
// Attempt to get replies from the database
|
2016-03-13 20:35:51 +00:00
|
|
|
$replies = DB::table('comments')
|
|
|
|
->where('comment_category', $this->category)
|
|
|
|
->where('comment_reply_to', $comment['comment_id'])
|
|
|
|
->orderBy('comment_id', 'desc')
|
|
|
|
->get();
|
2015-09-18 21:56:54 +00:00
|
|
|
|
|
|
|
// Check if this was a reply to something
|
|
|
|
if ($replies) {
|
|
|
|
// Save the replies
|
|
|
|
$layer[$comment['comment_id']]['comment_replies'] = $this->sortComments($replies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $layer;
|
2015-09-15 20:37:29 +00:00
|
|
|
}
|
2015-10-06 19:51:07 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get a single comment.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $cid ID of the comment.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return array The comment.
|
|
|
|
*/
|
2015-10-10 15:51:24 +00:00
|
|
|
public function getComment($cid)
|
|
|
|
{
|
|
|
|
// Get from database
|
2016-03-13 20:35:51 +00:00
|
|
|
$comment = DB::table('comments')
|
|
|
|
->where('comment_id', $cid)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $comment ? get_object_vars($comment[0]) : [];
|
2015-10-10 15:51:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get the votes for a comment.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $cid ID of the comment.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return array The votes.
|
|
|
|
*/
|
2015-10-10 21:17:50 +00:00
|
|
|
public function getVotes($cid)
|
|
|
|
{
|
|
|
|
// Get from database
|
2016-03-13 20:35:51 +00:00
|
|
|
$comment = DB::table('comment_votes')
|
|
|
|
->where('vote_comment', $cid)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $comment;
|
2015-10-10 21:17:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Creating a new comment.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $uid ID of the user creating the comment.
|
|
|
|
* @param int $reply ID of the comment that is being replied to.
|
|
|
|
* @param string $content Contents of the comment.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return array Response identifier.
|
|
|
|
*/
|
2015-10-06 19:51:07 +00:00
|
|
|
public function makeComment($uid, $reply, $content)
|
|
|
|
{
|
|
|
|
// Check if the comment is long enough
|
2015-12-04 14:19:10 +00:00
|
|
|
if (strlen($content) < Config::get('comment_min_length')) {
|
2015-10-06 19:51:07 +00:00
|
|
|
return [0, 'TOO_SHORT'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the comment isn't too long
|
2015-12-04 14:19:10 +00:00
|
|
|
if (strlen($content) > Config::get('comment_max_length')) {
|
2015-10-06 19:51:07 +00:00
|
|
|
return [0, 'TOO_LONG'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert into database
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('comments')
|
|
|
|
->insert([
|
|
|
|
'comment_category' => $this->category,
|
|
|
|
'comment_timestamp' => time(),
|
|
|
|
'comment_poster' => (int) $uid,
|
|
|
|
'comment_reply_to' => (int) $reply,
|
|
|
|
'comment_text' => $content,
|
|
|
|
]);
|
2015-10-06 19:51:07 +00:00
|
|
|
|
|
|
|
// Return success
|
|
|
|
return [1, 'SUCCESS'];
|
|
|
|
}
|
2015-10-10 15:51:24 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Making a vote.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $uid User making this vote.
|
|
|
|
* @param int $cid ID of the comment that is being voted on.
|
|
|
|
* @param int $mode Positive or negative vote.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return bool Always returns true.
|
|
|
|
*/
|
2015-10-10 21:17:50 +00:00
|
|
|
public function makeVote($uid, $cid, $mode)
|
|
|
|
{
|
|
|
|
// Attempt to get previous vote
|
2016-03-13 20:35:51 +00:00
|
|
|
$vote = DB::table('comment_votes')
|
|
|
|
->where('vote_user', $uid)
|
|
|
|
->where('vote_comment', $cid)
|
|
|
|
->get();
|
2015-10-10 21:17:50 +00:00
|
|
|
|
|
|
|
// Check if anything was returned
|
|
|
|
if ($vote) {
|
|
|
|
// Check if the vote that's being casted is the same
|
2016-03-13 20:35:51 +00:00
|
|
|
if ($vote[0]->vote_state == $mode) {
|
2015-10-10 21:17:50 +00:00
|
|
|
// Delete the vote
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('comment_votes')
|
|
|
|
->where('vote_user', $uid)
|
|
|
|
->where('vote_comment', $cid)
|
|
|
|
->delete();
|
2015-10-10 21:17:50 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise update the vote
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('comment_votes')
|
|
|
|
->where('vote_user', $uid)
|
|
|
|
->where('vote_comment', $cid)
|
|
|
|
->update([
|
|
|
|
'vote_state' => $mode,
|
|
|
|
]);
|
2015-10-10 21:17:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Create a vote
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('comment_votes')
|
|
|
|
->insert([
|
|
|
|
'vote_user' => $uid,
|
|
|
|
'vote_comment' => $cid,
|
|
|
|
'vote_state' => $mode,
|
|
|
|
]);
|
2015-10-10 21:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Remove a comment
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $cid ID of the comment to remove.
|
|
|
|
*/
|
2015-10-10 15:51:24 +00:00
|
|
|
public function removeComment($cid)
|
|
|
|
{
|
|
|
|
// Remove from database
|
2016-03-13 20:35:51 +00:00
|
|
|
DB::table('comments')
|
|
|
|
->where('comment_id', $cid)
|
|
|
|
->delete();
|
2015-10-10 15:51:24 +00:00
|
|
|
}
|
2015-09-03 19:44:14 +00:00
|
|
|
}
|