Some work, mainly committing so my next commit can be a little smaller.

This commit is contained in:
Floorb 2021-10-22 21:43:35 -04:00
parent c7bb6e20fa
commit c1ed98a5bd
5 changed files with 74 additions and 112 deletions

View file

@ -6,6 +6,10 @@ use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;
class Paste extends Model {
public const VISIBILITY_PUBLIC = 0;
public const VISIBILITY_UNLISTED = 1;
public const VISIBILITY_PRIVATE = 2;
protected $table = 'pastes';
protected $guarded = [];

View file

@ -31,12 +31,12 @@ function urlForPaste(Paste $paste) : string {
return "/paste.php?id={$paste->id}";
}
function urlForMember(User $member_name) : string {
function urlForMember(User $user) : string {
if (PP_MOD_REWRITE) {
return '/user/' . urlencode($member_name);
return '/user/' . urlencode($user->username);
}
return '/user.php?name=' . urlencode($member_name);
return '/user.php?name=' . urlencode($user->username);
}
function optionsForSelect(array $displays, array $values, string $currentSelection = null) : string {
@ -190,10 +190,8 @@ $title = Trim($row['title']);
$baseurl = Trim($row['baseurl']);
$site_name = Trim($row['site_name']);
$email = Trim($row['email']);
$ga = Trim($row['google_analytics']);
$additional_scripts = Trim($row['additional_scripts']);
// Setup theme
$default_theme = 'bulma';

View file

@ -57,14 +57,23 @@ function tagsToHtml(array | Collection $tags) : string {
return $output;
}
function tagsToHtmlUser(string | array $tags, $profile_username) : string {
function tagsToHtmlUser(string | array | Collection $tags, $profile_username) : string {
$output = "";
if (is_a($tags, Collection::class)) {
$tags = $tags->toArray();
}
if (is_array($tags)) {
$tagsSplit = array_map(function($tag) { return $tag['name']; }, $tags);
} else {
$tagsSplit = explode(",", $tags);
}
if (count($tagsSplit) === 0) {
return '<span class="tag is-warning">No tags</span>';
}
foreach ($tagsSplit as $tag) {
if (stripos($tag, 'nsfw') !== false) {
$tag = strtoupper($tag);

View file

@ -119,7 +119,7 @@
}
?>
<?php if ($current_user && $current_user->username === $profile_username): ?>
<?php if ($is_current_user): ?>
Some of your statistics:
<br />
Total pastes: <?= $profile_total_pastes ?> &mdash;
@ -138,7 +138,7 @@
<li data-target="second-tab"><a>Favorites</a></li>
</ul>
</div>
<?php endif;?>
<?php endif;?>
<div class="tab-content" id="first-tab">
<table id="archive" class="table is-fullwidth is-hoverable">
<thead>
@ -155,86 +155,28 @@
} ?>
</tr>
</thead>
<tbody>
<?php
foreach ($profile_pastes as $row) {
$title = Trim($row['title']);
$p_id = $row['id'];
$p_code = Trim($row['code']);
$p_date = new DateTime($row['created_at']);
$p_dateui = $p_date->format("d F Y");
$p_views = Trim($row['views']);
$p_visible = intval($row['visible']);
$tagArray = array_map(function ($tag) {
return $tag['name'];
}, getPasteTags($conn, $p_id));
$p_tags = implode(',', $tagArray);
$p_visible = match ($p_visible) {
0 => 'Public',
1 => 'Unlisted',
2 => 'Private'
};
$p_link = urlForPaste($p_id);
$p_delete_message = "'Are you sure you want to delete this paste?'";
$p_delete_link = (PP_MOD_REWRITE) ? "user.php?del&user=$profile_username&id=$p_id" : "user.php?del&user=$profile_username&id=$p_id";
$p_tag_link = (PP_MOD_REWRITE) ? "user.php?user=$profile_username&q=$p_tags" : "user.php?user=$profile_username&q=$tags";
$title = truncate($title, 20, 50);
// Guests only see public pastes
if (!$is_current_user) {
if ($row['visible'] == 0) {
echo '<tr>
<td>
<a href="' . urlForPaste($p_id) . '" title="' . $title . '">' . ($title) . '</a>
</td>
<td data-sort="' . $p_date->format('U') . '" class="td-center">
<span>' . $p_dateui . '</span>
</td>
<td class="td-center">
' . $p_views . '
</td>
<td class="td-left">';
if (strlen($p_tags) > 0) {
echo tagsToHtmlUser($p_tags,$profile_username);
} else {
echo ' <span class="tag is-warning">No tags</span>';
}
echo '</td>
</tr>';
}
} else { ?>
<?php foreach ($profile_pastes as $paste): ?>
<?php
$escaped_title = pp_html_escape(truncate($paste->title, 20, 50));
$p_date = new DateTime($paste->created_at);
$p_visible = match (intval($paste->visible)) {
0 => 'Public',
1 => 'Unlisted',
2 => 'Private'
};
?>
<?php if ($is_current_user || $row['visible'] == Paste::VISIBILITY_PUBLIC): ?>
<tr>
<td>
<a href="<?= urlForPaste($p_id) ?>" title="<?= $title ?>"><?= $title ?></a>
</td>
<td data-sort="<?= $p_date->format('U') ?>" class="td-center">
<span><?= $p_dateui ?></span>
</td>
<td class="td-center">
<?= $p_visible ?>
</td>
<td class="td-center">
<?= $p_views ?>
</td>
<td class="td-center">
<?= strtoupper($p_code) ?>
</td>
<td class="td-center">
<form action="' . urlForPaste($p_id) . '" method="POST">
</form>
<a href="' . $protocol . $baseurl . '/' . $p_delete_link . '" title="' . $title . '" onClick="return confirm(' . $p_delete_message . ')"><i class="far fa-trash-alt fa-lg" aria-hidden="true"></i></a>
</td>
</tr>
<?php }
}
?>
<td><a href="<?= urlForPaste($paste) ?>" title="<?= $escaped_title ?>"><?= $escaped_title ?></a></td>
<td data-sort="<?= $p_date->format('U') ?>" class="td-center"><?= $p_date->format('d F Y') ?></td>
<td class="td-center"><?= $p_visible; ?></td>
<td class="td-center"><?= $paste->views ?></td>
<td class="td-left"><?= tagsToHtmlUser($paste->tags, $profile_username); ?></td>
<!-- Delete button here? -->
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
@ -261,11 +203,35 @@
<td class="td-center">Date Favourited</td>
<td class="td-center">Status</td>
<td class="td-center">Tags</td>
<?php //if (isset($_SESSION) && $_SESSION['username'] == $profile_username) {
//echo "<td>Delete</td>";
//} ?>
</tr>
</thead>
<tbody>
<?php foreach ($profile_favs as $paste): ?>
<?php
$escaped_title = pp_html_escape(truncate($paste->title, 20, 50));
$p_date = new DateTime($paste->created_at);
?>
<?php if ($is_current_user || $row['visible'] == Paste::VISIBILITY_PUBLIC): ?>
<tr>
<td><a href="<?= urlForPaste($paste) ?>" title="<?= $escaped_title ?>"><?= $escaped_title ?></a></td>
<td data-sort="<?= $p_date->format('U') ?>" class="td-center"><?= $p_date->format('d F Y') ?></td>
<td class="td-center"><?= $p_visible; ?></td>
<td class="td-center"><?= $paste->views ?></td>
<td class="td-left"><?= tagsToHtmlUser($paste->tags, $profile_username); ?></td>
<!-- Delete button here? -->
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td class="td-right">Title</td>
<td class="td-center">Date Favourited</td>
<td class="td-center">Status</td>
<td class="td-center">Tags</td>
</tr>
</tfoot>
<tbody>
<?php
foreach ($profile_favs as $row) {
@ -303,14 +269,13 @@
</td>
<td class="td-left">';
if (strlen($f_tags) > 0) {
echo tagsToHtmlUser($f_tags,$profile_username);
echo tagsToHtmlUser($f_tags,$profile_username);
} else {
echo ' <span class="tag is-warning">No tags</span>';
}
echo '</td>
</tr>';
echo '</td></tr>';
}
}
?>

View file

@ -1,12 +1,10 @@
<?php
define('IN_PONEPASTE', 1);
require_once('includes/common.php');
require_once('includes/functions.php');
use PonePaste\Models\User;
use PonePaste\Models\Paste;
if (empty($_GET['user'])) {
// No username provided
@ -28,7 +26,6 @@ $p_title = $profile_username . "'s Public Pastes";
// FIXME: This should be incoming faves
$total_pfav = $profile_info->favourites->count();
$total_yfav = $profile_info->favourites->count();
// Badges
@ -39,30 +36,19 @@ $profile_badge = match ($profile_info['badge']) {
default => '',
};
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ?');
$query->execute([$profile_info['id']]);
$profile_total_pastes = intval($query->fetch(PDO::FETCH_NUM)[0]);
$profile_total_pastes = $profile_info->pastes->count();
$profile_total_public = $profile_info->pastes->where('visible', 0)->count();
$profile_total_unlisted = $profile_info->pastes->where('visible', 1)->count();
$profile_total_private = $profile_info->pastes->where('visible', 2)->count();
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ? AND visible = 0');
$query->execute([$profile_info['id']]);
$profile_total_public = intval($query->fetch(PDO::FETCH_NUM)[0]);
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ? AND visible = 1');
$query->execute([$profile_info['id']]);
$profile_total_unlisted = intval($query->fetch(PDO::FETCH_NUM)[0]);
$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE user_id = ? AND visible = 2');
$query->execute([$profile_info['id']]);
$profile_total_private = intval($query->fetch(PDO::FETCH_NUM)[0]);
$query = $conn->prepare('SELECT SUM(views) FROM pastes WHERE user_id = ?');
$query->execute([$profile_info['id']]);
$profile_total_paste_views = intval($query->fetch(PDO::FETCH_NUM)[0]);
$profile_join_date = $profile_info['date'];
$profile_pastes = getUserPastes($conn, $profile_info['id']);
$profile_pastes = $profile_info->pastes;
$profile_favs = $profile_info->favourites;
$is_current_user = ($current_user !== null) && ($profile_info->id == $current_user->id);