forked from Adleraci/adlerka.top
Test meme voting
This commit is contained in:
58
lib/meme.php
58
lib/meme.php
@@ -27,6 +27,14 @@ function renderMeme(int $id, int $authorId, string $title, string $textContent,
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE__', '/' . htmlspecialchars($filePath), $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_DELETE_BUTTON__', (isModerator() || $_SESSION['ID'] == $authorId) ? "<button onclick=\"deleteMeme($id);\"><i class='ri-delete-bin-line'></i></button>" : '', $meme_out);
|
||||
|
||||
$meme_votes = calculateNetVotes($id);
|
||||
$meme_upvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, true);\"> <i class=\"ri-arrow-up-line\"></i></button>" : '';
|
||||
$meme_downvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, false);\"> <i class=\"ri-arrow-down-line\"></i></button>" : '';
|
||||
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', $meme_votes, $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_UPVOTE__', $meme_upvote, $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_DOWNVOTE__', $meme_downvote, $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_ID__', $id, $meme_out);
|
||||
|
||||
return str_replace('__TEMPLATE_MEME_TEXT__', htmlspecialchars($textContent), $meme_out);
|
||||
}
|
||||
@@ -69,7 +77,7 @@ function renderMemeGallery(): string
|
||||
return $meme_gallery_out;
|
||||
}
|
||||
|
||||
function deleteMeme(int $memeId): array
|
||||
function deleteMeme(int $memeID): array
|
||||
{
|
||||
global $mysqli;
|
||||
$out = ["Status" => "Fail"];
|
||||
@@ -77,14 +85,58 @@ function deleteMeme(int $memeId): array
|
||||
$query = !isModerator() ? 'DELETE FROM Memes WHERE ID = ? AND AuthorID = ?' : 'DELETE FROM Memes WHERE ID = ?';
|
||||
$stmtDelete = $mysqli->prepare($query);
|
||||
if (!isModerator()) {
|
||||
$stmtDelete->bind_param('ii', $memeId, $_SESSION['ID']);
|
||||
$stmtDelete->bind_param('ii', $memeID, $_SESSION['ID']);
|
||||
} else {
|
||||
$stmtDelete->bind_param('i', $memeId);
|
||||
$stmtDelete->bind_param('i', $memeID);
|
||||
}
|
||||
$stmtDelete->execute();
|
||||
if ($stmtDelete->affected_rows > 0) {
|
||||
$out['Status'] = 'Success';
|
||||
}
|
||||
$stmtDelete->close();
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function voteMeme(int $memeID, bool $isUpvote): array
|
||||
{
|
||||
global $mysqli;
|
||||
$out = ["Status" => "Fail"];
|
||||
$vote = $isUpvote ? 1 : 0;
|
||||
$memeVoteConn = $mysqli->prepare('INSERT INTO MemeVotes (MemeID, UserID, isUpvote) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE isUpvote = VALUES(isUpvote)');
|
||||
$memeVoteConn->bind_param('iii', $memeID, $_SESSION['ID'], $vote);
|
||||
$memeVoteConn->execute();
|
||||
if ($memeVoteConn->affected_rows > 0) {
|
||||
$out['Status'] = 'Success';
|
||||
}
|
||||
$memeVoteConn->close();
|
||||
return $out;
|
||||
}
|
||||
|
||||
function calculateNetVotes(int $memeID): int
|
||||
{
|
||||
global $mysqli;
|
||||
|
||||
$query = 'SELECT isUpvote FROM MemeVotes WHERE MemeID = ?';
|
||||
$stmt = $mysqli->prepare($query);
|
||||
$stmt->bind_param('i', $memeID);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
$netVotes = 0;
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$netVotes += ($row['isUpvote'] == 1) ? 1 : -1;
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
|
||||
return $netVotes;
|
||||
}
|
||||
|
||||
function getMemeVotes(int $memeID): array
|
||||
{
|
||||
return [
|
||||
"Status" => "Success",
|
||||
"NetVotes" => calculateNetVotes($memeID)
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user