forked from Adleraci/adlerka.top
MEME
This commit is contained in:
parent
0fca1b000b
commit
20ca96ba05
@ -106,7 +106,7 @@ async function displayList(data, elementId, deleteFunction) {
|
|||||||
|
|
||||||
if ("function" === typeof deleteFunction) {
|
if ("function" === typeof deleteFunction) {
|
||||||
const th = document.createElement("th");
|
const th = document.createElement("th");
|
||||||
th.appendChild(document.createTextNode("Delete"));
|
th.appendChild(document.createTextNode("<i class='ri-delete-bin-line'></i>"));
|
||||||
headerRow.appendChild(th);
|
headerRow.appendChild(th);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ async function displayList(data, elementId, deleteFunction) {
|
|||||||
if ("function" === typeof deleteFunction) {
|
if ("function" === typeof deleteFunction) {
|
||||||
const td = document.createElement("td");
|
const td = document.createElement("td");
|
||||||
const deleteButton = document.createElement('button');
|
const deleteButton = document.createElement('button');
|
||||||
deleteButton.textContent = "Delete";
|
deleteButton.textContent = "<i class='ri-delete-bin-line'></i>";
|
||||||
deleteButton.onclick = () => deleteFunction(line.ID);
|
deleteButton.onclick = () => deleteFunction(line.ID);
|
||||||
td.appendChild(deleteButton);
|
td.appendChild(deleteButton);
|
||||||
dataRow.appendChild(td);
|
dataRow.appendChild(td);
|
||||||
@ -629,6 +629,20 @@ function addMeme() {
|
|||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function deleteMeme(memeId) {
|
||||||
|
let formData = new FormData();
|
||||||
|
formData.append('action', 'deleteMeme');
|
||||||
|
formData.append('meme_id', memeId);
|
||||||
|
|
||||||
|
let xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('POST', '/meme', true);
|
||||||
|
xhr.onload = function () {
|
||||||
|
const resp = JSON.parse(xhr.responseText);
|
||||||
|
handleResponse(resp, "Meme bol zmazaný", "Nastala chyba pri mazaní meme-u");
|
||||||
|
};
|
||||||
|
xhr.send(formData);
|
||||||
|
}
|
||||||
|
|
||||||
async function getMemeImages() {
|
async function getMemeImages() {
|
||||||
let memeImageSelector = document.getElementById("meme_image_input");
|
let memeImageSelector = document.getElementById("meme_image_input");
|
||||||
let fileList = await getFileList();
|
let fileList = await getFileList();
|
||||||
|
@ -8,6 +8,7 @@ function endpoint($endpoint_data): array
|
|||||||
return match ($endpoint_data["action"]) {
|
return match ($endpoint_data["action"]) {
|
||||||
"addMeme" => addMeme($endpoint_data['meme_title'], $endpoint_data['meme_text'], $endpoint_data['meme_image_id']),
|
"addMeme" => addMeme($endpoint_data['meme_title'], $endpoint_data['meme_text'], $endpoint_data['meme_image_id']),
|
||||||
"renderGallery" => renderMemeGallery(),
|
"renderGallery" => renderMemeGallery(),
|
||||||
|
"deleteMeme" => deleteMeme($endpoint_data['meme_id']),
|
||||||
default => ["Status" => "Fail", "message" => "Invalid action"],
|
default => ["Status" => "Fail", "message" => "Invalid action"],
|
||||||
};
|
};
|
||||||
}
|
}
|
30
lib/meme.php
30
lib/meme.php
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once "lib/upload.php";
|
require_once "lib/upload.php";
|
||||||
|
require_once "lib/account.php";
|
||||||
|
|
||||||
function addMeme(string $title, string $memeText, int $imageID): array
|
function addMeme(string $title, string $memeText, int $imageID): array
|
||||||
{
|
{
|
||||||
@ -9,7 +10,7 @@ function addMeme(string $title, string $memeText, int $imageID): array
|
|||||||
if (isLoggedIn() && fileExists($imageID, false) && !empty($title) && !empty($memeText) && !empty($imageID) && $imageID > 0) {
|
if (isLoggedIn() && fileExists($imageID, false) && !empty($title) && !empty($memeText) && !empty($imageID) && $imageID > 0) {
|
||||||
$stmtMemeAdd = $mysqli->prepare('INSERT INTO Memes (AuthorID, Title, TextContent, FileID) VALUES (?, ?, ?, ?)');
|
$stmtMemeAdd = $mysqli->prepare('INSERT INTO Memes (AuthorID, Title, TextContent, FileID) VALUES (?, ?, ?, ?)');
|
||||||
$stmtMemeAdd->bind_param('issi', $_SESSION['ID'], htmlspecialchars($title), htmlspecialchars($memeText), $imageID);
|
$stmtMemeAdd->bind_param('issi', $_SESSION['ID'], htmlspecialchars($title), htmlspecialchars($memeText), $imageID);
|
||||||
if($stmtMemeAdd->execute() && $stmtMemeAdd->affected_rows > 0) {
|
if ($stmtMemeAdd->execute() && $stmtMemeAdd->affected_rows > 0) {
|
||||||
$output["Status"] = "Success";
|
$output["Status"] = "Success";
|
||||||
$output["Meme"] = "Funny";
|
$output["Meme"] = "Funny";
|
||||||
}
|
}
|
||||||
@ -17,7 +18,7 @@ function addMeme(string $title, string $memeText, int $imageID): array
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderMeme(string $title, string $textContent, string $createdAt, string $filePath, string $userNickname): string
|
function renderMeme(int $id, string $title, string $textContent, string $createdAt, string $filePath, string $userNickname): string
|
||||||
{
|
{
|
||||||
global $routerConfig;
|
global $routerConfig;
|
||||||
$meme_template = file_get_contents($routerConfig['template_dir'] . "meme.html");
|
$meme_template = file_get_contents($routerConfig['template_dir'] . "meme.html");
|
||||||
@ -26,6 +27,8 @@ function renderMeme(string $title, string $textContent, string $createdAt, strin
|
|||||||
$meme_out = str_replace('__TEMPLATE_MEME_AUTHOR__', htmlspecialchars($userNickname), $meme_out);
|
$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_DATE__', htmlspecialchars($createdAt), $meme_out);
|
||||||
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE__', '/' . htmlspecialchars($filePath), $meme_out);
|
$meme_out = str_replace('__TEMPLATE_MEME_IMAGE__', '/' . htmlspecialchars($filePath), $meme_out);
|
||||||
|
$meme_out = str_replace('__TEMPLATE_MEME_DELETE_BUTTON__', isModerator() ? "<button onclick=\"deleteMeme($id);\"><i class='ri-delete-bin-line'></i></button>" : '', $meme_out);
|
||||||
|
|
||||||
|
|
||||||
return str_replace('__TEMPLATE_MEME_TEXT__', htmlspecialchars($textContent), $meme_out);
|
return str_replace('__TEMPLATE_MEME_TEXT__', htmlspecialchars($textContent), $meme_out);
|
||||||
}
|
}
|
||||||
@ -54,7 +57,7 @@ function renderMemeGallery(): string
|
|||||||
$memes_out = '';
|
$memes_out = '';
|
||||||
while ($stmtlist->fetch()) {
|
while ($stmtlist->fetch()) {
|
||||||
if (str_starts_with($fileType, 'image')) {
|
if (str_starts_with($fileType, 'image')) {
|
||||||
$memes_out .= renderMeme($title, $textContent, $createdAt, $filePath, $userNickname);
|
$memes_out .= renderMeme($memeID, $title, $textContent, $createdAt, $filePath, $userNickname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$meme_gallery_out = str_replace('__TEMPLATE_MEMES_HERE__', $memes_out, $meme_gallery_template);
|
$meme_gallery_out = str_replace('__TEMPLATE_MEMES_HERE__', $memes_out, $meme_gallery_template);
|
||||||
@ -63,3 +66,24 @@ function renderMemeGallery(): string
|
|||||||
$stmtlist->close();
|
$stmtlist->close();
|
||||||
return $meme_gallery_out;
|
return $meme_gallery_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteMeme(int $memeId): string
|
||||||
|
{
|
||||||
|
global $mysqli;
|
||||||
|
$out = ["Status" => "Fail"];
|
||||||
|
if (isLoggedIn()) {
|
||||||
|
$query = !isAdmin() ? 'DELETE FROM Memes WHERE ID = ? AND AuthorID = ?' : 'DELETE FROM Memes WHERE ID = ?';
|
||||||
|
$stmtDelete = $mysqli->prepare($query);
|
||||||
|
if (!isAdmin()) {
|
||||||
|
$stmtDelete->bind_param('ii', $memeId, $_SESSION['id']);
|
||||||
|
} else {
|
||||||
|
$stmtDelete->bind_param('i', $memeId);
|
||||||
|
}
|
||||||
|
$stmtDelete->execute();
|
||||||
|
$stmtDelete->fetch();
|
||||||
|
if ($stmtDelete->affected_rows > 0) {
|
||||||
|
$out['Status'] = 'Success';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $out;
|
||||||
|
}
|
@ -241,11 +241,23 @@ function getImageURL(int $imageFileID) :string
|
|||||||
|
|
||||||
function deleteFile(int $fileID) :string
|
function deleteFile(int $fileID) :string
|
||||||
{
|
{
|
||||||
|
global $mysqli;
|
||||||
$out = ["Status" => "Fail"];
|
$out = ["Status" => "Fail"];
|
||||||
$file_location = fileExists($fileID, false);
|
if(isLoggedIn()) {
|
||||||
if ($file_location){
|
$file_location = fileExists($fileID, !isAdmin());
|
||||||
if(unlink($file_location)) {
|
$query = !isAdmin() ? 'DELETE FROM Files WHERE ID = ? AND UploadedBy = ?' : 'DELETE FROM Files WHERE ID = ?';
|
||||||
$out['Status'] = 'Success';
|
$stmtDelete = $mysqli->prepare($query);
|
||||||
|
if (!isAdmin()) {
|
||||||
|
$stmtDelete->bind_param('ii', $fileID, $_SESSION['id']);
|
||||||
|
} else {
|
||||||
|
$stmtDelete->bind_param('i', $fileID);
|
||||||
|
}
|
||||||
|
$stmtDelete->execute();
|
||||||
|
$stmtDelete->fetch();
|
||||||
|
if ($file_location) {
|
||||||
|
if (unlink($file_location) && $stmtDelete->affected_rows > 0) {
|
||||||
|
$out['Status'] = 'Success';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $out;
|
return $out;
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
<h2 class='meme_title'>__TEMPLATE_MEME_TITLE__</h2>
|
<h2 class='meme_title'>__TEMPLATE_MEME_TITLE__</h2>
|
||||||
<div class="meme_body">
|
<div class="meme_body">
|
||||||
<p class='meme_author'><i class="ri-user-line"></i>__TEMPLATE_MEME_AUTHOR__</p>
|
<p class='meme_author'><i class="ri-user-line"></i>__TEMPLATE_MEME_AUTHOR__</p>
|
||||||
|
__TEMPLATE_MEME_DELETE_BUTTON__
|
||||||
<p class='meme_date'><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__" width="500" class="meme_image">
|
<img src="__TEMPLATE_MEME_IMAGE__" width="500" class="meme_image">
|
||||||
<p class="meme_text">__TEMPLATE_MEME_TEXT__</p>
|
<p class="meme_text">__TEMPLATE_MEME_TEXT__</p>
|
||||||
|
Loading…
Reference in New Issue
Block a user