forked from Adleraci/adlerka.top
Update meme voting
This commit is contained in:
17
lib/meme.php
17
lib/meme.php
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once "lib/upload.php";
|
||||
require_once "lib/account.php";
|
||||
@@ -18,13 +19,15 @@ function addMeme(string $title, string $memeText, int $imageID): array
|
||||
return $output;
|
||||
}
|
||||
|
||||
function renderMeme(int $id, int $authorId, string $title, string $textContent, string $createdAt, string $filePath, string $userNickname, string $meme_template): string
|
||||
function renderMeme(int $id, int $authorId, string $title, string $textContent, string $createdAt, string $filePath, int $imageWidth, int $imageHeight, string $userNickname, string $meme_template): string
|
||||
{
|
||||
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_TITLE__', htmlspecialchars($title), $meme_template);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_AUTHOR__', htmlspecialchars($userNickname), $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_DATE__', htmlspecialchars($createdAt), $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE__', '/' . htmlspecialchars($filePath), $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE_WIDTH__', '/' . strval($imageWidth), $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE_HEIGHT__', '/' . strval($imageHeight), $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);
|
||||
@@ -48,10 +51,10 @@ function renderMeme(int $id, int $authorId, string $title, string $textContent,
|
||||
$meme_upvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, 1);\"> <i id='meme_votes_upvote_$id' class=\"ri-arrow-up-circle-$meme_upvote_active\"></i></button>" : '';
|
||||
$meme_downvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, 0);\"> <i id='meme_votes_downvote_$id' class=\"ri-arrow-down-circle-$meme_downvote_active\"></i></button>" : '';
|
||||
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', $meme_net_votes, $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', strval($meme_net_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);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_ID__', strval($id), $meme_out);
|
||||
$meme_out = str_replace('__TEMPLATE_MEME_VOTE_COUNTER_CLASS__', $meme_vote_counter_class, $meme_out);
|
||||
|
||||
return str_replace('__TEMPLATE_MEME_TEXT__', htmlspecialchars($textContent), $meme_out);
|
||||
@@ -61,7 +64,7 @@ function renderMemeGallery(): string
|
||||
{
|
||||
global $mysqli;
|
||||
global $routerConfig;
|
||||
$stmtlist = $mysqli->prepare('SELECT Memes.ID, Memes.Title, Memes.TextContent, Memes.CreatedAt, Memes.AuthorID, Files.Path, Files.Type, Users.Nickname FROM Memes INNER JOIN Users ON Memes.AuthorID = Users.ID INNER JOIN Files ON Memes.FileID = Files.ID');
|
||||
$stmtlist = $mysqli->prepare('SELECT Memes.ID, Memes.Title, Memes.TextContent, Memes.CreatedAt, Memes.AuthorID, Files.Path, Files.Width, Files.Height, Files.Type, Users.Nickname FROM Memes INNER JOIN Users ON Memes.AuthorID = Users.ID INNER JOIN Files ON Memes.FileID = Files.ID');
|
||||
|
||||
// Execute the prepared statement
|
||||
$memeID = 0;
|
||||
@@ -72,8 +75,10 @@ function renderMemeGallery(): string
|
||||
$fileType = "";
|
||||
$userNickname = "";
|
||||
$createdAt = "";
|
||||
$imageWidth = 0;
|
||||
$imageHeight = 0;
|
||||
// Bind the result variables
|
||||
$stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $authorID, $filePath, $fileType, $userNickname);
|
||||
$stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $authorID, $filePath, $imageWidth, $imageHeight, $fileType, $userNickname);
|
||||
$stmtlist->execute();
|
||||
|
||||
$meme_gallery_template = file_get_contents($routerConfig['template_dir'] . 'meme_gallery.html');
|
||||
@@ -84,7 +89,7 @@ function renderMemeGallery(): string
|
||||
$stmtlist->store_result();
|
||||
while ($stmtlist->fetch()) {
|
||||
if (str_starts_with($fileType, 'image')) {
|
||||
$memes_out .= renderMeme($memeID, $authorID, $title, $textContent, $createdAt, $filePath, $userNickname, $meme_template);
|
||||
$memes_out .= renderMeme($memeID, $authorID, $title, $textContent, $createdAt, $filePath, $imageWidth, $imageHeight, $userNickname, $meme_template);
|
||||
}
|
||||
}
|
||||
$meme_add = isLoggedIn() ? file_get_contents($routerConfig['template_dir'] . 'meme_add.html') : '';
|
||||
|
Reference in New Issue
Block a user