Fix tags on discover.php

This commit is contained in:
Floorb 2021-08-17 13:13:04 -04:00
parent 9ac3a2ab4e
commit 2c7b3cb877
4 changed files with 40 additions and 32 deletions

View file

@ -6,27 +6,6 @@ define('IN_PONEPASTE', 1);
require_once('../includes/common.php'); require_once('../includes/common.php');
require_once('../includes/NonRetardedSSP.class.php'); require_once('../includes/NonRetardedSSP.class.php');
function tagsToHtml(string $tags) : string {
$output = "";
$tagsSplit = explode(",", $tags);
foreach ($tagsSplit as $tag) {
if (stripos($tag, 'nsfw') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-danger";
} elseif (stripos($tag, 'SAFE') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-success";
} elseif (str_contains($tag, '/')) {
$tagcolor = "tag is-primary";
} else {
$tagcolor = "tag is-info";
}
$output .= '<a href="/archive?q=' . urlencode($tag) . '"><span class="' . $tagcolor . '">' . pp_html_escape(ucfirst($tag)) . '</span></a>';
}
return $output;
}
function transformDataRow($row) { function transformDataRow($row) {
$titleHtml = '<a href="/' . urlencode($row[0]) . '">' . pp_html_escape($row[1]) . '</a>'; $titleHtml = '<a href="/' . urlencode($row[0]) . '">' . pp_html_escape($row[1]) . '</a>';
$authorHtml = '<a href="' . urlencode($row[2]) . '">' . pp_html_escape($row[2]) . '</a>'; $authorHtml = '<a href="' . urlencode($row[2]) . '">' . pp_html_escape($row[2]) . '</a>';

View file

@ -21,6 +21,8 @@ header('Content-Type: text/html; charset=utf-8');
function transformPasteRow(array $row) : array { function transformPasteRow(array $row) : array {
global $conn;
return [ return [
'id' => $row['id'], 'id' => $row['id'],
'title' => $row['title'], 'title' => $row['title'],
@ -28,7 +30,8 @@ function transformPasteRow(array $row) : array {
'time' => $row['created_at'], 'time' => $row['created_at'],
'time_update' => $row['updated_at'], 'time_update' => $row['updated_at'],
'friendly_update_time' => friendlyDateDifference(new DateTime($row['updated_at']), new DateTime()), 'friendly_update_time' => friendlyDateDifference(new DateTime($row['updated_at']), new DateTime()),
'friendly_time' => friendlyDateDifference(new DateTime($row['created_at']), new DateTime()) 'friendly_time' => friendlyDateDifference(new DateTime($row['created_at']), new DateTime()),
'tags' => getPasteTags($conn, $row['id'])
]; ];
} }

View file

@ -72,6 +72,32 @@ function sandwitch($str) {
} }
function tagsToHtml(string | array $tags) : string {
$output = "";
if (is_array($tags)) {
$tagsSplit = array_map(function($tag) { return $tag['name']; }, $tags);
} else {
$tagsSplit = explode(",", $tags);
}
foreach ($tagsSplit as $tag) {
if (stripos($tag, 'nsfw') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-danger";
} elseif (stripos($tag, 'SAFE') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-success";
} elseif (str_contains($tag, '/')) {
$tagcolor = "tag is-primary";
} else {
$tagcolor = "tag is-info";
}
$output .= '<a href="/archive?q=' . urlencode($tag) . '"><span class="' . $tagcolor . '">' . pp_html_escape(ucfirst($tag)) . '</span></a>';
}
return $output;
}
function getevent($conn, $event_name, $count) { function getevent($conn, $event_name, $count) {
$query = $conn->prepare("SELECT id, visible, title, date, now_time, views, member FROM pastes WHERE visible='1' AND tagsys LIKE '%?%' $query = $conn->prepare("SELECT id, visible, title, date, now_time, views, member FROM pastes WHERE visible='1' AND tagsys LIKE '%?%'
ORDER BY RAND () LIMIT 0, ?"); ORDER BY RAND () LIMIT 0, ?");

View file

@ -55,8 +55,8 @@
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time> <time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p> </p>
<?php <?php
if (!empty($paste['tags'])) { if (count($paste['tags']) !== 0) {
echo sandwitch($paste['tags']); echo tagsToHtml($paste['tags']);
} else { } else {
echo ' <span class="tag is-warning">No tags</span>'; echo ' <span class="tag is-warning">No tags</span>';
} }
@ -93,8 +93,8 @@
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time> <time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p> </p>
<?php <?php
if (!empty($paste['tags'])) { if (count($paste['tags']) !== 0) {
echo sandwitch($paste['tags']); echo tagsToHtml($paste['tags']);
} else { } else {
echo ' <span class="tag is-warning">No tags</span>'; echo ' <span class="tag is-warning">No tags</span>';
} }
@ -132,8 +132,8 @@
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time> <time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p> </p>
<?php <?php
if (!empty($paste['tags'])) { if (count($paste['tags']) !== 0) {
echo sandwitch($paste['tags']); echo tagsToHtml($paste['tags']);
} else { } else {
echo ' <span class="tag is-warning">No tags</span>'; echo ' <span class="tag is-warning">No tags</span>';
} }
@ -171,8 +171,8 @@
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_update_time'] ?></time> <time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_update_time'] ?></time>
</p> </p>
<?php <?php
if (!empty($paste['tags'])) { if (count($paste['tags']) !== 0) {
echo sandwitch($paste['tags']); echo tagsToHtml($paste['tags']);
} else { } else {
echo ' <span class="tag is-warning">No tags</span>'; echo ' <span class="tag is-warning">No tags</span>';
} }
@ -210,8 +210,8 @@
<time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time> <time datetime="<?= $paste['time'] ?>"><?= $paste['friendly_time'] ?></time>
</p> </p>
<?php <?php
if (!empty($paste['tags'])) { if (count($paste['tags']) !== 0) {
echo sandwitch($paste['tags']); echo tagsToHtml($paste['tags']);
} else { } else {
echo ' <span class="tag is-warning">No tags</span>'; echo ' <span class="tag is-warning">No tags</span>';
} }