2015-09-03 19:44:14 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* A flexible comment system
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
|
|
|
* Class Comments
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class Comments
|
|
|
|
{
|
2015-09-18 21:56:54 +00:00
|
|
|
public $comments = []; // Array containing comments
|
|
|
|
public $category; // Comment category
|
|
|
|
public $count = 0; // Amount of comments
|
|
|
|
|
2015-09-15 20:37:29 +00:00
|
|
|
// Constructor
|
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
|
|
|
|
$comments = Database::fetch(
|
|
|
|
'comments',
|
|
|
|
true,
|
|
|
|
[
|
|
|
|
'comment_category' => [$this->category, '='],
|
|
|
|
'comment_reply_to' => ['0', '='],
|
|
|
|
],
|
|
|
|
['comment_id', true]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Feed them into the sorter
|
|
|
|
$this->comments = $this->sortComments($comments);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorting
|
|
|
|
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) {
|
|
|
|
// Attach the poster
|
2016-01-22 20:07:44 +00:00
|
|
|
$comment['comment_poster'] = User::construct($comment['comment_poster']);
|
2016-01-17 01:58:31 +00:00
|
|
|
$comment['comment_text'] = Utils::parseEmotes(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) {
|
|
|
|
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
|
|
|
|
$replies = Database::fetch('comments', true, [
|
|
|
|
'comment_category' => [$this->category, '='],
|
|
|
|
'comment_reply_to' => [$comment['comment_id'], '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2015-10-10 15:51:24 +00:00
|
|
|
// Getting a single comment
|
|
|
|
public function getComment($cid)
|
|
|
|
{
|
|
|
|
// Get from database
|
|
|
|
return Database::fetch('comments', false, [
|
|
|
|
'comment_id' => [$cid, '='],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-10-10 21:17:50 +00:00
|
|
|
// Getting comment votes
|
|
|
|
public function getVotes($cid)
|
|
|
|
{
|
|
|
|
// Get from database
|
|
|
|
return Database::fetch('comment_votes', true, [
|
|
|
|
'vote_comment' => [$cid, '='],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-10-10 15:51:24 +00:00
|
|
|
// Creating
|
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
|
|
|
|
Database::insert('comments', [
|
|
|
|
'comment_category' => $this->category,
|
|
|
|
'comment_timestamp' => time(),
|
|
|
|
'comment_poster' => $uid,
|
|
|
|
'comment_reply_to' => (int) $reply,
|
|
|
|
'comment_text' => $content,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Return success
|
|
|
|
return [1, 'SUCCESS'];
|
|
|
|
}
|
2015-10-10 15:51:24 +00:00
|
|
|
|
2015-10-10 21:17:50 +00:00
|
|
|
// Voting
|
|
|
|
public function makeVote($uid, $cid, $mode)
|
|
|
|
{
|
|
|
|
// Attempt to get previous vote
|
|
|
|
$vote = Database::fetch('comment_votes', false, [
|
|
|
|
'vote_user' => [$uid, '='],
|
|
|
|
'vote_comment' => [$cid, '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Check if anything was returned
|
|
|
|
if ($vote) {
|
|
|
|
// Check if the vote that's being casted is the same
|
|
|
|
if ($vote['vote_state'] == $mode) {
|
|
|
|
// Delete the vote
|
|
|
|
Database::delete('comment_votes', [
|
|
|
|
'vote_user' => [$uid, '='],
|
|
|
|
'vote_comment' => [$cid, '='],
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
// Otherwise update the vote
|
|
|
|
Database::update('comment_votes', [
|
|
|
|
[
|
|
|
|
'vote_state' => $mode,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'vote_user' => [$uid, '='],
|
|
|
|
'vote_comment' => [$cid, '='],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Create a vote
|
|
|
|
Database::insert('comment_votes', [
|
|
|
|
'vote_user' => $uid,
|
|
|
|
'vote_comment' => $cid,
|
|
|
|
'vote_state' => $mode,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-10 15:51:24 +00:00
|
|
|
// Deleting
|
|
|
|
public function removeComment($cid)
|
|
|
|
{
|
|
|
|
// Remove from database
|
|
|
|
return Database::delete('comments', [
|
|
|
|
'comment_id' => [$cid, '='],
|
|
|
|
]);
|
|
|
|
}
|
2015-09-03 19:44:14 +00:00
|
|
|
}
|