From c1ed98a5bdf0b6095538af23e38e2fc10e2bcec3 Mon Sep 17 00:00:00 2001 From: Floorb <132411956+Neetpone@users.noreply.github.com> Date: Fri, 22 Oct 2021 21:43:35 -0400 Subject: [PATCH] Some work, mainly committing so my next commit can be a little smaller. --- includes/Models/Paste.php | 4 + includes/common.php | 8 +- includes/functions.php | 11 ++- theme/bulma/user_profile.php | 137 +++++++++++++---------------------- user.php | 26 ++----- 5 files changed, 74 insertions(+), 112 deletions(-) diff --git a/includes/Models/Paste.php b/includes/Models/Paste.php index 30a32aa..42f826e 100644 --- a/includes/Models/Paste.php +++ b/includes/Models/Paste.php @@ -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 = []; diff --git a/includes/common.php b/includes/common.php index 4151e8b..6abe9cc 100644 --- a/includes/common.php +++ b/includes/common.php @@ -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'; diff --git a/includes/functions.php b/includes/functions.php index 9a9a351..75746ab 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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 'No tags'; + } + foreach ($tagsSplit as $tag) { if (stripos($tag, 'nsfw') !== false) { $tag = strtoupper($tag); diff --git a/theme/bulma/user_profile.php b/theme/bulma/user_profile.php index 0e56b9b..c12c429 100644 --- a/theme/bulma/user_profile.php +++ b/theme/bulma/user_profile.php @@ -119,7 +119,7 @@ } ?> - username === $profile_username): ?> + Some of your statistics:
Total pastes: — @@ -138,7 +138,7 @@
  • Favorites
  • - +
    @@ -155,86 +155,28 @@ } ?> - - 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 ' - - - - - '; - } - } else { ?> + + title, 20, 50)); + $p_date = new DateTime($paste->created_at); + $p_visible = match (intval($paste->visible)) { + 0 => 'Public', + 1 => 'Unlisted', + 2 => 'Private' + }; + ?> + - - - - - - - - + + + + + + + + + @@ -261,11 +203,35 @@ - Delete"; - //} ?> + + + title, 20, 50)); + $p_date = new DateTime($paste->created_at); + ?> + + + + + + + + + + + + + + + + + + + + + - '; + echo ''; } } ?> diff --git a/user.php b/user.php index 2134866..b991298 100644 --- a/user.php +++ b/user.php @@ -1,12 +1,10 @@ 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);
    - ' . ($title) . ' - - ' . $p_dateui . ' - - ' . $p_views . ' - '; - if (strlen($p_tags) > 0) { - echo tagsToHtmlUser($p_tags,$profile_username); - } else { - echo ' No tags'; - } - - - echo '
    - - - - - - - - - - -
    - -
    - -
    format('d F Y') ?>views ?>tags, $profile_username); ?>
    Date Favourited Status Tags
    format('d F Y') ?>views ?>tags, $profile_username); ?>
    TitleDate FavouritedStatusTags
    '; if (strlen($f_tags) > 0) { - echo tagsToHtmlUser($f_tags,$profile_username); + echo tagsToHtmlUser($f_tags,$profile_username); } else { echo ' No tags'; } - echo '