Test meme rendering

This commit is contained in:
Bruno Rybársky 2024-04-25 09:40:21 +02:00
parent 7382c6ea4b
commit 0253ed6e04
5 changed files with 56 additions and 12 deletions

@ -7,6 +7,7 @@ function endpoint($endpoint_data): array
return match ($endpoint_data["action"]) { return match ($endpoint_data["action"]) {
"addMeme" => addMeme($endpoint_data['memetext'], $endpoint_data['imageid']), "addMeme" => addMeme($endpoint_data['memetext'], $endpoint_data['imageid']),
"renderGallery" => renderMemeGallery(),
default => ["Status" => "Fail", "message" => "Invalid action"], default => ["Status" => "Fail", "message" => "Invalid action"],
}; };
} }

@ -13,10 +13,24 @@ function addMeme(string $memeText, int $imageID): bool
return false; return false;
} }
function renderMeme(string $title, string $textContent, string $createdAt, string $filePath, string $userNickname): string
{
global $routerConfig;
$meme_template = file_get_contents($routerConfig->template_dir . "meme.html");
$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);
return str_replace('__TEMPLATE_MEME_TEXT__', htmlspecialchars($textContent), $meme_out);
}
function renderMemeGallery(): string function renderMemeGallery(): string
{ {
global $mysqli; global $mysqli;
$stmtlist = $mysqli->prepare('SELECT Memes.ID, Memes.Title Memes.TextContent, Memes.CreatedAt, Files.Path, Files.Type, Users.Nickname FROM Memes INNER JOIN Users ON Memes.AuthorID = Users.ID INNER JOIN Files ON Memes.FileID = Files.ID'); global $routerConfig;
$stmtlist = $mysqli->prepare('SELECT Memes.ID, Memes.Title, Memes.TextContent, Memes.CreatedAt, Files.Path, 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 // Execute the prepared statement
$stmtlist->execute(); $stmtlist->execute();
@ -30,12 +44,18 @@ function renderMemeGallery(): string
// Bind the result variables // Bind the result variables
$stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $filePath, $fileType, $userNickname); $stmtlist->bind_result($memeID, $title, $textContent, $createdAt, $filePath, $fileType, $userNickname);
// Fetch the results $meme_gallery_template = file_get_contents($routerConfig->template_dir . "meme_gallery.html");
while ($stmtlist->fetch()) {
// Fetch the results
$memes_out = '';
while ($stmtlist->fetch()) {
if ($fileType == 'image') {
$memes_out .= renderMeme($title, $textContent, $createdAt, $filePath, $userNickname);
} }
}
$meme_gallery_out = str_replace('__TEMPLATE_MEMES_HERE__', $memes_out, $meme_gallery_template);
// Close the statement // Close the statement
$stmtlist->close(); $stmtlist->close();
return ""; return $meme_gallery_out;
} }

18
pages/memes/index.php Normal file

@ -0,0 +1,18 @@
<?php
require_once "lib/router.php";
require_once "lib/meme.php";
global $routerConfig;
$output = renderMemeGallery();
return [
"output" => $output,
"parameters" =>
[
"minimal_permission_level" => 1,
"secret" => "no",
"page_title" => "Účet"
]
];

@ -1,8 +1,9 @@
<article> <article class="meme">
<h2 class='meme'>__TEMPLATE_ARTICLE_TITLE__</h2> <h2 class='meme_title'>__TEMPLATE_MEME_TITLE__</h2>
<div class="memebody"> <div class="meme_body">
<p class='newsauthor'><i class="ri-user-line"></i>__TEMPLATE_MEME_AUTHOR__</p> <p class='meme_author'><i class="ri-user-line"></i>__TEMPLATE_MEME_AUTHOR__</p>
<p class='newsdate'><i class="ri-calendar-line"></i>__TEMPLATE_MEME_DATE__</p> <p class='meme_date'><i class="ri-calendar-line"></i>__TEMPLATE_MEME_DATE__</p>
<img src="__TEMPLATE_MEME_IMAGE__" <img src="__TEMPLATE_MEME_IMAGE__" class="meme_image">
<p class="meme_text">__TEMPLATE_MEME_TEXT__</p>
</div> </div>
</article> </article>

@ -1,7 +1,11 @@
<!--suppress HtmlUnknownTag, HtmlUnknownTag -->
<page minimal_permission_level="1" secret="no" page_title="Memes"></page> <page minimal_permission_level="1" secret="no" page_title="Memes"></page>
<header> <header>
<h1 class="title">Adlerka Memes</h1> <h1 class="title">Adlerka Memes</h1>
<p>Skoro, ako <a href="https://reddit.com/r/adlerka" target="_blank">r/adlerka</a>, ale lepšie.</p> <p>Skoro, ako <a href="https://reddit.com/r/adlerka" target="_blank">r/adlerka</a>, ale lepšie.</p>
<hr> <hr>
</header> </header>
<main>
<div id="meme_gallery">
__TEMPLATE_MEMES_HERE__
</div>
</main>